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 #Path to clone the metal3 dev env repo
16 M3PATH="$(go env GOPATH)/src/github.com/metal3-io"
17 #Path to clone the baremetal operator repo
18 BMOPATH="${M3PATH}/baremetal-operator"
20 IMAGE_URL=http://172.22.0.1/images/${BM_IMAGE}
21 IMAGE_CHECKSUM=http://172.22.0.1/images/${BM_IMAGE}.md5sum
23 function clone_repos {
25 if [[ -d ${BMOPATH} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
28 if [ ! -d "${BMOPATH}" ] ; then
30 git clone "${BMOREPO}"
34 git checkout "${BMOBRANCH}"
39 function deprovision_compute_node {
41 if kubectl get baremetalhost $name -n metal3 &>/dev/null; then
42 kubectl patch baremetalhost $name -n metal3 --type merge \
43 -p '{"spec":{"image":{"url":"","checksum":""}}}'
47 # documentation for the values below may be found at
48 # https://cloudinit.readthedocs.io/en/latest/topics/modules.html
49 function create_userdata {
53 COMPUTE_NODE_FQDN="$name.akraino.icn.org"
55 # validate that the user isn't expecting the deprecated
56 # COMPUTE_NODE_PASSWORD to be used
57 if [ "$password" != "${COMPUTE_NODE_PASSWORD:-$password}" ]; then
59 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
60 Unset COMPUTE_NODE_PASSWORD and retry.
65 printf "#cloud-config\n" > $name-userdata.yaml
66 if [ -n "$password" ]; then
67 if [ -n "$username" ]; then
68 passwd=$(mkpasswd --method=SHA-512 --rounds 4096 "$password")
69 printf "users:" >> $name-userdata.yaml
70 printf "\n - name: ""%s" "$username" >> $name-userdata.yaml
71 printf "\n lock_passwd: False" >> $name-userdata.yaml # necessary to allow password login
72 printf "\n passwd: ""%s" "$passwd" >> $name-userdata.yaml
73 printf "\n sudo: \"ALL=(ALL) NOPASSWD:ALL\"" >> $name-userdata.yaml
75 printf "password: ""%s" "$password" >> $name-userdata.yaml
77 printf "\nchpasswd: {expire: False}\n" >> $name-userdata.yaml
78 printf "ssh_pwauth: True\n" >> $name-userdata.yaml
81 if [ -n "$COMPUTE_NODE_FQDN" ]; then
82 printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >> $name-userdata.yaml
83 printf "\n" >> $name-userdata.yaml
85 printf "disable_root: false\n" >> $name-userdata.yaml
86 printf "ssh_authorized_keys:\n - " >> $name-userdata.yaml
88 if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
89 yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
92 cat $HOME/.ssh/id_rsa.pub >> $name-userdata.yaml
93 cloud_init_scripts >> $name-userdata.yaml
94 printf "\n" >> $name-userdata.yaml
97 create_networkdata() {
99 node_networkdata $name > $name-networkdata.json
102 function launch_baremetal_operator {
103 docker pull $IRONIC_BAREMETAL_IMAGE
104 kubectl apply -f bmo/namespace/namespace.yaml
105 kubectl apply -f bmo/rbac/service_account.yaml -n metal3
106 kubectl apply -f bmo/rbac/role.yaml -n metal3
107 kubectl apply -f bmo/rbac/role_binding.yaml
108 kubectl apply -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
109 kubectl apply -f bmo/operator/no_ironic/operator.yaml -n metal3
112 function remove_baremetal_operator {
113 kubectl delete -f bmo/operator/no_ironic/operator.yaml -n metal3
114 kubectl delete -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
115 kubectl delete -f bmo/rbac/role_binding.yaml
116 kubectl delete -f bmo/rbac/role.yaml -n metal3
117 kubectl delete -f bmo/rbac/service_account.yaml -n metal3
118 kubectl delete -f bmo/namespace/namespace.yaml
121 function cloud_init_scripts {
122 # set_dhcp_indentifier.sh:
123 # The IP address assigned to the provisioning NIC will change
124 # due to IPA using the MAC address as the client ID and systemd
125 # using a different ID. Tell systemd to use the MAC as the
126 # client ID. We can't do this in the network data as only the
127 # JSON format is supported by metal3, and the JSON format does
128 # not support the dhcp-identifier field.
129 # set_kernel_cmdline.sh:
130 # The "intel_iommu=on iommu=pt" kernel command line is necessary
134 - path: /var/lib/cloud/scripts/per-instance/set_dhcp_identifier.sh
140 sed -i -e '/dhcp4: true$/!b' -e 'h;s/\S.*/dhcp-identifier: mac/;H;g' /etc/netplan/50-cloud-init.yaml
142 - path: /var/lib/cloud/scripts/per-instance/set_kernel_cmdline.sh
148 grub_file=${1:-"/etc/default/grub"}
149 kernel_parameters="intel_iommu=on iommu=pt"
150 sed -i~ "/^GRUB_CMDLINE_LINUX=/{h;s/\(=\".*\)\"/\1 ${kernel_parameters}\"/};\${x;/^$/{s//GRUB_CMDLINE_LINUX=\"${kernel_parameters}\"/;H};x}" "$grub_file"
156 function apply_userdata_credential {
158 cat <<EOF > ./$name-user-data-credential.yaml
161 userData: $(base64 -w 0 $name-userdata.yaml)
164 name: $name-user-data
168 kubectl apply -n metal3 -f $name-user-data-credential.yaml
171 apply_networkdata_credential() {
173 cat <<EOF > ./$name-network-data-credential.yaml
176 networkData: $(base64 -w 0 $name-networkdata.json)
179 name: $name-network-data
183 kubectl apply -n metal3 -f $name-network-data-credential.yaml
186 function make_bm_hosts {
187 while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
188 create_userdata $name $os_username $os_password
189 apply_userdata_credential $name
190 create_networkdata $name
191 apply_networkdata_credential $name
193 GO111MODULE=auto go run $GOPATH/src/github.com/metal3-io/baremetal-operator/cmd/make-bm-worker/main.go \
194 -address "ipmi://$ipmi_address" \
195 -password "$ipmi_password" \
196 -user "$ipmi_username" \
197 -boot-mac "$boot_mac" \
198 "$name" > $name-bm-node.yaml
200 printf " image:" >> $name-bm-node.yaml
201 printf "\n url: ""%s" "$IMAGE_URL" >> $name-bm-node.yaml
202 printf "\n checksum: ""%s" "$IMAGE_CHECKSUM" >> $name-bm-node.yaml
203 printf "\n userData:" >> $name-bm-node.yaml
204 printf "\n name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
205 printf "\n namespace: metal3" >> $name-bm-node.yaml
206 printf "\n networkData:" >> $name-bm-node.yaml
207 printf "\n name: ""%s" "$name""-network-data" >> $name-bm-node.yaml
208 printf "\n namespace: metal3" >> $name-bm-node.yaml
209 printf "\n rootDeviceHints:" >> $name-bm-node.yaml
210 printf "\n minSizeGigabytes: 48\n" >> $name-bm-node.yaml
211 kubectl apply -f $name-bm-node.yaml -n metal3
215 function configure_nodes {
216 if [ ! -d $IRONIC_DATA_DIR ]; then
217 mkdir -p $IRONIC_DATA_DIR
220 #make sure nodes.json file in /opt/ironic/ are configured
221 if [ ! -f $IRONIC_DATA_DIR/nodes.json ]; then
222 cp $PWD/nodes.json.sample $IRONIC_DATA_DIR/nodes.json
226 function remove_bm_hosts {
227 while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
228 deprovision_compute_node $name
233 while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
234 kubectl delete --ignore-not-found=true bmh $name -n metal3
235 kubectl delete --ignore-not-found=true secrets $name-bmc-secret -n metal3
236 kubectl delete --ignore-not-found=true secrets $name-user-data -n metal3
237 if [ -f $name-bm-node.yaml ]; then
238 rm -rf $name-bm-node.yaml
241 if [ -f $name-user-data-credential.yaml ]; then
242 rm -rf $name-user-data-credential.yaml
245 if [ -f $name-userdata.yaml ]; then
246 rm -rf $name-userdata.yaml
253 if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
254 rm -rf $IRONIC_DATA_DIR/nodes.json
258 function apply_bm_hosts {
259 list_nodes | make_bm_hosts
262 function deprovision_all_hosts {
263 list_nodes | remove_bm_hosts
266 if [ "$1" == "launch" ]; then
268 launch_baremetal_operator
272 if [ "$1" == "deprovision" ]; then
274 deprovision_all_hosts
278 if [ "$1" == "provision" ]; then
284 if [ "$1" == "clean" ]; then
290 if [ "$1" == "remove" ]; then
291 remove_baremetal_operator
295 echo "Usage: metal3.sh"
296 echo "launch - Launch the metal3 operator"
297 echo "provision - provision baremetal node as specified in common.sh"
298 echo "deprovision - deprovision baremetal node as specified in common.sh"
299 echo "clean - clean all the bmh resources"
300 echo "remove - remove baremetal operator"