Skip to content

Commit 0737ca1

Browse files
committed
vm setup ui
1 parent 4b564b8 commit 0737ca1

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

cloud-init.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ write_files:
4040
DNSIP=$(dig +short $APPSEC_HOSTNAME)
4141
echo "Checking that DNS recort for $APPSEC_HOSTNAME points to $VMPUBLICIP"
4242
if [ "$VMPUBLICIP" == "$DNSIP" ]; then
43-
echo "SUCCESS: DNS points to this VM."
43+
echo -e "\033[32m SUCCESS: DNS points to this VM."
4444
else
4545
if [ -z "$DNSIP" ]; then
46-
echo "DNS record not defined. Create: $APPSEC_HOSTNAME IN A $VMPUBLICIP"
46+
echo -e "\033[31m DNS record not defined. Create: $APPSEC_HOSTNAME IN A $VMPUBLICIP"
4747
else
48-
echo "DNS record points to ***wrong*** IP: $DNSIP, but it should be $VMPUBLICIP"
48+
echo -e "\033[31m DNS record points to ***wrong*** IP: $DNSIP, but it should be $VMPUBLICIP"
4949
fi
50-
echo "!!! FAILED !!!: please setup DNS record for $APPSEC_HOSTNAME"
50+
echo -e "\033[31m FAILED: please setup DNS record for $APPSEC_HOSTNAME"
5151
fi
5252
permissions: '0755'
5353

setup-vm.sh

+41-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
#!/bin/bash
22

33
export RANDOM_ID="$(openssl rand -hex 3)"
4-
export MY_RESOURCE_GROUP_NAME="myVMResourceGroup$RANDOM_ID"
4+
export MY_RESOURCE_GROUP_NAME="appsec-$RANDOM_ID-rg"
55
export REGION=westeurope
6-
export MY_VM_NAME="myVM$RANDOM_ID"
6+
export MY_VM_NAME="appsec-$RANDOM_ID"
77
export MY_USERNAME=azureuser
88
export MY_VM_IMAGE="Canonical:0001-com-ubuntu-minimal-jammy:minimal-22_04-lts-gen2:latest"
99

1010
# create resource group
11-
az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION
11+
echo "Creating resource group $MY_RESOURCE_GROUP_NAME in $REGION"
12+
RGRESP=$(az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION -o json)
13+
if [ $? -ne 0 ]; then
14+
echo -e "\033[31m Failed to create resource group $MY_RESOURCE_GROUP_NAME"
15+
exit 1
16+
fi
17+
RGSTATUS=$(echo $RGRESP | jq -r '.properties.provisioningState')
18+
echo "Resource group status: $RGSTATUS"
19+
echo
1220

1321
# get cloud-init.txt
1422
curl -o cloud-init.txt https://raw.githubusercontent.com/mkol5222/appsec-chart/main/cloud-init.yml
1523

1624
# create VM
1725
# https://learn.microsoft.com/en-us/azure/virtual-machines/linux/quick-create-cli
1826

19-
az vm create \
27+
echo "Creating VM $MY_VM_NAME in $MY_RESOURCE_GROUP_NAME"
28+
RESPVM=$(az vm create \
2029
--resource-group $MY_RESOURCE_GROUP_NAME \
2130
--name $MY_VM_NAME \
2231
--image $MY_VM_IMAGE \
@@ -25,17 +34,40 @@ az vm create \
2534
--custom-data cloud-init.txt \
2635
--assign-identity \
2736
--size Standard_DS2_v2 \
28-
--public-ip-sku Standard
37+
--public-ip-sku Standard )
38+
39+
if [ $? -ne 0 ]; then
40+
echo -e "\033[31m Failed to create VM $MY_VM_NAME"
41+
exit 1
42+
fi
43+
44+
VMSTATUS=$(echo $RESPVM | jq -r '.powerState')
45+
echo "VM status: $VMSTATUS"
46+
echo
2947

30-
az vm open-port -g $MY_RESOURCE_GROUP_NAME -n $MY_VM_NAME --port 22,80,443
48+
echo "Opening ports 22, 80, 443"
49+
RESPPORTS=$(az vm open-port -g $MY_RESOURCE_GROUP_NAME -n $MY_VM_NAME --port 22,80,443)
50+
if [ $? -ne 0 ]; then
51+
echo -e "\033[31m Failed to open ports"
52+
exit 1
53+
fi
54+
echo "Ports opened"
3155

32-
az vm extension set \
56+
echo "Enabling AAD login for the VM"
57+
RESPAAD=$(az vm extension set \
3358
--publisher Microsoft.Azure.ActiveDirectory \
3459
--name AADSSHLoginForLinux \
3560
--resource-group $MY_RESOURCE_GROUP_NAME \
36-
--vm-name $MY_VM_NAME
61+
--vm-name $MY_VM_NAME )
62+
if [ $? -ne 0 ]; then
63+
echo -e "\033[31m Failed to enable AAD login"
64+
exit 1
65+
fi
66+
echo "AAD login enabled"
3767

68+
echo "Getting public IP address of the VM $MY_VM_NAME"
3869
export IP_ADDRESS=$(az vm show --show-details --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --query publicIps --output tsv)
70+
echo "Public IP address: $IP_ADDRESS"
3971

4072
alias sshvm="ssh -o StrictHostKeyChecking=no $MY_USERNAME@$IP_ADDRESS"
4173

@@ -49,6 +81,6 @@ echo "az ssh vm -n $MY_VM_NAME -g $MY_RESOURCE_GROUP_NAME --local-user azureuser
4981
chmod +x "sshvm-$RANDOM_ID"
5082

5183
echo
52-
echo "VM created. You can now connect to it using 'sshvm' command"
84+
echo -e "\033[32m SUCCESS: VM created. You can now connect to it using 'sshvm' command"
5385
echo "To destroy the VM, run 'destroyvm-$RANDOM_ID'"
5486
echo

0 commit comments

Comments
 (0)