0b863aa17aa0670b2b186f5715d5bd6642e1440e
[csit.git] / resources / libraries / bash / function / setup.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2018 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -exuo pipefail
17
18
19 function ansible_host () {
20     # Setup host via ansible playbook.
21     #
22     # Variable read:
23     # - ${HOST} - Server production IP address (Linux).
24
25     set -exuo pipefail
26
27     pushd "${TOOLS_DIR}"/testbed-setup/ansible || die "Pushd failed!"
28     ssh-copy-id -o StrictHostKeyChecking=no testuser@"${HOST}"
29     ansible-playbook --vault-id vault_pass --extra-vars '@vault.yml' \
30         --inventory production site.yaml --limit ${HOST} || {
31         die "Failed to ansible host!"
32     }
33     popd || die "Popd failed!"
34 }
35
36
37 function installed () {
38     # Check if the given utility is installed. Fail if not installed.
39     #
40     # Arguments:
41     # - ${1} - Utility to check.
42     # Returns:
43     # - 0 - If command is installed.
44     # - 1 - If command is not installed.
45
46     set -exuo pipefail
47
48     command -v "${1}"
49 }
50
51
52 function pxe_cimc () {
53     # Reboot server with next boot set to PXE, disables PXE after server is UP
54     # to prevent endless loop.
55     #
56     # Variable read:
57     # - ${HOST} - Server production IP address (Linux).
58     # - ${MGMT} - Server management IP address (IPMI/CIMC).
59     # - ${USER} - CIMC user.
60     # - ${PASS} - CIMC pass.
61
62     set -exuo pipefail
63
64     pushd "${TOOLS_DIR}"/testbed-setup/cimc || die "Pushd failed!"
65     ./cimc.py -u "${USER}" -p "${PASS}" "${MGMT}" --debug -pxe || {
66         die "Failed to send the PXE reboot command!"
67     }
68     for i in $(seq 1 500); do
69         warn "Waiting for server to become reachable ... " || die
70         if pingable "${HOST}"; then
71             ./cimc.py -u "${USER}" -p "${PASS}" "${MGMT}" --debug -hdd || {
72                 die "Failed to send the HDD command!"
73             }
74             ssh-keygen -f "/home/testuser/.ssh/known_hosts" -R "${HOST}" || {
75                 die "Failed to remove obsolete SSH key!"
76             }
77             warn "Server reachable, PXE running!" || die
78             break
79         fi
80     done
81     popd || die "Popd failed!"
82 }
83
84
85 function pxe_host () {
86     # Reboot host into PXE mode and detect once it is up.
87     #
88     # Variable read:
89     # - ${@} - All script arguments from command line.
90     # Variable set:
91     # - ${HOST} - Server production IP address (Linux).
92     # - ${MGMT} - Server management IP address (IPMI/CIMC).
93     # - ${USER} - User.
94     # - ${PASS} - Pass.
95
96     set -exuo pipefail
97
98     if ! installed ipmitool; then
99         die "ipmitool not present. Please install before continue!"
100     fi
101
102     HOST=${1}
103     MGMT=${2}
104     USER=${3}
105     PASS=${4}
106
107     ipmi="ipmitool -I lanplus -H ${MGMT} -U ${USER} -P ${PASS} chassis status"
108     cimc="./cimc.py -u ${USER} -p ${PASS} ${MGMT} --mac-table"
109     pushd "${TOOLS_DIR}"/testbed-setup/cimc || die "Pushd failed!"
110     if ${ipmi}; then
111         warn "This is IMPI managed server!"
112         pxe_supermicro || die
113     elif ${cimc}; then
114         warn "This is CIMC managed server!"
115         pxe_cimc || die
116     else
117         die "Unknown management console!"
118     fi
119     popd || die "Popd failed!"
120
121     wait_for_ssh || die
122 }
123
124
125 function pxe_supermicro () {
126     # Reboot server with next boot set to PXE.
127     #
128     # Variable read:
129     # - ${HOST} - Server production IP address (Linux).
130     # - ${MGMT} - Server management IP address (IPMI/CIMC).
131     # - ${USER} - IPMI user.
132     # - ${PASS} - IPMI pass.
133
134     set -exuo pipefail
135
136     if ! installed ipmitool; then
137         die "ipmitool not present. Please install before continue!"
138     fi
139
140     cmd="chassis bootdev pxe"
141     ipmitool -I lanplus -H "${MGMT}" -U "${USER}" -P "${PASS}" "${cmd}" || {
142         die "Failed to send the ${cmd} command!"
143     }
144     cmd="power reset"
145     ipmitool -I lanplus -H "${MGMT}" -U "${USER}" -P "${PASS}" "${cmd}" || {
146         die "Failed to send the ${cmd} command!"
147     }
148     for i in $(seq 1 500); do
149         warn "Waiting for server to become reachable ... " || die
150         if pingable "${HOST}"; then
151             ssh-keygen -f "/home/testuser/.ssh/known_hosts" -R "${HOST}" || {
152                 die "Failed to remove obsolete SSH key!"
153             }
154             warn "Server reachable, PXE running!" || die
155             break
156         fi
157     done
158 }
159
160
161 function pingable () {
162     # Check if the host is reachable over ping. Fail if not reachable.
163     #
164     # Arguments:
165     # - ${1} - Host to check.
166     # Returns:
167     # - 0 - If host is reachable over ping.
168     # - 1 - If host is not reachable over ping.
169
170     set -exuo pipefail
171
172     ping -q -c 1 "${1}" 2>&1 > /dev/null
173 }
174
175
176 function wait_for_ssh () {
177     # Check if the host is reachable over SSH. Fail if not reachable.
178     #
179     # Variable read:
180     # - ${HOST} - Server production IP address (Linux).
181
182     set -exuo pipefail
183
184     while ! ssh "${HOST}"
185     do
186         warn "SSH not yet reachable, trying again!"
187         sleep 2
188     done
189     warn "SSH reachable!"
190 }