forked from microsoft/AcademicContent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-vm-image.sh
More file actions
253 lines (218 loc) · 6.57 KB
/
create-vm-image.sh
File metadata and controls
253 lines (218 loc) · 6.57 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash
# ------------------------------------------------------------------
# Title:
# Azure CS University DS Sandboxes Project
#
# Description:
# The script creates an image from the specified VM
#
# Dependency:
# https://github.com/Azure/azure-cli
# ------------------------------------------------------------------
# Current Version
version="1.0.4"
#
# Purpose: Create new image
#
createImage() {
local vmName=$1
local resourceGroupName=$2
local hostName=$3
local userName=$4
local rand=$(cat /proc/sys/kernel/random/uuid)
local outputFile=Image-$rand.log
local osType=$(az vm show -g $resourceGroupName -n $vmName --query "storageProfile.osDisk.osType" -o tsv)
local osDiskId=$(az vm show -g $resourceGroupName -n $vmName --query "storageProfile.osDisk.managedDisk.id" -o tsv)
local location=$(az group show --n $resourceGroupName --query "location" -o tsv)
local imageResourceGroupName=$resourceGroupName-images
local plan=$(az vm show -g $resourceGroupName -n $vmName --query "plan.name" -o tsv)
if [ -z "$plan" ]; then
plan="Custom"
fi
local snapshotName=snap-$osType-$plan-$rand
local imageName=image-$osType-$plan-$rand
local defaultPassword=tS8QmuOZkwF7xzxdQ
local isActive=false
echo "Deprovision VM $vmName"
if [[ $osType == "Linux" ]]; then
ssh -t $userName@$hostName 'sudo waagent -deprovision -force;exit'
else
az vm extension delete --name CustomScript --vm-name $vmName --resource-group $resourceGroupName
az vm extension set \
--publisher Microsoft.Compute \
--version 1.8 \
--name CustomScriptExtension \
--vm-name $vmName \
--resource-group $resourceGroupName \
--no-auto-upgrade \
--settings '{"fileUris": ["https://raw.githubusercontent.com/MSFTImagine/computerscience/master/Scripts/Bulk-Sandbox-Deployment-Automation-Bash/modules/sysprep.ps1"], "commandToExecute":"powershell -ExecutionPolicy Unrestricted -File sysprep.ps1"}' 1>/dev/null
echo Waiting deprovision...
sleep 10m
fi
echo "Deallocating VM $vmName"
az vm deallocate --resource-group $resourceGroupName --name $vmName
echo "Generalizing VM $vmName"
az vm generalize --resource-group $resourceGroupName --name $vmName
if [ -z "$osDiskId" ]; then
osDiskId=$(az vm show -g $resourceGroupName -n $vmName --query "storageProfile.osDisk.vhd.uri" -o tsv)
fi
az group create --name $imageResourceGroupName --location $location 1>/dev/null
echo "Creating snapshot $snapshotName"
az snapshot create -g $imageResourceGroupName -n $snapshotName --source $osDiskId
snapshotId=$(az snapshot show --name "$snapshotName" --resource-group "$imageResourceGroupName" --query [id] -o tsv)
echo "Creating image $imageName"
az image create --resource-group $imageResourceGroupName --name $imageName --source $snapshotId --os-type $osType
imageId=$(az image show --name "$imageName" --resource-group "$imageResourceGroupName" --query [id] -o tsv)
echo "Deleting VM $vmName and related resources"
deleteResources "$resourceGroupName" "$vmName"
if [[ $plan == "Custom" ]]; then
echo "Restore VM..."
az vm create --resource-group $resourceGroupName --name $vmName --image $imageId --admin-username $userName --admin-password $defaultPassword --authentication-type password
echo "Restore VM successfully completed"
fi
if [ $? -ne 0 ]; then
echo "An error occurred during the image preparation" >>$outputFile
else
echo -n "
The image preparation was completed successfully!
To create a new VM, run the following script with parameters:
$(tput setaf 2)./deploy-vm.sh -in input.csv -out output.csv -l westus -s Standard_DS3_v2 -st premium -d manage -m off -i $imageId$(tput sgr 0)
"
echo $imageId >>$outputFile
fi
}
#
# Purpose: Delete unnecessary resources.
#
deleteResources() {
local rgName=$1
local name=$2
az vm delete -g $rgName -n $name --yes
az network nic delete -g $rgName -n "$name-net"
az network public-ip delete -g $rgName -n "$name-ip"
az network nsg delete -g $rgName -n "$name-nsg"
az network vnet delete -g $rgName -n "$name-vnet"
}
#
# Purpose: Show help information
#
usage() {
echo -n "
Usage: $0 [options...] [arguments...]
This script allows you to expand the capabilities of this script to any VM, not just DSVMs
Options:
-vm, --vmname The name of the Virtual Machine
-g, --group Name of resource group
-ht, --host Public IP address or DNS url
-u, --username Virtual Machine username
-subid, --subscriptionId Subscription Id
---------------------------------------------------------------------
-h, --help Display this help and exit
-v, --version Output version information and exit
---------------------------------------------------------------------
Examples:
- Create image from VM:
$(tput setaf 4)$0 -vm <Virtual Machine Name> -g <Resource Group Name> -ht <DNS or IP address> -u <User Name> -subid <Subscription Id>$(tput sgr 0)
---------------------------------------------------------------------
"
}
#
# Purpose: Main section
#
main() {
local vmName
local resourceGroup
local hostName
local userName
local subscriptionId
if [ $# -eq 0 ]; then
echo "No arguments supplied"
usage
exit 0
fi
#Check version
if [ $1 == '-v' ] || [ $1 == '--version' ]; then
echo "$(basename $0) ${version}"
exit 0
fi
while test $# -gt 0; do
case "$1" in
-h | --help)
usage
exit 0
;;
-vm | --vmname)
shift
if test $# -gt 0; then
vmName=$1
else
echo "no vm name specified"
exit 1
fi
shift
;;
-g | --group)
shift
if test $# -gt 0; then
resourceGroup=$1
else
echo "no resource group specified"
exit 1
fi
shift
;;
-ht | --host)
shift
if test $# -gt 0; then
hostName=$1
else
echo "no host name specified"
exit 1
fi
shift
;;
-u | --username)
shift
if test $# -gt 0; then
userName=$1
else
echo "no username specified"
exit 1
fi
shift
;;
-subid | --subscriptionId)
shift
if test $# -gt 0; then
subscriptionId=$1
else
echo "no subscriptionId specified"
exit 1
fi
shift
;;
*)
echo "Unknown parameter"
usage
exit 0
;;
esac
done
#Check arguments
[[ "${vmName:?}" ]]
[[ "${vmName:?}" ]]
[[ "${resourceGroup:?}" ]]
[[ "${hostName:?}" ]]
[[ "${userName:?}" ]]
[[ "${subscriptionId:?}" ]]
#login to azure using your credentials if needed
az account show 1>/dev/null
if [ $? != 0 ]; then
az login
fi
#Set the context to the subscription Id
az account set --subscription $subscriptionId
createImage "$vmName" "$resourceGroup" "$hostName" "$userName"
return 0
}
main "$@"