2018-12-21 00:18:34 +03:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2018-12-26 23:49:43 +03:00
|
|
|
"""
|
2018-12-31 22:37:43 +03:00
|
|
|
Class used for testing template-based deployment of the Avere vFXT product.
|
|
|
|
|
|
|
|
Objects require the following environment variables at instantiation:
|
|
|
|
* AVERE_ADMIN_PW
|
|
|
|
* AVERE_CONTROLLER_PW
|
|
|
|
* AZURE_CLIENT_ID
|
|
|
|
* AZURE_CLIENT_SECRET
|
|
|
|
* AZURE_TENANT_ID
|
|
|
|
* AZURE_SUBSCRIPTION_ID
|
2018-12-26 23:49:43 +03:00
|
|
|
"""
|
|
|
|
|
2018-12-21 23:34:28 +03:00
|
|
|
import json
|
2019-01-05 06:43:16 +03:00
|
|
|
import logging
|
2018-12-21 00:18:34 +03:00
|
|
|
import os
|
2019-01-02 18:40:55 +03:00
|
|
|
from datetime import datetime
|
2018-12-31 22:37:43 +03:00
|
|
|
from pprint import pformat
|
|
|
|
from random import choice
|
2019-01-02 18:40:55 +03:00
|
|
|
from string import ascii_lowercase
|
2018-12-28 19:38:34 +03:00
|
|
|
|
2018-12-27 23:22:03 +03:00
|
|
|
from azure.common.credentials import ServicePrincipalCredentials
|
2019-01-05 06:43:16 +03:00
|
|
|
from azure.mgmt.network import NetworkManagementClient
|
2018-12-27 23:22:03 +03:00
|
|
|
from azure.mgmt.resource import ResourceManagementClient
|
2019-01-03 01:06:11 +03:00
|
|
|
from azure.mgmt.resource.resources.models import DeploymentMode, TemplateLink
|
2018-12-21 00:18:34 +03:00
|
|
|
|
2018-12-31 22:37:43 +03:00
|
|
|
|
|
|
|
class AvereTemplateDeploy:
|
|
|
|
def __init__(self, deploy_params={}, resource_group=None,
|
2019-01-05 06:43:16 +03:00
|
|
|
location='eastus2'):
|
2018-12-31 22:37:43 +03:00
|
|
|
"""Initialize, authenticate to Azure, generate deploy params."""
|
2019-01-03 01:06:11 +03:00
|
|
|
self.template_link = TemplateLink(
|
|
|
|
uri='https://raw.githubusercontent.com/' +
|
|
|
|
'Azure/Avere/master/src/vfxt/azuredeploy-auto.json')
|
2018-12-31 22:37:43 +03:00
|
|
|
|
|
|
|
self.deploy_params = deploy_params
|
|
|
|
self.resource_group = self.deploy_params.pop('resourceGroup',
|
|
|
|
resource_group)
|
|
|
|
self.location = self.deploy_params.pop('location', location)
|
2019-01-04 22:11:39 +03:00
|
|
|
self.ssh_pub_key_path = os.path.expanduser('~/.ssh/id_rsa.pub')
|
2018-12-31 22:37:43 +03:00
|
|
|
|
2019-01-05 06:43:16 +03:00
|
|
|
logging.debug('> Loading Azure credentials')
|
|
|
|
sp_creds = ServicePrincipalCredentials(
|
|
|
|
client_id=os.environ['AZURE_CLIENT_ID'],
|
|
|
|
secret=os.environ['AZURE_CLIENT_SECRET'],
|
|
|
|
tenant=os.environ['AZURE_TENANT_ID']
|
|
|
|
)
|
2018-12-31 22:37:43 +03:00
|
|
|
self.rm_client = ResourceManagementClient(
|
2019-01-05 06:43:16 +03:00
|
|
|
credentials=sp_creds,
|
|
|
|
subscription_id=os.environ['AZURE_SUBSCRIPTION_ID']
|
|
|
|
)
|
|
|
|
self.nm_client = NetworkManagementClient(
|
|
|
|
credentials=sp_creds,
|
2018-12-31 22:37:43 +03:00
|
|
|
subscription_id=os.environ['AZURE_SUBSCRIPTION_ID']
|
|
|
|
)
|
|
|
|
|
|
|
|
if not self.deploy_params:
|
2019-01-02 18:40:55 +03:00
|
|
|
gen_id = 'av' + \
|
|
|
|
datetime.utcnow().strftime('%m%d%H%M%S') + \
|
|
|
|
choice(ascii_lowercase)
|
|
|
|
self.resource_group = gen_id + '-rg'
|
2018-12-31 22:37:43 +03:00
|
|
|
self.deploy_params = {
|
|
|
|
'virtualNetworkResourceGroup': self.resource_group,
|
2019-01-02 18:40:55 +03:00
|
|
|
'virtualNetworkName': gen_id + '-vnet',
|
|
|
|
'virtualNetworkSubnetName': gen_id + '-subnet',
|
|
|
|
'avereBackedStorageAccountName': gen_id + 'sa',
|
|
|
|
'controllerName': gen_id + '-con',
|
2019-01-05 06:43:16 +03:00
|
|
|
'controllerAdminUsername': 'azureuser',
|
2019-01-04 22:11:39 +03:00
|
|
|
'controllerAuthenticationType': 'sshPublicKey'
|
2018-12-26 20:27:21 +03:00
|
|
|
}
|
2019-01-05 06:43:16 +03:00
|
|
|
logging.debug('> Generated deploy parameters: \n{}'.format(
|
2018-12-31 22:37:43 +03:00
|
|
|
json.dumps(self.deploy_params, indent=4)))
|
|
|
|
|
2019-01-05 06:43:16 +03:00
|
|
|
self.controller_name = self.deploy_params['controllerName']
|
|
|
|
self.controller_user = self.deploy_params['controllerAdminUsername']
|
|
|
|
|
2018-12-31 22:37:43 +03:00
|
|
|
def create_resource_group(self):
|
|
|
|
"""Creates the Azure resource group for this deployment."""
|
2019-01-05 06:43:16 +03:00
|
|
|
logging.debug('> Creating resource group: ' + self.resource_group)
|
2018-12-31 22:37:43 +03:00
|
|
|
return self.rm_client.resource_groups.create_or_update(
|
|
|
|
self.resource_group,
|
|
|
|
{'location': self.location}
|
2018-12-27 23:22:03 +03:00
|
|
|
)
|
2018-12-21 00:18:34 +03:00
|
|
|
|
2018-12-31 22:37:43 +03:00
|
|
|
def delete_resource_group(self):
|
|
|
|
"""Deletes the Azure resource group for this deployment."""
|
2019-01-05 06:43:16 +03:00
|
|
|
logging.debug('> Deleting resource group: ' + self.resource_group)
|
2018-12-31 22:37:43 +03:00
|
|
|
return self.rm_client.resource_groups.delete(self.resource_group)
|
|
|
|
|
2019-01-04 18:21:33 +03:00
|
|
|
def deploy(self, add_secrets_params=True):
|
2018-12-31 22:37:43 +03:00
|
|
|
"""Deploys the Avere vFXT template."""
|
2019-01-05 06:43:16 +03:00
|
|
|
logging.debug('> Deploying template')
|
2018-12-31 22:37:43 +03:00
|
|
|
|
2019-01-04 18:21:33 +03:00
|
|
|
params = {**self.deploy_params}
|
|
|
|
if add_secrets_params:
|
2019-01-04 22:11:39 +03:00
|
|
|
with open(self.ssh_pub_key_path, 'r') as ssh_pub_key_file:
|
|
|
|
ssh_pub_key = ssh_pub_key_file.read()
|
|
|
|
|
2019-01-04 18:21:33 +03:00
|
|
|
deploy_secrets = {
|
|
|
|
'adminPassword': os.environ['AVERE_ADMIN_PW'],
|
|
|
|
'controllerPassword': os.environ['AVERE_CONTROLLER_PW'],
|
|
|
|
'servicePrincipalAppId': os.environ['AZURE_CLIENT_ID'],
|
|
|
|
'servicePrincipalPassword': os.environ['AZURE_CLIENT_SECRET'],
|
2019-01-04 22:11:39 +03:00
|
|
|
'servicePrincipalTenant': os.environ['AZURE_TENANT_ID'],
|
|
|
|
'controllerSSHKeyData': ssh_pub_key
|
2019-01-04 18:21:33 +03:00
|
|
|
}
|
|
|
|
params = {**deploy_secrets, **params}
|
2018-12-27 23:22:03 +03:00
|
|
|
|
2018-12-31 22:37:43 +03:00
|
|
|
return self.rm_client.deployments.create_or_update(
|
|
|
|
resource_group_name=self.resource_group,
|
2019-01-03 01:06:11 +03:00
|
|
|
deployment_name='avere_template_deploy',
|
2018-12-27 23:22:03 +03:00
|
|
|
properties={
|
|
|
|
'mode': DeploymentMode.incremental,
|
2018-12-31 22:37:43 +03:00
|
|
|
'parameters': {k: {'value': v} for k, v in params.items()},
|
2019-01-03 01:06:11 +03:00
|
|
|
'template_link': self.template_link
|
2018-12-27 23:22:03 +03:00
|
|
|
}
|
|
|
|
)
|
2018-12-31 22:37:43 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return pformat(vars(self), indent=4)
|
2018-12-21 00:18:34 +03:00
|
|
|
|
2018-12-26 23:58:21 +03:00
|
|
|
|
2018-12-21 18:40:57 +03:00
|
|
|
if __name__ == '__main__':
|
2018-12-31 22:37:43 +03:00
|
|
|
pass
|