Created
November 1, 2019 20:18
-
-
Save awels/a5cd1347f143a5b3fb880c7404a8f1f0 to your computer and use it in GitHub Desktop.
import/export shell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash -e | |
| function _kubectl(){ | |
| kubectl "$@" | |
| } | |
| if [ -z $1 ]; then | |
| echo "node to export not provided" | |
| exit 1 | |
| fi | |
| if command -v python3 &>/dev/null; then | |
| echo Python 3 is installed, continuing | |
| else | |
| echo Python 3 is not installed, this script requires python. | |
| fi | |
| #check if node exists. | |
| node=$(_kubectl get nodes $1) | |
| echo "Found node $1" | |
| echo "Creating output directory" | |
| mkdir -p ./$1 | |
| python3 export_pv.py > ./$1/export.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash -e | |
| KUBEVIRT_NS=${KUBEVIRT_NS:-kubevirt} | |
| CDI_NS=${CDI_NS:-cdi} | |
| function _kubectl(){ | |
| kubectl "$@" | |
| } | |
| if [ -z $1 ]; then | |
| echo "node to import to not provided" | |
| exit 1 | |
| fi | |
| if command -v python3 &>/dev/null; then | |
| echo Python 3 is installed, continuing | |
| else | |
| echo Python 3 is not installed, this script requires python. | |
| fi | |
| #check if node exists. | |
| node=$(_kubectl get nodes $1) | |
| echo "Found node $1" | |
| echo "Verifying input directory exists." | |
| if [ ! -d "./$1" ]; then | |
| echo "Directory $1 DOES NOT exist." | |
| exit 1 | |
| fi | |
| echo "Verifying input file exists." | |
| if [ ! -f "./$1/export.json" ]; then | |
| echo "Data file DOES NOT exist in $1." | |
| exit 1 | |
| fi | |
| orgCvoReplicas=$(_kubectl get deployment cluster-version-operator -n openshift-cluster-version -o jsonpath="{@.spec.replicas}") | |
| echo "Bringing down CVO and OLM, warning this will generate cluster health alerts!!!" | |
| _kubectl scale -n openshift-cluster-version deployment/cluster-version-operator --replicas=0 | |
| cvoReplicas=$(_kubectl get deployment cluster-version-operator -n openshift-cluster-version -o jsonpath="{@.status.readyReplicas}") | |
| while (( 0 != cvoReplicas )); do | |
| sleep 5; | |
| cvoReplicas=$(_kubectl get deployment cluster-version-operator -n openshift-cluster-version -o jsonpath="{@.status.readyReplicas}") | |
| done | |
| orgOlmReplicas=$(_kubectl get deployment olm-operator -n openshift-operator-lifecycle-manager -o jsonpath="{@.spec.replicas}") | |
| _kubectl scale -n openshift-operator-lifecycle-manager deployment/olm-operator --replicas=0 | |
| omlReplicas=$(_kubectl get deployment olm-operator -n openshift-operator-lifecycle-manager -o jsonpath="{@.status.readyReplicas}") | |
| while (( 0 != omlReplicas )); do | |
| sleep 5; | |
| omlReplicas=$(_kubectl get deployment olm-operator -n openshift-operator-lifecycle-manager -o jsonpath="{@.status.readyReplicas}") | |
| done | |
| echo "Verified CVO and OLM are down, bringing down kubevirt and CDI" | |
| # D/S namespace is openshift-cnv I believe, but testing here with kubevirt and cdi | |
| orgKubevirtOperatorReplicas=$(_kubectl get deployment virt-operator -n $KUBEVIRT_NS -o jsonpath="{@.spec.replicas}") | |
| _kubectl scale -n $KUBEVIRT_NS deployment/virt-operator --replicas=0 | |
| kubevirtOperatorReplicas=$(_kubectl get deployment virt-operator -n $KUBEVIRT_NS -o jsonpath="{@.status.readyReplicas}") | |
| while (( 0 != kubevirtOperatorReplicas )); do | |
| sleep 5; | |
| kubevirtOperatorReplicas=$(_kubectl get deployment virt-operator -n $KUBEVIRT_NS -o jsonpath="{@.status.readyReplicas}") | |
| done | |
| orgKubevirtControllerReplicas=$(_kubectl get deployment virt-controller -n $KUBEVIRT_NS -o jsonpath="{@.spec.replicas}") | |
| _kubectl scale -n $KUBEVIRT_NS deployment/virt-controller --replicas=0 | |
| kubevirtControllerReplicas=$(_kubectl get deployment virt-controller -n $KUBEVIRT_NS -o jsonpath="{@.status.readyReplicas}") | |
| while (( 0 != kubevirtControllerReplicas )); do | |
| sleep 5; | |
| kubevirtControllerReplicas=$(_kubectl get deployment virt-controller -n $KUBEVIRT_NS -o jsonpath="{@.status.readyReplicas}") | |
| done | |
| orgCDIOperatorReplicas=$(_kubectl get deployment cdi-operator -n $CDI_NS -o jsonpath="{@.spec.replicas}") | |
| _kubectl scale -n $CDI_NS deployment/cdi-operator --replicas=0 | |
| cdiOperatorReplicas=$(_kubectl get deployment cdi-operator -n $CDI_NS -o jsonpath="{@.status.readyReplicas}") | |
| while (( 0 != cdiOperatorReplicas )); do | |
| sleep 5; | |
| cdiOperatorReplicas=$(_kubectl get deployment cdi-operator -n $CDI_NS -o jsonpath="{@.status.readyReplicas}") | |
| done | |
| orgCDIControllerReplicas=$(_kubectl get deployment cdi-deployment -n $CDI_NS -o jsonpath="{@.spec.replicas}") | |
| _kubectl scale -n $CDI_NS deployment/cdi-deployment --replicas=0 | |
| cdiControllerReplicas=$(_kubectl get deployment cdi-deployment -n $CDI_NS -o jsonpath="{@.status.readyReplicas}") | |
| while (( 0 != cdiControllerReplicas )); do | |
| sleep 5; | |
| cdiControllerReplicas=$(_kubectl get deployment cdi-deployment -n $CDI_NS -o jsonpath="{@.status.readyReplicas}") | |
| done | |
| echo "Kubevirt and CDI are down, importing Virtual Machines" | |
| #don't exit on failure, we need to restore | |
| set +e | |
| python3 import_pv.py < ./$1/export.json | |
| echo "Finished importing" | |
| _kubectl scale -n $CDI_NS deployment/cdi-deployment --replicas=$orgCDIControllerReplicas | |
| _kubectl scale -n $CDI_NS deployment/cdi-operator --replicas=$orgCDIOperatorReplicas | |
| _kubectl scale -n $KUBEVIRT_NS deployment/virt-controller --replicas=$orgKubevirtControllerReplicas | |
| _kubectl scale -n $KUBEVIRT_NS deployment/virt-operator --replicas=$orgKubevirtOperatorReplicas | |
| echo "Kubevirt and CDI restored" | |
| _kubectl scale -n openshift-operator-lifecycle-manager deployment/olm-operator --replicas=$orgOlmReplicas | |
| _kubectl scale -n openshift-cluster-version deployment/cluster-version-operator --replicas=$orgCvoReplicas | |
| echo "Finished restoring cluster operations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment