-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
132 lines (114 loc) · 2.91 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/ksh
#set -vx
#Default
ANSIBLE_PLAYBOOK="${PWD}/vmware-guest.yml"
ANSIBLE_PYTHON_INTERPRETER='/usr/bin/python'
builds="rhel7 rhel8"
_packer='/usr/local/bin/packer'
_ksvalidator='/usr/bin/ksvalidator'
_terraform='/usr/local/bin/terraform'
network='192.168.0'
oct='20'
MyProgname=$(basename $0)
MyHost=`uname -n`
MyDate=`date '+%m.%d.%y'`
PATH=/usr/sbin:/usr/bin:/sbin:/bin
export PATH
######################################
#Functions
######################################
log(){
message="$@"
printf '\e[1;32m %s\e[0m\n' "$MyHost $message" 1>&2
}
cleanup(){
for file in "$@"
do
if [ -f "$file" ] ; then
rm "$file"
fi
done
}
error_exit(){
message="$@"
printf '\e[1;31m %s\e[0m\n' "$MyHost ERROR: $message" 1>&2
exit 1
}
exit_state(){
returnCode=$?
message="$@"
if [ "$returnCode" -eq "0" ] ; then
printf '\e[1;32m %s\e[0m\n' "$MyHost $message OK. "
else
printf '\e[1;31m %s\e[0m\n' "$MyHost ERROR: $message FAILED. "
exit 1
fi
lastExit=$returnCode
}
usage(){
cat << EOF
Usage: build.sh
-c <clean templates>
-p <build packer>
-t <build terraform>
EOF
exit 1
}
######################################
#Main
######################################
if [ "$#" -ne '1' ]; then
usage;
fi
if [ -z ${SECRET_USER_PASS+x} ]; then
error_exit "var SECRET_USER_PASS is not set";
fi
if [ -z ${SECRET_VM_PASS+x} ]; then
error_exit "var SECRET_VM_PASS is not set";
fi
while getopts ':cpt' opt; do
case $opt in
c)
build='clean' >&2
;;
p)
build='packer' >&2
;;
t)
build='terraform' >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage;
exit 1
;;
esac
done
if [ "$build" == 'clean' ] ; then
if [ -f ${ANSIBLE_PLAYBOOK} ] ; then
source scl_source enable rh-python36
ansible-playbook -v -i ${PWD}/hosts ${ANSIBLE_PLAYBOOK} -e ansible_python_interpreter=${ANSIBLE_PYTHON_INTERPRETER}
fi
fi
if [ "$build" == 'packer' ] ; then
for OS in ${builds[@]}; do
$_packer build -force -var-file=./packer/vars/${OS}.json packer/rhel.json
exit_state "Packer ${OS} template build "
find ./packer_cache -type f -name "*.iso" -exec /bin/rm -f {} \;
find ./packer_cache -type f -name "*.iso.lock" -exec /bin/rm -f {} \;
done
fi
if [ "$build" == 'terraform' ] ; then
cd terraform
for OS in ${builds[@]}; do
vmhostname=${OS}
vmtemplate=`echo ${OS} | tr '[:lower:]' '[:upper:]'`
if [ ! -f terraform.tfstate.d/${OS} ] ; then
$_terraform workspace new "${OS}"
fi
$_terraform init
$_terraform workspace select "${OS}"
$_terraform apply -var vm_hostname="${vmhostname}" -var vm_template="${vmtemplate}" -var vsphere_password="${SECRET_VM_PASS}" -var ssh_password="${SECRET_USER_PASS}" -var vm_ip_addr="${network}.${oct}" -var "ansible-supplement-redhat=1" --auto-approve
oct=`expr $oct + 1`
done
fi