fix vm creation issue (#14691)

This commit is contained in:
Alan Ren
2021-03-12 10:53:02 -08:00
committed by GitHub
parent b670ffc7e6
commit f83925ce38
4 changed files with 678 additions and 118 deletions

View File

@@ -2,7 +2,8 @@
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
@@ -162,21 +163,15 @@
"cell_type": "code",
"source": [
"suffix = time.strftime(\"%y%m%d%H%M%S\", time.localtime())\n",
"network_security_group = f'nsg{suffix}'\n",
"public_ip_address_name = f'ip{suffix}'\n",
"iot_hub_name = f'hub{suffix}'\n",
"iot_hub_sku = 'S1'\n",
"iot_hub_units = 4\n",
"iot_device_id = f'vm{suffix}'\n",
"vm_size = 'Standard_DS3_v2'\n",
"vnet_name = f'net{suffix}'\n",
"subnet_name = f'subnet{suffix}'\n",
"subnet_address_prefix = '10.0.0.0/24'\n",
"vnet_address_prefix = '10.0.0.0/16'\n",
"azure_storage_account = f'sa{suffix}'\n",
"storage_account_container = 'sqldatabasepackage'\n",
"sql_lcid = '1033'\n",
"sql_collation = 'SQL_Latin1_General_CP1_CI_AS'"
"sql_collation = 'SQL_Latin1_General_CP1_CI_AS'\n",
"vm_size = 'Standard_DS1_v2'"
],
"metadata": {
"azdata_cell_guid": "19ebeaf4-94c9-4d2b-bd9f-e3c6bf7f2dda",
@@ -265,6 +260,34 @@
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### Create IoT hub"
],
"metadata": {
"azdata_cell_guid": "e37a04c3-515d-4cb7-99b2-f8bc6167510e"
}
},
{
"cell_type": "code",
"source": [
"hub_list = run_command(f'az iot hub list --resource-group {azure_resource_group}', returnObject=True)\n",
"hub_list = [hub for hub in hub_list if hub['name'] == iot_hub_name]\n",
"if len(hub_list) == 0:\n",
" run_command(f'az iot hub create --name {iot_hub_name} --resource-group {azure_resource_group} --location {azure_location} --sku {iot_hub_sku} --unit {iot_hub_units}')\n",
"else:\n",
" print(f'IoT hub \\\"{iot_hub_name}\\\" already exists')"
],
"metadata": {
"azdata_cell_guid": "f9f5e4ec-82a5-45df-a408-ddb0fb21847c",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
@@ -319,25 +342,26 @@
{
"cell_type": "markdown",
"source": [
"### Create network security group"
"### Add the Edge device to the IoT hub"
],
"metadata": {
"azdata_cell_guid": "b308771b-138a-40ce-a9d3-1d15094d537b"
"azdata_cell_guid": "fbc5f4ac-dfe0-4543-ace1-49b796251910"
}
},
{
"cell_type": "code",
"source": [
"nsg_list = run_command(f'az network nsg list --resource-group {azure_resource_group}', returnObject=True)\n",
"nsg_list = [nsg for nsg in nsg_list if nsg['name'] == network_security_group]\n",
"if len(nsg_list) == 0:\n",
" run_command(f'az network nsg create --name {network_security_group} --resource-group {azure_resource_group} --location {azure_location}')\n",
" run_command(f'az network nsg rule create --name \\\"SQL\\\" --nsg-name {network_security_group} --priority 100 --resource-group {azure_resource_group} --access Allow --description \\\"Allow SQL\\\" --destination-address-prefixes \\\"*\\\" --destination-port-ranges {sql_port} --direction Inbound --source-address-prefixes Internet --protocol Tcp')\n",
"device_list = run_command(f'az iot hub device-identity list --edge-enabled true --hub-name {iot_hub_name} --resource-group {azure_resource_group}', returnObject=True)\n",
"device_list = [device for device in device_list if device['deviceId'] == iot_device_id]\n",
"if len(device_list) == 0:\n",
" run_command(f'az iot hub device-identity create --device-id {iot_device_id} --hub-name {iot_hub_name} --resource-group {azure_resource_group} --edge-enabled true')\n",
"else:\n",
" print(f'Network security group \\\"{network_security_group}\\\" already exists.')"
" print(f'Edge device \\\"{iot_device_id}\\\" already exists.')\n",
"connection_string = run_command(f'az iot hub device-identity show-connection-string --device-id {iot_device_id} --hub-name {iot_hub_name} --resource-group {azure_resource_group}', returnObject=True)\n",
"connection_string = connection_string['connectionString']"
],
"metadata": {
"azdata_cell_guid": "99cbb95c-b109-4b2e-909b-ff71a62754fb",
"azdata_cell_guid": "c183c3e3-8699-4f29-993b-07bf848336e3",
"tags": [
"hide_input"
]
@@ -357,103 +381,25 @@
{
"cell_type": "code",
"source": [
"vm_list = run_command(f'az vm list --resource-group {azure_resource_group}', returnObject=True)\n",
"vm_list = [vm for vm in vm_list if vm['name'] == iot_device_id]\n",
"if len(vm_list) == 0:\n",
" vm_image = run_command(f'az vm image list --all --location {azure_location} --offer iot_edge_vm_ubuntu --publisher microsoft_iot_edge --sku ubuntu_1604_edgeruntimeonly', returnObject=True)\n",
" image_urn = vm_image[0]['urn']\n",
" run_command(f'az vm image terms accept --urn {image_urn}')\n",
" vm_password_placeholder = '<admin_password>'\n",
" create_vm_command_template = f'az vm create --name {iot_device_id} --resource-group {azure_resource_group} --admin-username {vm_admin} --admin-password {vm_password_placeholder} --authentication-type password --image {image_urn} --location {azure_location} --nsg {network_security_group} --public-ip-address \\\"{public_ip_address_name}\\\" --public-ip-address-allocation static --public-ip-sku Standard --size {vm_size} --subnet {subnet_name} --subnet-address-prefix \\\"{subnet_address_prefix}\\\" --vnet-name {vnet_name} --vnet-address-prefix \\\"{vnet_address_prefix}\\\"'\n",
" run_command(create_vm_command_template.replace(vm_password_placeholder, vm_password), displayCommand=create_vm_command_template.replace(vm_password_placeholder, '******'))\n",
"else:\n",
" print(f'VM \\\"{iot_device_id}\\\" already exists, skipping the vm creation.')\n",
"ip_address = run_command(f'az vm show -d -g {azure_resource_group} -n {iot_device_id} --query publicIps', returnObject=True)"
"iot_deploy_result = run_command((f\"az deployment group create \"\r\n",
"f\"--resource-group {azure_resource_group} \"\r\n",
"f\"--template-uri \\\"https://aka.ms/iotedge-vm-deploy\\\" \"\r\n",
"f\"--parameters vmSize={vm_size} \"\r\n",
"f\"--parameters dnsLabelPrefix={iot_device_id} \"\r\n",
"f\"--parameters adminUsername={vm_admin} \"\r\n",
"f\"--parameters deviceConnectionString={connection_string} \"\r\n",
"f\"--parameters authenticationType=password \"\r\n",
"f\"--parameters adminPasswordOrKey=\\\"{vm_password}\\\"\"), returnObject=True)\r\n",
"vm_resource = [resource for resource in iot_deploy_result['properties']['dependencies'] if resource['resourceType'] == 'Microsoft.Compute/virtualMachines']\r\n",
"if len(vm_resource) != 1:\r\n",
" sys.exit('Failed to deploy the IoT Edge VM')\r\n",
"vm_name = vm_resource[0]['resourceName']\r\n",
"nsg_name = vm_name.replace('vm-','nsg-')\r\n",
"ip_address = run_command(f'az vm show -d -g {azure_resource_group} -n {vm_name} --query publicIps', returnObject=True)\r\n",
"run_command(f'az network nsg rule create --name \\\"SQL\\\" --nsg-name {nsg_name} --priority 100 --resource-group {azure_resource_group} --access Allow --description \\\"Allow SQL\\\" --destination-address-prefixes \\\"*\\\" --destination-port-ranges {sql_port} --direction Inbound --source-address-prefixes Internet --protocol Tcp')"
],
"metadata": {
"azdata_cell_guid": "157fc38f-cf2a-40c6-9c9e-88f45cc5c62f",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### Create IoT hub"
],
"metadata": {
"azdata_cell_guid": "e37a04c3-515d-4cb7-99b2-f8bc6167510e"
}
},
{
"cell_type": "code",
"source": [
"hub_list = run_command(f'az iot hub list --resource-group {azure_resource_group}', returnObject=True)\n",
"hub_list = [hub for hub in hub_list if hub['name'] == iot_hub_name]\n",
"if len(hub_list) == 0:\n",
" run_command(f'az iot hub create --name {iot_hub_name} --resource-group {azure_resource_group} --location {azure_location} --sku {iot_hub_sku} --unit {iot_hub_units}')\n",
"else:\n",
" print(f'IoT hub \\\"{iot_hub_name}\\\" already exists')"
],
"metadata": {
"azdata_cell_guid": "f9f5e4ec-82a5-45df-a408-ddb0fb21847c",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### Add the Edge device to the IoT hub"
],
"metadata": {
"azdata_cell_guid": "fbc5f4ac-dfe0-4543-ace1-49b796251910"
}
},
{
"cell_type": "code",
"source": [
"device_list = run_command(f'az iot hub device-identity list --edge-enabled true --hub-name {iot_hub_name} --resource-group {azure_resource_group}', returnObject=True)\n",
"device_list = [device for device in device_list if device['deviceId'] == iot_device_id]\n",
"if len(device_list) == 0:\n",
" run_command(f'az iot hub device-identity create --device-id {iot_device_id} --hub-name {iot_hub_name} --resource-group {azure_resource_group} --edge-enabled true')\n",
"else:\n",
" print(f'Edge device \\\"{iot_device_id}\\\" already exists.')"
],
"metadata": {
"azdata_cell_guid": "c183c3e3-8699-4f29-993b-07bf848336e3",
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"### Configure Edge on the device"
],
"metadata": {
"azdata_cell_guid": "069db017-9169-499a-839b-9cd73ea7d01e"
}
},
{
"cell_type": "code",
"source": [
"connection_string = run_command(f'az iot hub device-identity show-connection-string --device-id {iot_device_id} --hub-name {iot_hub_name} --resource-group {azure_resource_group}', returnObject=True)\n",
"connection_string = connection_string['connectionString']\n",
"script = f'/etc/iotedge/configedge.sh \\'{connection_string}\\''\n",
"run_command(f'az vm run-command invoke -g {azure_resource_group} -n {iot_device_id} --command-id RunShellScript --script \\\"{script}\\\"')"
],
"metadata": {
"azdata_cell_guid": "9ec1e31a-79aa-49f4-a0e5-16f8d7c2dd21",
"azdata_cell_guid": "c8590c65-b274-460d-9659-97e81d2fd3ea",
"tags": [
"hide_input"
]