Last active
August 29, 2015 14:26
-
-
Save bfosterscripps/1bc1fdfa2607dfceac40 to your computer and use it in GitHub Desktop.
Create and install packages based on a list of filters and a directory full of packages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Create a package with a given set of root paths | |
# HTA-1305 | |
declare -a filterPaths=( | |
/content/dam/images/hgtv/fullset/2010/5/27/0/CI-Jamie-Durie_outdoor-room-LA-horjd107_s4x3.jpg | |
/content/dam/images/hgtv/fullset/2013/6/11/1/Original_Marianne-Canada-Crafternoon-Boat-Bag-Step1_h.jpg | |
/content/dam/images/hgtv/fullset/2015/1/0/RX-HGMAG026_Felt-DIY-075-a-4x3.jpg | |
/content/dam/images/hgtv/fullset/2012/2/1/0/CI-Thibaut_Barrowgate-living-room-wallpaper-settee_s3x4.jpg | |
/content/dam/images/hgtv/fullset/2015/1/16/0/Tobi-Fairley_Riverside-Penthouse_living-room.jpg | |
/content/dam/images/hgtv/fullset/2015/1/30/0/Hyde-Evans-Design_Oregon-Coast_Porthole-View.jpg | |
/content/dam/images/diy/unsized/2015/7/20/0/bc2015_front-yard_17_front-entrance-92x69.jpg | |
/content/hgtv-com/en/HTA-1305 | |
/content/diy-com/en/HTA-1305 | |
/content/gac-com/en/HTA-1305 | |
/content/dam/images/diy/fullset/2015/6/29/1/bc2015_front-yard_06_high-angle-view-house_h.jpg | |
/content/dam/images/hgtv/fullset/2013/1/15/1/DP_Judith-Balis-Eclectic-Kitchen-Breakfast-Area_s4x3.jpg | |
/content/dam/images/hgtv/fullset/2015/1/0/Bryan-Sebring_Steve-and-Anne-Basement-door.jpg | |
/content/dam/images/hgtv/fullset/2014/10/29/0/dh2015_front-yard_01_hero-shot_h.jpg | |
/content/dam/images/hgtv/fullset/2009/1/12/0/HINPR101-LivRoomAfter_s4x3.jpg | |
/content/dam/images/diy/unsized/2015/7/20/0/bc2015_front-yard_17_front-entrance-92x69.jpg | |
) | |
# change this when you implement it yourself, as well as the username below! | |
groupName="Brandon" | |
function requestPass() { | |
username="162084" | |
#get password from user | |
echo -n "Enter password. >" | |
read -s answer | |
pass="$answer" | |
echo "" | |
} | |
mkdir -p packages | |
function specifyPackageName() { | |
read -p "Please specify package name. > " packageName | |
} | |
function specifyEnvironment() { | |
#check to see if input was provided | |
if [ $# -eq 0 ]; then | |
#echo "No arguments supplied." | |
#reset environment so you can install to different place than build | |
env="" | |
read -p "Please specify Environment, e.g. dev4, prod2, or local. > " answer | |
specifyEnvironment $answer | |
echo -n "" | |
else | |
echo "You passed $1 into this and number of args is $#" | |
env=$1 | |
if [[ "$env" != "local" ]]; then | |
requestPass | |
testEnvHostname="author1.hgtv-$env.sni.hgtv.com:4502" | |
envHttpCode="$(curl -u $username:$pass -sL --head -w %{http_code} http://$testEnvHostname -o /dev/null)" | |
if [[ "$envHttpCode" = "200" ]]; then | |
hostname="$testEnvHostname" | |
else | |
echo "$testEnvHostname returns a code of $envHttpCode," | |
read -p "Please specify Environment, e.g. dev4, prod2, or local. > " answer | |
specifyEnvironment $answer | |
fi | |
elif [[ "$env" =~ "local" ]]; then | |
echo "Environment is local: $env" | |
#local hostname | |
hostname="localhost:4502" | |
username="admin" | |
pass="admin" | |
fi | |
fi | |
} | |
function createPackage() { | |
echo "What would you like to name the package you are creating?" | |
specifyPackageName | |
echo "Which environment would you like to create this package on?" | |
specifyEnvironment | |
updateCurl="curl -u $username:$pass -X POST -F packageName=$packageName -F groupName=$groupName -F filter=" | |
count="0" | |
last="${#filterPaths[@]}" # the total number of elements in the path array above | |
for path in "${filterPaths[@]}" | |
do | |
count=`expr $count + 1` | |
if [ $count -eq 1 ] && [ $count -eq $last ]; then # this is the first and only filter path | |
updateCurl="$updateCurl[{\"root\":\"$path\",\"rules\":[]}]" | |
else | |
if [ $count -eq 1 ]; then #this is the first element, start opening [ and forego the comma | |
updateCurl="$updateCurl[{\"root\":\"$path\",\"rules\":[]}" | |
else | |
if [ $count -eq $last ]; then #this is the last element, have ending ] | |
updateCurl="$updateCurl,{\"root\":\"$path\",\"rules\":[]}]" | |
else | |
updateCurl="$updateCurl,{\"root\":\"$path\",\"rules\":[]}" | |
fi | |
fi | |
fi | |
done | |
updateCurl="$updateCurl -F packageName=$packageName -F path=/etc/packages/$groupName/$packageName.zip http://$hostname/crx/packmgr/update.jsp" | |
createCurl="curl -u $username:$pass -X POST http://$hostname/crx/packmgr/service/.json/etc/packages/$groupName/$packageName.zip?cmd=create -F packageName=$packageName -F groupName=$groupName" | |
buildCurl="curl --connect-timeout 3600 --max-time 3600 -u $username:$pass -X POST http://$hostname/crx/packmgr/service/.json/etc/packages/$groupName/$packageName.zip?cmd=build" | |
echo "Creating package!" | |
echo "$($createCurl)" | |
echo "Adding filters to package!" | |
echo "$($updateCurl)" | |
echo "Building package!" | |
echo "$($buildCurl)" | |
downloadCurl="curl -o ./packages/$packageName.zip -u $username:$pass http://$hostname/etc/packages/$groupName/$packageName.zip" | |
echo "Downloading package!" | |
echo "$($downloadCurl)" | |
} | |
function installPackage() { | |
echo "Which environment would you like to install to?" | |
specifyEnvironment | |
package="./packages/$packageName.zip" | |
doesExist="curl -u $username:$pass -sL --head -w %{http_code} http://$hostname/etc/packages/$groupName/$packageName.zip -o /dev/null" | |
responseCode="$($doesExist)" | |
if [[ $responseCode = "200" ]]; then | |
echo "Deleting package $packageName: " | |
echo "$(curl -w 'Response Code: %{http_code} | Connect: %{time_connect} | TTFB: %{time_starttransfer} | Total time: %{time_total} \n' -o /dev/null -u $username:$pass -F package=@$package http://$hostname/crx/packmgr/service/.json/?cmd=delete)" | |
else | |
echo "Package doesn't exist (response code: $responseCode); no need to delete." | |
fi | |
echo "Uploading package $packageName: " | |
echo "$(curl -w 'Response Code: %{http_code} | Connect: %{time_connect} | TTFB: %{time_starttransfer} | Total time: %{time_total} \n' -o /dev/null -u $username:$pass -F package=@$package http://$hostname/crx/packmgr/service/.json/?cmd=upload)" | |
installUrl="http://$hostname/crx/packmgr/service/.json/etc/packages/$groupName/$packageName.zip?cmd=install" | |
echo "Installing Package $packageName:" | |
echo "$( curl --connect-timeout 3600 -u $username:$pass -X POST "$installUrl" )" | |
} | |
function specifyAction() { | |
#check to see if input was provided | |
if [ $# -eq 0 ]; then | |
#echo "No arguments supplied." | |
echo -n "" | |
else | |
action=$1 | |
fi | |
if [[ $action =~ C ]]; then | |
echo "Your action is to create a package: $action" | |
createPackage | |
else | |
if [[ $action =~ I ]]; then | |
echo "Your action is to install packages: $action" | |
echo "What package would you like to install?" | |
specifyPackageName "" | |
installPackage | |
else | |
if [[ $action =~ B ]]; then | |
echo "Your action is to do both, create and install: $action" | |
createPackage | |
installPackage | |
else | |
read -p "Please specify action: Create package, Install package, or Both. >" action | |
specifyAction $action | |
fi | |
fi | |
fi | |
} | |
specifyAction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment