repo-update 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. set -e;
  3. if [[ "$1" != "" ]];then
  4. host_to_sync="$1";
  5. force=true;
  6. else
  7. force=false;
  8. fi;
  9. root="$(readlink -f $(dirname $BASH_SOURCE)/..)";
  10. . $root/etc/repo.conf
  11. . $root/etc/sites.conf
  12. for val in "${sites[@]}"; do
  13. unset site;
  14. declare -A site;
  15. eval "site=($val)";
  16. host=${site[host]};
  17. user=${site[username]};
  18. if ! $force || [[ "$host_to_sync" == "all" ]] || [[ "$host_to_sync" == "$user@$host" ]];then
  19. echo "Updating $user@$host";
  20. case ${site[type]} in
  21. rsync)
  22. rsyncd(){
  23. echo -e "\t\tSyncing $1";
  24. ARGS="-rzvlhe ssh --progress --stats --exclude=\".gitignore\" $1 $user@$host:${site[root]}";
  25. FILES=$(expect -c "
  26. set timeout -1
  27. log_user 1
  28. spawn rsync --dry-run $ARGS;
  29. expect {
  30. \"yes/no\" {
  31. send yes\r
  32. exp_continue
  33. }
  34. password: {
  35. log_user 0;
  36. send $(echo ${site[password]} | base64 --decode | gpg -d -q --no-mdc-warning)\r;
  37. log_user 1;
  38. exp_continue
  39. }
  40. eof {
  41. exit
  42. }
  43. }
  44. " | grep "^Number of regular files transferred:" | awk '{print $6}');
  45. if [[ "$FILES" == "" ]]; then
  46. FILES="0";
  47. fi;
  48. echo -e "\t\t\tFiles to transfer: $FILES";
  49. expect -c "
  50. set timeout -1
  51. set count 0
  52. log_user 0
  53. spawn rsync $ARGS;
  54. expect {
  55. to-chk {
  56. set count \"[expr \$count + 1]\"
  57. send_user \"\t\t\t\tDone \";
  58. send_user \$count;
  59. send_user \"/$FILES\n\";
  60. exp_continue
  61. }
  62. yes/no {
  63. send yes\r
  64. exp_continue
  65. }
  66. password: {
  67. log_user 0;
  68. send $(echo ${site[password]} | base64 --decode | gpg -d -q --no-mdc-warning)\r;
  69. send_user \"\t\t\tLogged In\n\"
  70. exp_continue
  71. }
  72. eof {
  73. send_user \"\t\t\tDone\n\"
  74. exit
  75. }
  76. }
  77. send_user \"\t\tTimed Out\n\"
  78. ";
  79. }
  80. echo -e "\tUploading files to ${site[root]}";
  81. rsyncd $root/skel/;
  82. rsyncd $root/repo/latest/;
  83. echo -e "\tDone.";
  84. ;;
  85. ssh)
  86. escp(){
  87. length=$(($#-1));
  88. files=${@:1:$length};
  89. expect -c "
  90. set timeout 1
  91. log_user 0
  92. spawn scp -r $files \"$user@$host:${site[root]}/${!#}\"
  93. expect {
  94. yes/no {
  95. send yes\r;
  96. exp_continue
  97. }
  98. password: {
  99. send $(echo ${site[password]} | base64 --decode | gpg -d -q --no-mdc-warning)\r
  100. set timeout -1
  101. }
  102. eof {
  103. exit
  104. }
  105. }";
  106. }
  107. essh(){
  108. args="$@";
  109. expect -c "
  110. set timeout 1
  111. log_user 0
  112. spawn ssh $user@$host $args
  113. expect {
  114. yes/no {
  115. send yes\r;
  116. exp_continue
  117. }
  118. password: {
  119. send $(echo ${site[password]} | base64 --decode | gpg -d -q --no-mdc-warning)\r
  120. set timeout -1
  121. }
  122. eof {
  123. exit
  124. }
  125. }";
  126. }
  127. upload_local_dir(){
  128. find $root/$1 -mindepth 1 | while read file; do
  129. if [ -f "$file" ] && [[ "$(basename $file)" != '.gitignore' ]] && [[ "$(basename $file)" != '$reponame.db' ]];then
  130. echo -e "\t\tF: $(basename $file)";
  131. nfile=${file:((${#root}+${#1}+2))};
  132. escp $file $nfile;
  133. elif [ -d "$file" ];then
  134. file=${file:((${#root}+${#1}+2))};
  135. echo -e "\t\tD: $file";
  136. essh mkdir -p ${site[root]}/$2/$file;
  137. fi;
  138. done;
  139. }
  140. echo -e "\tUploading files to ${site[root]}";
  141. essh mkdir -p ${site[root]}/;
  142. upload_local_dir skel;
  143. if $force; then
  144. upload_local_dir repo/latest;
  145. else
  146. upload_local_dir repo/updates;
  147. ls -l $root/repo/latest/ | egrep '^d' | awk '{print $9}' | while read arch; do
  148. if [ -d "$root/repo/latest/$arch" ];then
  149. escp $root/repo/latest/$arch/$reponame.db.tar.gz /$arch/$reponame.db.tar.gz;
  150. essh cd ${site[root]}/$arch && rm -f $reponame.db && ln -s $reponame.db.tar.gz $reponame.db;
  151. fi;
  152. done;
  153. fi;
  154. echo -e "\tDone.";
  155. ;;
  156. *)
  157. echo -e "\tType '${site[type]}' not implemented.";
  158. esac;
  159. fi;
  160. done;
  161. if ! $force; then
  162. ls -l $root/repo/updates/ | egrep '^d' | awk '{print $9}' | while read arch; do
  163. rm -f $root/repo/updates/$arch/*.pkg.tar.xz;
  164. done;
  165. fi;