-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcustomize-web2-vm.start
145 lines (120 loc) · 5.45 KB
/
customize-web2-vm.start
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
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/sh
#
# This script setups the entire OVF VM based on properties
#
echo "- Retrieving OVF information"
FILE_CUSTOMIZATION="/etc/customization.state"
FILE_OVFENVIRONMENT="/tmp/ovfenvironment.xml"
#
# If customization was already launch, exit right away
# Do the following ONLY if the VM is started from the vAPP
# If not from the vApp then the VM will keep its previous files settings
if [ -s $FILE_CUSTOMIZATION ]
then
exit 0
fi
# Customization process
echo "> Retrieving OVF information..."
# Retrieve OVF settings from VMware Tools.
/usr/bin/vmtoolsd --cmd "info-get guestinfo.ovfEnv" > $FILE_OVFENVIRONMENT
if [ -s "$FILE_OVFENVIRONMENT" ]
then
echo $(date) > $FILE_CUSTOMIZATION
# setting the environment
#Web server 1 details
web1_name=$(cat $FILE_OVFENVIRONMENT | grep web1_name | cut -d '"' -f 4 | head -1)
web1_ip=$(cat $FILE_OVFENVIRONMENT | grep web1_ip | cut -d '"' -f 4 | head -1)
web1_netmask=$(cat $FILE_OVFENVIRONMENT | grep web1_netmask | cut -d '"' -f 4 | head -1)
web1_gw=$(cat $FILE_OVFENVIRONMENT | grep web1_gw | cut -d '"' -f 4 | head -1)
#Web server 2 details
web2_name=$(cat $FILE_OVFENVIRONMENT | grep web2_name | cut -d '"' -f 4 | head -1)
web2_ip=$(cat $FILE_OVFENVIRONMENT | grep web2_ip | cut -d '"' -f 4 | head -1)
web2_netmask=$(cat $FILE_OVFENVIRONMENT | grep web2_netmask | cut -d '"' -f 4 | head -1)
web2_gw=$(cat $FILE_OVFENVIRONMENT | grep web2_gw | cut -d '"' -f 4 | head -1)
# App Server 1 details
app1_name=$(cat $FILE_OVFENVIRONMENT | grep app1_name | cut -d '"' -f 4 | head -1)
app1_ip=$(cat $FILE_OVFENVIRONMENT | grep app1_ip | cut -d '"' -f 4 | head -1)
app1_netmask=$(cat $FILE_OVFENVIRONMENT | grep app1_netmask | cut -d '"' -f 4 | head -1)
app1_gw=$(cat $FILE_OVFENVIRONMENT | grep app1_gw | cut -d '"' -f 4 | head -1)
# App Server 2 details
app2_name=$(cat $FILE_OVFENVIRONMENT | grep app2_name | cut -d '"' -f 4 | head -1)
app2_ip=$(cat $FILE_OVFENVIRONMENT | grep app2_ip | cut -d '"' -f 4 | head -1)
app2_netmask=$(cat $FILE_OVFENVIRONMENT | grep app2_netmask | cut -d '"' -f 4 | head -1)
app2_gw=$(cat $FILE_OVFENVIRONMENT | grep app2_gw | cut -d '"' -f 4 | head -1)
# DB Server 1 details
# db_name=$(cat $FILE_OVFENVIRONMENT | grep db_name | cut -d '"' -f 4 | head -1)
# db_ip=$(cat $FILE_OVFENVIRONMENT | grep db_ip | cut -d '"' -f 4 | head -1)
# db_netmask=$(cat $FILE_OVFENVIRONMENT | grep db_netmask | cut -d '"' -f 4 | head -1)
# db_gw=$(cat $FILE_OVFENVIRONMENT | grep web_gw | cut -d '"' -f 4 | head -1)
echo "Alpine Linux Settings" >> $FILE_CUSTOMIZATION
echo "=====================" >> $FILE_CUSTOMIZATION
echo "hostname: $web2_name" >> $FILE_CUSTOMIZATION
echo "ipaddress: $web2_ip" >> $FILE_CUSTOMIZATION
echo "netprefix: $web2_netmask" >> $FILE_CUSTOMIZATION
echo "gateway $web2_gw" >> $FILE_CUSTOMIZATION
#
# Update the hosts file to reflect the settings
#
echo "- Setting application environment"
echo "127.0.0.1 localhost" > /etc/hosts
echo "$web2_ip $web2_name" >> /etc/hosts
#echo "$db_ip $db_name" >> /etc/hosts
echo "$app1_ip $app1_name" >> /etc/hosts
echo "$app2_ip $app2_name" >> /etc/hosts
#
#
# Networking settings
#
echo "- Setting networking environment"
setup-hostname $web2_name
hostname -F /etc/hostname
# Setup Alpine networking (if either ip/netprefix/gw is missing, we keep dhcp)
if [ -z "$web2_ip" ] || [ -z "$web2_netmask" ] || [ -z "$web2_gw" ]
then
echo "DHCP CONFIG, skipping..."
else
echo "STATIC CONFIG, configuring..."
cat > /etc/network/interfaces <<-EOF
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
######
## This file is autogenerated by the OVF templatei
######
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
allow-hotplug eth0
iface eth0 inet static
address $web2_ip
netmask $web2_netmask
gateway $web2_gw
EOF
# restart networking
rc-service networking restart
fi
## THE END
/etc/init.d/networking restart
ifdown eth0
ifup eth0
#invoke-rc.d hostname.sh start
service nginx stop
cp /etc/nginx/http.d/default.conf /etc/nginx/http.d/default.conf-orig
sed -i "3 i \ " /etc/nginx/http.d/default.conf
sed -i '4 i \upstream app{ ' /etc/nginx/http.d/default.conf
sed -i "5 i \ server $app1_ip:8080;" /etc/nginx/http.d/default.conf
sed -i "6 i \ server $app2_ip:8080;" /etc/nginx/http.d/default.conf
sed -i "7 i \}" /etc/nginx/http.d/default.conf
sed -i "8 i \ " /etc/nginx/http.d/default.conf
sed -i "9 i \ " /etc/nginx/http.d/default.conf
sed -i "17s/return 404/#return 404/" /etc/nginx/http.d/default.conf
sed -i "18 i \ proxy_pass http:\/\/app ;" /etc/nginx/http.d/default.conf
sed -i '19 i \ proxy_set_header Host $host;' /etc/nginx/http.d/default.conf
sed -i '20 i \ proxy_set_header X-Real-IP $remote_addr;' /etc/nginx/http.d/default.conf
sed -i '21 i \ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' /etc/nginx/http.d/default.conf
sed -i '22 i \ proxy_set_header X-Forwarded-Proto $scheme;' /etc/nginx/http.d/default.conf
service nginx start
reboot
fi
exit 0