From e88a5c5c1fb1588fbe18ba9359fdf0df0eefafc6 Mon Sep 17 00:00:00 2001 From: Vladislav Kuzmin Date: Tue, 13 Apr 2021 15:53:18 +0400 Subject: [PATCH] Move virsh calls to generic container Relates-To: #517 Change-Id: I96178dfb9addbe76b8897bbe4cd58f150563b81a --- .../function/phase-helpers/kustomization.yaml | 2 + .../virsh-destroy-vms/kustomization.yaml | 6 +++ .../virsh-destroy-vms/virsh_destroy_vms.sh | 27 ++++++++++++++ .../kustomization.yaml | 6 +++ .../virsh_eject_cdrom_images.sh | 30 +++++++++++++++ manifests/phases/executors.yaml | 37 +++++++++++++++++++ manifests/phases/phases.yaml | 20 ++++++++++ tools/deployment/30_deploy_controlplane.sh | 14 ++----- tools/deployment/35_deploy_worker_node.sh | 11 ++---- 9 files changed, 136 insertions(+), 17 deletions(-) create mode 100644 manifests/function/phase-helpers/virsh-destroy-vms/kustomization.yaml create mode 100644 manifests/function/phase-helpers/virsh-destroy-vms/virsh_destroy_vms.sh create mode 100644 manifests/function/phase-helpers/virsh-eject-cdrom-images/kustomization.yaml create mode 100644 manifests/function/phase-helpers/virsh-eject-cdrom-images/virsh_eject_cdrom_images.sh diff --git a/manifests/function/phase-helpers/kustomization.yaml b/manifests/function/phase-helpers/kustomization.yaml index d4007eeff..4aea68b50 100644 --- a/manifests/function/phase-helpers/kustomization.yaml +++ b/manifests/function/phase-helpers/kustomization.yaml @@ -7,3 +7,5 @@ resources: - wait_pods - pause_bmh - wait_cluster +- virsh-eject-cdrom-images +- virsh-destroy-vms diff --git a/manifests/function/phase-helpers/virsh-destroy-vms/kustomization.yaml b/manifests/function/phase-helpers/virsh-destroy-vms/kustomization.yaml new file mode 100644 index 000000000..a1f99eb72 --- /dev/null +++ b/manifests/function/phase-helpers/virsh-destroy-vms/kustomization.yaml @@ -0,0 +1,6 @@ +configMapGenerator: +- name: virsh-destroy-vms + options: + disableNameSuffixHash: true + files: + - script=virsh_destroy_vms.sh diff --git a/manifests/function/phase-helpers/virsh-destroy-vms/virsh_destroy_vms.sh b/manifests/function/phase-helpers/virsh-destroy-vms/virsh_destroy_vms.sh new file mode 100644 index 000000000..c8c55bd2a --- /dev/null +++ b/manifests/function/phase-helpers/virsh-destroy-vms/virsh_destroy_vms.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -ex + +EPHEMERAL_DOMAIN_NAME="air-ephemeral" + +if type "virsh" > /dev/null; then + for vm in $(virsh list --all --name --state-running |grep ${EPHEMERAL_DOMAIN_NAME}) + do + echo "Stop ephemeral node '$vm'" 1>&2 + virsh destroy $vm 1>&2 + done +else + echo "Can't find virsh" 1>&2 +fi diff --git a/manifests/function/phase-helpers/virsh-eject-cdrom-images/kustomization.yaml b/manifests/function/phase-helpers/virsh-eject-cdrom-images/kustomization.yaml new file mode 100644 index 000000000..8fb0b343d --- /dev/null +++ b/manifests/function/phase-helpers/virsh-eject-cdrom-images/kustomization.yaml @@ -0,0 +1,6 @@ +configMapGenerator: +- name: virsh-eject-cdrom-images + options: + disableNameSuffixHash: true + files: + - script=virsh_eject_cdrom_images.sh diff --git a/manifests/function/phase-helpers/virsh-eject-cdrom-images/virsh_eject_cdrom_images.sh b/manifests/function/phase-helpers/virsh-eject-cdrom-images/virsh_eject_cdrom_images.sh new file mode 100644 index 000000000..68709e3ea --- /dev/null +++ b/manifests/function/phase-helpers/virsh-eject-cdrom-images/virsh_eject_cdrom_images.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -ex + +EPHEMERAL_DOMAIN_NAME="air-ephemeral" + +if type "virsh" > /dev/null; then + echo "Ensure all cdrom images are ejected." 1>&2 + for vm in $(virsh list --all --name |grep -v ${EPHEMERAL_DOMAIN_NAME}) + do + virsh domblklist $vm | + awk 'NF==2 {print $1}' | + grep -v Target | + xargs -I{} virsh change-media $vm {} --eject || : 1>&2 + done +else + echo "Can't find virsh" 1>&2 +fi diff --git a/manifests/phases/executors.yaml b/manifests/phases/executors.yaml index 3eb54f16b..63c811bec 100644 --- a/manifests/phases/executors.yaml +++ b/manifests/phases/executors.yaml @@ -423,3 +423,40 @@ configRef: kind: ConfigMap name: kubectl-wait-cluster apiVersion: v1 +--- +apiVersion: airshipit.org/v1alpha1 +kind: GenericContainer +metadata: + name: virsh-eject-cdrom-images + labels: + airshipit.org/deploy-k8s: "false" +spec: + image: quay.io/airshipit/toolbox-virsh:latest + hostNetwork: true + mounts: + - type: bind + src: /var/run/libvirt/libvirt-sock + dst: /var/run/libvirt/libvirt-sock +configRef: + kind: ConfigMap + name: virsh-eject-cdrom-images + apiVersion: v1 +--- +apiVersion: airshipit.org/v1alpha1 +kind: GenericContainer +metadata: + name: virsh-destroy-vms + labels: + airshipit.org/deploy-k8s: "false" +spec: + image: quay.io/airshipit/toolbox-virsh:latest + hostNetwork: true + mounts: + - type: bind + src: /var/run/libvirt/libvirt-sock + dst: /var/run/libvirt/libvirt-sock +configRef: + kind: ConfigMap + name: virsh-destroy-vms + apiVersion: v1 + diff --git a/manifests/phases/phases.yaml b/manifests/phases/phases.yaml index e2c4dfda5..76e2a72ae 100644 --- a/manifests/phases/phases.yaml +++ b/manifests/phases/phases.yaml @@ -390,3 +390,23 @@ config: kind: GenericContainer name: kubectl-wait-cluster documentEntryPoint: ephemeral/controlplane +--- +apiVersion: airshipit.org/v1alpha1 +kind: Phase +metadata: + name: virsh-eject-cdrom-images +config: + executorRef: + apiVersion: airshipit.org/v1alpha1 + kind: GenericContainer + name: virsh-eject-cdrom-images +--- +apiVersion: airshipit.org/v1alpha1 +kind: Phase +metadata: + name: virsh-destroy-vms +config: + executorRef: + apiVersion: airshipit.org/v1alpha1 + kind: GenericContainer + name: virsh-destroy-vms diff --git a/tools/deployment/30_deploy_controlplane.sh b/tools/deployment/30_deploy_controlplane.sh index 47bc80fa5..84043af33 100755 --- a/tools/deployment/30_deploy_controlplane.sh +++ b/tools/deployment/30_deploy_controlplane.sh @@ -18,16 +18,10 @@ EPHEMERAL_DOMAIN_NAME="air-ephemeral" # TODO (dukov) this is needed due to sushy tools inserts cdrom image to # all vms. This can be removed once sushy tool is fixed -if type "virsh" > /dev/null; then - echo "Ensure all cdrom images are ejected." - for vm in $(sudo virsh list --all --name |grep -v ${EPHEMERAL_DOMAIN_NAME}) - do - sudo virsh domblklist $vm | - awk 'NF==2 {print $1}' | - grep -v Target | - xargs -I{} sudo virsh change-media $vm {} --eject || : - done -fi +# Scripts for this phase placed in manifests/function/phase-helpers/virsh-eject-cdrom-images/ +# To get ConfigMap for this phase, execute `airshipctl phase render --source config -k ConfigMap` +# and find ConfigMap with name virsh-eject-cdrom-images +airshipctl phase run virsh-eject-cdrom-images --debug echo "Create target k8s cluster resources" airshipctl phase run controlplane-ephemeral --debug diff --git a/tools/deployment/35_deploy_worker_node.sh b/tools/deployment/35_deploy_worker_node.sh index b77225e4c..fff9453bb 100755 --- a/tools/deployment/35_deploy_worker_node.sh +++ b/tools/deployment/35_deploy_worker_node.sh @@ -22,13 +22,10 @@ WORKER_NODE=${WORKER_NODE:-"node03"} EPHEMERAL_DOMAIN_NAME="air-ephemeral" # all vms. This can be removed once sushy tool is fixed -if type "virsh" > /dev/null; then - for vm in $(sudo virsh list --all --name --state-running |grep ${EPHEMERAL_DOMAIN_NAME}) - do - echo "Stop ephemeral node '$vm'" - sudo virsh destroy $vm - done -fi +# Scripts for this phase placed in manifests/function/phase-helpers/virsh-destroy-vms/ +# To get ConfigMap for this phase, execute `airshipctl phase render --source config -k ConfigMap` +# and find ConfigMap with name virsh-destroy-vms +airshipctl phase run virsh-destroy-vms --debug node_timeout () { end=$(($(date +%s) + $TIMEOUT))