4 exec >> /root/user_data.out
7 MOUNT_POINT=${1:-'/opt/app/virc'}
8 LABEL=${2:-'VIRC_DATA'}
9 DESCRIPTION=${3:-'vIRC data volume'}
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})
18 # Exit with message if not root
19 [[ $UID -ne 0 ]] && logger -t $TAG "Not root. Exiting." && exit 2
21 # Create filesystem if none
22 if [[ -z ${FSTYPE} ]] ; then
24 if [[ $? -eq 0 ]] ; then
25 logger -t $TAG "Created xfs filesystem on $DISK_NAME."
27 logger -t $TAG "ERROR: Could not create xfs on $DISK_NAME. Exiting."
32 DISK_UUID=$(lsblk -no UUID ${DISK_NAME})
34 # Create label if none
35 [[ -z ${DISK_LABEL} ]] && xfs_admin -L ${LABEL} ${DISK_NAME}
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."
43 logger -t $TAG "ERROR: Could not create mount point at $MOUNT_POINT. Exiting"
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
55 mount | grep ${DISK_NAME} | grep ${MOUNT_POINT}
57 if [[ ${mount_check_1} -eq 0 ]] && [[ ${mount_check_2} -eq 0 ]]; then
58 logger -t $TAG "Successfully mounted $DISK_NAME at $MOUNT_POINT."
60 logger -t $TAG "ERROR: Could not mount $DISK_NAME at $MOUNT_POINT. Exiting."
69 hostname='__hostname__'
73 default_gateway='__default_gateway__'
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__'
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__'
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__'
99 # function to add underscore
105 net_scripts=/etc/sysconfig/network-scripts
107 # update network scripts with static ips and gateways
108 nic_count=($(ls -1d /sys/class/net/eth* | wc -l))
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}
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}
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}
142 echo 'NETMASK=255.255.255.0' >> ${net_scripts}/ifcfg-eth${j}
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}
153 echo "DEFROUTE=no" >> ${net_scripts}/ifcfg-eth${j}
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
166 # Set default gateway
167 [[ ${default_gateway} != $(add_underscore 'default_gateway') ]] && echo GATEWAY=${default_gateway} >> ${net_scripts}/ifcfg-eth0
169 if [[ ${domain} != $(add_underscore 'domain') ]] ; then
170 echo DOMAIN=${domain} >> ${net_scripts}/ifcfg-eth0
171 echo kernel.domainname=${domain} >> /etc/sysctl.conf
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
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
191 service network restart
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
203 virc_cc_environment='__virc_cc_environment__'
204 virc_cc_version='__virc_cc_version__'
205 virc_cc_version_file='__virc_cc_version_file__'
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
216 mechid_user_name=$(grep 'SWM_AUTOUSER=' /tmp/input.env | cut -f 2 -d '=')
217 useradd -g mechid -p 'pahfhrkSZmUs.' ${mechid_user_name}
219 ### Workaround ### REMOVE WHEN BUG FIXED ###
220 #mkdir -p /etc/chef/trusted_certs/
222 # Get packages to install from input.env, then delete from 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
228 ./platform-init-1.5.5.sh /tmp/input.env
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)
237 #/opt/app/aft/aftswmcli/bin/swmcli component pkginstall -c ${swm_install_pkgs} -n $(hostname).$(domainname) -w -fi -fs
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
250 for package in ${swm_install_pkgs[@]} ; do
251 /opt/app/aft/aftswmcli/bin/swmcli component pkginstall -c ${package} -n $(hostname).$(domainname) -w -fi
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}
263 for v in $(echo ${VERSION} | tr "." "\n")
265 if [ "$v" -ge 0 -a "$v" -le 9 ]; then
266 COOKBOOK_VERSION=${COOKBOOK_VERSION}0$v
268 COOKBOOK_VERSION=${COOKBOOK_VERSION}$v
272 COOKBOOK_VERSION="${COOKBOOK_VERSION:0:4}.1${COOKBOOK_VERSION:4:4}.1${COOKBOOK_VERSION:8:4}"
274 #cd /home/$USER/chef-repo
275 mkdir -p /home/$USER/scripts/$ENV
276 chown -R ${mechid_user_name}:mechid /home/$USER
278 chef_config_path="/home/${mechid_user_name}/chef-repo/.chef/knife.rb"
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}
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}
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>>>"