4 LIBDIR="$(dirname "$(dirname "$(dirname "$PWD")")")"
8 source $LIBDIR/env/lib/common.sh
10 if [[ $EUID -ne 0 ]]; then
11 echo "This script must be run as root"
15 IMAGE_URL=http://172.22.0.1/images/${BM_IMAGE}
16 IMAGE_CHECKSUM=http://172.22.0.1/images/${BM_IMAGE}.md5sum
18 function get_default_interface_ipaddress {
20 local _default_interface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
21 local _ipv4address=$(ip addr show dev $_default_interface | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }')
22 eval $_ip="'$_ipv4address'"
25 function create_ssh_key {
26 #ssh key for compute node to communicate back to bootstrap server
27 mkdir -p $BUILD_DIR/ssh_key
28 ssh-keygen -C "compute.icn.akraino.lfedge.org" -f $BUILD_DIR/ssh_key/id_rsa
29 cat $BUILD_DIR/ssh_key/id_rsa.pub >> $HOME/.ssh/authorized_keys
32 function set_compute_key {
33 _SSH_LOCAL_KEY=$(cat $BUILD_DIR/ssh_key/id_rsa)
36 - path: /opt/ssh_id_rsa
44 function deprovision_compute_node {
46 kubectl patch baremetalhost $name -n metal3 --type merge \
47 -p '{"spec":{"image":{"url":"","checksum":""}}}'
50 function set_compute_ssh_config {
51 get_default_interface_ipaddress default_addr
53 - path: /root/.ssh/config
57 Host bootstrapmachine $default_addr
58 HostName $default_addr
59 IdentityFile /opt/ssh_id_rsa
61 - path: /etc/apt/sources.list
65 deb [trusted=yes] ssh://$USER@$default_addr:$LOCAL_APT_REPO ./
69 function create_userdata {
71 COMPUTE_NODE_FQDN="$name.akraino.icn.org"
72 printf "#cloud-config\n" > $name-userdata.yaml
73 if [ -n "$COMPUTE_NODE_PASSWORD" ]; then
74 printf "password: ""%s" "$COMPUTE_NODE_PASSWORD" >> $name-userdata.yaml
75 printf "\nchpasswd: {expire: False}\n" >> $name-userdata.yaml
76 printf "ssh_pwauth: True\n" >> $name-userdata.yaml
79 if [ -n "$COMPUTE_NODE_FQDN" ]; then
80 printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >> $name-userdata.yaml
81 printf "\n" >> $name-userdata.yaml
83 printf "disable_root: false\n" >> $name-userdata.yaml
84 printf "ssh_authorized_keys:\n - " >> $name-userdata.yaml
86 if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
87 yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
90 cat $HOME/.ssh/id_rsa.pub >> $name-userdata.yaml
91 network_config_files >> $name-userdata.yaml
92 printf "\n" >> $name-userdata.yaml
95 function launch_baremetal_operator {
96 docker pull integratedcloudnative/baremetal-operator:v1.0-icn
97 kubectl apply -f bmo/namespace/namespace.yaml
98 kubectl apply -f bmo/rbac/service_account.yaml -n metal3
99 kubectl apply -f bmo/rbac/role.yaml -n metal3
100 kubectl apply -f bmo/rbac/role_binding.yaml
101 kubectl apply -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
102 kubectl apply -f bmo/operator/no_ironic/operator.yaml -n metal3
105 function remove_baremetal_operator {
106 kubectl delete -f bmo/operator/no_ironic/operator.yaml -n metal3
107 kubectl delete -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
108 kubectl delete -f bmo/rbac/role_binding.yaml
109 kubectl delete -f bmo/rbac/role.yaml -n metal3
110 kubectl delete -f bmo/rbac/service_account.yaml -n metal3
111 kubectl delete -f bmo/namespace/namespace.yaml
114 function network_config_files {
117 - path: /opt/ironic_net.sh
123 for intf in /sys/class/net/*; do
124 sudo ifconfig `basename $intf` up
125 sudo dhclient -nw `basename $intf`
129 - path: /opt/user_net.sh
135 route add default gw $PROVIDER_NETWORK_GATEWAY
136 sed -i -e 's/^#DNS=.*/DNS=$PROVIDER_NETWORK_DNS/g' /etc/systemd/resolved.conf
137 systemctl daemon-reload
138 systemctl restart systemd-resolved
140 - [ /opt/ironic_net.sh ]
141 - [ /opt/user_net.sh ]
145 function apply_userdata_credential {
147 cat <<EOF > ./$name-user-data-credential.yaml
150 userData: $(base64 -w 0 $name-userdata.yaml)
153 name: $name-user-data
157 kubectl apply -n metal3 -f $name-user-data-credential.yaml
160 function make_bm_hosts {
161 while read -r name username password address; do
162 create_userdata $name
163 apply_userdata_credential $name
165 go run $GOPATH/src/github.com/metal3-io/baremetal-operator/cmd/make-bm-worker/main.go \
166 -address "ipmi://$address" \
167 -password "$password" \
169 "$name" > $name-bm-node.yaml
171 printf " image:" >> $name-bm-node.yaml
172 printf "\n url: ""%s" "$IMAGE_URL" >> $name-bm-node.yaml
173 printf "\n checksum: ""%s" "$IMAGE_CHECKSUM" >> $name-bm-node.yaml
174 printf "\n userData:" >> $name-bm-node.yaml
175 printf "\n name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
176 printf "\n namespace: metal3\n" >> $name-bm-node.yaml
177 kubectl apply -f $name-bm-node.yaml -n metal3
181 function configure_nodes {
182 if [ ! -d $IRONIC_DATA_DIR ]; then
183 mkdir -p $IRONIC_DATA_DIR
186 #make sure nodes.json file in /opt/ironic/ are configured
187 if [ ! -f $IRONIC_DATA_DIR/nodes.json ]; then
188 cp $PWD/nodes.json.sample $IRONIC_DATA_DIR/nodes.json
192 function remove_bm_hosts {
193 while read -r name username password address; do
194 deprovision_compute_node $name
199 while read -r name username password address; do
200 kubectl delete bmh $name -n metal3
201 kubectl delete secrets $name-bmc-secret -n metal3
202 kubectl delete secrets $name-user-data -n metal3
203 if [ -f $name-bm-node.yaml ]; then
204 rm -rf $name-bm-node.yaml
207 if [ -f $name-user-data-credential.yaml ]; then
208 rm -rf $name-user-data-credential.yaml
211 if [ -f $name-userdata.yaml ]; then
212 rm -rf $name-userdata.yaml
219 if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
220 rm -rf $IRONIC_DATA_DIR/nodes.json
224 function apply_bm_hosts {
225 list_nodes | make_bm_hosts
228 function deprovision_all_hosts {
229 list_nodes | remove_bm_hosts
232 if [ "$1" == "launch" ]; then
233 launch_baremetal_operator
237 if [ "$1" == "deprovision" ]; then
239 deprovision_all_hosts
243 if [ "$1" == "provision" ]; then
249 if [ "$1" == "clean" ]; then
255 if [ "$1" == "remove" ]; then
256 remove_baremetal_operator
260 echo "Usage: metal3.sh"
261 echo "launch - Launch the metal3 operator"
262 echo "provision - provision baremetal node as specified in common.sh"
263 echo "deprovision - deprovision baremetal node as specified in common.sh"
264 echo "clean - clean all the bmh resources"
265 echo "remove - remove baremetal operator"
268 #Following code is tested for the offline mode
269 #Will be intergrated for the offline mode for ICNi v.0.1.0 beta
273 #set_compute_ssh_config