Last active
July 25, 2018 18:22
-
-
Save MingweiSamuel/46b952a4f9475be80d9affc4ef5e8a5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from azure.common.exceptions import CloudError | |
from azure.common.client_factory import get_client_from_cli_profile | |
from azure.mgmt.resource import ResourceManagementClient | |
from azure.mgmt.compute import ComputeManagementClient | |
from azure.mgmt.network import NetworkManagementClient | |
SSH_USERNAME = 'ubuntu' | |
LOCATION = 'westus' | |
RESOURCE_GROUP_NAME = 'paz' | |
VNET_NAME = 'paz-vnet' | |
SUBNET_NAME = 'paz-subnet' | |
# Public key generated from private key file: ssh-keygen -y -f ./common_dev_key.pem | |
PUBLIC_KEY = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDI4y4oKtNfBweaC5Pn3hd1Vp1XJ1RMYVu884mtDKoSjkG26QiberXh5xfeehi6VubHJ7DYg/te1YdwCHIM' + \ | |
'Jmo/ZyQJhbljc95GJQcRhXYYQBXEExraCip1Z1HhSU1+W9/pJJmQxekTWkCcg8jQ6lluRBF2F2TDHB1VuHR3pgO9hIixLnBKYkkrPNFZkh0t5c47csWMqop/j9+Yx4xbI' + \ | |
'cA9+yKXMONsdZiOd5RDiRfW52aBJ2XNwKhtWrOyM5Wj2gtoz7SIwVNMKehr8iz7Q4LzD8UQCBOLS8czY63mj7CXeCsGV6QVwRnRcOGIytVSooLJtaG0ftqoDwmW3G8SQfZT' | |
resource_group_client = get_client_from_cli_profile(ResourceManagementClient) | |
network_client = get_client_from_cli_profile(NetworkManagementClient) | |
compute_client = get_client_from_cli_profile(ComputeManagementClient) | |
name = 'vm-swap-test19' | |
instance_type = 'Standard_B1s' | |
# Create ip address. | |
# https://docs.microsoft.com/en-us/python/api/azure.mgmt.network.v2017_11_01.operations.publicipaddressesoperations?view=azure-python#create-or-update | |
print 'Creating ip address for instance.' | |
ip_address_creation_result = network_client.public_ip_addresses.create_or_update( | |
RESOURCE_GROUP_NAME, | |
name + '-ip-address', | |
parameters={ | |
'location': LOCATION, | |
'public_ip_allocation_method': 'Static' | |
} | |
) | |
ip_address = ip_address_creation_result.result() | |
# Get subnet. | |
subnet_info = network_client.subnets.get( | |
RESOURCE_GROUP_NAME, | |
VNET_NAME, | |
SUBNET_NAME | |
) | |
# Create nic. | |
# (all azure vms must have an explicit nic) | |
print 'Creating default nic for instance.' | |
nic_creation_result = network_client.network_interfaces.create_or_update( | |
RESOURCE_GROUP_NAME, | |
name + '-nic-0', | |
parameters={ | |
'location': LOCATION, | |
'ip_configurations': [{ | |
'name': name + '-ip-config', | |
'public_ip_address': ip_address, | |
'subnet': { | |
'id': subnet_info.id | |
} | |
}], | |
'primary': True | |
} | |
) | |
nic = nic_creation_result.result() | |
# Create vm. :D | |
print 'Creating vm "{}".'.format(name) | |
# Tell Azure to create VM and wait for result. | |
# https://docs.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2017_12_01.operations.virtualmachinesoperations?view=azure-python#create-or-update | |
vm_creation_result = compute_client.virtual_machines.create_or_update( | |
RESOURCE_GROUP_NAME, | |
name, | |
parameters={ | |
'location': LOCATION, | |
'os_profile': { | |
'computer_name': name, | |
'admin_username': SSH_USERNAME, | |
'admin_password': 'onodera_best_girl', | |
'linux_configuration': { | |
'disable_password_authentication': True, | |
'ssh': { | |
'public_keys': [ | |
{ | |
'path': '/home/' + SSH_USERNAME + '/.ssh/authorized_keys', | |
'key_data': PUBLIC_KEY | |
} | |
] | |
} | |
} | |
}, | |
'hardware_profile': { | |
'vm_size': instance_type | |
}, | |
'storage_profile': { | |
'image_reference': { | |
'publisher': 'Canonical', | |
'offer': 'UbuntuServer', | |
'sku': '14.04.5-LTS', | |
'version': 'latest' | |
}, | |
'data_disks': [ | |
{ | |
'lun': 42, | |
'name': name + '-swap-os-disk', | |
'create_option': 'Empty', | |
'disk_size_gb': 64, | |
'managed_disk': { | |
'storage_account_type': 'Premium_LRS' | |
} | |
} | |
] | |
}, | |
'network_profile': { | |
'network_interfaces': [{ | |
'id': nic.id, | |
'primary': True | |
}] | |
} | |
} | |
) | |
vm_info = vm_creation_result.result() | |
print 'Created VM.' | |
vm_info = compute_client.virtual_machines.get( | |
RESOURCE_GROUP_NAME, | |
name | |
) | |
print 'Detach swap disk.' | |
swap_os_disk = vm_info.storage_profile.data_disks.pop() | |
vm_update_result = compute_client.virtual_machines.create_or_update( | |
RESOURCE_GROUP_NAME, | |
name, | |
parameters=vm_info | |
) | |
vm_update_result.wait() | |
print 'Detached swap disk.' | |
# print 'Creating swap disk.' | |
# # https://docs.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2018_04_01.operations.disksoperations?view=azure-python#create-or-update | |
# disk_create_result = compute_client.disks.create_or_update( | |
# RESOURCE_GROUP_NAME, | |
# name + '-swap-os-disk', | |
# { | |
# 'location': LOCATION, | |
# 'disk_size_gb': 100, | |
# 'creation_data': { | |
# 'create_option': 'empty' | |
# }, | |
# 'sku': { | |
# 'name': 'Premium_LRS' | |
# } | |
# } | |
# ) | |
# new_disk = disk_create_result.result() | |
print 'Attaching swap disk as os disk' | |
print vm_info.storage_profile | |
# print vm_info.storage_profile.os_disk.create_option | |
# vm_info.storage_profile.image_reference = None | |
vm_info.storage_profile.os_disk = { | |
'create_option': vm_info.storage_profile.os_disk.create_option, #'attach', | |
'managed_disk': { | |
# 'id': new_disk.id, | |
'id': swap_os_disk.managed_disk.id, | |
'storage_account_type': 'Premium_LRS' | |
} | |
} | |
# vm_info.storage_profile.os_disk.managed_disk = { | |
# 'id': swap_os_disk.managed_disk.id, | |
# 'storage_account_type': 'Premium_LRS' | |
# } | |
vm_update_result = compute_client.virtual_machines.create_or_update( | |
RESOURCE_GROUP_NAME, | |
name, | |
parameters=vm_info) | |
vm_update_result.wait() |
Author
MingweiSamuel
commented
Jul 25, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment