-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-start-remove-vm-provisioning
More file actions
executable file
Β·139 lines (117 loc) Β· 4.1 KB
/
auto-start-remove-vm-provisioning
File metadata and controls
executable file
Β·139 lines (117 loc) Β· 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
# ===============================================
# π§ OpenShift VM Manager (Provision / Start / Delete)
# ===============================================
NAMESPACE="uat-infra-tools"
# List of available templates
declare -A templates
templates=(
["1"]="centos-stream9-desktop"
["2"]="rhel8-desktop"
["3"]="windows2k22-vm-template"
["4"]="windows2k25"
)
# --- Function to create a VM ---
create_vm() {
echo ""
echo "Select a template to provision:"
echo "1) CentOS Stream 9 Desktop"
echo "2) RHEL 8 Desktop"
echo "3) Windows Server 2022 (Template)"
echo "4) Windows Server 2025"
echo ""
read -p "Enter your choice [1-4]: " choice
if [[ -z "${templates[$choice]}" ]]; then
echo "β Invalid choice. Exiting."
exit 1
fi
TEMPLATE=${templates[$choice]}
read -p "Enter VM name: " VM_NAME
[[ -z "$VM_NAME" ]] && { echo "β VM name cannot be empty."; exit 1; }
echo ""
echo "==============================================="
echo " You have selected:"
echo " Template: $TEMPLATE"
echo " VM Name: $VM_NAME"
echo " Namespace: $NAMESPACE"
echo "==============================================="
echo ""
read -p "Proceed with provisioning? (y/n): " confirm
[[ "$confirm" != "y" && "$confirm" != "Y" ]] && { echo "β Operation cancelled."; exit 0; }
echo "π Provisioning VM from template..."
oc process -n "$NAMESPACE" "$TEMPLATE" NAME="$VM_NAME" | oc apply -f - || {
echo "β Failed to create VM. Exiting."
exit 1
}
echo "β³ Waiting for VM resource to appear..."
for i in {1..20}; do
STATUS=$(oc get vm "$VM_NAME" -n "$NAMESPACE" -o jsonpath='{.status.printableStatus}' 2>/dev/null)
[[ -n "$STATUS" ]] && { echo "β
VM detected: $STATUS"; break; }
sleep 5
done
echo "β³ Waiting for VM to reach 'Stopped'..."
for i in {1..60}; do
STATUS=$(oc get vm "$VM_NAME" -n "$NAMESPACE" -o jsonpath='{.status.printableStatus}' 2>/dev/null)
[[ "$STATUS" == "Stopped" ]] && { echo "β
VM is in 'Stopped' state."; break; }
echo " Current status: ${STATUS:-Pending} (waiting...)"
sleep 5
done
echo ""
echo "π Starting the VM..."
virtctl start vm "$VM_NAME" -n "$NAMESPACE"
echo "β³ Waiting for VM to be 'Running'..."
for i in {1..60}; do
STATUS=$(oc get vm "$VM_NAME" -n "$NAMESPACE" -o jsonpath='{.status.printableStatus}' 2>/dev/null)
[[ "$STATUS" == "Running" ]] && { echo "β
VM is Running!"; break; }
echo " Current status: ${STATUS:-Pending} (waiting...)"
sleep 5
done
echo ""
echo "==============================================="
echo "π¦ Final VM Status:"
oc get vm "$VM_NAME" -n "$NAMESPACE"
echo ""
echo "π§© VMI Status:"
oc get vmi "$VM_NAME" -n "$NAMESPACE" || echo "β οΈ No VMI found yet."
}
# --- Function to delete a VM ---
delete_vm() {
echo ""
oc get vm -n "$NAMESPACE"
echo ""
read -p "Enter the VM name to delete: " VM_NAME
[[ -z "$VM_NAME" ]] && { echo "β VM name cannot be empty."; exit 1; }
read -p "Are you sure you want to delete VM '$VM_NAME'? (y/n): " confirm
[[ "$confirm" != "y" && "$confirm" != "Y" ]] && { echo "β Operation cancelled."; exit 0; }
echo "ποΈ Deleting VM '$VM_NAME'..."
oc delete vm "$VM_NAME" -n "$NAMESPACE" --ignore-not-found
oc delete vmi "$VM_NAME" -n "$NAMESPACE" --ignore-not-found
echo "β
VM '$VM_NAME' and its instance have been removed."
}
# --- Function to show VM status ---
show_status() {
echo ""
echo "==============================================="
echo "π¦ Virtual Machines:"
oc get vm -n "$NAMESPACE"
echo ""
echo "π§© Virtual Machine Instances:"
oc get vmi -n "$NAMESPACE"
}
# --- Main menu ---
echo "==============================================="
echo " βοΈ OpenShift VM Management Menu"
echo "==============================================="
echo "1) Create & Auto-Start VM"
echo "2) Delete VM"
echo "3) Show VM Status"
echo "4) Exit"
echo "==============================================="
read -p "Enter your choice [1-4]: " action
case $action in
1) create_vm ;;
2) delete_vm ;;
3) show_status ;;
4) echo "π Exiting."; exit 0 ;;
*) echo "β Invalid option." ;;
esac