pkg.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. . paths.conf
  2. function diff_db() {
  3. while read -r repo; do
  4. for i in added deleted modified; do
  5. rm -f ${DL_CACHE}/${i}
  6. touch ${DL_CACHE}/${i}
  7. done
  8. diff -N ${REPO_STORE2}/$(basename ${repo}) ${repo} \
  9. | awk -v added="${DL_CACHE}/added" -v deleted="${DL_CACHE}/deleted" \
  10. '/^</ { print substr($0, 3) >> deleted } /^>/ { print substr($0, 3) >> added }'
  11. cat "${DL_CACHE}/added" "${DL_CACHE}/deleted" | sort | \
  12. awk -v line="" 'line == $1 { print $1; line=$1; next } { line=$1 }' > "${DL_CACHE}/modified"
  13. grep -vf "${DL_CACHE}/modified" "${DL_CACHE}/added" > "${DL_CACHE}/added.0"
  14. mv "${DL_CACHE}/added.0" "${DL_CACHE}/added"
  15. grep -vf "${DL_CACHE}/modified" "${DL_CACHE}/deleted" > "${DL_CACHE}/deleted.0"
  16. mv "${DL_CACHE}/deleted.0" "${DL_CACHE}/deleted"
  17. for option in "added" "deleted" "modified"; do
  18. while read -r line; do
  19. case $(basename ${option}) in
  20. 'added')
  21. echo "NEW -- ${line}"
  22. ;;
  23. 'modified')
  24. echo "MOD -- ${line}"
  25. ;;
  26. 'deleted')
  27. echo "DEL -- ${line}"
  28. ;;
  29. esac
  30. done < "${DL_CACHE}/${option}"
  31. done
  32. done < <(find ${REPO_STORE} -regex '.*' -type f)
  33. }
  34. function store_db() {
  35. _log info "Backing up database"
  36. while read -r line; do
  37. cp ${line} ${REPO_STORE2}/$(basename ${line})
  38. done < <(find ${REPO_STORE} -type f)
  39. return 0
  40. }
  41. function update_db() {
  42. while read -r line; do
  43. [ "$(echo $line | sed 's/[[:space:]]*#.*/comment/')" = "comment" ] && continue
  44. URL=$(echo $line | cut -f3 -d' ')
  45. REPO=$(echo $line | cut -f2 -d' ')
  46. METHOD=$(echo $line | cut -f1 -d' ')
  47. _log info "pulling ${URL} using ${METHOD}"
  48. case ${METHOD} in
  49. SCP)
  50. scp ${URL}/index ${DL_CACHE}/${REPO} 2>&1 > /dev/null
  51. ;;
  52. esac
  53. if [ $? -ne 0 ]; then
  54. _log error "Could not pull ${URL}"
  55. fi
  56. gzip -dc ${DL_CACHE}/${REPO} 2>/dev/null | sort > ${REPO_STORE}/${REPO}
  57. if [ $? -ne 0 ]; then
  58. _log error "cannot unpack ${REPO}"
  59. fi
  60. done < ./repos.conf
  61. }
  62. function run_pkg() {
  63. PKG_NAME=$(lookup_pkg "${1}" | cut -s -f2 -d' ')
  64. DEPS="$(lookup_pkg \"${1}\" | cut -s -f3 -d' ' | tr ',' ' ')"
  65. LOG="${LOG_STORE}/${2}/"
  66. if ! [ -d "${2}" ]; then
  67. mkdir ${LOG}
  68. touch ${LOG}/kickoff
  69. fi
  70. printf 'TIME %s %s\n' ${PKG_NAME} $(date --rfc-3339=seconds | tr ' ' '_') >> ${LOG}/kickoff
  71. printf 'START %s %s\n' ${PKG_NAME} ${1} >> ${LOG}/kickoff
  72. bash ${PKG_STORE}/${PKG_NAME} 2>&1 >> ${LOG}/${PKG_NAME}
  73. }
  74. function lookup_pkg() {
  75. PKG_NAME="${1}"
  76. GREP_RESULT="$(grep -H -m1 "^${PKG_NAME} " ${REPO_STORE}/* | tr ' ' ';')"
  77. if [ "$(echo "${GREP_RESULT}" | wc -l)" -gt 1 ]; then
  78. REPOS=''
  79. for i in ${GREP_RESULT}; do
  80. REPOS="${REPOS} $(basename $(echo ${i} | cut -f1 -d':'))"
  81. done
  82. _log error "${PKG_NAME} exists in ${REPOS}" >&2
  83. return 1
  84. fi
  85. [ -z "${GREP_RESULT}" ] && return 3
  86. GREP_RESULT="$(echo ${GREP_RESULT} | tr ';' ' ')"
  87. printf '%s %s %s\n' "$(basename $(echo ${GREP_RESULT} | cut -f1 -d':'))" "$(echo ${GREP_RESULT} | cut -f2 -d' ')" "$(echo ${GREP_RESULT} | cut -s -f3 -d' ')"
  88. return $?
  89. }
  90. function depends_pkg() {
  91. PKG_NAME="${1}"
  92. lookup_pkg ${PKG_NAME} | cut -s -f3- -d' ' | tr ',' ' '
  93. return 0
  94. }
  95. function download_pkg() {
  96. REPO_NAME="${1}"
  97. PKG_NAME="${2}"
  98. REPO_BUFF="$(grep ${REPO_NAME} ./repos.conf)"
  99. METHOD="$(echo ${REPO_BUFF} | cut -f1 -d' ')"
  100. REPO_URL="$(echo ${REPO_BUFF} | cut -f3- -d' ')"
  101. _log info "downloading ${PKG_NAME} from ${REPO_NAME}"
  102. case ${METHOD} in
  103. SCP)
  104. scp ${REPO_URL}/${PKG_NAME} ${DL_CACHE}/${PKG_NAME}.download 2>&1 > /dev/null
  105. return
  106. ;;
  107. HTTP)
  108. _log error 'not supported'
  109. # curl -sLi -F action=GET_JOB -F file_name=${PKG_NAME} ${STORESERVER} > ${DL_CACHE}/${PKG_NAME}.download
  110. ;;
  111. #TODO: Add plugin support
  112. esac
  113. [ $? -ne 0 ] && return 1;
  114. awk -v header="${DL_CACHE}/${PKG_NAME}.header" -v body="${DL_CACHE}/${PKG_NAME}.body" -v bl=1 \
  115. 'bl{bl=0; h=($0 ~ /HTTP\/1/)} /^\r?$/{bl=1} {gsub($0, /\r\n/, ""); print $0>(h?header:body)}' ${DL_CACHE}/${PKG_NAME}.download
  116. rm -f ${DL_CACHE}/${PKG_NAME}.download
  117. awk '/HTTP/ { print "status:" $2 } /Date/ { print $0 }' "${DL_CACHE}/${PKG_NAME}.header" \
  118. > "${DL_CACHE}/${PKG_NAME}.header".tmp 2>/dev/null && mv "${DL_CACHE}/${PKG_NAME}.header.tmp" "${DL_CACHE}/${PKG_NAME}.header"
  119. [ $? -ne 0 ] && { return 2; }
  120. sed -i '/^\r*\n*$/d' "${DL_CACHE}/${PKG_NAME}.header" "${DL_CACHE}/${PKG_NAME}.body"
  121. HTTP_OK=$(awk -F':' '/^status/{ if($2 == "200") { exit 0 } else { exit 1 }}' "${DL_CACHE}/${PKG_NAME}.header")
  122. return ${HTTP_OK}
  123. }
  124. function prepare_pkg() {
  125. PKG_NAME="${1}"
  126. ! [ -f "${DL_CACHE}/${PKG_NAME}.download" ] && { return 1; }
  127. base64 -d "${DL_CACHE}/${PKG_NAME}.download" > "${DL_CACHE}/${PKG_NAME}.stage1"
  128. awk -v p=1 -v meta="${PKG_META_STORE}/${PKG_NAME}" '/^---$/{ p=0;next } p { print $0 > meta } !p{ print $0 }' ${DL_CACHE}/${PKG_NAME}.stage1 \
  129. | gunzip 2>/dev/null > ${DL_CACHE}/${PKG_NAME}.stage2
  130. cp ${DL_CACHE}/${PKG_NAME}.stage2 ${PKG_STORE}/${PKG_NAME}
  131. cat ${PKG_META_STORE}/${PKG_NAME}
  132. }