-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-provisioning
More file actions
executable file
Β·133 lines (111 loc) Β· 3.28 KB
/
template-provisioning
File metadata and controls
executable file
Β·133 lines (111 loc) Β· 3.28 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
#!/bin/bash
# ===============================================
# π§ OpenShift VM Template Provisioner & Auto Starter
# ===============================================
# Namespace where templates are located
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"
)
echo "==============================================="
echo " π§ OpenShift VM Template Provisioner"
echo "==============================================="
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
# Validate input
if [[ -z "${templates[$choice]}" ]]; then
echo "β Invalid choice. Exiting."
exit 1
fi
TEMPLATE=${templates[$choice]}
# Ask for VM name
read -p "Enter VM name: " VM_NAME
if [[ -z "$VM_NAME" ]]; then
echo "β VM name cannot be empty."
exit 1
fi
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
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
echo "β Operation cancelled."
exit 0
fi
# Step 1: Provision the VM
echo "π Provisioning VM from template..."
oc process -n "$NAMESPACE" "$TEMPLATE" NAME="$VM_NAME" | oc apply -f -
if [[ $? -ne 0 ]]; then
echo "β Failed to create VM. Please check logs."
exit 1
fi
# Step 2: Wait until VM shows up
echo ""
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)
if [[ -n "$STATUS" ]]; then
echo "β
VM detected: $STATUS"
break
fi
sleep 5
done
# Step 3: Wait until VM becomes Stopped
echo ""
echo "β³ Waiting for VM status to become 'Stopped'..."
for i in {1..60}; do
STATUS=$(oc get vm "$VM_NAME" -n "$NAMESPACE" -o jsonpath='{.status.printableStatus}' 2>/dev/null)
if [[ "$STATUS" == "Stopped" ]]; then
echo "β
VM is in 'Stopped' state."
break
fi
echo " Current status: ${STATUS:-Pending} (waiting...)"
sleep 5
done
# Step 4: Start the VM automatically
echo ""
echo "π Starting the VM..."
virtctl start vm "$VM_NAME" -n "$NAMESPACE"
if [[ $? -ne 0 ]]; then
echo "β Failed to start VM. Please check logs."
exit 1
fi
# Step 5: Wait for VM to be running
echo ""
echo "β³ Waiting for VM to enter 'Running' state..."
for i in {1..60}; do
STATUS=$(oc get vm "$VM_NAME" -n "$NAMESPACE" -o jsonpath='{.status.printableStatus}' 2>/dev/null)
if [[ "$STATUS" == "Running" ]]; then
echo "β
VM is Running!"
break
fi
echo " Current status: ${STATUS:-Pending} (waiting...)"
sleep 5
done
# Step 6: Show final status
echo ""
echo "==============================================="
echo "π¦ Final VM Status:"
oc get vm "$VM_NAME" -n "$NAMESPACE"
echo ""
echo "==============================================="
echo "π§© VMI Status:"
oc get vmi "$VM_NAME" -n "$NAMESPACE" || echo "β οΈ No VMI found yet."
echo ""
echo "β
All done."