5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
20 USER_CONFIG_FILE=/etc/userconfig/user_config.yaml
21 LOG_DIR=/var/log/start-menu
22 RC_FILE=/tmp/start_menu.rc
23 DEBUG_FLAG=/opt/start-menu/config/__debug
25 function ask_for_device()
31 devices=($(ls /sys/class/net))
33 if [ -z "$device" ]; then
36 for dev in ${devices[*]}; do
37 if [ "$dev" != "lo" ]; then
38 mac=$(cat /sys/class/net/$dev/address)
39 if [ "$dev" == "$device" ]; then
44 radiolist="${radiolist}$dev $mac $status "
47 device=$(dialog --clear --stdout --no-cancel --radiolist "Select network device:" 0 $DEFAULT_WIDTH 10 $radiolist)
49 if [ "$RC" -eq 0 ]; then
62 while [ "$rc" -ne 0 ]; do
63 address=$(dialog --clear --stdout --inputbox "Enter IP address for $device (CIDR format):" 0 $DEFAULT_WIDTH $address)
64 if [ "$?" -ne 0 ]; then
68 if [ -z "$address" ]; then
69 dialog --clear --stdout --msgbox "No address entered" 0 0
71 # check if valid address
72 err=$(ipcalc -c $address 2>&1)
74 if [ "$rc" -ne 0 ]; then
75 dialog --clear --stdout --msgbox "$err" 0 0
77 # check if network address (prefix given)
78 if [[ $address != */* ]]; then
79 dialog --clear --stdout --msgbox "No CIDR prefix given" 0 0
90 function ask_for_gateway()
96 while [ "$rc" -ne 0 ]; do
97 gateway=$(dialog --clear --stdout --inputbox "Enter gateway address for $device:" 0 $DEFAULT_WIDTH $gateway)
98 if [ "$?" -ne 0 ]; then
102 if [ -z "$gateway" ]; then
103 dialog --clear --stdout --msgbox "No address entered" 0 0
105 err=$(ipcalc -c $gateway 2>&1)
107 if [ "$rc" -ne 0 ]; then
108 dialog --clear --stdout --msgbox "$err" 0 0
118 function ask_for_vlan()
124 while [ "$rc" -eq 255 ]; do
125 dialog --clear --stdout --yesno "Set VLAN for $device?" 0 0
127 if [ "$rc" -eq 0 ]; then
129 while [ "$id_rc" -ne 0 ]; do
130 vlanid=$(dialog --clear --stdout --inputbox "Enter VLAN ID:" 0 $DEFAULT_WIDTH $vlanid)
132 if [ "$id_rc" -eq 255 ]; then
142 function wait_for_gateway()
145 for i in {1..180}; do
147 if [[ $GATEWAY = *:* ]]; then
148 ping -6 -w 1 -c 1 $GATEWAY
150 ping -w 1 -c 1 $GATEWAY
152 if [ "$?" -eq 0 ]; then
153 echo -e "\nping to network gateway OK.\n"
160 echo -e "\nping to network gateway failed."
162 if [ "$vlanid" != "" ] ; then
163 ip link delete vlan$vlanid
164 rm -f /etc/sysconfig/network-scripts/*vlan$vlanid*
166 ip a delete $address dev $device
167 rm -f /etc/sysconfig/network-scripts/*$device*
169 ip link set $device down
170 dialog --colors --stdout --clear --cr-wrap --title "ERROR" \
171 --msgbox '\n\Z1 Can not ping gateway! \n Shut down link!' 8 29
174 function run_external_network_create_command()
180 echo -e "Creating external network."
181 echo -e " Device : $device"
182 echo -e "Address : $address"
183 echo -e "Gateway : $gateway"
184 [ -z "$vlanid" ] || echo -e " VlanId : $vlanid"
186 mkdir -p /etc/os-net-config/
187 if [[ $gateway = *:* ]]; then
190 defaultroute=0.0.0.0/0
192 if [ "$vlanid" != "" ]; then
193 sed "s|PHYDEV|${device}|g;s|VLANID|${vlanid}|g;s|IPADDR|${address}|g;s|DEFAULTROUTE|${defaultroute}|g;s|GATEWAY|${gateway}|g" os_net_config_vlan_template.yml > /etc/os-net-config/config.yaml
195 sed "s|PHYDEV|${device}|g;s|IPADDR|${address}|g;s|DEFAULTROUTE|${defaultroute}|g;s|GATEWAY|${gateway}|g" os_net_config_template.yml > /etc/os-net-config/config.yaml
199 rm -rf /etc/os-net-config/config.yaml
203 while [ $i -lt $max_retries ]; do
204 echo "Waiting for interface ${device} to come up..."
205 if_status="$(cat /sys/class/net/${device}/operstate)"
206 [ "$if_status" = "up" ] && break
210 echo "Link status of interface $device : $if_status"
211 [ "$if_status" != "up" ] && \
212 dialog --colors --stdout --clear --cr-wrap --title "ERROR" --msgbox '\n\Z1 Link does not come up!' 8 29
215 function set_external_network()
223 device=$(ask_for_device "$device")
224 if [ "$?" -ne 0 ]; then
228 address=$(ask_for_ip "$device" "$address")
229 if [ "$?" -ne 0 ]; then
233 addr_no_mask=$(awk -F\/ '{print $1}' <<< "$address")
235 gateway=$(ask_for_gateway "$device" "$gateway")
236 if [ "$?" -ne 0 ]; then
240 vlanid=$(ask_for_vlan "$device" "$vlanid")
241 if [ "$?" -ne 0 ]; then
245 # echo is an ugly hack to workaround dialog bug (ok button loses two first lines of output)
246 (echo -e -n "\n\n"; run_external_network_create_command 2>&1) | tee $LOG_DIR/external_net_create.log | dialog --clear --stdout --progressbox "Setting up external network:" $DEFAULT_HEIGHT $DEFAULT_WIDTH
248 # echo is an ugly hack to workaround dialog bug (ok button loses two first lines of output)
250 (echo -e -n "\n\n"; wait_for_gateway $gateway 2>&1) | tee $LOG_DIR/wait_for_gateway.log | sed -ru "s/.{$[DEFAULT_WIDTH-4]}/&\n /g" | dialog --clear --stdout --programbox "Verify ping to gateway:" $DEFAULT_HEIGHT $DEFAULT_WIDTH
255 function installation_start()
261 if validate_user_config ${config_file}; then
262 pushd /opt/cmframework/scripts/
263 if ./bootstrap.sh ${config_file} ${mode}; then
264 echo -e "################################################################################"
265 echo -e "######################### All Done!! ########################################"
266 echo -e "################################################################################"
269 echo -e "Installation failed!!!"
272 echo -e "Validation failed!!!"
277 function start_install()
282 (echo -e -n "\n\n";installation_start ${config_file} --install 2>&1) | tee $LOG_DIR/start_install.log
285 function validate_user_config()
289 if [ -e ${config_file} ]; then
298 echo -e "Starting main menu"
300 while [ "$selection" != "X" ]; do
302 while [ "$rc" -ne 0 ]; do
303 rm -f /tmp/dialog.out
304 selection=$(dialog --clear --stdout --no-cancel --menu "Installation" 0 0 10 \
305 0 "Set external network" \
306 1 "Start installation")
314 if start_install ${USER_CONFIG_FILE}; then
327 function create_config_user()
329 mkdir -p /etc/userconfig/
330 userconfig_passwd=$(python -c "import crypt, getpass, pwd; print crypt.crypt('userconfig')")
331 useradd --home-dir=/etc/userconfig/ --password=$userconfig_passwd userconfig
332 chown userconfig:userconfig /etc/userconfig
335 function delete_config_user()
341 while [[ "$(systemctl is-system-running)" == "starting" ]]; do
342 echo "start-menu waiting for systemctl to finish startup..."
348 # disable kernel logs to console
349 echo 3 > /proc/sysrq-trigger
353 if [ -e $DEBUG_FLAG ]; then
354 logger "start-menu in debug mode"
358 if [ -e ${USER_CONFIG_FILE} ]; then
359 if start_install ${USER_CONFIG_FILE}; then
360 echo "Install succeeded"
364 echo "Install failed, check logs ($LOG_DIR)"