Add certificates support
[ta/image-provision.git] / dracut / modules / 00netconfig / net_config_get_iso.sh
1 #!/bin/sh
2
3 # Copyright 2019 Nokia
4
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
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17
18 source /usr/lib/installmedia-lib.sh
19
20 _CONFIGS_DIR="/tmp/cloud_configs"
21 if [ -e ${_CONFIGS_DIR}/network_config ];then
22     warn "net_config_get_iso: Creating local network configurations, to download boot.iso."
23     source ${_CONFIGS_DIR}/network_config
24
25     if [ $VLAN ]; then
26       warn "net_config_get_iso: Configuring VLAN network"
27       ip link add link $DEV name ${DEV}.${VLAN} type vlan id $VLAN
28       ip a a $IP dev ${DEV}.${VLAN}
29       ip link set up dev ${DEV}.${VLAN}
30       ip link set up dev ${DEV}
31       ip r a default via ${DGW} dev ${DEV}.${VLAN}
32       EXT_DEV="${DEV}.${VLAN}"
33     else
34       warn "net_config_get_iso: Configuring non-VLAN network"
35       ip a a $IP dev ${DEV}
36       ip link set up dev ${DEV}
37       ip r a default via ${DGW} dev ${DEV}
38       EXT_DEV="${DEV}"
39     fi
40     
41     wait_count=0
42     ip link show ${EXT_DEV} | grep "LOWER_UP" > /dev/null 2>&1
43     while [ $? != 0 ]
44     do
45       sleep 2
46       ((wait_count++))
47       if [[ $wait_count == 10 ]]; then
48         warn "net_config_get_iso: Link on ${EXT_DEV} did not come-up. Cannot proceed!"
49         exit 1
50       fi
51       warn "net_config_get_iso: Waiting for link to come-up on ${EXT_DEV}..."
52       ip link show ${EXT_DEV} | grep "LOWER_UP" > /dev/null 2>&1
53     done
54     
55     warn "net_config_get_iso: Link on ${EXT_DEV} came-up. Proceeding."
56     
57     if [ ${NAMESERVER} ]; then
58       echo "nameserver ${NAMESERVER}" > /etc/resolv.conf
59     fi
60     
61     # Check if DGW is pinging. If not exit. User may want to cross-check his network_config file and network and fix it.
62     warn "net_config_get_iso: Checking if DGW ${DGW} is pinging."
63     ping_wait_count=0
64     gw_ping_status=1
65     while [ ${gw_ping_status} != 0 ]
66     do
67         ((ping_wait_count++))
68         if [[ ${ping_wait_count} == 120 ]]; then
69             warn "net_config_get_iso: Provided default gateway ${DGW} is not pinging. Exiting installation."
70             exit 1
71         fi
72         if [[ ${DGW} = *:* ]]; then
73             ping -6 -c 1 -w 1 ${DGW} > /dev/null 2>&1
74         else
75             ping -c 1 -w 1 ${DGW} > /dev/null 2>&1
76         fi
77         gw_ping_status=$?
78     done
79
80     CERT_ARGS=""
81     if [ -f ${_CONFIGS_DIR}/clientcert.pem ]; then
82         CERT_ARGS="--certificate ${_CONFIGS_DIR}/clientcert.pem --private-key ${_CONFIGS_DIR}/clientkey.pem --ca-certificate ${_CONFIGS_DIR}/cacert.pem --no-check-certificate"
83     fi 
84     warn "Downloading Full ISO from URL: ${ISO_URL}"
85     if ! wget --connect-timeout 5 --read-timeout 100 ${CERT_ARGS} --no-check-certificate ${ISO_URL} --progress=dot:giga -O $BOOTCD_LOCATION; then
86         warn "net_config_get_iso: Failed to download ISO from URL: ${ISO_URL}. Exiting installation."
87         exit 1
88     fi
89 fi