Feature 10982: Public Cloud Robot Suite
[osm/tests.git] / cloud-scripts / create-osm-vm.sh
1 #!/usr/bin/env bash
2 #######################################################################################
3 # Copyright ETSI Contributors and Others.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #######################################################################################
18
19 # Create a new VM for installing OSM and its NSG on Azure. SSH key pair files ~/.ssh/id_rsa
20 # and ~/.ssh/id_rsa.pub must exist. The NEW_OSM_IP variable is set with IP address.
21 # To do this it reads the following environment variables:
22 # - OSM_IMAGE_NAME: name of the new VM
23 # - RESOURCE_GROUP: name of the resource-group where the VM will be created
24 # - VNET_NAME: name of the virtual network when creating a new one or referencing an existing one
25 # - VIM_MGMT_NET: name or ID of the subnet to which the VM will be connected
26 # - SOURCE_IMAGE_NAME: name of operating system image used (e.g. "Canonical:0001-com-ubuntu-server-jammy:22_04-lts:latest")
27 # - FLAVOR_NAME: the VM size to be created (e.g. "Standard_D4as_v4")
28 # - PRIORITY: "Low", "Regular" or Spot"
29 function create_azure_vm {
30 # Create the VM in resource-group
31 set -eux
32 az vm create --resource-group "${RESOURCE_GROUP}" --name "${OSM_IMAGE_NAME}" --image "${SOURCE_IMAGE_NAME}" --size "${FLAVOR_NAME}" --vnet-name "${VNET_NAME}" --subnet "${VIM_MGMT_NET}" --public-ip-address "" --admin-username ubuntu --priority "${PRIORITY}" --os-disk-size-gb 64
33 export NEW_OSM_IP=$(az vm show -d -g "${RESOURCE_GROUP}" -n "${OSM_IMAGE_NAME}" --query privateIps | tr -d \")
34
35 # Add a security group rule
36 INTERFACE_ID=$(az vm show --resource-group ${RESOURCE_GROUP} --name ${OSM_IMAGE_NAME} --query networkProfile.networkInterfaces[0].id)
37 INTERFACE_ID=${INTERFACE_ID:1:-1}
38 SECURITY_GROUP_ID=$(az network nic show --id ${INTERFACE_ID} --query networkSecurityGroup.id)
39 SECURITY_GROUP_ID=${SECURITY_GROUP_ID:1:-1}
40 SECURITY_GROUP_NAME=$(az resource show --ids ${SECURITY_GROUP_ID} --query name)
41 SECURITY_GROUP_NAME=${SECURITY_GROUP_NAME:1:-1}
42 az network nsg rule create -n osm --nsg-name ${SECURITY_GROUP_NAME} --priority 2000 -g ${RESOURCE_GROUP} --description "NBI and Prometheus ports" --protocol TCP --destination-port-ranges 9999 9091
43 }
44
45 if [ -n "${1:-}" ]; then # If there is an argument, it must be the hostname
46 export OSM_IMAGE_NAME=$1
47 else
48 export OSM_IMAGE_NAME=osmtest$(date '+%Y%m%d%H%M')
49 fi
50
51 # Branch by CLOUD_TYPE value ("azure", "gcp")
52 if [ "${CLOUD_TYPE}" == "azure" ]; then
53 # Azure VIM
54 create_azure_vm
55 elif [ "${CLOUD_TYPE}" == "gcp" ]; then
56 # Google Cloud VIM
57 export OSM_IMAGE_NAME=$(echo $OSM_IMAGE_NAME | sed "s/_/-/g")
58 echo "CLOUD_TYPE '${CLOUD_TYPE}' still not supported"
59 exit
60 else
61 echo "Invalid cloud type: ${CLOUD_TYPE}"
62 fi
63
64 # Log new environment variables
65 mkdir -p ${ROBOT_REPORT_FOLDER}
66 cat <<EOF > ${ROBOT_REPORT_FOLDER}/osm_environment.rc
67 export CLOUD_TYPE="${CLOUD_TYPE}"
68 export OSM_HOSTNAME="${NEW_OSM_IP}"
69 export OSM_IMAGE_NAME="${OSM_IMAGE_NAME}"
70 EOF
71 echo File with new environment was created at ${ROBOT_REPORT_FOLDER}/osm_environment.rc