Advertisement
s243a

make_test_repo (2)

Dec 26th, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.02 KB | None | 0 0
  1. #!/bin/bash
  2. # https://gitlab.com/sc0ttj/Pkg/merge_requests/21#note_262770355
  3. # /etc/apt/sources.list should have the following line (non-free & contrib are optional)
  4. #deb http://ftp.de.debian.org/debian buster main non-free contrib
  5. . /etc/DISTRO_SPECS
  6. UPDATE_REPO=yes #The repo will be updated if it doesn't exist.
  7. INSTALL_WEBSERVER=yes #On puppylinux if webserver="busybox_httpd" sinccy httpd is included in puppy's busybox.
  8. START_WEBSERVER=yes
  9. UPDATE_HOSTS_FILE=yes #Adds an item to the host file for the test repo
  10. DOWNLOAD_PACKAGES=yes #This will be done anyway if UPDATE_REPO=yes
  11. TEST_CMD=ppa2pup
  12. #webserver=nginx
  13. USE_CONFIG_FILE_FOR_WEB_SERVER=yes #
  14. webserver="busybox_httpd" #httpd is part of busybox, so if httpd is specified we shouldn't install any web server.
  15. # Try running these web servers before installing anything
  16.  
  17. web_server_fallbacks=( "nginx" "apache" "lighttpd" "httpd" )
  18. #If for some reason there is no webserver installed, then try installing one of these.  
  19.  
  20. web_server_install_fallbacks=( "${web_server_fallbacks[@]}" )
  21.  
  22.  
  23.  
  24. port=80 #If port isn't 80 then maybe we need to run pkg with proxychains.
  25.  
  26. config_file="" #Leave this black for the default settings
  27. #config_file=/etc/httpd2.conf
  28.  
  29. repo_name_in=${DISTRO_BINARY_COMPAT}-main #Comment
  30. repo_url_in=http://archive.ubuntu.com/ubuntu/ #TODO: look this up if it isn't specified
  31.  
  32. repo_url_out=http://archive.ubuntu.cherrypicked.com/repo/ubuntu/
  33. repo_name_out=${repo_name_in//-main/-cherrypicked}
  34.  
  35. [ -z "$repo_url_out" ] && localhost
  36.  
  37. #Uncoment the next line to use a previously generated awk program
  38. #AWK_PRG_Path_in=/usr/share/pkg/testing/filter_repo
  39. #Uncoment the next line to generate a new awk program
  40. AWK_PRG_Path_out=/usr/share/pkg/testing/filter_repo
  41. #Or comment out both lines above to use an inline (i.e. string) based awk program
  42.  
  43. ########### Chose a repo file to filter
  44.  
  45. distro_in=${DISTRO_BINARY_COMPAT}; distro_out="$distro_in"
  46. distro_ver_in=${DISTRO_COMPAT_VERSION}; distro_ver_out="$distro_ver_in"
  47. stream_in=main; stream_out=cherrypicked
  48.  
  49. ########### Some Document Paths
  50.  
  51. #https://stackoverflow.com/questions/10674867/nginx-default-public-www-location
  52. #www_root=/usr/share/nginx/html #This is the default web folder for nginx in ubuntu
  53. www_root=/var/www/html #This was also suggested
  54. #www_root=/var/www/ #This is the default web folder for apache.
  55. REPO_DB_DOC_FILE_in=/var/packages/Packages-${distro_in}-${distro_ver_in}-${stream_in}
  56. #REPO_DB_DOC_FILE_out=~/Packages-${distro_ver_out}-${distro_ver_out}-${stream_out}
  57.  
  58. TMPDIR=/tmp
  59. DL_FNAME=ppa_Packages.gz
  60. DL_FILE_EX=.gz
  61.  
  62. arch='i386'
  63. case $(uname -m) in
  64.   i*86) arch='i386'  ;;
  65.   *64)  arch='amd64' ;;
  66. esac
  67.  
  68. repo_root_rel_path=repo/${distro_out}
  69. repo_root_path=${www_root}/${repo_root_rel_path}
  70. doc_path=${repo_root_path}/dists/${distro_ver_out}/${stream_out}/binary-${arch} #/Packages.gz
  71.  
  72.  
  73. mkdir -p $doc_path #This is where we will save the repo db doc file on our system.
  74.  
  75. download_url_in=$(echo "$repo_url_in" | sed -e 's#/$##g')/dists/${distro_ver_in}/${stream_in}/binary-${arch}/Packages.gz
  76. #download_url_out=$(echo "$repo_url_in" | sed -e 's#/$##g')/dists/${distro_ver_in}/${stream_in}/binary-${arch}/Packages.gz
  77.  
  78.  
  79. function is_running(){
  80.   local repo_url=$1
  81.   repo_urls=( "$repo_url" "localhost" "127.0.0.1" )
  82.   for a_repo_url in "${repo_urls[@]}"; do
  83.     if [ -z "$(wget --spider -S "$repo_url" 2>&1 | awk '/HTTP\// {print $2}')" ]; then
  84.       if [ "$repo_url" != "$a_repo_url" ]; then
  85.         echo "Webserver is running but hostname mapping is broken" >&2
  86.         echo "Check hosts file, dns cache or dns server"  >&2
  87.       fi
  88.       echo "true"
  89.     else
  90.       echo "false"
  91.     fi
  92.   done
  93. }
  94. function set_webserver_options(){
  95.   local webserver=$1
  96.   case "$webserver" in
  97.   *)
  98.     options=()
  99.     for option in -p -h -c; do
  100.       case "$option" in
  101.       -p) [ ! -z "$PORT" ] && options+=( "$_p" "$PORT" ); ;;
  102.       -h) [ ! -z "$www_root" ] && options+=( "$_h" "$www_root" ); ;;
  103.       -c)
  104.         [ -z "$config_file" ] && config_file="$set_config_file_rtn"
  105.         [ ! -z "$config_file" ] && options+=( "$_c" "$config_file" ); ;;
  106.       esac
  107.     done
  108.     "$WEB_SERVER_CMD" ${options[@]}; ;;
  109.   esac
  110. }
  111. function set_config_file(){
  112.   local webserver="$1"
  113.   if [ -z "$config_file" ] && [ "$USE_CONFIG_FILE_FOR_WEB_SERVER" = yes ] ; then
  114.     case "$webserver" in
  115.     httpd|busybox_httpd)
  116.       set_config_file_rtn=/usr/share/pkg/testing/webserver/httpd/httpd.conf
  117.       if [ ! -f "$set_config_file_rtn" ]; then
  118.         mkdir -p "$(dirname "$set_config_file_rtn")"
  119.         echo "directory auto index" > "$set_config_file_rtn"
  120.       fi
  121.     esac
  122.      
  123.   fi
  124. }
  125. function run_webserver(){
  126.   local webserver=$1
  127.   set_config_file $1
  128.   set_webserver_options $1
  129.   "$WEB_SERVER_CMD" ${options[@]}
  130.   if [  ! "`is_running`" = true ]; then
  131.     if [ "`is_running`" = true ]; then
  132.       do_action_result=success
  133.     else
  134.       do_action_result=failed
  135.     fi
  136.   else
  137.     echo "Webserver already running" >&2
  138.   fi    
  139. }
  140. function get_cmd(){
  141.   local webserver="$1"
  142.    
  143.   #First Get the webserver command 
  144.   case "$webserver" in
  145.   "busybox_httpd"|"busybox")
  146.     if  [ $(busybox --list | grep -c httpd) -gt 0 ]; then
  147.       WEB_SERVER_CMD="busybox_httpd"
  148.     fi; ;;
  149.   *)
  150.      if [ ! -z "`which "$webserver"`" ]; then
  151.        WEB_SERVER_CMD="$webserver"
  152.      fi; ;;
  153.   esac
  154.   #TODO then get the otions for the web server
  155.   case "$webserver" in
  156.   *)
  157.     _p="-p" #The port Number
  158.     _c="-c" #the option for the configuation file
  159.     _h="-h" #the option for the webserver root (AKA home directory"
  160.   esac
  161. }  
  162. function do_action(){
  163.   local web_server="$1"; shift
  164.   local cmd="$1"; shift
  165.   case "$cmd" in
  166.   get_cmd)
  167.     get_cmd "$web_server"
  168.     if [ -z "$WEB_SERVER_CMD" ]; then
  169.       do_action_result=failed
  170.     else
  171.       do_action_result=success
  172.     fi; ;;
  173.   run|run-install)
  174.     [ -z "$WEB_SERVER_CMD" ] && get_cmd "$web_server"
  175.     if [ -z "$WEB_SERVER_CMD" ] && [ "$INSTALL_WEBSERVER" = yes ]; then
  176.       if [ "$cmd" = "run-install" ] || [ "$2" = "install" ]; then
  177.         pkg --get "$web_server"
  178.         get_cmd "$web_server"
  179.       fi
  180.     fi
  181.     [ -z "$WEB_SERVER_CMD" ] &&  run $@
  182.       if [ "$(is_running)" = true ]; then
  183.         do_action_result=success
  184.       else
  185.         run_webserver "$web_server" #set_webserver_options
  186.         #do_action_result=failed
  187.       fi
  188.   esac    
  189. }
  190. function do_actions(){
  191.   for action in "$@"; do
  192.     if [ "$1" = get_cmd ] || [ "$1" = run -a "$2" != "install" ]; then
  193.       web_servers=( "$webserver" "${web_server_fallbacks[@]}" )
  194.     else
  195.       web_servers=( "$webserver" "${web_server_install_fallbacks[@]}" )
  196.     fi 
  197.     for a_web_server in "${web_servers[@]}"; do
  198.       case "$a_web_server" in
  199.       "busybox_httpd"|httpd|busybox)
  200.         do_action "httpd" "$@";
  201.         ;;
  202.       *)
  203.         do_action "$a_web_server" "$a_web_server"; ;;
  204.       esac
  205.       if [ "$do_action_result" = success ]; then
  206.         break 2
  207.       fi  
  208.     done
  209.   done
  210. }
  211.  
  212. function echo_filter_line(){
  213.     read a_pkg_name
  214.     echo "pkg_filter[\""$a_pkg_name"\"]=\"true\""
  215. }
  216. [ ! -f "${doc_path}/Packages.gz" ] && UPDATE_REPO=yes
  217. if [ "$UPDATE_REPO" = "yes" ]; then
  218.   export PKG_KEEP_EPOCH_IN_DOC_REPO=true
  219.   pkg --repo-update
  220.  
  221.  
  222. #fi
  223.   DL_FNAME_Path=${TMPDIR}/${DL_FNAME}
  224.   rm "$DL_FNAME_Path"
  225.   DL_DOC_FILE_PATH=${DL_FNAME_Path%"$DL_FILE_EX"}
  226.   rm "$DL_DOC_FILE_PATH"
  227.  
  228.   wget --quiet "$download_url_in" -O "$DL_FNAME_Path" 1>/dev/null \
  229.     || download_failed=true
  230.  
  231.   gunzip ${TMPDIR}/${DL_FNAME}
  232.  
  233.  
  234.  
  235.  
  236.  
  237.   AWK_PRG_1=\
  238. 'BEGIN {FS="|"; OFS="|"}
  239. { if ($1 ~ /^[^|]+:[^|]+$/  ){
  240.    print $1 "|" $2 "|" $3 #We might want to use some of these other fields for a different application
  241. }}'
  242.  
  243.   awk_filter_str='function init_filter(){'
  244.  
  245.   filter_lines_path=/tmp/filter_lines
  246.   rm "$filter_lines_path"
  247.   while read pkg_record; do
  248.     #PKG_NAME_ONLY="$(echo "$pkg_record" | cut -f2 -d'|')"
  249.     #write_filter_line "$PKG_NAME_ONLY"
  250.     echo "$pkg_record" | cut -f2 -d'|' | echo_filter_line
  251.   done < <( cat $REPO_DB_DOC_FILE_in | awk "$AWK_PRG_1" ) \
  252.   | sort -R | head -n 3 >> "$filter_lines_path" #| tr "\n" " " )
  253.   #cat $REPO_DB_DOC_FILE_in | awk "$AWK_PRG"
  254.   #read -p "Press enter to continue"
  255.   awk_filter_str="$awk_filter_str
  256. $(cat $filter_lines_path)"'
  257. }
  258. function filter_accept(s){ #Return true if we are to print the result
  259.  if ( pkg_filter[s] == "true" ){
  260.    return "true"
  261.  } else {
  262.    return "false"
  263.  }
  264. }'
  265.  
  266.   AWK_PRG="$awk_filter_str"'
  267. function stripEpoch(s){
  268.  sub(/^([0-9]*:)?/,"",s)
  269.  return  s
  270. }
  271. BEGIN {init_filter()}
  272. /^Package:/ { PKG=$0; sub(/^Package: /,"",PKG); FILTER_ACTION=filter_accept(PKG)}
  273. #/^Version:/ {VER=$0; sub(/^Package: /,"",VER);  
  274. {if (FILTER_ACTION == "true"){
  275.    print $0
  276.  }
  277. }'
  278.  
  279.   if [ -z "$AWK_PRG_Path_in" ] && [ -z "$AWK_PRG_Path_out" ]; then
  280.     awk "$AWK_PRG" "$DL_DOC_FILE_PATH" > ${doc_path}/Packages.gz
  281.   else
  282.     if [ -z "$AWK_PRG_Path_in" ]; then
  283.       echo "#!`which gawk` -f" > "$AWK_PRG_Path_out"
  284.       mkdir -p dirname "$(dirname $AWK_PRG_Path_out)"  
  285.       echo "$AWK_PRG" >> "$AWK_PRG_Path_out"
  286.       chmod +x "${AWK_PRG_Path_out}"
  287.       AWK_PRG_Path_in="${AWK_PRG_Path_out}"
  288.     fi
  289.     awk -f "$AWK_PRG_Path_in" "$DL_DOC_FILE_PATH"  > ${doc_path}/Packages
  290.   fi
  291.   gzip "${doc_path}/Packages" > "${doc_path}/Packages.gz"
  292. fi
  293. if [ "$UPDATE_REPO" = "yes" ] || [ "$DOWNLOAD_PACKAGES" = yes ]; then
  294.   AWK_PRG_3=\
  295. '/^Filename:/ {
  296.      system("wget --quiet \"$repo_url_in\" -O \"" RROOT "/" FPATH "\" 1>/dev/null")
  297.      }'
  298.    cat "${doc_path}/Packages" | awk -v "RROOT=\"$repo_root_path\"" \
  299.     "$AWK_PRG_3"
  300.   do_actions run install #Starts the web server
  301.  
  302.   if [ $(pkg --repo-list | grep -c "$repo_name_out") -eq 0 ]; then
  303.     ( exec <<< "$repo_name_out"
  304.       pkg add-repo "$repo_url_out" "$distro_ver_out" "$stream_out" )
  305.   fi
  306. fi  
  307. if [ "$UPDATE_HOSTS_FILE" = yes ]; then
  308.   protocol="${repo_url_out%%//*}"
  309.   protocol_stripped="${repo_url_out#*//}"
  310.   repo_domain_out="${protocol_stripped%%/*}"
  311.   repo_domain_out="$protocol//$repo_domain_out"
  312.   #TODO: maybe add port 80 ping test here.
  313.   if [ $(grep -c -F "$repo_domain_out" /etc/hosts) = 0 ]; then
  314.     echo "127.0.0.1" "$repo_domain_out" >> /etc/hosts
  315.     #http://murga-linux.com/puppy/viewtopic.php?p=1045805#1045805
  316.     service dns-clean restart || service nscd restart || \
  317.         systemctl restart nscd.service || nscd -l hosts
  318.   fi
  319. fi
  320. if [ "$START_WEBSERVER" = "yes" ]; then
  321.   do_actions run install
  322.  
  323.   #if [ -z "$(wget --spider -S "http://localhost" 2>&1 | awk '/HTTP\// {print $2}')" ]; then
  324.   #  case "$webserver" in
  325.   #  *)
  326.   #    options=()
  327.   #    for option in -p -h -c; do
  328.   #      case "$option" in
  329.   #      -p) [ ! -z "$PORT" ] && options+=( "$_p" "$PORT" ); ;;
  330.   #      -h) [ ! -z "$www_root" ] && options+=( "$_h" "$www_root" ); ;;
  331.   #      -c) [ ! -z "$config_file" ] && options+=( "$_c" "$config_file" ); ;;
  332.   #      esac
  333.   #    done
  334.   #    "$WEB_SERVER_CMD" ${options[@]}; ;;
  335.   #  esac
  336.   #fi
  337. fi
  338. case "$TEST_CMD" in
  339. ppa2pup) PKG_PPA2PUP_FN=ppa2pup pkg --repo-update; ;;
  340. ppa2pup_gawk) pkg --repo-update; ;;
  341. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement