X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=deploy%2Fmetal3%2Fscripts%2F01_metal3.sh;h=77597dbb4a2d819096865c4dcc18b79ae900aad1;hb=fb8a0fe0cb2c32037ec16ad2b377aa63a42b114a;hp=6e6a1e9908e4db7397484dcdd39b4d5c845d7b68;hpb=330beeba6da6f89791d68f5dcfa76f2e7e110b24;p=icn.git diff --git a/deploy/metal3/scripts/01_metal3.sh b/deploy/metal3/scripts/01_metal3.sh index 6e6a1e9..77597db 100755 --- a/deploy/metal3/scripts/01_metal3.sh +++ b/deploy/metal3/scripts/01_metal3.sh @@ -1,5 +1,5 @@ -#!/bin/bash -set +ex +#!/usr/bin/env bash +set -eu -o pipefail LIBDIR="$(dirname "$(dirname "$(dirname "$PWD")")")" @@ -12,125 +12,144 @@ if [[ $EUID -ne 0 ]]; then exit 1 fi +#Path to clone the metal3 dev env repo +M3PATH="$(go env GOPATH)/src/github.com/metal3-io" +#Path to clone the baremetal operator repo +BMOPATH="${M3PATH}/baremetal-operator" + IMAGE_URL=http://172.22.0.1/images/${BM_IMAGE} IMAGE_CHECKSUM=http://172.22.0.1/images/${BM_IMAGE}.md5sum -function get_default_inteface_ipaddress { - local _ip=$1 - local _default_interface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route) - local _ipv4address=$(ip addr show dev $_default_interface | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }') - eval $_ip="'$_ipv4address'" -} - -function create_ssh_key { - #ssh key for compute node to communicate back to bootstrap server - mkdir -p $BUILD_DIR/ssh_key - ssh-keygen -C "compute.icn.akraino.lfedge.org" -f $BUILD_DIR/ssh_key/id_rsa - cat $BUILD_DIR/ssh_key/id_rsa.pub >> $HOME/.ssh/authorized_keys -} - -function set_compute_key { - _SSH_LOCAL_KEY=$(cat $BUILD_DIR/ssh_key/id_rsa) - cat << EOF -write_files: -- path: /opt/ssh_id_rsa - owner: root:root - permissions: '0600' - content: | - $_SSH_LOCAL_KEY -EOF +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 deprovision_compute_node { name="$1" - kubectl patch baremetalhost $name -n metal3 --type merge \ - -p '{"spec":{"image":{"url":"","checksum":""}}}' -} - -function set_compute_ssh_config { - get_default_inteface_ipaddress default_addr - cat << EOF -- path: /root/.ssh/config - owner: root:root - permissions: '0600' - content: | - Host bootstrapmachine $default_addr - HostName $default_addr - IdentityFile /opt/ssh_id_rsa - User $USER -- path: /etc/apt/sources.list - owner: root:root - permissions: '0665' - content: | - deb [trusted=yes] ssh://$USER@$default_addr:$LOCAL_APT_REPO ./ -EOF + if kubectl get baremetalhost $name -n metal3 &>/dev/null; then + kubectl patch baremetalhost $name -n metal3 --type merge \ + -p '{"spec":{"image":{"url":"","checksum":""}}}' + fi } +# 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 < $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 - network_config_files >> $name-userdata.yaml + cloud_init_scripts >> $name-userdata.yaml printf "\n" >> $name-userdata.yaml } +create_networkdata() { + name="$1" + node_networkdata $name > $name-networkdata.json +} + function launch_baremetal_operator { - if [ ! -d $GOPATH/src/github.com/metal3-io/baremetal-operator ]; then - go get github.com/metal3-io/baremetal-operator - git checkout 3d40caa29dce82878d83aeb7f8dab4dc4a856160 - fi + 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 + kubectl apply -f bmo/rbac/role_binding.yaml + kubectl apply -f bmo/crds/metal3.io_baremetalhosts_crd.yaml + kubectl apply -f bmo/operator/no_ironic/operator.yaml -n metal3 +} - pushd $GOPATH/src/github.com/metal3-io/baremetal-operator - make deploy - popd +function remove_baremetal_operator { + kubectl delete -f bmo/operator/no_ironic/operator.yaml -n metal3 + kubectl delete -f bmo/crds/metal3.io_baremetalhosts_crd.yaml + kubectl delete -f bmo/rbac/role_binding.yaml + kubectl delete -f bmo/rbac/role.yaml -n metal3 + kubectl delete -f bmo/rbac/service_account.yaml -n metal3 + kubectl delete -f bmo/namespace/namespace.yaml } -function network_config_files { +function cloud_init_scripts { + # set_dhcp_indentifier.sh: + # The IP address assigned to the provisioning NIC will change + # due to IPA using the MAC address as the client ID and systemd + # using a different ID. Tell systemd to use the MAC as the + # client ID. We can't do this in the network data as only the + # JSON format is supported by metal3, and the JSON format does + # not support the dhcp-identifier field. + # set_kernel_cmdline.sh: + # The "intel_iommu=on iommu=pt" kernel command line is necessary + # for QAT support. cat << 'EOF' write_files: -- path: /opt/ironic_net.sh +- path: /var/lib/cloud/scripts/per-instance/set_dhcp_identifier.sh owner: root:root permissions: '0777' content: | #!/usr/bin/env bash - set -xe - for intf in /sys/class/net/*; do - sudo ifconfig `basename $intf` up - sudo dhclient -nw `basename $intf` - done -EOF -cat << EOF -- path: /opt/user_net.sh + set -eux -o pipefail + sed -i -e '/dhcp4: true$/!b' -e 'h;s/\S.*/dhcp-identifier: mac/;H;g' /etc/netplan/50-cloud-init.yaml + netplan apply +- path: /var/lib/cloud/scripts/per-instance/set_kernel_cmdline.sh owner: root:root permissions: '0777' content: | #!/usr/bin/env bash - set -xe - route add default gw $PROVIDER_NETWORK_GATEWAY - sed -i -e 's/^#DNS=.*/DNS=$PROVIDER_NETWORK_DNS/g' /etc/systemd/resolved.conf - systemctl daemon-reload - systemctl restart systemd-resolved -runcmd: - - [ /opt/ironic_net.sh ] - - [ /opt/user_net.sh ] + set -eux -o pipefail + grub_file=${1:-"/etc/default/grub"} + kernel_parameters="intel_iommu=on iommu=pt" + sed -i~ "/^GRUB_CMDLINE_LINUX=/{h;s/\(=\".*\)\"/\1 ${kernel_parameters}\"/};\${x;/^$/{s//GRUB_CMDLINE_LINUX=\"${kernel_parameters}\"/;H};x}" "$grub_file" + update-grub + reboot EOF } @@ -149,15 +168,32 @@ EOF kubectl apply -n metal3 -f $name-user-data-credential.yaml } +apply_networkdata_credential() { + name="$1" + cat < ./$name-network-data-credential.yaml +apiVersion: v1 +data: + networkData: $(base64 -w 0 $name-networkdata.json) +kind: Secret +metadata: + name: $name-network-data + namespace: metal3 +type: Opaque +EOF + kubectl apply -n metal3 -f $name-network-data-credential.yaml +} + 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 + create_networkdata $name + apply_networkdata_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" \ + GO111MODULE=auto go run $GOPATH/src/github.com/metal3-io/baremetal-operator/cmd/make-bm-worker/main.go \ + -address "ipmi://$ipmi_address" \ + -password "$ipmi_password" \ + -user "$ipmi_username" \ "$name" > $name-bm-node.yaml printf " image:" >> $name-bm-node.yaml @@ -165,7 +201,12 @@ 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 networkData:" >> $name-bm-node.yaml + printf "\n name: ""%s" "$name""-network-data" >> $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 } @@ -182,21 +223,35 @@ 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 + + if [ -f $name-user-data-credential.yaml ]; then + rm -rf $name-user-data-credential.yaml + fi + + if [ -f $name-userdata.yaml ]; then + rm -rf $name-userdata.yaml + fi done } function clean_all { list_nodes | cleanup + if [ -f $IRONIC_DATA_DIR/nodes.json ]; then + rm -rf $IRONIC_DATA_DIR/nodes.json + fi } function apply_bm_hosts { @@ -208,6 +263,7 @@ function deprovision_all_hosts { } if [ "$1" == "launch" ]; then + clone_repos launch_baremetal_operator exit 0 fi @@ -230,16 +286,15 @@ if [ "$1" == "clean" ]; then exit 0 fi +if [ "$1" == "remove" ]; then + remove_baremetal_operator + exit 0 +fi + echo "Usage: metal3.sh" echo "launch - Launch the metal3 operator" echo "provision - provision baremetal node as specified in common.sh" echo "deprovision - deprovision baremetal node as specified in common.sh" -echo "clean - clean all the resources" +echo "clean - clean all the bmh resources" +echo "remove - remove baremetal operator" exit 1 - -#Following code is tested for the offline mode -#Will be intergrated for the offline mode for ICNi v.0.1.0 beta -#create_ssh_key -#create_userdata -#set_compute_key -#set_compute_ssh_config