[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / test / resources / org / openecomp / validation / validators / attGuideLineValidator / baseHeatExposeResourceUsingGetResource / positive / user_data.sh
1 #!/bin/bash
2
3 set -x
4 exec >> /root/user_data.out
5 exec 2>&1
6
7 MOUNT_POINT=${1:-'/opt/app/virc'}
8 LABEL=${2:-'VIRC_DATA'}
9 DESCRIPTION=${3:-'vIRC data volume'}
10
11 TAG=VIRC_PROVISIONING
12
13 DISK_ID=$(ls -1 /dev/disk/by-id | tail -n1)
14 DISK_NAME=$(readlink -f /dev/disk/by-id/${DISK_ID})
15 FSTYPE=$(lsblk -o FSTYPE -n ${DISK_NAME})
16 DISK_LABEL=$(lsblk -o LABEL -n ${DISK_NAME})
17
18 # Exit with message if not root
19 [[ $UID -ne 0 ]] && logger -t $TAG "Not root. Exiting." && exit 2
20
21 # Create filesystem if none
22 if [[ -z ${FSTYPE} ]] ; then
23         mkfs.xfs ${DISK_NAME}
24         if [[ $? -eq 0 ]] ; then
25                 logger -t $TAG "Created xfs filesystem on $DISK_NAME."
26         else
27                 logger -t $TAG "ERROR: Could not create xfs on $DISK_NAME. Exiting."
28                 exit 90
29         fi
30 fi
31 sleep 0.5
32 DISK_UUID=$(lsblk -no UUID ${DISK_NAME})
33
34 # Create label if none
35 [[ -z ${DISK_LABEL} ]] && xfs_admin -L ${LABEL} ${DISK_NAME}
36
37 # Create mount point if it does not exist
38 if [[ ! -d ${MOUNT_POINT} ]] ; then
39         mkdir -p ${MOUNT_POINT}
40         if [[ $? -eq 0 ]] ; then
41                 logger -t $TAG "Created mount point at $MOUNT_POINT."
42         else
43                 logger -t $TAG "ERROR: Could not create mount point at $MOUNT_POINT. Exiting"
44                 exit 80
45         fi
46 fi
47
48 # Only add to /etc/fstab if not already there
49 grep -q ${DISK_UUID} /etc/fstab
50 if [[ $? -ne 0 ]] ; then
51         echo "# Following mount for ${DESCRIPTION}" >> /etc/fstab
52         echo "UUID=${DISK_UUID} ${MOUNT_POINT}  xfs     defaults        0 0" >> /etc/fstab
53         mount -a
54         mount_check_1=$?
55         mount | grep ${DISK_NAME} | grep ${MOUNT_POINT}
56         mount_check_2=$?
57         if [[ ${mount_check_1} -eq 0 ]] && [[ ${mount_check_2} -eq 0 ]]; then
58                 logger -t $TAG "Successfully mounted $DISK_NAME at $MOUNT_POINT."
59         else
60                 logger -t $TAG "ERROR: Could not mount $DISK_NAME at $MOUNT_POINT. Exiting."
61                 exit 70
62         fi
63 fi
64
65 ###
66 ### Configure network
67 ###
68
69 hostname='__hostname__'
70 domain='__domain__'
71 dns1='__dns1__'
72 dns2='__dns2__'
73 default_gateway='__default_gateway__'
74
75 # 1 disable, 0 enable
76 ipv6_enable=1
77
78 port_mac[0]='__port_mac_0__'
79 port_ip[0]='__port_ip_0__'
80 port_netmask[0]='__port_netmask_0__'
81 port_gateway[0]='__port_gateway_0__'
82 port_def_route[0]='__port_def_route_0__'
83 port_dhcp[0]='__port_dhcp_0__'
84
85 port_mac[1]='__port_mac_1__'
86 port_ip[1]='__port_ip_1__'
87 port_netmask[1]='__port_netmask_1__'
88 port_gateway[1]='__port_gateway_1__'
89 port_def_route[1]='__port_def_route_1__'
90 port_dhcp[1]='__port_dhcp_1__'
91
92 port_mac[2]='__port_mac_2__'
93 port_ip[2]='__port_ip_2__'
94 port_netmask[2]='__port_netmask_2__'
95 port_gateway[2]='__port_gateway_2__'
96 port_def_route[2]='__port_def_route_2__'
97 port_dhcp[2]='__port_dhcp_2__'
98
99 # function to add underscore
100 add_underscore(){
101   echo "__${1}__"
102 }
103
104 # filenames
105 net_scripts=/etc/sysconfig/network-scripts
106
107 # update network scripts with static ips and gateways
108 nic_count=($(ls -1d /sys/class/net/eth* | wc -l))
109 for i in {0..2} ; do
110   if [[ ${port_mac[i]} != "__port_mac_${i}__" && \
111     ( ${port_ip[i]} != "__port_ip_${i}__" || ${port_dhcp[i]} != "__port_dhcp_${i}__" ) ]] ; then
112     for (( j=0 ; j<${nic_count} ; j++ )) ; do
113       nic_mac=$(cat /sys/class/net/eth${j}/address) 
114       if [[ ${port_mac[i]} == ${nic_mac} ]] ; then
115         echo "NAME=eth${j}" > ${net_scripts}/ifcfg-eth${j}
116         echo "DEVICE=eth${j}" >> ${net_scripts}/ifcfg-eth${j}
117         if [[ ${port_dhcp[i]} =~ (yes|Yes|True|true) ]] ; then
118           echo "BOOTPROTO=dhcp" >> ${net_scripts}/ifcfg-eth${j}
119         elif [[ ${port_ip[i]} != "__port_ip_${i}__" ]] && [[ ${port_ip[i]} =~ .*:.* ]] ; then
120           [[ ${ipv6_enable} -eq 1 ]] && ipv6_enable=0
121           echo "BOOTPROTO=none" >> ${net_scripts}/ifcfg-eth${j}
122           echo "IPV6INIT=yes" >> ${net_scripts}/ifcfg-eth${j}
123           echo "IPV6ADDR=${port_ip[i]}" >> ${net_scripts}/ifcfg-eth${j}
124           if [[ ${port_gateway[i]} != "__port_gateway_${i}__" ]] ; then
125             echo "IPV6_DEFAULTGW=${port_gateway[i]}" >> ${net_scripts}/ifcfg-eth${j}
126           elif [[ ${port_gateway[i]} == $(add_underscore 'port_gateway_0') ]] && [[ ${default_gateway} != $(add_underscore 'default_gateway') ]] ; then
127             echo "IPV6_DEFAULTGW=${default_gateway}" >> ${net_scripts}/ifcfg-eth${j}
128           fi
129         elif [[ ${port_ip[i]} != "__port_ip_${i}__" ]] ; then
130           echo "BOOTPROTO=none" >> ${net_scripts}/ifcfg-eth${j}
131           echo "IPADDR=${port_ip[i]}" >> ${net_scripts}/ifcfg-eth${j}
132           # Set gateway if provided. If not set, set eth0 to default
133           if [[ ${port_gateway[i]} != "__port_gateway_${i}__" ]] ; then
134             echo "GATEWAY=${port_gateway[i]}" >> ${net_scripts}/ifcfg-eth${j}
135           elif [[ ${port_gateway[i]} == $(add_underscore 'port_gateway_0') ]] && [[ ${default_gateway} != $(add_underscore 'default_gateway') ]] ; then
136             echo "GATEWAY=${default_gateway}" >> ${net_scripts}/ifcfg-eth${j}
137           fi
138           # Set netmask if provided. Else set netmask to 255.255.255.0
139           if [[ ${port_netmask[i]} != "__port_netmask_${i}__" ]] ; then
140             echo "NETMASK=${port_netmask[i]}" >> ${net_scripts}/ifcfg-eth${j}
141           else
142             echo 'NETMASK=255.255.255.0' >> ${net_scripts}/ifcfg-eth${j}
143           fi
144         fi
145         echo "ONBOOT=yes" >> ${net_scripts}/ifcfg-eth${j}
146         echo "HWADDR=${nic_mac}" >> ${net_scripts}/ifcfg-eth${j}
147         # Set to DEFROUTE to no, unless otherwise stated. If not stated set to yes on eth0
148         if [[ ${port_def_route[i]} =~ (yes|Yes|True|true) ]] ; then
149           echo "DEFROUTE=yes" >> ${net_scripts}/ifcfg-eth${j}
150         elif [[ ${port_def_route[i]} == $(add_underscore 'port_def_route_0') ]] ; then
151           echo "DEFROUTE=yes" >> ${net_scripts}/ifcfg-eth${j}
152         else
153           echo "DEFROUTE=no" >> ${net_scripts}/ifcfg-eth${j}
154         fi
155       fi
156     done
157   fi
158 done
159
160 # Set DNS
161 if [[ ${dns1} != $(add_underscore 'dns1') ]] ; then
162   echo "PEERDNS=yes" >> ${net_scripts}/ifcfg-eth0
163   echo "DNS1=${dns1}" >> ${net_scripts}/ifcfg-eth0
164   [[ ${dns2} != $(add_underscore 'dns2') ]] && echo "DNS2=${dns2}" >> ${net_scripts}/ifcfg-eth0
165 fi
166 # Set default gateway
167 [[ ${default_gateway} != $(add_underscore 'default_gateway') ]] && echo GATEWAY=${default_gateway} >> ${net_scripts}/ifcfg-eth0
168 # Set domain
169 if [[ ${domain} != $(add_underscore 'domain') ]] ; then
170   echo DOMAIN=${domain} >> ${net_scripts}/ifcfg-eth0
171   echo kernel.domainname=${domain} >> /etc/sysctl.conf
172   sysctl -p
173 fi
174 # Set hostname
175 if [[ ${hostname} != $(add_underscore 'hostname') ]] ; then
176   hostnamectl set-hostname ${hostname}
177   sed -i "s/\(^127\.0\.0\.1 .*\)/\1 ${hostname}/" /etc/hosts
178   sed -i "s/\(^::1 .*\)/\1 ${hostname}/" /etc/hosts
179   sed -i "s/\(^127\.0\.0\.1 .*\)/\1 ${hostname}.${domain}/" /etc/hosts
180   sed -i "s/\(^::1 .*\)/\1 ${hostname}.${domain}/" /etc/hosts
181 fi
182
183 # Enable ipv6 if there is an ipv6 address supplied in env
184 if [[ ${ipv6_enable} -eq 0 ]] ; then
185   echo net.ipv6.conf.all.disable_ipv6 = 0 >> /etc/sysctl.conf
186   echo net.ipv6.conf.default.disable_ipv6 = 0 >> /etc/sysctl.conf
187   echo net.ipv6.conf.lo.disable_ipv6 = 0 >> /etc/sysctl.conf
188   sysctl -p
189 fi
190
191 service network restart
192
193 ## Disable Password Login for MechID group
194 echo "Match Group mechid" >> /etc/ssh/sshd_config
195 echo -e "\tPasswordAuthentication no" >> /etc/ssh/sshd_config
196 systemctl restart sshd
197
198 ###
199 # Install SWM
200 ###
201
202 ## SWM variables
203 virc_cc_environment='__virc_cc_environment__'
204 virc_cc_version='__virc_cc_version__'
205 virc_cc_version_file='__virc_cc_version_file__'
206
207 cd /tmp
208 wget 'https://codecloud.web.att.com/projects/ST_VIRC/repos/virc/browse/SWM/Install/input.env?raw' -O input.env
209 wget 'https://codecloud.web.att.com/projects/ST_VIRC/repos/virc/browse/SWM/Install/platform-init-1.5.5.sh?raw' -O platform-init-1.5.5.sh 
210 wget 'https://codecloud.web.att.com/projects/ST_VIRC/repos/virc/browse/SWM/Install/hostname.props?raw' -O $(hostname).props
211 sed -i "s/$(add_underscore 'virc_environment')/${virc_environment}/g" $(hostname).props
212 sed -i "s/$(add_underscore 'virc_version')/${virc_version}/g" input.env
213 chmod +x platform-init-1.5.5.sh
214
215 ## Add MechID user
216 mechid_user_name=$(grep 'SWM_AUTOUSER=' /tmp/input.env | cut -f 2 -d '=')
217 useradd -g mechid -p 'pahfhrkSZmUs.' ${mechid_user_name}
218
219 ### Workaround ### REMOVE WHEN BUG FIXED ###
220 #mkdir -p /etc/chef/trusted_certs/
221
222 # Get packages to install from input.env, then delete from input.env
223 . /tmp/input.env
224 swm_install_pkgs=(${SWM_INIT_PACKAGES})
225 swm_install_pkg_deps=(${SWM_INIT_PACKAGE_DEPS})
226 sed -i '/SWM_INIT_PACKAGES="/,/\"/d' /tmp/input.env
227
228 ./platform-init-1.5.5.sh /tmp/input.env
229
230 ## Install SWM packages after SWM installation
231 export AFTSWM_USERNAME=${mechid_user_name}
232 mechid_user_enc_passwd=$(grep 'SWM_AUTOCRED=' /tmp/input.env | cut -f 2 -d '=')
233 export AFTSWM_PASSWORD=${mechid_user_enc_passwd}
234 export HOSTNAME=$(hostname)
235 #export HOME=/root
236
237 #/opt/app/aft/aftswmcli/bin/swmcli component pkginstall -c ${swm_install_pkgs} -n $(hostname).$(domainname) -w -fi -fs
238 #sleep 5
239 #cd
240 echo $SHELL
241 whoami
242 env
243 pwd
244
245 # install swm packages one at a time
246 for package in ${swm_install_pkg_deps[@]} ; do
247   /opt/app/aft/aftswmcli/bin/swmcli component pkginstall -c ${package} -n $(hostname).$(domainname) -w -fi
248 done
249
250 for package in ${swm_install_pkgs[@]} ; do
251   /opt/app/aft/aftswmcli/bin/swmcli component pkginstall -c ${package} -n $(hostname).$(domainname) -w -fi
252 done
253
254 ### Run Chef Prep Scripts ###
255 USER=${mechid_user_name}
256 COOKBOOK_NAME='virc_cc'
257 VERSION=${virc_cc_version}
258 ENV=${virc_cc_environment}
259 VERSION_FILE=${virc_cc_version_file}
260
261 COOKBOOK_VERSION=""
262
263 for v in $(echo ${VERSION} | tr "." "\n")
264 do
265     if [ "$v" -ge 0 -a "$v" -le 9 ]; then
266         COOKBOOK_VERSION=${COOKBOOK_VERSION}0$v
267     else
268         COOKBOOK_VERSION=${COOKBOOK_VERSION}$v
269     fi
270 done
271
272 COOKBOOK_VERSION="${COOKBOOK_VERSION:0:4}.1${COOKBOOK_VERSION:4:4}.1${COOKBOOK_VERSION:8:4}"
273
274 #cd /home/$USER/chef-repo
275 mkdir -p /home/$USER/scripts/$ENV
276 chown -R ${mechid_user_name}:mechid /home/$USER
277
278 chef_config_path="/home/${mechid_user_name}/chef-repo/.chef/knife.rb"
279
280 su - -c "/usr/bin/knife client delete $(hostname).$(domainname) -y -c ${chef_config_path}" ${mechid_user_name}
281 su - -c "/usr/bin/knife node delete $(hostname).$(domainname) -y -c ${chef_config_path}" ${mechid_user_name}
282
283 su - -c "/usr/bin/knife cookbook show $COOKBOOK_NAME $COOKBOOK_VERSION files Pyswm.py -c ${chef_config_path} > /home/${mechid_user_name}/scripts/$ENV/Pyswm.py" ${mechid_user_name}
284 #/usr/bin/knife cookbook show $COOKBOOK_NAME $COOKBOOK_VERSION files Pyswm.pyc > /home/$USER/scripts/$ENV/Pyswm.pyc
285 su - -c "/usr/bin/knife cookbook show $COOKBOOK_NAME $COOKBOOK_VERSION files install_swm.py -c ${chef_config_path} > /home/$USER/scripts/$ENV/install_swm.py" ${mechid_user_name}
286 su - -c "/usr/bin/knife cookbook show $COOKBOOK_NAME $COOKBOOK_VERSION files swm-installer-config.json -c ${chef_config_path} > /home/$USER/scripts/$ENV/swm-installer-config.json" ${mechid_user_name}
287
288 #cd /home/$USER/scripts/$ENV
289 su - -c "chmod 755 /home/$USER/scripts/$ENV/install_swm.py" ${mechid_user_name}
290 su - -c "cd /home/$USER/scripts/$ENV; ./install_swm.py $VERSION $ENV --components-nodes=\"vIRC-cc:$(hostname).$(domainname)\" --version-file=${VERSION_FILE}" ${mechid_user_name}
291 #./install_swm.py $VERSION $ENV --components-nodes="<<<ComponentName.FQDN>>>"
292