1 #!/bin/bash
   2 
   3 #
   4 # Script to install/uninstall packages produced by jpackage jtreg
   5 # tests doing platform specific packaging.
   6 #
   7 # The script will install/uninstall all packages from the files
   8 # found in the current directory or the one specified with command line option.
   9 #
  10 # When jtreg jpackage tests are executed with jpackage.test.output
  11 # Java property set, produced package files (msi, exe, deb, rpm, etc.) will
  12 # be saved in the directory specified with this property.
  13 #
  14 # Usage example:
  15 # # Set directory where to save package files from jtreg jpackage tests
  16 # JTREG_OUTPUT_DIR=/tmp/jpackage_jtreg_packages
  17 #
  18 # # Run tests and fill $JTREG_OUTPUT_DIR directory with package files
  19 # jtreg -Djpackage.test.output=$JTREG_OUTPUT_DIR ...
  20 #
  21 # # Install all packages
  22 # manage_pachages.sh -d $JTREG_OUTPUT_DIR
  23 #
  24 # # Uninstall all packages
  25 # manage_pachages.sh -d $JTREG_OUTPUT_DIR -u
  26 #
  27 
  28 #
  29 # When using with MSI installers, Cygwin shell from which this script is
  30 # executed should be started as administrator. Otherwise silent installation
  31 # won't work.
  32 #
  33 
  34 # Fail fast
  35 set -e; set -o pipefail;
  36 
  37 
  38 help_usage ()
  39 {
  40     echo "Usage: `basename $0` [OPTION]"
  41     echo "Options:"
  42     echo "  -h        - print this message"
  43     echo "  -v        - verbose output"
  44     echo "  -d <dir>  - path to directory where to look for package files"
  45     echo "  -u        - uninstall packages instead of the default install"
  46     echo "  -t        - dry run, print commands but don't execute them"
  47 }
  48 
  49 error ()
  50 {
  51   echo "$@" > /dev/stderr
  52 }
  53 
  54 fatal ()
  55 {
  56   error "$@"
  57   exit 1
  58 }
  59 
  60 fatal_with_help_usage ()
  61 {
  62   error "$@"
  63   help_usage
  64   exit 1
  65 }
  66 
  67 
  68 # Directory where to look for package files.
  69 package_dir=$PWD
  70 
  71 # Script debug.
  72 verbose=
  73 
  74 # Operation mode.
  75 mode=install
  76 
  77 dryrun=
  78 
  79 while getopts "vhd:ut" argname; do
  80     case "$argname" in
  81         v) verbose=yes;;
  82         t) dryrun=yes;;
  83         u) mode=uninstall;;
  84         d) package_dir="$OPTARG";;
  85         h) help_usage; exit 0;;
  86         ?) help_usage; exit 1;;
  87     esac
  88 done
  89 shift $(( OPTIND - 1 ))
  90 
  91 [ -d "$package_dir" ] || fatal_with_help_usage "Package directory [$package_dir] is not a directory"
  92 
  93 [ -z "$verbose" ] || set -x
  94 
  95 
  96 function find_packages_of_type ()
  97 {
  98     # sort output alphabetically
  99     find "$package_dir" -maxdepth 1 -type f -name '*.'"$1" | sort
 100 }
 101 
 102 function find_packages ()
 103 {
 104     local package_suffixes=(deb rpm msi exe)
 105     for suffix in "${package_suffixes[@]}"; do
 106         if [ "$mode" == "uninstall" ]; then
 107             packages=$(find_packages_of_type $suffix | tac)
 108         else
 109             packages=$(find_packages_of_type $suffix)
 110         fi
 111         if [ -n "$packages" ]; then
 112             package_type=$suffix
 113             break;
 114         fi
 115     done
 116 }
 117 
 118 
 119 # RPM
 120 install_cmd_rpm ()
 121 {
 122     echo sudo rpm --install "$@"
 123 }
 124 uninstall_cmd_rpm ()
 125 {
 126     local package_name=$(rpm -qp --queryformat '%{Name}' "$@")
 127     echo sudo rpm -e "$package_name"
 128 }
 129 
 130 # DEB
 131 install_cmd_deb ()
 132 {
 133     echo sudo dpkg -i "$@"
 134 }
 135 uninstall_cmd_deb ()
 136 {
 137     local package_name=$(dpkg-deb -f "$@" Package)
 138     echo sudo dpkg -r "$package_name"
 139 }
 140 
 141 # MSI
 142 install_cmd_msi ()
 143 {
 144     echo msiexec /qn /norestart /i $(cygpath -w "$@")
 145 }
 146 uninstall_cmd_msi ()
 147 {
 148     echo msiexec /qn /norestart /x $(cygpath -w "$@")
 149 }
 150 
 151 # EXE
 152 install_cmd_exe ()
 153 {
 154     echo "$@"
 155 }
 156 uninstall_cmd_exe ()
 157 {
 158     error No implemented
 159 }
 160 
 161 
 162 # Find packages
 163 packages=
 164 find_packages
 165 if [ -z "$packages" ]; then
 166     echo "No packages found in $package_dir directory"
 167     exit
 168 fi
 169 
 170 # Build list of commands to execute
 171 declare -a commands
 172 for p in $packages; do
 173     commands[${#commands[@]}]=$(${mode}_cmd_${package_type} "$p")
 174 done
 175 
 176 if [ -z "$dryrun" ]; then
 177     # Run commands
 178     for cmd in "${commands[@]}"; do
 179         echo Running: $cmd
 180         $cmd || true;
 181     done
 182 else
 183     # Print commands
 184     for cmd in "${commands[@]}"; do echo $cmd; done
 185 fi