s243a

Draft: make_test_reppo

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