Do not error when a deleted resource is not found
[icn.git] / deploy / metal3 / scripts / 01_metal3.sh
index dbfa2d3..d419b82 100755 (executable)
@@ -15,6 +15,22 @@ fi
 IMAGE_URL=http://172.22.0.1/images/${BM_IMAGE}
 IMAGE_CHECKSUM=http://172.22.0.1/images/${BM_IMAGE}.md5sum
 
+function clone_repos {
+    mkdir -p "${M3PATH}"
+    if [[ -d ${BMOPATH} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
+      rm -rf "${BMOPATH}"
+    fi
+    if [ ! -d "${BMOPATH}" ] ; then
+        pushd "${M3PATH}"
+        git clone "${BMOREPO}"
+        popd
+    fi
+    pushd "${BMOPATH}"
+    git checkout "${BMOBRANCH}"
+    git pull -r || true
+    popd
+}
+
 function get_default_interface_ipaddress {
     local _ip=$1
     local _default_interface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
@@ -43,8 +59,10 @@ EOF
 
 function deprovision_compute_node {
     name="$1"
-    kubectl patch baremetalhost $name -n metal3 --type merge \
-    -p '{"spec":{"image":{"url":"","checksum":""}}}'
+    if kubectl get baremetalhost $name -n metal3 &>/dev/null; then
+        kubectl patch baremetalhost $name -n metal3 --type merge \
+        -p '{"spec":{"image":{"url":"","checksum":""}}}'
+    fi
 }
 
 function set_compute_ssh_config {
@@ -66,25 +84,49 @@ function set_compute_ssh_config {
 EOF
 }
 
+# documentation for the values below may be found at
+# https://cloudinit.readthedocs.io/en/latest/topics/modules.html
 function create_userdata {
     name="$1"
+    username="$2"
+    password="$3"
     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
+
+    # validate that the user isn't expecting the deprecated
+    # COMPUTE_NODE_PASSWORD to be used
+    if [ "$password" != "${COMPUTE_NODE_PASSWORD:-$password}" ]; then
+        cat <<EOF
+COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
+Unset COMPUTE_NODE_PASSWORD and retry.
+EOF
+        exit 1
+    fi
+
     printf "#cloud-config\n" >  $name-userdata.yaml
-    if [ -n "$COMPUTE_NODE_PASSWORD" ]; then
-    printf "password: ""%s" "$COMPUTE_NODE_PASSWORD" >>  $name-userdata.yaml
-    printf "\nchpasswd: {expire: False}\n" >>  $name-userdata.yaml
-    printf "ssh_pwauth: True\n" >>  $name-userdata.yaml
+    if [ -n "$password" ]; then
+        if [ -n "$username" ]; then
+            passwd=$(mkpasswd --method=SHA-512 --rounds 4096 "$password")
+            printf "users:" >>  $name-userdata.yaml
+            printf "\n  - name: ""%s" "$username" >>  $name-userdata.yaml
+            printf "\n    lock_passwd: False" >>  $name-userdata.yaml # necessary to allow password login
+            printf "\n    passwd: ""%s" "$passwd" >>  $name-userdata.yaml
+            printf "\n    sudo: \"ALL=(ALL) NOPASSWD:ALL\"" >>  $name-userdata.yaml
+        else
+            printf "password: ""%s" "$password" >>  $name-userdata.yaml
+        fi
+        printf "\nchpasswd: {expire: False}\n" >>  $name-userdata.yaml
+        printf "ssh_pwauth: True\n" >>  $name-userdata.yaml
     fi
 
     if [ -n "$COMPUTE_NODE_FQDN" ]; then
-    printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >>  $name-userdata.yaml
-    printf "\n" >>  $name-userdata.yaml
+        printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >>  $name-userdata.yaml
+        printf "\n" >>  $name-userdata.yaml
     fi
     printf "disable_root: false\n" >>  $name-userdata.yaml
     printf "ssh_authorized_keys:\n  - " >>  $name-userdata.yaml
 
     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
-    yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
+        yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
     fi
 
     cat $HOME/.ssh/id_rsa.pub >>  $name-userdata.yaml
@@ -93,7 +135,7 @@ function create_userdata {
 }
 
 function launch_baremetal_operator {
-    docker pull integratedcloudnative/baremetal-operator:v1.0-icn
+    docker pull $IRONIC_BAREMETAL_IMAGE
     kubectl apply -f bmo/namespace/namespace.yaml
     kubectl apply -f bmo/rbac/service_account.yaml -n metal3
     kubectl apply -f bmo/rbac/role.yaml -n metal3
@@ -158,14 +200,14 @@ EOF
 }
 
 function make_bm_hosts {
-    while read -r name username password address; do
-        create_userdata $name
+    while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
+        create_userdata $name $os_username $os_password
         apply_userdata_credential $name
 
         go run $GOPATH/src/github.com/metal3-io/baremetal-operator/cmd/make-bm-worker/main.go \
-           -address "ipmi://$address" \
-           -password "$password" \
-           -user "$username" \
+           -address "ipmi://$ipmi_address" \
+           -password "$ipmi_password" \
+           -user "$ipmi_username" \
            "$name" > $name-bm-node.yaml
 
         printf "  image:" >> $name-bm-node.yaml
@@ -173,7 +215,9 @@ function make_bm_hosts {
         printf "\n    checksum: ""%s" "$IMAGE_CHECKSUM" >> $name-bm-node.yaml
         printf "\n  userData:" >> $name-bm-node.yaml
         printf "\n    name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
-        printf "\n    namespace: metal3\n" >> $name-bm-node.yaml
+        printf "\n    namespace: metal3" >> $name-bm-node.yaml
+        printf "\n  rootDeviceHints:" >> $name-bm-node.yaml
+        printf "\n    minSizeGigabytes: 48\n" >> $name-bm-node.yaml
         kubectl apply -f $name-bm-node.yaml -n metal3
     done
 }
@@ -190,16 +234,16 @@ function configure_nodes {
 }
 
 function remove_bm_hosts {
-    while read -r name username password address; do
+    while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
         deprovision_compute_node $name
     done
 }
 
 function cleanup {
-    while read -r name username password address; do
-        kubectl delete bmh $name -n metal3
-        kubectl delete secrets $name-bmc-secret -n metal3
-        kubectl delete secrets $name-user-data -n metal3
+    while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
+        kubectl delete --ignore-not-found=true bmh $name -n metal3
+        kubectl delete --ignore-not-found=true secrets $name-bmc-secret -n metal3
+        kubectl delete --ignore-not-found=true secrets $name-user-data -n metal3
         if [ -f $name-bm-node.yaml ]; then
             rm -rf $name-bm-node.yaml
         fi
@@ -230,6 +274,7 @@ function deprovision_all_hosts {
 }
 
 if [ "$1" == "launch" ]; then
+    clone_repos
     launch_baremetal_operator
     exit 0
 fi