commit 5c54de249f5bb0a512bde9c0d0b84a07eb024257 Author: anjia0532 Date: Tue Jun 19 11:40:17 2018 +0800 init repo diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..16ceb61 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +services: + - docker +before_install: + - docker login -u${user_name} -p${pass_word} + - chmod +x ./deploy.sh + - ./deploy.sh + +script: + - echo hello \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7838cda --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 AnJia + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.tpl b/README.tpl new file mode 100644 index 0000000..b9d5570 --- /dev/null +++ b/README.tpl @@ -0,0 +1,58 @@ +Google Container Registry Mirror [last sync ${current_date} UTC] +------- + +[![Sync Status](https://travis-ci.org/anjia0532/gcr.io_mirror.svg?branch=sync)](https://travis-ci.org/anjia0532/gcr.io_mirror) + +Syntax +------- + +```bash +gcr.io/namespace/image_name:image_tag +#eq +${user_name}/namespace.image_name:image_tag +``` + +Example +------- + +```bash +docker pull gcr.io/google-containers/federation-controller-manager-arm64:v1.3.1-beta.1 +# eq +docker pull ${user_name}/google-containers.federation-controller-manager-arm64:v1.3.1-beta.1 +``` + +ReTag ${user_name} images to gcr.io +------- + +```bash +# replace gcr.io/google-containers/federation-controller-manager-arm64:v1.3.1-beta.1 to real image +# this will convert gcr.io/google-containers/federation-controller-manager-arm64:v1.3.1-beta.1 +# to ${user_name}/google-containers.federation-controller-manager-arm64:v1.3.1-beta.1 and pull it + +images=$(cat img.txt) +#or +#images=$(cat <0)|{k: .key,t: .value.tag[0],v: .value.timeUploadedMs} | "tf=${dir}"+.t+".tmp;echo "+.k+">${tf};touch -amd \"$(date \"+%F %T\" -d @" + .v[0:10] +")\" ${tf}"' | while read i; do + eval $i + done +} + +function compare() +{ + + echo -e "${yellow}compare image diff ...${plain}" + find ./gcr.io_mirror/ -name "*.tmp" | while read t + do + dir=$(dirname $t) + name=$(basename $t .tmp) + if [ -e ${dir}/${name}.tag ] && [ $(cat ${dir}/${name}.tag)x = $(cat $t)x ]; then + rm -rf $t; + else + [[ -e ${dir}/${name}.tag ]] && rm -rf ${dir}/${name}.tag + fi + done +} + +function pull_push_diff() +{ + n=$1 + img=$2 + all_of_imgs=$(find ./gcr.io_mirror -type f \( ! -iname "*.md" \) |wc -l) + current_ns_imgs=$(find ./gcr.io_mirror/${n}/ -type f \( ! -iname "*.md" \) |wc -l) + tmps=($(find ./gcr.io_mirror/${n}/${img}/ -type f \( -iname "*.tmp" \) -exec basename {} .tmp \; | uniq)) + + echo -e "${red}wait for mirror${plain}/${yellow}gcr.io/${n}/* images${plain}/${green}all of images${plain}:${red}${#tmps[@]}${plain}/${yellow}${current_ns_imgs}${plain}/${green}${all_of_imgs}${plain}" + + for tag in ${tmps[@]} ; do + echo -e "${yellow}mirror ${n}/${img}/${tag}...${plain}" + lock=./gcr.io_mirror/${n}/${img}/${tag}.lck + [[ -e $lock ]] && continue; + echo "${tag}">$lock + + docker pull gcr.io/${n}/${img}:${tag} + docker tag gcr.io/${n}/${img}:${tag} ${user_name}/${n}.${img}:${tag} + docker push ${user_name}/${n}.${img}:${tag} + + [[ -e ./commit.lck ]] && break + + mv ./gcr.io_mirror/${n}/${img}/${tag}.tmp ./gcr.io_mirror/${n}/${img}/${tag}.tag + + echo -e "[gcr.io/${n}/${image}:${tag}](https://hub.docker.com/r/${user_name}/${n}.${image}/tags/)\n\n" >> CHANGES.md + rm -rf $lock + done +} + +function mirror() +{ + num=$(find ./gcr.io_mirror/ -type f \( -iname "*.tmp" \) |wc -l) + if [ $num -eq 0 ]; then + ns=$(cat ./gcr_namespaces 2>/dev/null || echo google-containers) + for n in ${ns[@]} ; do + process_run "init_namespace $n" + done + wait ${!} + fi + + compare + + tmps=$(find ./gcr.io_mirror/ -type f \( -iname "*.tmp" \) -exec dirname {} \; | uniq | cut -d'/' -f3-4) + + if [ -z "$tmps" ]; then + echo -e "${red} wait for push ${tmps[@]}" + for img in ${tmps[@]} ; do + n=$(echo ${img}|cut -d'/' -f1) + image=$(echo ${img}|cut -d'/' -f2) + process_run "pull_push_diff $n $image" + done + wait ${!} + fi + + images=($(find ./gcr.io_mirror/ -type f -name "*.tag" |uniq|sort)) + + cp ./gcr.io_mirror/CHANGES.md ./CHANGES1.md 2>/dev/null + + find ./gcr.io_mirror/ -type f -name "*.md" -exec rm -rf {} \; + + for img in ${images[@]} ; do + n=$(echo ${img}|cut -d'/' -f3) + image=$(echo ${img}|cut -d'/' -f4) + tag=$(basename ${img} .tag) + mkdir -p ./gcr.io_mirror/${n}/${image} + if [ ! -e ./gcr.io_mirror/${n}/${image}/README.md ]; then + echo -e "\n[gcr.io/${n}/${image}](https://hub.docker.com/r/${user_name}/${n}.${image}/tags/)\n-----\n\n" >> ./gcr.io_mirror/${n}/${image}/README.md + echo -e "\n[gcr.io/${n}/${image}](https://hub.docker.com/r/${user_name}/${n}.${image}/tags/)\n-----\n\n" >> ./gcr.io_mirror/${n}/README.md + fi + + echo -e "[gcr.io/${n}/${image}:${tag}](https://hub.docker.com/r/${user_name}/${n}.${image}/tags/)\n\n" >> ./gcr.io_mirror/${n}/${image}/README.md + done + + commit +} + +function commit() +{ + echo 1 > ./commit.lck + ns=($(cat ./gcr_namespaces 2>/dev/null || echo google-containers)) + readme=./gcr.io_mirror/README.md + export current_date=$(date +'%Y-%m-%d %H:%M') + envsubst < README.tpl >"${readme}" + + echo -e "Mirror ${#ns[@]} namespaces image from gcr.io\n-----\n\n" >> "${readme}" + for n in ${ns[@]} ; do + echo -e "[gcr.io/${n}/*](./${n}/README.md)\n\n" >> "${readme}" + done + + if [ -s CHANGES.md ]; then + (echo -e "## $(date +'%Y-%m-%d %H:%M') \n" && cat CHANGES.md && cat CHANGES1.md 2>/dev/null) >> gcr.io_mirror/CHANGES.md + fi + + echo -e "${red} commit to github master" + git -C ./gcr.io_mirror pull + git -C ./gcr.io_mirror add -A + git -C ./gcr.io_mirror commit -m "sync gcr.io's images at $(date +'%Y-%m-%d %H:%M')" + git -C ./gcr.io_mirror push -f "https://${user_name}:${GH_TOKEN}@github.com/${user_name}/gcr.io_mirror.git" master:master + + echo -e "${red} commit to github master:done" + echo 1 > ./commit.done +} + +mirror & + +while true; +do + duration=$SECONDS + if [[ -e ./commit.done ]] || [[ $duration -ge $sec ]]; then + + [[ $duration -ge $sec ]] && echo -e "${red} more than $(expr $sec / 60) min,abort this build" + + [[ ! -e ./commit.done ]] && commit + + IFS=$'\n'; for i in $(jobs); do echo "$i"; done + kill $(jobs -p) + IFS=$'\n'; for i in $(jobs); do echo "$i"; done + break + + else + docker_dir=$(docker info | grep "Docker Root Dir" | cut -d':' -f2) + used=$(df -h ${docker_dir}|awk '{if(NR>1)print $5}') + echo -e "${red} duration:${duration}s, docker root dir :${docker_dir}:used:${used}" + [[ ${used} > '70%' ]] && docker system prune -f -a + sleep 30 + fi +done + +sleep 120 +echo "${red} bye bye" \ No newline at end of file diff --git a/gcr_namespaces b/gcr_namespaces new file mode 100644 index 0000000..79bd555 --- /dev/null +++ b/gcr_namespaces @@ -0,0 +1,6 @@ +google-samples +kubernetes-helm +k8s-minikube +tf-on-k8s-dogfood +spinnaker-marketplace +google-containers \ No newline at end of file diff --git a/process-utils.sh b/process-utils.sh new file mode 100644 index 0000000..abe7319 --- /dev/null +++ b/process-utils.sh @@ -0,0 +1,36 @@ +_PROCESS_PIPE_NAME="/tmp/$(cat /proc/sys/kernel/random/uuid)" +_PROCESS_PIPE_ID=10 + +function _create_pipe() +{ + + mkfifo ${_PROCESS_PIPE_NAME} + eval exec "${_PROCESS_PIPE_ID}""<>${_PROCESS_PIPE_NAME}" + + for ((i=0; i< $1; i++)) + do + echo >&${_PROCESS_PIPE_ID} + done +} + +function process_init() +{ + _create_pipe $1 +} + +function process_run() +{ + cmd=$1 + + if [ -z "$cmd" ]; then + echo "please input command to run" + exit 1 + fi + + read -u${_PROCESS_PIPE_ID} + { + $cmd + echo >&${_PROCESS_PIPE_ID} + }& +} + \ No newline at end of file