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 # For macOS 68 if !(type "tac" &> /dev/null;) then 69 tac_cmd='tail -r' 70 else 71 tac_cmd=tac 72 fi 73 74 # Directory where to look for package files. 75 package_dir=$PWD 76 77 # Script debug. 78 verbose= 79 80 # Operation mode. 81 mode=install 82 83 dryrun= 84 85 while getopts "vhd:ut" argname; do 86 case "$argname" in 87 v) verbose=yes;; 88 t) dryrun=yes;; 89 u) mode=uninstall;; 90 d) package_dir="$OPTARG";; 91 h) help_usage; exit 0;; 92 ?) help_usage; exit 1;; 93 esac 94 done 95 shift $(( OPTIND - 1 )) 96 97 [ -d "$package_dir" ] || fatal_with_help_usage "Package directory [$package_dir] is not a directory" 98 99 [ -z "$verbose" ] || set -x 100 101 102 function find_packages_of_type () 103 { 104 # sort output alphabetically 105 find "$package_dir" -maxdepth 1 -type f -name '*.'"$1" | sort 106 } 107 108 function find_packages () 109 { 110 local package_suffixes=(deb rpm msi exe pkg dmg) 111 for suffix in "${package_suffixes[@]}"; do 112 if [ "$mode" == "uninstall" ]; then 113 packages=$(find_packages_of_type $suffix | $tac_cmd) 114 else 115 packages=$(find_packages_of_type $suffix) 116 fi 117 if [ -n "$packages" ]; then 118 package_type=$suffix 119 break; 120 fi 121 done 122 } 123 124 125 # RPM 126 install_cmd_rpm () 127 { 128 echo sudo rpm --install "$@" 129 } 130 uninstall_cmd_rpm () 131 { 132 local package_name=$(rpm -qp --queryformat '%{Name}' "$@") 133 echo sudo rpm -e "$package_name" 134 } 135 136 # DEB 137 install_cmd_deb () 138 { 139 echo sudo dpkg -i "$@" 140 } 141 uninstall_cmd_deb () 142 { 143 local package_name=$(dpkg-deb -f "$@" Package) 144 echo sudo dpkg -r "$package_name" 145 } 146 147 # MSI 148 install_cmd_msi () 149 { 150 echo msiexec /qn /norestart /i $(cygpath -w "$@") 151 } 152 uninstall_cmd_msi () 153 { 154 echo msiexec /qn /norestart /x $(cygpath -w "$@") 155 } 156 157 # EXE 158 install_cmd_exe () 159 { 160 echo "$@" 161 } 162 uninstall_cmd_exe () 163 { 164 error No implemented 165 } 166 167 # PKG 168 install_cmd_pkg () 169 { 170 echo sudo /usr/sbin/installer -allowUntrusted -pkg "\"$@\"" -target / 171 } 172 uninstall_cmd_pkg () 173 { 174 local pname=`basename $@` 175 local appname="$(cut -d'-' -f1 <<<"$pname")" 176 if [ "$appname" = "CommonInstallDirTest" ]; then 177 echo sudo rm -rf "/Applications/jpackage/\"$appname.app\"" 178 else 179 echo sudo rm -rf "/Applications/\"$appname.app\"" 180 fi 181 } 182 183 # DMG 184 install_cmd_dmg () 185 { 186 local pname=`basename $@` 187 local appname="$(cut -d'-' -f1 <<<"$pname")" 188 local command=() 189 if [ "$appname" = "CommonLicenseTest" ]; then 190 command+=("{" yes "|" hdiutil attach "\"$@\"" ">" /dev/null) 191 else 192 command+=("{" hdiutil attach "\"$@\"" ">" /dev/null) 193 fi 194 195 command+=(";" sudo cp -R "\"/Volumes/$appname/$appname.app\"" /Applications ">" /dev/null) 196 command+=(";" hdiutil detach "\"/Volumes/$appname\"" ">" /dev/null ";}") 197 198 echo "${command[@]}" 199 } 200 uninstall_cmd_dmg () 201 { 202 local pname=`basename $@` 203 local appname="$(cut -d'-' -f1 <<<"$pname")" 204 echo sudo rm -rf "/Applications/\"$appname.app\"" 205 } 206 207 # Find packages 208 packages= 209 find_packages 210 if [ -z "$packages" ]; then 211 echo "No packages found in $package_dir directory" 212 exit 213 fi 214 215 # Build list of commands to execute 216 declare -a commands 217 IFS=$'\n' 218 for p in $packages; do 219 commands[${#commands[@]}]=$(${mode}_cmd_${package_type} "$p") 220 done 221 222 if [ -z "$dryrun" ]; then 223 # Run commands 224 for cmd in "${commands[@]}"; do 225 echo Running: $cmd 226 eval $cmd || true; 227 done 228 else 229 # Print commands 230 for cmd in "${commands[@]}"; do echo $cmd; done 231 fi