Last active
December 28, 2015 19:59
-
-
Save richorama/7553736 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
# More information on Python SDK for Windows Azure | |
# http://www.windowsazure.com/en-us/develop/python/how-to-guides/service-management/ | |
from azure import * | |
from azure.servicemanagement import * | |
import time | |
import base64 | |
subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' | |
certificate_path = 'mycert.pem' | |
name = "NameOfYourCloudService" | |
description = 'created by python' | |
# LOCATIONS ('East Asia','Southeast Asia','North Europe','West Europe','East US','North Central US','South Central US','West US') | |
location = 'West Europe' | |
username = 'azureuser' | |
password = 'Password!' | |
image_name = 'b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-13_10-amd64-server-20131113-en-us-30GB' | |
# instructions on creating certificate http://www.windowsazure.com/en-us/manage/linux/how-to-guides/ssh-into-linux/ | |
fingerprint = 'FingerprintOfYourSshCertificate' | |
sms = ServiceManagementService(subscription_id, certificate_path) | |
def wait_for_completion(promise): | |
if not promise: return | |
while True: | |
time.sleep(5) | |
operation_result = sms.get_operation_status(promise.request_id) | |
print(' ' + operation_result.status) | |
if (operation_result.status == "Succeeded"): | |
return | |
print "creating an affinity group" | |
sms.create_affinity_group(name, name, location, description) | |
print "creating a storage account" | |
result = sms.create_storage_account(name, description, name, affinity_group=name)# | |
wait_for_completion(result) | |
print "creating a hosted service" | |
result = sms.create_hosted_service(service_name = name, label = name, description=description, affinity_group=name) | |
wait_for_completion(result) | |
print "adding certificate" | |
with open('sshcert.cer', 'r') as content_file: | |
certificate = base64.b64encode(content_file.read()) | |
result = sms.add_service_certificate(service_name = name, data = certificate, certificate_format = "pfx", password = "") | |
wait_for_completion(result) | |
print "creating machine" | |
linux_config = LinuxConfigurationSet(name, username, password, True) | |
linux_config.ssh.public_keys = [PublicKey(fingerprint = fingerprint, path = '/home/' + username + '/.ssh/authorized_keys')] | |
os_hd = OSVirtualHardDisk(image_name, 'http://' + name + '.blob.core.windows.net/vhd/' + name + '.vhd') | |
network_configuration = ConfigurationSet() | |
network_configuration.configuration_set_type = 'NetworkConfiguration' | |
network_configuration.input_endpoints = list() | |
input_endpoint = ConfigurationSetInputEndpoint(name="ssh", protocol=u'tcp', port=22 , local_port=22, load_balanced_endpoint_set_name = name) | |
network_configuration.input_endpoints.append(input_endpoint) | |
data_virtual_hard_disks = list() | |
vhd = DataVirtualHardDisk() | |
vhd.host_caching = u'None' | |
vhd.disk_label = name | |
vhd.lun = 0 | |
vhd.logical_disk_size_in_gb = 128 | |
vhd.media_link = 'http://' + name + '.blob.core.windows.net/vhd/' + name + "data" + '.vhd' | |
data_virtual_hard_disks.append(vhd) | |
# the first machine creates the deployment | |
result = sms.create_virtual_machine_deployment(service_name=name, | |
deployment_name=name, | |
deployment_slot='production', | |
label=name, | |
role_name=name, | |
system_config=linux_config, | |
os_virtual_hard_disk=os_hd, | |
network_config=network_configuration, | |
availability_set_name=name, | |
data_virtual_hard_disks=data_virtual_hard_disks, | |
role_size="Small") | |
wait_for_completion(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment