Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
#38
Browse files Browse the repository at this point in the history
  • Loading branch information
taivop committed Oct 6, 2016
1 parent 0c7bf25 commit 9be1740
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 61 deletions.
46 changes: 19 additions & 27 deletions scripts/py/deployer.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,41 @@
"""A deployer class to deploy a template on Azure"""
import os.path
import json
from haikunator import Haikunator
import logging
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import DeploymentMode
from azure.common.credentials import UserPassCredentials


class Deployer(object):
""" Initialize the deployer class with subscription, resource group and public key.

:raises IOError: If the public key path cannot be read (access or not exists)
:raises KeyError: If AZURE_CLIENT_ID, AZURE_CLIENT_SECRET or AZURE_TENANT_ID env
variables or not defined
"""
name_generator = Haikunator()

def __init__(self, user_email, user_password, subscription_id, resource_group, template_path, pub_ssh_key_path):
def __init__(self, user_email, user_password, subscription_id, resource_group, template_path, parameters):
self.resource_group = resource_group
self.template_path = template_path
self.dns_label_prefix = self.name_generator.haikunate()

pub_ssh_key_path = os.path.expanduser(pub_ssh_key_path)
# Will raise if file not exists or not enough permissions
with open(pub_ssh_key_path, 'r') as pub_ssh_file_fd:
self.pub_ssh_key = pub_ssh_file_fd.read()
self.parameters = parameters

self.credentials = UserPassCredentials(user_email, user_password)
self.client = ResourceManagementClient(self.credentials, subscription_id)

LOG_FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)

def deploy(self):
"""Deploy the template to a resource group."""

logging.info("Initializing Deployer class with resource group '{}' and template at '{}'."
.format(self.resource_group, self.template_path))
logging.info("Parameters: " + str(self.parameters))

self.client.resource_groups.create_or_update(
self.resource_group,
{
'location': 'westeurope'
}
self.resource_group, {'location': 'westeurope'}
)

with open(self.template_path, 'r') as template_file_fd:
template = json.load(template_file_fd)

parameters = {
'sshKeyData': self.pub_ssh_key,
'vmName': 'azure-deployment-sample-vm',
'dnsLabelPrefix': self.dns_label_prefix
}
parameters = {k: {'value': v} for k, v in parameters.items()}
parameters = {k: {'value': v} for k, v in self.parameters.items()}

# TODO review deployment mode -- what do I want?
deployment_properties = {
'mode': DeploymentMode.incremental,
'template': template,
Expand All @@ -56,11 +44,15 @@ def deploy(self):

deployment_async_operation = self.client.deployments.create_or_update(
self.resource_group,
'azure-sample',
'azure-sample', # TODO what is this parameter?
deployment_properties
)
deployment_async_operation.wait()

logging.info("Deployment complete, resource group {} created.".format(self.resource_group))

def destroy(self):
"""Destroy the given resource group"""
logging.info("Destroying resource group {}...".format(self.resource_group))
self.client.resource_groups.delete(self.resource_group)
logging.info("Destroyed.")
34 changes: 0 additions & 34 deletions scripts/py/pyazure.py

This file was deleted.

39 changes: 39 additions & 0 deletions scripts/py/test_deployment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os.path
from haikunator import Haikunator
from deployer import Deployer

# region ---- Credentials ----
my_subscription_id = "0003c64e-455e-4794-8665-a59c04a8961b"
my_email = "[email protected]"
my_password = "4D0$1QcK5:Nsn:jd!'1j4Uw'j*"
# endregion

# region ---- Parameters ----
my_resource_group = 'azure-python-deployment-sample-2'
my_pub_ssh_key_path = os.path.expanduser('~/.ssh/id_rsa.pub')
template_path = "azure-templates/test_template.json"
dns_label_prefix = Haikunator().haikunate()

pub_ssh_key_path = os.path.expanduser(my_pub_ssh_key_path)
with open(pub_ssh_key_path, 'r') as pub_ssh_file_fd:
pub_ssh_key = pub_ssh_file_fd.read()

parameters = {
'sshKeyData': pub_ssh_key,
'vmName': 'azure-deployment-sample-vm',
'dnsLabelPrefix': dns_label_prefix
}
# endregion

# region ---- Deployment ----
# Initialize the deployer class
deployer = Deployer(my_email, my_password, my_subscription_id, my_resource_group, template_path, parameters)

# Deploy the template
my_deployment = deployer.deploy()

print("Done deploying!\n\nConnect via: `ssh azureSample@{}.westeurope.cloudapp.azure.com`".format(dns_label_prefix))
# endregion

# Destroy the resource group which contains the deployment
deployer.destroy()

0 comments on commit 9be1740

Please sign in to comment.