Skip to content

Commit

Permalink
dependson like armviz
Browse files Browse the repository at this point in the history
  • Loading branch information
lee212 committed Oct 15, 2016
1 parent dbc1a81 commit 13e4682
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 24 deletions.
89 changes: 67 additions & 22 deletions docs/azure_quickstart_templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ deploying these templates in Python with its (directory) name.

::

>> from simpleazure.azure_quickstart_templates import AzureQuickStart as aqst
>> template = aqst.get_template('101-vm-sshkey')
>> template.metadata()
>>> from simpleazure.azure_quickstart_templates import AzureQuickStart as aqst
>>> template = aqst.get_template('101-vm-sshkey')
>>> template.metadata()
dateUpdated 2015-06-05
description This template allows you to create a Virtual M...
githubUsername squillace
Expand All @@ -27,8 +27,8 @@ templates are fetched and ready to deploy. For example:

::

>> templates = aqst.get_templates()
>> templates.ten()
>>> templates = aqst.get_templates()
>>> templates.ten()
100-blank-template Blank Template
101-acs-dcos Azure Container Service - DC/OS
101-acs-mesos Azure Container Service - DC/OS
Expand All @@ -40,7 +40,7 @@ templates are fetched and ready to deploy. For example:
101-application-gateway-public-ip-ssl-offload Create an Application Gateway with Public IP
101-automation-runbook-getvms Create Azure Automation Runbook to retrieve Az...

Details for a template are also available like metadata (from metadata.json):
Details for a template are also available, for example, metadata (from metadata.json) is displayed:

::
>>> templates['101-acs-dcos'].metadata()
Expand All @@ -52,14 +52,14 @@ Details for a template are also available like metadata (from metadata.json):



>> from simpleazure import arm
>> from simpleazure.azure_quickstart_templates import AzureQuickStart as aqst
>> arm = arm.ARM() # Azure Resource Manager object
>> aqst = aqst.AzureQuickStartTemplate()
>> vm_sshkey_template = aqst.get_template('101-vm-sshkey')
>> vm_sshkey_template.requirements()
>>> from simpleazure import arm
>>> from simpleazure.azure_quickstart_templates import AzureQuickStart as aqst
>>> arm = arm.ARM() # Azure Resource Manager object
>>> aqst = aqst.AzureQuickStartTemplate()
>>> vm_sshkey_template = aqst.get_template('101-vm-sshkey')
>>> vm_sshkey_template.requirements()
{u'sshKeyData': u'GEN-SSH-PUB-KEY'}
>> arm.set_parameter("sshKeyData", "ssh-rsa AAAB... hrlee@quickstart")
>>> arm.set_parameter("sshKeyData", "ssh-rsa AAAB... hrlee@quickstart")

.. comment::
- statistics for deploying time, number of resources, price tags, options, limitations (versions, os distribution)
Expand All @@ -74,16 +74,21 @@ Details for a template are also available like metadata (from metadata.json):
Searching Template
-------------------------------------------------------------------------------

Simple Azure supports template search with a keyword from the Azure QuickStart
Templates. Let's find templates that use 'rhel' (Red Hat Enterprise Linux) in a
description. It found 13 templates and the first ten items are like:
Simple Azure supports a template search from the Azure QuickStart Templates
with a keyword. Let's find templates that use 'rhel' (Red Hat Enterprise Linux)
in a description.

::

>> rhel_templates = aqst.search("rhel")
>> len(rhel_templates)
>>> rhel_templates = aqst.search("rhel")

It found 13 templates and the first ten items are like:

::
>>> len(rhel_templates)
13
>> rhel_templates.ten()

>>> rhel_templates.ten()
101-vm-full-disk-encrypted-rhel Red Hat Enterprise Linux 7.2 VM (Fully Encrypted)
101-vm-simple-rhel Red Hat Enterprise Linux VM (RHEL 7.2 or RHEL ...
201-encrypt-running-linux-vm Enable encryption on a running Linux VM.
Expand All @@ -95,18 +100,40 @@ description. It found 13 templates and the first ten items are like:
sap-2-tier-marketplace-image 2-tier configuration for use with SAP NetWeaver
vsts-tomcat-redhat-vm Red Hat Tomcat server for use with Team Servic...

See metadata of the second templates ``101-vm-simple-rhel``:
Template Details
-------------------------------------------------------------------------------

Simple Azure provides Template() object functions to quickly review template
details such as required parameters, template descriptions and resource
information.

Metadata
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

See metadata of the template ``101-vm-simple-rhel`` from the search results
above:

::

>> rhel_templates['101-vm-simple-rhel'].metadata()
>>> rhel_templates['101-vm-simple-rhel'].metadata()
dateUpdated 2016-02-23
description This template will deploy a Red Hat Enterprise...
githubUsername BorisB2015
itemDisplayName Red Hat Enterprise Linux VM (RHEL 7.2 or RHEL ...
summary This template will deploy RedHat (RHEL) VM, us...

It requires these parameters:
>>> rhel_templates['101-vm-simple-rhel'].metadata().description
u'This template will deploy a Red Hat Enterprise Linux VM (RHEL 7.2 or
RHEL 6.7), using the Pay-As-You-Go RHEL VM image for the selected
version on Standard D1 VM in the location of your chosen resource group
with an additional 100 GiB data disk attached to the VM. Additional
charges apply to this image - consult Azure VM Pricing page for
details.'

Parameters
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

We may want to know what parameters are required to deploy for this template:

::
Expand All @@ -115,6 +142,24 @@ It requires these parameters:
adminUsername
vmName

These parameters need to be set before deploying the template.

Resources
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It seems that ``101-vm-simple-rhel`` deploys a Virtual Machine with Standard D1.
For more detail about resources are:

::

>>> rhel['101-vm-simple-rhel'].resources()
Microsoft.Compute/virtualMachines {u'name': u'[parameters('vmName')]', u'apiVers...
Microsoft.Network/networkInterfaces {u'name': u'[variables('nicName')]', u'apiVers...
Microsoft.Network/publicIPAddresses {u'properties': {u'publicIPAllocationMethod': ...
Microsoft.Network/virtualNetworks {u'properties': {u'subnets': [{u'name': u"[var...
Microsoft.Storage/storageAccounts {u'properties': {u'accountType': u'[variables(...

There are five services are used to deploy a RHEL virtual machine.

Simple Azure Features
-------------------------------------------------------------------------------
Expand Down
92 changes: 90 additions & 2 deletions simpleazure/template/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

#TODO: replace pandas data series. data frame with simple functions
#

from pprint import pprint
import pandas as pd
from collections import OrderedDict
from collections import OrderedDict, defaultdict
from itertools import islice, izip_longest

class Templates(OrderedDict):
Expand Down Expand Up @@ -61,10 +63,18 @@ def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)

def tree(): return defaultdict(tree)
def dicts(t): return {k: dicts(t[k]) for k in t}
def depth(d, level=1):
if not isinstance(d, dict) or not d:
return level
return max(depth(d[k], level + 1) for k in d)

class Template(dict):

azuredeploy = parameters = metadata = nested = scripts = etc = None
special_placeholders = [ 'GEN-UNIQUE', 'GEN-UNIQUE-', 'GEN-SSH-PUB-KEY', 'GEN-PASSWORD' ]
dependency = None

def metadata(self):
return pd.Series(self['metadata'])
Expand All @@ -87,12 +97,90 @@ def resources(self):
new = {}
for f in self['azuredeploy']['resources']:
try:
new[f['type']] = f['dependsOn']
new[f['type']] = f
except:
new[f['type']] = ""

return pd.Series(new)

def dependson(self):
new = tree()
for resource in self['azuredeploy']['resources']:
try:
if 'dependsOn' in resource:
for depend in resource['dependsOn']:
depend_typename = self._get_typename(depend)
val = self._lookup(depend_typename)
new[resource['type']][depend_typename] = val
except:
new[resource['type']] = self._run_template_functions(resource['name'])

depth_cnt = 0
pick_one = None
for k,v in new.iteritems():
depth_k = depth(v)
if depth_k > depth_cnt:
pick_one = k
depth_cnt = depth_k

temp = tree()
temp[pick_one] = new[pick_one]
self.dependency = dicts(temp)
return dicts(temp)

def dependson_print(self):
pprint(self.dependency)

def _run_template_functions(self, value):
concat = self._concat
variables = self._variables
try:
return eval(value)[0]
except Exception as e:
return value

def _get_typename(self, value):
try:
result = self._run_template_functions(value).split("/")
return "/".join(result[0:2])
except Exception as e:
return value

def _lookup(self, typename):
new = tree()
try:
for resource in self['azuredeploy']['resources']:
if typename == resource['type']:
if 'dependsOn' in resource:
for depend in resource['dependsOn']:
depend_typename = self._get_typename(depend)
val = self._lookup(depend_typename)
new[depend_typename] = val
else:
val = self._run_template_functions(resource['name'])
new[val] = {}
return new
except:
return new

# Azure Template function
def _concat(self, *args):
strings = ""
for arg in args:
strings += arg
return strings

# Azure Template function
def _variables(self, name):
try:
var = self['azuredeploy']['variables'][name]
except:
var = name
try:
return eval(var)
except:
return var

def requirements(self):
required = {}

Expand Down

0 comments on commit 13e4682

Please sign in to comment.