Skip to content

Instantly share code, notes, and snippets.

@jnikolak
Last active May 29, 2018 00:47
Show Gist options
  • Save jnikolak/b09d4a20f5150c65e05a8fe3421b160f to your computer and use it in GitHub Desktop.
Save jnikolak/b09d4a20f5150c65e05a8fe3421b160f to your computer and use it in GitHub Desktop.
automated script to setup provisioning
#!/bin/bash
#set -x
if [ -d /etc/foreman-installer ];then
password=$(grep admin_password /etc/foreman-installer/scenarios.d/satellite-answers.yaml| egrep -v pulp | awk '{print $2}')
else
password=$(grep admin_password /etc/katello-installer/answers.katello-installer.yaml| egrep -v pulp | awk '{print $2}')
fi
if [ -f ~/.hammer/cli_config.yml ];then
mv ~/.hammer/cli_config.yml /tmp/cli_config.yml
cat > ~/.hammer/cli_config.yml <<EOF
:foreman:
:username: 'admin'
:password: '$password'
EOF
else
mkdir -p ~/.hammer
cat > ~/.hammer/cli_config.yml <<EOF
:foreman:
:username: 'admin'
:password: '$password'
EOF
fi
domain=$(hostname -d)
hmgateway=$(ip route | grep default | awk '{print $3}')
hmip3oct=$(ip route | grep default | awk '{print $3}'| cut -f1-3 -d.)
hmip=$(ip route get $hmgateway | awk 'NR==1 {print $NF}')
#older way for #hmint=$(ip a | grep -B2 $hmnet | cut -f2 -d: | head -n1)
hmint=$(ip route get $hmip3oct | awk 'NR==1 {print $4}')
hmnetaddr=$(ip route get $hmip3oct | awk 'NR==1 {print $2}')
hmnetmask=$(ifconfig | grep netmask | awk 'NR==1 {print $4}')
hmprefix=$(ip a | grep $hmip | cut -f1 -d'b'|awk -F'/' '{print $2}')
hmto=$(ipcalc -b "$hmnetaddr"/$hmprefix | awk -F "." '/BRO/{print""substr($1,11)"."$2"."$3"."$4-2}')
hmfrom=$(ipcalc -n "$hmnetaddr"/$hmprefix | awk -F "." '/NETWORK/{print substr($1,9)"."$2"."$3"."$4+3}')
hmdns=$(nslookup $(hostname -f) | grep Server | awk '{print $2}')
# original ipcalc -nmb 172.17.0.0/24 | awk -F "." '/NETWORK/{print "FIRST "substr($1,9)"."$2"."$3"."$4+1};/BROADCAST/{print "LAST "substr($1,11)"."$2"."$3"."$4-1}'
#Enable Rhel 7 Server #155
rhel=$(hammer product list --organization-id 1 | egrep -i "Red.Hat.Enterprise.Linux.Server\s\s" | awk '{print $1}')
rhel2=$(hammer repository-set list --organization-id 1 --product-id $rhel | egrep -i "\|.Red.Hat.Enterprise.Linux.7.Server\s.R" | awk '{print $1}')
hammer repository-set enable --id $rhel2 --product-id $rhel --organization-id 1 --releasever 7Server --basearch x86_64
#Collections #46 and #2808
colle=$(hammer product list --organization-id 1 | egrep -i "Red.Hat.So(\w+).C(\w+).for.RHEL.S(\w+)" | awk '{print $1}')
colle2=$(hammer repository-set list --organization-id 1 --product-id $colle | egrep -i "s.RPMs*" | grep "x 7 Server" | awk '{print $1}')
hammer repository-set enable --id $colle2 --product-id $colle --organization-id 1 --releasever 7Server --basearch x86_64
#tools product = #155(rhel) and tools = #4831
tools=$(hammer repository-set list --organization-id 1 --product 'Red Hat Enterprise Linux Server' | egrep -i 'satellite tools\s6.2\s.*7 Server\)\s.R' | awk '{print $1}')
hammer repository-set enable --product-id $rhel --id $tools --organization-id 1 --basearch x86_64
#Kickstart product id = #155(rhel) and kickstart=#2455
kick=$(hammer repository-set list --organization-id 1 --product 'Red Hat Enterprise Linux Server' | egrep -i "kickstart" | egrep -i '7 Server\s.k' | awk '{print $1}')
hammer repository-set enable --id $kick --product-id $rhel --organization-id 1 --releasever 7.2 --basearch x86_64
hammer repository-set enable --id $kick --product-id $rhel --organization-id 1 --releasever 7.3 --basearch x86_64
#This method will run jobs in background, new tasks could run before it finishes.
sync=$(hammer --csv repository list --organization-id 1 | egrep -vi 'id' |awk -F, '{print $1}')
for i in $sync; do hammer repository synchronize --async --id $i --organization-id 1;done
#This method will run jobs in foreground, one by one
for i in $(hammer --csv repository list --organization-id 1 | grep -vi '^ID' | awk -F, {'print $1'})
do
hammer repository synchronize --async --id ${i} --organization-id 1
done
for i in $(hammer --csv --csv-separator "," repository list --organization-id 1 | awk -F'R' '{print $1}'| egrep -vi "id"|paste -sd "")
do
hammer content-view create --name cv_prov --repository-ids $i --description "for_provisioning" --organization-id 1
done
prov=$(hammer content-view list --organization-id 1 | egrep prov | awk '{print $1}')
hammer content-view publish --id $prov --organization-id 1 --async
sleep 120
hammer content-view info --id $prov
running=$(hammer task list |egrep '\| running'|egrep -v "Listen on")
if [ "$running" = "" ] ; then
hammer activation-key create --name "ak_prov" \
--organization-id 1 --content-view-id "$prov" \
--lifecycle-environment Library
else
printf "still running"
fi
ak_sub=$(hammer subscription list --organization-id 1 | egrep -i 'Employee SKU' | awk '{print $1}')
hammer activation-key add-subscription --id 1 --subscription-id $ak_sub
#domg=$(hammer domain list | egrep "$domain")
if [ "$domg" = "" ] ; then
hammer domain create --name $(hostname -d)
else
hammer domain update --id 1 --organization-ids 1 --location-ids 2
fi
hammer subnet create --name $domain --network $hmnetaddr --mask $hmnetmask --organization-ids 1 --domain-ids 1 \
--boot-mode DHCP \
--ipam DHCP \
--from $hmfrom \
--to $hmto \
--gateway $hmgateway \
--dns-primary $hmdns \
--location-ids 2 \
--dhcp-id 1 \
--tftp-id 1
hmsub=$(hammer subnet list | cut -f1 -d'|'|egrep -v 'ID|-')
hmcv=$(hammer content-view list --organization-id 1 | egrep 'cv_prov'| awk '{print $1}')
hammer hostgroup create --name "hg_prov" \
--organization-id 1 \
--content-view-id $hmcv \
--environment-id 1 \
--architecture-id 1 \
--domain-id 1 \
--subnet-id $hmsub \
--operatingsystem-id 2 \
--partition-table-id 61 \
--medium-id 9 \
--organization-ids 1 \
--location-ids 2 \
--lifecycle-environment "Library" \
--content-source-id 1 \
--puppet-ca-proxy-id 1 \
--root-pass Changeme123! \
--puppet-proxy-id 1
# Set up the medium to use the new organization
hm_med=$(hammer medium list | grep 7.2 | awk '{print $1}')
hammer medium update --id $hm_med --organization-ids 1
hammer compute-resource create --name rhevm2 --url http://localhost \
--organization-ids 1 \
--location-ids 2 \
### Creating a compute resource
hammer compute-resource create --provider Ovirt --name rhevm2 --url https://yourRHEVFQDN/api --user admin@internal --password yourPW --organization-ids 1 --location-ids 2
## The production environment isn't yet a part of our org
hammer environment update --id 1 --organization-ids 1 --location-ids 2
### Finished that should be all you need
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment