repo-update 4.4 KB

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