]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/initscripts/helper/exoscale-setup
cloud: Execute user-data scripts at the end of initialization
[people/pmueller/ipfire-2.x.git] / src / initscripts / helper / exoscale-setup
CommitLineData
e06d8de9 1#!/bin/bash
66c36198
PM
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
e06d8de9
MT
21
22. /etc/sysconfig/rc
23. ${rc_functions}
24
25# Set PATH to find our own executables
26export PATH=/usr/local/sbin:/usr/local/bin:${PATH}
27
28# Exoscale only supports a MTU of 1500
29DEFAULT_MTU=1500
30
31get() {
32 local file="${1}"
33
34 wget -qO - "http://metadata.exoscale.com/latest/${file}"
35}
36
37import_exoscale_configuration() {
38 local instance_id="$(get meta-data/instance-id)"
39
40 boot_mesg "Importing Exoscale configuration for instance ${instance_id}..."
41
42 # Store instance ID
43 echo "${instance_id}" > /var/run/exoscale-instance-id
44
45 # Initialise system settings
46 local hostname=$(get meta-data/local-hostname)
47
48 # Set hostname
49 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
50 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
51 fi
52
53 # Set domainname
54 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
df892010 55 local domainname="localdomain"
e06d8de9 56
df892010
MT
57 # If the hostname contains a dot we strip the last
58 # part and use it as our domain name
59 if [[ ${hostname} =~ "\." ]]; then
60 domainname="${hostname#*.}"
61 fi
e06d8de9
MT
62
63 echo "DOMAINNAME=${domainname}" >> /var/ipfire/main/settings
64 fi
65
66 # Create setup user
67 if ! getent passwd setup &>/dev/null; then
68 useradd setup -s /usr/bin/run-setup -g nobody -m
69
70 # Unlock the account
71 usermod -p "x" setup
72 fi
73
a7d8d352
MT
74 # Import SSH key for setup user
75 local key=$(get "meta-data/public-keys")
76 if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
77 mkdir -p "/home/setup/.ssh"
78 chmod 700 "/home/setup/.ssh"
79 chown setup.nobody "/home/setup/.ssh"
80
81 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
82 chmod 600 "/home/setup/.ssh/authorized_keys"
83 chown setup.nobody "/home/setup/.ssh/authorized_keys"
84 fi
e06d8de9 85
e06d8de9
MT
86 # Import any previous settings for the local interfaces
87 eval $(/usr/local/bin/readhash <(grep -E "^(GREEN|ORANGE)_.*=" /var/ipfire/ethernet/settings 2>/dev/null))
88
89 # Import network configuration
90 # After this, no network connectivity will be available from this script due to the
91 # renaming of the network interfaces for which they have to be shut down
92 local config_type=1
93 : > /var/ipfire/ethernet/settings
94
95 local device
96 for device in /sys/class/net/*; do
97 # Fetch the interface index
98 local ifindex="$(<${device}/ifindex)"
99 local address="$(<${device}/address)"
100
101 case "${ifindex}" in
102 # loopback
103 1)
104 continue
105 ;;
106
107 # The first interface will always be the public-facing one (i.e. RED)
108 2)
109 (
110 echo "RED_TYPE=DHCP"
111 echo "RED_DEV=red0"
112 echo "RED_MACADDR=${address}"
113 echo "RED_DESCRIPTION='${ifindex}'"
114 echo "RED_MTU=1500"
115 ) >> /var/ipfire/ethernet/settings
116 ;;
117
118 # GREEN
119 3)
120 # Set some sensible defaults for GREEN
121 if [ -z "${GREEN_ADDRESS}" ]; then
122 GREEN_ADDRESS="10.0.0.1"
123 GREEN_NETMASK="255.255.255.0"
124 GREEN_NETADDRESS="10.0.0.0"
e06d8de9
MT
125 fi
126
127 (
128 echo "GREEN_DEV=green0"
129 echo "GREEN_MACADDR=${address}"
130 echo "GREEN_DESCRIPTION='${ifindex}'"
131 echo "GREEN_ADDRESS=${GREEN_ADDRESS}"
132 echo "GREEN_NETMASK=${GREEN_NETMASK}"
133 echo "GREEN_NETADDRESS=${GREEN_NETADDRESS}"
e06d8de9
MT
134 echo "GREEN_MTU=${DEFAULT_MTU}"
135 ) >> /var/ipfire/ethernet/settings
136 ;;
137
138 # ORANGE
139 4)
140 config_type=2
141
142 # Set some sensible defaults for ORANGE
143 if [ -z "${ORANGE_ADDRESS}" ]; then
144 ORANGE_ADDRESS="10.0.1.1"
145 ORANGE_NETMASK="255.255.255.0"
146 ORANGE_NETADDRESS="10.0.1.0"
e06d8de9
MT
147 fi
148
149 (
150 echo "ORANGE_DEV=orange0"
151 echo "ORANGE_MACADDR=${address}"
152 echo "ORANGE_DESCRIPTION='${ifindex}'"
153 echo "ORANGE_ADDRESS=${ORANGE_ADDRESS}"
154 echo "ORANGE_NETMASK=${ORANGE_NETMASK}"
155 echo "ORANGE_NETADDRESS=${ORANGE_NETADDRESS}"
e06d8de9
MT
156 echo "ORANGE_MTU=${DEFAULT_MTU}"
157 ) >> /var/ipfire/ethernet/settings
158 ;;
159 esac
160 done
161
162 # Save CONFIG_TYPE
163 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
164
165 # Actions performed only on the very first start
166 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
167 # Disable using ISP nameservers
168 sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
169
170 # Enable SSH
171 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
172
173 # Disable SSH password authentication
174 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
175
176 # Enable SSH key authentication
177 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
178
179 # Apply SSH settings
180 /usr/local/bin/sshctrl
181
182 # Mark SSH to start immediately (but not right now)
183 touch /var/ipfire/remote/enablessh
184 chown nobody:nobody /var/ipfire/remote/enablessh
185
186 # Firewall rules for SSH and WEBIF
187 (
188 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
189 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
190 ) >> /var/ipfire/firewall/input
191
9e413732
MT
192 # Download user-data
193 local user_data="$(get user-data)"
194
195 # Save user-data script to be executed later
196 if [ "${user_data:0:2}" = "#!" ]; then
197 echo "${user_data}" > /tmp/user-data.script
198 chmod 700 /tmp/user-data.script
199
200 # Run the user-data script
201 local now="$(date -u +"%s")"
202 /tmp/user-data.script &>/var/log/user-data.log.${now}
203
204 # Delete the script right away
205 rm /tmp/user-data.script
206 fi
207
e06d8de9
MT
208 # This script has now completed the first steps of setup
209 touch /var/ipfire/main/firstsetup_ok
210 fi
211
212 # All done
213 echo_ok
214}
215
216case "${reason}" in
217 PREINIT)
218 # Bring up the interface
219 ip link set "${interface}" up
220 ;;
221
222 BOUND|RENEW|REBIND|REBOOT)
223 # Remove any previous IP addresses
224 ip addr flush dev "${interface}"
225
226 # Add (or re-add) the new IP address
227 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
228
229 # Add the default route
230 ip route add default via "${new_routers}"
231
232 # Setup DNS
233 for domain_name_server in ${new_domain_name_servers}; do
234 echo "nameserver ${domain_name_server}"
235 done > /etc/resolv.conf
236
237 # The system is online now
238 touch /var/ipfire/red/active
239
240 # Import Exoscale configuration
241 import_exoscale_configuration
242 ;;
243
244 EXPIRE|FAIL|RELEASE|STOP)
245 # The system is no longer online
246 rm -f /var/ipfire/red/active
247
248 # Remove all IP addresses
249 ip addr flush dev "${interface}"
250
251 # Shut down the interface
252 ip link set "${interface}" down
253 ;;
254
255 *)
256 echo "Unhandled reason: ${reason}" >&2
257 exit 2
258 ;;
259esac
260
261# Terminate
262exit 0