def deploy_template(self): """ Deploy the targeted template and parameters :param module: Ansible module containing the validated configuration for the deployment template :param client: resource management client for azure :param conn_info: connection info needed :return: """ deploy_parameter = DeploymentProperties(self.deployment_mode) if not self.parameters_link: deploy_parameter.parameters = self.parameters else: deploy_parameter.parameters_link = ParametersLink( uri=self.parameters_link ) if not self.template_link: deploy_parameter.template = self.template else: deploy_parameter.template_link = TemplateLink( uri=self.template_link ) params = ResourceGroup(location=self.location, tags=self.tags) try: self.rm_client.resource_groups.create_or_update(self.resource_group_name, params) except CloudError as exc: self.fail("Resource group create_or_update failed with status code: %s and message: %s" % (exc.status_code, exc.message)) try: result = self.rm_client.deployments.create_or_update(self.resource_group_name, self.deployment_name, deploy_parameter) deployment_result = None if self.wait_for_deployment_completion: deployment_result = self.get_poller_result(result) while deployment_result.properties is None or deployment_result.properties.provisioning_state not in ['Canceled', 'Failed', 'Deleted', 'Succeeded']: time.sleep(self.wait_for_deployment_polling_period) deployment_result = self.rm_client.deployments.get(self.resource_group_name, self.deployment_name) except CloudError as exc: failed_deployment_operations = self._get_failed_deployment_operations(self.deployment_name) self.log("Deployment failed %s: %s" % (exc.status_code, exc.message)) self.fail("Deployment failed with status code: %s and message: %s" % (exc.status_code, exc.message), failed_deployment_operations=failed_deployment_operations) if self.wait_for_deployment_completion and deployment_result.properties.provisioning_state != 'Succeeded': self.log("provisioning state: %s" % deployment_result.properties.provisioning_state) failed_deployment_operations = self._get_failed_deployment_operations(self.deployment_name) self.fail('Deployment failed. Deployment id: %s' % deployment_result.id, failed_deployment_operations=failed_deployment_operations) return deployment_result
def deploy(self, lifetime: int = 60*60*24): with ResourceManagementClient(self.credentials, self.subscription) as rm_client: resourceGroup = self.resourceGroupPrefix + self.haikunator.haikunate(token_length=16) filledVariables = self.__fill_variables(self.template.get("parameters", dict())) region = self.defaultRegion if "region" in filledVariables: region = filledVariables["region"]["value"] self.logger.info("Deploying to region %s and RG %s", region, resourceGroup) if rm_client.resource_groups.check_existence(resourceGroup): raise Exception(f"Resourcegroup named '{resourceGroup}' already exists") createdOn = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0) deleteBy = createdOn + datetime.timedelta(seconds = lifetime) rm_client.resource_groups.create_or_update(resourceGroup, ResourceGroup( location = region, tags = { 'DeleteBy': deleteBy.isoformat(), 'CreatedOn': createdOn.isoformat(), } ) ) return rm_client.deployments.create_or_update( resourceGroup, "deploy-" + resourceGroup, Deployment(properties = DeploymentProperties( mode = DeploymentMode.incremental, template = self.template, parameters = filledVariables ) ) )
def deploy_pools(self, new_pool_sizes, is_scale_up): from azure.mgmt.resource.resources.models import DeploymentProperties, TemplateLink for pool in self.agent_pools: if is_scale_up and pool.actual_capacity < new_pool_sizes[ pool.name]: self.arm_parameters[pool.name + 'Offset'] = { 'value': pool.actual_capacity } self.arm_parameters[pool.name + 'Count'] = { 'value': new_pool_sizes[pool.name] } if is_scale_up: self.prepare_template_for_scale_up(self.arm_template) properties = DeploymentProperties(template=self.arm_template, template_link=None, parameters=self.arm_parameters, mode='incremental') smc = get_mgmt_service_client(ResourceManagementClient) return smc.deployments.create_or_update(self.resource_group_name, "autoscaler-deployment", properties, raw=False)
def _deploy_arm_template_core(resource_group_name, template_file=None, template_uri=None, deployment_name=None, parameter_list=None, mode='incremental', validate_only=False, no_wait=False): from azure.mgmt.resource.resources.models import DeploymentProperties, TemplateLink if bool(template_uri) == bool(template_file): raise CLIError('please provide either template file path or uri, but not both') parameters = _merge_parameters(parameter_list) template = None template_link = None if template_uri: template_link = TemplateLink(uri=template_uri) else: template = get_file_json(template_file) missing = _find_missing_parameters(parameters, template) if len(missing) > 0: prompt_parameters = _prompt_for_parameters(missing) for param_name in prompt_parameters: parameters[param_name] = prompt_parameters[param_name] properties = DeploymentProperties(template=template, template_link=template_link, parameters=parameters, mode=mode) smc = get_mgmt_service_client(ResourceManagementClient) if validate_only: return smc.deployments.validate(resource_group_name, deployment_name, properties, raw=no_wait) else: return smc.deployments.create_or_update(resource_group_name, deployment_name, properties, raw=no_wait)
def arm_deploy_template_new_storage(cli_ctx, resource_group_name, registry_name, location, sku, storage_account_name, admin_user_enabled, deployment_name=None): """Deploys ARM template to create a container registry with a new storage account. :param str resource_group_name: The name of resource group :param str registry_name: The name of container registry :param str location: The name of location :param str sku: The SKU of the container registry :param str storage_account_name: The name of storage account :param bool admin_user_enabled: Enable admin user :param str deployment_name: The name of the deployment """ from azure.mgmt.resource.resources.models import DeploymentProperties from azure.cli.core.util import get_file_json import os parameters = _parameters( registry_name=registry_name, location=location, sku=sku, admin_user_enabled=admin_user_enabled, storage_account_name=storage_account_name) file_path = os.path.join(os.path.dirname(__file__), 'template_new_storage.json') template = get_file_json(file_path) properties = DeploymentProperties(template=template, parameters=parameters, mode='incremental') return _arm_deploy_template( get_arm_service_client(cli_ctx).deployments, resource_group_name, deployment_name, properties)
def deploy(self): # LOCATE TEMPLATE: all templates are stored with the application, and the specific one to deploy is identified by 'course_name' template_path = os.path.join(os.path.dirname(__file__), 'templates', self.course_name + '.json') with open(template_path, 'r') as template_file_fd: template = json.load(template_file_fd) # IDENTIFY PARAMETERS: these are unique to a specific template, so we'd have to play around with this when we expand parameters = { 'vmName': self.course_name, } parameters = {k: {'value': v} for k, v in parameters.items()} # SET PROPERTIES FOR DEPLOYMENT deployment_prop = DeploymentProperties(mode='Complete', template=template, parameters=parameters) # RUN DEPLOYMENT deployment_async_operation = self.client.deployments.create_or_update( self.resource_group, self.deploy_name, deployment_prop) # PLAYING AROUND WITH FIXES TO REDIRECT ISSUE: Aug 17th, 2018 # deployment_async_operation.wait(180) # This is a hardcoded timeout time, need to fix eventually # result = deployment_async_operation.result() deployState = deployment_async_operation.state() # while deployState != 'Succeeded': # deployment_async_operation.wait(3) # deployState = deployment_async_operation.state() if deployState == 'Succeeded': result = deployment_async_operation.result()
def _deploy_arm_template_core(resource_group_name, deployment_name, template_file_path, parameters_file_path=None, mode='incremental', validate_only=False): from azure.mgmt.resource.resources.models import DeploymentProperties parameters = None if parameters_file_path: parameters = get_file_json(parameters_file_path) if parameters: parameters = parameters.get('parameters', parameters) template = get_file_json(template_file_path) properties = DeploymentProperties(template=template, parameters=parameters, mode=mode) smc = get_mgmt_service_client(ResourceManagementClient) if validate_only: return smc.deployments.validate(resource_group_name, deployment_name, properties) else: return smc.deployments.create_or_update(resource_group_name, deployment_name, properties)
def deploy_pools(self, new_pool_sizes): from azure.mgmt.resource.resources.models import DeploymentProperties, TemplateLink for pool in self.scalable_pools: if new_pool_sizes[pool.name] == 0: # This is required as 0 is not an accepted value for the Count parameter, # but setting the offset to 1 actually prevent the deployment # from changing anything self.arm_parameters[pool.name + 'Count'] = {'value': 1} self.arm_parameters[pool.name + 'Offset'] = {'value': 1} else: # We don't need to set the offset parameter as we are directly specifying each # resource in the template instead of using Count func self.arm_parameters[pool.name + 'Count'] = { 'value': new_pool_sizes[pool.name] } template = prepare_template_for_scale_out(self.arm_template, self.agent_pools, new_pool_sizes) properties = DeploymentProperties(template=template, template_link=None, parameters=self.arm_parameters, mode='incremental') deployment_id = str(uuid.uuid4()).split('-')[0] deployment_name = "autoscaler-deployment-{}".format(deployment_id) smc = get_mgmt_service_client(ResourceManagementClient) logger.info('Deployment {} started...'.format(deployment_name)) return smc.deployments.create_or_update(self.resource_group_name, deployment_name, properties, raw=False)
def _deploy_arm_template_core(resource_group_name, template_file=None, template_uri=None, deployment_name=None, parameters=None, mode='incremental', validate_only=False, no_wait=False): from azure.mgmt.resource.resources.models import DeploymentProperties, TemplateLink if bool(template_uri) == bool(template_file): raise CLIError('please provide either template file path or uri, but not both') if parameters: parameters = json.loads(parameters) if parameters: parameters = parameters.get('parameters', parameters) template = None template_link = None if template_uri: template_link = TemplateLink(uri=template_uri) else: template = get_file_json(template_file) properties = DeploymentProperties(template=template, template_link=template_link, parameters=parameters, mode=mode) smc = get_mgmt_service_client(ResourceManagementClient) if validate_only: return smc.deployments.validate(resource_group_name, deployment_name, properties, raw=no_wait) else: return smc.deployments.create_or_update(resource_group_name, deployment_name, properties, raw=no_wait)
def run(self, subscription_id, deployment_name, group_name, template_uri, parameters_uri): credentials = self.credentials resource_client = ResourceManagementClient( ResourceManagementClientConfiguration( credentials, subscription_id)) template = TemplateLink( uri=template_uri, ) parameters = ParametersLink( uri=parameters_uri, ) result = resource_client.deployments.create_or_update( group_name, deployment_name, Deployment( properties=DeploymentProperties( mode=DeploymentMode.incremental, template_link=template, parameters_link=parameters, ) ) ) return result
def deploy(self): """Deploy the template to a resource group.""" resource_group_params = {'location': self.location} self.client.resource_groups.create_or_update(self.resource_group, resource_group_params) template_path = os.path.join(os.path.dirname(__file__), 'template.json') with open(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()} deployment_properties = DeploymentProperties( mode=DeploymentMode.incremental, template=template, parameters=parameters) deployment_async_operation = self.client.deployments.begin_create_or_update( self.resource_group, 'azure-sample', Deployment(properties=deployment_properties)) deployment_async_operation.wait()
def deploy_extensions(self, ext_json): self.log.info(f"Deploying extension template: {ext_json}") retry = 0 max_retry = 5 while retry < max_retry: try: props = DeploymentProperties(template=ext_json, mode=DeploymentMode.incremental) poller = self.__compute_manager.resource_client.deployments.begin_create_or_update( self.__vm_data.rg_name, 'TestDeployment', Deployment(properties=props)) # Wait a max of 10 mins poller.wait(timeout=10 * 60) if poller.done(): break else: raise TimeoutError( "Extension deployment timed out after 10 mins") except CloudError as ce: self.log.warning(f"Cloud Error: {ce}", exc_info=True) retry += 1 err_msg = str(ce) if "'code': 'Conflict'" in err_msg and retry < max_retry: self.log.warning( "({0}/{1}) Conflict Error when deploying extension in VMSS, trying again in 1 sec (Error: {2})" .format(retry, max_retry, ce)) # Since this was a conflicting operation, sleeping for a second before retrying sleep(1) else: raise self.log.info("Successfully deployed extensions")
def deploy_acs_template( cls, azure_wrapper, template_uri, location, group_name, public_key, master_prefix, agent_prefix, admin_name, oauth_enabled: Union[bool, str], vm_size, agent_count, name_suffix, vm_diagnostics_enabled: bool): """ Creates a new resource group and deploys a ACS DC/OS template to it """ assert master_prefix != agent_prefix, 'Master and agents must have unique prefixs' validate_hostname_prefix(master_prefix) validate_hostname_prefix(agent_prefix) deployment_name = DEPLOYMENT_NAME.format(group_name) # Resource group must be created before validation can occur if azure_wrapper.rmc.resource_groups.check_existence(group_name): raise Exception("Group name already exists / taken: {}".format(group_name)) log.info('Starting resource group_creation') with contextlib.ExitStack() as stack: azure_wrapper.rmc.resource_groups.create_or_update( group_name, ResourceGroup(location=location)) stack.callback(azure_wrapper.rmc.resource_groups.delete, group_name) log.info('Resource group created: {}'.format(group_name)) template_params = { 'sshRSAPublicKey': public_key, # must be lower or validation will fail 'masterEndpointDNSNamePrefix': master_prefix, 'agentEndpointDNSNamePrefix': agent_prefix, 'linuxAdminUsername': admin_name, 'agentVMSize': vm_size, 'agentCount': agent_count, 'nameSuffix': name_suffix, 'oauthEnabled': repr(oauth_enabled).lower(), # oauth uses string, vm diagnostic uses bool 'enableVMDiagnostics': vm_diagnostics_enabled} log.info('Provided template parameters: {}'.format(template_params)) # azure requires that parameters be provided as {key: {'value': value}} template_parameters = {k: {'value': v} for k, v in template_params.items()} deployment_properties = DeploymentProperties( template_link=TemplateLink(uri=template_uri), mode=DeploymentMode.incremental, parameters=template_parameters) log.info('Checking with Azure to validate template deployment') result = azure_wrapper.rmc.deployments.validate( group_name, deployment_name, properties=deployment_properties) if result.error: for details in result.error.details: log.error('{}: {}'.format(details.code, details.message)) error_msgs = '\n'.join(['{}: {}'.format(d.code, d.message) for d in result.error.details]) raise Exception("Template verification failed!\n{}".format(error_msgs)) log.info('Template successfully validated') log.info('Starting template deployment') azure_wrapper.rmc.deployments.create_or_update( group_name, deployment_name, deployment_properties) stack.pop_all() return cls(group_name, azure_wrapper)
def _deploy_arm_template_core(cli_ctx, resource_group_name, deployment_name, template, parameters): from azure.mgmt.resource.resources.models import DeploymentProperties from azure.cli.core.commands import LongRunningOperation properties = DeploymentProperties(template=template, parameters=parameters, mode='incremental') client = resource_client_factory(cli_ctx) deploy_poll = client.deployments.create_or_update(resource_group_name, deployment_name, properties, raw=False) result = LongRunningOperation(cli_ctx)(deploy_poll) return result
def _create_kubernetes(resource_group_name, deployment_name, dns_name_prefix, name, ssh_key_value, admin_username="******", agent_count="3", agent_vm_size="Standard_D2_v2", location=None, service_principal=None, client_secret=None): from azure.mgmt.resource.resources.models import DeploymentProperties if not location: location = '[resourceGroup().location]' template = { "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "resources": [ { "apiVersion": "2016-09-30", "location": location, "type": "Microsoft.ContainerService/containerServices", "name": name, "properties": { "orchestratorProfile": { "orchestratorType": "Custom" }, "masterProfile": { "count": 1, "dnsPrefix": dns_name_prefix + '-k8s-masters' }, "agentPoolProfiles": [ { "name": "agentpools", "count": agent_count, "vmSize": agent_vm_size, "dnsPrefix": dns_name_prefix + '-k8s-agents', } ], "linuxProfile": { "ssh": { "publicKeys": [ { "keyData": ssh_key_value } ] }, "adminUsername": admin_username }, "servicePrincipalProfile": { "ClientId": service_principal, "Secret": client_secret }, "customProfile": { "orchestrator": "kubernetes" } } } ] } properties = DeploymentProperties(template=template, template_link=None, parameters=None, mode='incremental') smc = get_mgmt_service_client(ResourceManagementClient) return smc.deployments.create_or_update(resource_group_name, deployment_name, properties)
def _invoke_deployment(resource_group_name, deployment_name, template, parameters, validate, no_wait): from azure.mgmt.resource.resources import ResourceManagementClient from azure.mgmt.resource.resources.models import DeploymentProperties properties = DeploymentProperties(template=template, parameters=parameters, mode='incremental') smc = get_mgmt_service_client(ResourceManagementClient).deployments if validate: logger.info('==== BEGIN TEMPLATE ====') logger.info(json.dumps(template, indent=2)) logger.info('==== END TEMPLATE ====') return smc.validate(resource_group_name, deployment_name, properties) return smc.create_or_update(resource_group_name, deployment_name, properties, raw=no_wait)
def deploy_template(self): """ Deploy the targeted template and parameters :param module: Ansible module containing the validated configuration for the deployment template :param client: resource management client for azure :param conn_info: connection info needed :return: """ deploy_parameter = DeploymentProperties() deploy_parameter.mode = self.deployment_mode if not self.parameters_link: deploy_parameter.parameters = self.parameters else: deploy_parameter.parameters_link = ParametersLink( uri=self.parameters_link ) if not self.template_link: deploy_parameter.template = self.template else: deploy_parameter.template_link = TemplateLink( uri=self.template_link ) params = ResourceGroup(location=self.location, tags=self.tags) try: self.rm_client.resource_groups.create_or_update(self.resource_group_name, params) except CloudError as exc: self.fail("Resource group create_or_update failed with status code: %s and message: %s" % (exc.status_code, exc.message)) try: result = self.rm_client.deployments.create_or_update(self.resource_group_name, self.deployment_name, deploy_parameter) deployment_result = self.get_poller_result(result) if self.wait_for_deployment_completion: while deployment_result.properties.provisioning_state not in ['Canceled', 'Failed', 'Deleted', 'Succeeded']: time.sleep(self.wait_for_deployment_polling_period) deployment_result = self.rm_client.deployments.get(self.resource_group_name, self.deployment_name) except CloudError as exc: failed_deployment_operations = self._get_failed_deployment_operations(self.deployment_name) self.log("Deployment failed %s: %s" % (exc.status_code, exc.message)) self.fail("Deployment failed with status code: %s and message: %s" % (exc.status_code, exc.message), failed_deployment_operations=failed_deployment_operations) if self.wait_for_deployment_completion and deployment_result.properties.provisioning_state != 'Succeeded': self.log("provisioning state: %s" % deployment_result.properties.provisioning_state) failed_deployment_operations = self._get_failed_deployment_operations(self.deployment_name) self.fail('Deployment failed. Deployment id: %s' % deployment_result.id, failed_deployment_operations=failed_deployment_operations) return deployment_result
def _build_ase_deployment_properties(name, location, subnet_id, virtual_ip_type=None, front_end_scale_factor=None, front_end_sku=None, tags=None, kind='ASEv2', os_preference=None, zone_redundant=None): # InternalLoadBalancingMode Enum: None 0, Web 1, Publishing 2. # External: 0 (None), Internal: 3 (Web + Publishing) ilb_mode = 3 if virtual_ip_type == 'Internal' else 0 ase_properties = { 'name': name, 'location': location, 'InternalLoadBalancingMode': ilb_mode, 'virtualNetwork': { 'id': subnet_id } } if front_end_scale_factor: ase_properties['frontEndScaleFactor'] = front_end_scale_factor if front_end_sku: worker_sku = _map_worker_sku(front_end_sku) ase_properties['multiSize'] = worker_sku if os_preference: ase_properties['osPreference'] = os_preference if zone_redundant: ase_properties['zoneRedundant'] = zone_redundant ase_resource = { 'name': name, 'type': 'Microsoft.Web/hostingEnvironments', 'location': location, 'apiVersion': '2019-08-01', 'kind': kind, 'tags': tags, 'properties': ase_properties } deployment_template = ArmTemplateBuilder() deployment_template.add_resource(ase_resource) template = deployment_template.build() parameters = deployment_template.build_parameters() deploymentProperties = DeploymentProperties(template=template, parameters=parameters, mode='Incremental') deployment = Deployment(properties=deploymentProperties) return deployment
def deploy_template(self): logger.info("deploying arm template: %s", self.arm_template) with open(self.arm_template, "r") as template_handle: template = json.load(template_handle) client = get_client_from_cli_profile(ResourceManagementClient) client.resource_groups.create_or_update(self.resource_group, {"location": self.location}) expiry = (datetime.now(TZ_UTC) + timedelta(days=365)).strftime("%Y-%m-%dT%H:%M:%SZ") params = { "name": { "value": self.application_name }, "owner": { "value": self.owner }, "clientId": { "value": self.results["client_id"] }, "clientSecret": { "value": self.results["client_secret"] }, "signedExpiry": { "value": expiry }, "workbookData": { "value": self.workbook_data }, } deployment = Deployment( properties=DeploymentProperties(mode=DeploymentMode.incremental, template=template, parameters=params)) result = client.deployments.create_or_update(self.resource_group, gen_guid(), deployment).result() if result.properties.provisioning_state != "Succeeded": logger.error( "error deploying: %s", json.dumps(result.as_dict(), indent=4, sort_keys=True), ) sys.exit(1) self.results["deploy"] = result.properties.outputs logger.info("assigning the user managed identity role") assign_scaleset_role( self.application_name, self.results["deploy"]["scaleset-identity"]["value"])
def deploy_template(self, template_url, parameters): """ Deploys an ARM template from the provided_url using provided parameters to the resource group """ if not template_url: raise ValueError('ARM template URL not provided') template_json = DeployableResource._get_template_json(template_url) properties = DeploymentProperties(template=template_json, template_link=None, parameters=parameters, mode='incremental') client = DeployableResource._get_resource_mgmt_client() deployment = client.deployments.create_or_update( self.resource_group, self.deployment_name, properties) return deployment
def create_cluster(self, ): client = ResourceManagementClient(self.get_credential(), self.subscription_id) """Deploy the template to a resource group.""" client.resource_groups.get(self.resource_group_name) deployment_properties = DeploymentProperties( mode=DeploymentMode.incremental, template_link=TemplateLink(uri=self.template_link), parameters_link=TemplateLink(uri=self.parameters_link)) deployment_async_operation = client.deployments.create_or_update( self.resource_group_name, self.deploy_name, deployment_properties) deployment_async_operation.wait() return deployment_async_operation.result()
def _create_aks(self): with open(os.path.join(self.base_path, CreateAKS.PARAMETERS_JSON)) as in_file: parameters = json.load(in_file) parameters[u"resourceName"][u"value"] = self.context.aks_name parameters[u"agentCount"][u"value"] = self.context.node_count parameters[u"agentVMSize"][u'Value'] = self.context.vm_size parameters[u"osDiskSizeGB"][u'Value'] = int(self.context.os_disk_size) parameters[u"sshRSAPublicKey"][u'Value'] = self.context.public_key parameters[u"servicePrincipalClientId"][ u'Value'] = self.context.client_id parameters[u"servicePrincipalClientSecret"][ u'Value'] = self.context.secret parameters[u"kubernetesVersion"][u'Value'] = self.context.k8s_version parameters[u"dnsPrefix"][u'Value'] = "{0}-maprtech".format( self.context.aks_name) with open(os.path.join(self.base_path, CreateAKS.ARM_TEMPLATE_JSON)) as in_file: template = json.load(in_file) deployment_properties = DeploymentProperties() deployment_properties.template = template deployment_properties.parameters = parameters deployment_properties.mode = DeploymentMode.incremental Log.info( "{0}: Run K8S deployment test with {1} Nodes OS Disk Size {2}...". format(self.context.resource_group, self.context.node_count, self.context.os_disk_size), True) deployment_async_operation = self.resource_client.deployments.create_or_update( self.context.resource_group, "maprk8s.deployment", deployment_properties) deployment_async_operation.wait() Log.info("K8S cluster deployment complete", True)
def DeleteResources(self): template_path = os.path.join(os.path.dirname(__file__), 'templates', 'clearResources.json') with open(template_path, 'r') as template_file_fd: template = json.load(template_file_fd) parameters = {} deployment_prop = DeploymentProperties(mode='Complete', template=template, parameters=parameters) deployment_async_operation = self.client.deployments.create_or_update( self.resource_group, self.deploy_name + 'DeleteResources', deployment_prop) deployment_async_operation.wait()
def create_deployment_properties(self, template_url, parameters): """ Pulls the targeted template, checks parameter specs and casts user provided parameters to the appropriate type. Assertion is raised if there are unused parameters or invalid casting """ user_parameters = copy.deepcopy(parameters) type_cast_map = { 'string': str, 'secureString': str, 'int': int, 'bool': bool, 'object': check_json_object, 'secureObject': check_json_object, 'array': check_array } log.debug('Pulling Azure template for parameter validation...') r = requests.get(template_url) r.raise_for_status() template = r.json() if 'parameters' not in template: assert user_parameters is None, 'This template does not support parameters, ' \ 'yet parameters were supplied: {}'.format(user_parameters) log.debug('Constructing DeploymentProperties from user parameters: {}'. format(parameters)) template_parameters = {} for k, v in template['parameters'].items(): if k in user_parameters: # All templates parameters are required to have a type field. # Azure requires that parameters be provided as {key: {'value': value}}. template_parameters[k] = { 'value': type_cast_map[v['type']](user_parameters.pop(k)) } log.debug('Final template parameters: {}'.format(template_parameters)) if len(user_parameters) > 0: raise Exception( 'Unrecognized template parameters were supplied: {}'.format( user_parameters)) return DeploymentProperties(template=template, mode=DeploymentMode.incremental, parameters=template_parameters)
def deploy_pools(self, new_pool_sizes): print('deploying') from azure.mgmt.resource.resources.models import DeploymentProperties, TemplateLink parameters = get_file_json('./azuredeploy.parameters.json') parameters = parameters.get('parameters', parameters) original_parameters = deepcopy(parameters) for pool_name in new_pool_sizes: parameters[pool_name + 'Count'] = {'value': new_pool_sizes[pool_name]} logger.info('Requested size for {}: {}'.format(pool_name, new_pool_sizes[pool_name])) template = get_file_json('./azuredeploy.json') properties = DeploymentProperties(template=template, template_link=None, parameters=parameters, mode='complete') smc = get_mgmt_service_client(ResourceManagementClient) op = smc.deployments.create_or_update(self.resource_group_name, "autoscale", properties, raw=False) while not op.done(): print("Waiting for operation to finish...") time.sleep(30) print(op.result())
def deploy_template(module, client, conn_info): """ Deploy the targeted template and parameters :param module: Ansible module containing the validated configuration for the deployment template :param client: resource management client for azure :param conn_info: connection info needed :return: """ deployment_name = conn_info["deployment_name"] group_name = conn_info["resource_group_name"] deploy_parameter = DeploymentProperties() deploy_parameter.mode = "Complete" if module.params.get('parameters_link') is None: deploy_parameter.parameters = module.params.get('parameters') else: parameters_link = ParametersLink( uri=module.params.get('parameters_link')) deploy_parameter.parameters_link = parameters_link if module.params.get('template_link') is None: deploy_parameter.template = module.params.get('template') else: template_link = TemplateLink(uri=module.params.get('template_link')) deploy_parameter.template_link = template_link params = ResourceGroup(location=module.params.get('location'), tags=module.params.get('tags')) try: client.resource_groups.create_or_update(group_name, params) result = client.deployments.create_or_update(group_name, deployment_name, deploy_parameter) return result.result() # Blocking wait, return the Deployment object except CloudError as e: module.fail_json( msg='Deploy create failed with status code: %s and message: "%s"' % (e.status_code, e.message))
def arm_deploy_template_existing_storage(resource_group_name, #pylint: disable=too-many-arguments registry_name, location, sku, storage_account_name, admin_user_enabled, deployment_name=None): '''Deploys ARM template to create a container registry with an existing storage account. :param str resource_group_name: The name of resource group :param str registry_name: The name of container registry :param str location: The name of location :param str sku: The SKU of the container registry :param str storage_account_name: The name of storage account :param bool admin_user_enabled: Enable admin user :param str deployment_name: The name of the deployment ''' from azure.mgmt.resource.resources.models import DeploymentProperties from azure.cli.core._util import get_file_json import os storage_account_resource_group = \ get_resource_group_name_by_storage_account_name(storage_account_name) parameters = _parameters( registry_name=registry_name, location=location, sku=sku, admin_user_enabled=admin_user_enabled, storage_account_name=storage_account_name, storage_account_resource_group=storage_account_resource_group) file_path = os.path.join(os.path.dirname(__file__), 'template_existing_storage.json') template = get_file_json(file_path) properties = DeploymentProperties(template=template, parameters=parameters, mode='incremental') return _arm_deploy_template( get_arm_service_client().deployments, resource_group_name, deployment_name, properties)
def arm_deploy_template(resource_group_name, registry_name, location, storage_account_name, admin_user_enabled): '''Deploys ARM template to create a container registry with a new storage account. :param str resource_group_name: The name of resource group :param str registry_name: The name of container registry :param str location: The name of location :param str storage_account_name: The name of storage account :param bool admin_user_enabled: Enable admin user ''' from azure.mgmt.resource.resources.models import DeploymentProperties from azure.cli.core._util import get_file_json import os parameters = _parameters(registry_name, location, storage_account_name, admin_user_enabled) file_path = os.path.join(os.path.dirname(__file__), 'template.json') template = get_file_json(file_path) properties = DeploymentProperties(template=template, parameters=parameters, mode='incremental') return _arm_deploy_template(get_arm_service_client().deployments, resource_group_name, properties)
def deploy(self, parameters): """Deploy resource to resource group""" with open(self.get_template_path(), "r") as tfd: template = json.load(tfd) print("PARAMETERS={}".format(parameters)) # Deployment is an asynchronous operation deployment = self.client.deployments.begin_create_or_update( self.resource_group, self.name, Deployment( properties=DeploymentProperties( mode=DeploymentMode.incremental, template=template, parameters={ k: {"value": v} for k, v in parameters.items() } ) ) ) deployment.wait() return template
def arm_deploy_template(resource_group_name, registry_name, location, storage_account_name, admin_user_enabled): '''Deploys ARM template to create/update a container registry. :param str resource_group_name: The name of resource group :param str registry_name: The name of container registry :param str location: The name of location :param str storage_account_name: The name of storage account :param bool admin_user_enabled: Enable admin user ''' from azure.mgmt.resource.resources.models import DeploymentProperties from azure.cli.core._util import get_file_json import os parameters = _parameters(registry_name, location, storage_account_name, admin_user_enabled) storage_account_resource_group, _ = _parse_storage_account( storage_account_name) if storage_account_resource_group: file_path = os.path.join(os.path.dirname(__file__), 'template.existing.json') parameters['storageAccountResourceGroup'] = { 'value': storage_account_resource_group } else: file_path = os.path.join(os.path.dirname(__file__), 'template.new.json') parameters['storageAccountType'] = {'value': 'Standard_LRS'} template = get_file_json(file_path) properties = DeploymentProperties(template=template, parameters=parameters, mode='incremental') return _arm_deploy_template(get_arm_service_client().deployments, resource_group_name, properties)
def deploy_template(module, client, conn_info): """ Deploy the targeted template and parameters :param module: Ansible module containing the validated configuration for the deployment template :param client: resource management client for azure :param conn_info: connection info needed :return: """ deployment_name = conn_info["deployment_name"] group_name = conn_info["resource_group_name"] deploy_parameter = DeploymentProperties() deploy_parameter.mode = "Complete" if module.params.get('parameters_link') is None: deploy_parameter.parameters = module.params.get('parameters') else: parameters_link = ParametersLink( uri = module.params.get('parameters_link') ) deploy_parameter.parameters_link = parameters_link if module.params.get('template_link') is None: deploy_parameter.template = module.params.get('template') else: template_link = TemplateLink( uri = module.params.get('template_link') ) deploy_parameter.template_link = template_link params = ResourceGroup(location=module.params.get('location'), tags=module.params.get('tags')) try: client.resource_groups.create_or_update(group_name, params) result = client.deployments.create_or_update(group_name, deployment_name, deploy_parameter) return result.result() # Blocking wait, return the Deployment object except CloudError as e: module.fail_json(msg='Deploy create failed with status code: %s and message: "%s"' % (e.status_code, e.message))
def main(): validate_env() location = os.getenv('AZURE_LOCATION', 'East US') credentials = azure.common.credentials.ServicePrincipalCredentials( client_id=os.environ['AZURE_CLIENT_ID'], secret=os.environ['AZURE_CLIENT_SECRET'], tenant=os.environ['AZURE_TENANT_ID']) subscription_id = os.environ['AZURE_SUBSCRIPTION_ID'] template = TemplateLink(uri=os.environ['AZURE_TEMPLATE_URL']) # tenant_id = os.environ.get('AZURE_TENANT_ID') # client_id = os.environ.get('AZURE_CLIENT_ID') # client_secret = os.environ.get('AZURE_CLIENT_SECRET') group_name = 'testing' + ''.join(random.choice('01234567890abcdef') for n in range(10)) deployment_name = 'deployment{}'.format(uuid.uuid4().hex) rmc = ResourceManagementClient(credentials, subscription_id) template_parameters = get_env_params() # Output resource group print("Resource group name: {}".format(group_name)) print("Deployment name: {}".format(deployment_name)) azure_cluster = { 'resource_group_name': group_name, 'deployment_name': deployment_name} pkgpanda.util.write_json('azure-cluster.json', azure_cluster) # Create a new resource group print("Creating new resource group in location: {}".format(location)) if rmc.resource_groups.check_existence(group_name): print("ERROR: Group name already exists / taken: {}".format(group_name)) rmc.resource_groups.create_or_update( group_name, ResourceGroup(location=location)) test_successful = False try: deployment_properties = DeploymentProperties( template_link=template, mode=DeploymentMode.incremental, parameters=template_parameters) # Use RPC against azure to validate the ARM template is well-formed result = rmc.deployments.validate(group_name, deployment_name, properties=deployment_properties) if result.error: print("Template verification failed\n{}".format(result.error), file=sys.stderr) sys.exit(1) # Actually create a template deployment print("Creating template deployment ...") deploy_poller = rmc.deployments.create_or_update(group_name, deployment_name, deployment_properties) # Stop after 45 attempts (each one takes up to one minute) @retry(stop_max_attempt_number=45) def poll_deploy(): res = deploy_poller.result(timeout=60) print("Current deploy state: {}".format(res.properties.provisioning_state)) assert deploy_poller.done(), "Not done deploying." print("Waiting for template to deploy ...") try: poll_deploy() except: print("Current deploy status:\n{}".format(deploy_poller.result(0))) raise print("Template deployed successfully") assert deploy_poller.done(), "Deployment failed / polling didn't reach deployment done." deployment_result = deploy_poller.result() print(deployment_result.properties.outputs) master_lb = deployment_result.properties.outputs['masterFQDN']['value'] print("Template deployed using SSH private key: https://mesosphere.onelogin.com/notes/18444") print("For troubleshooting, master0 can be reached using: ssh -p 2200 {}@{}".format( get_value('linuxAdminUsername'), master_lb)) # Run test now, so grab IPs nmc = NetworkManagementClient(credentials, subscription_id) ip_buckets = { 'master': [], 'private': [], 'public': []} for resource in rmc.resource_groups.list_resources( group_name, filter=("resourceType eq 'Microsoft.Network/networkInterfaces' or " "resourceType eq 'Microsoft.Compute/virtualMachineScaleSets'")): if resource.type == 'Microsoft.Network/networkInterfaces': nics = [nmc.network_interfaces.get(group_name, resource.name)] elif resource.type == 'Microsoft.Compute/virtualMachineScaleSets': nics = list(nmc.network_interfaces.list_virtual_machine_scale_set_network_interfaces( virtual_machine_scale_set_name=resource.name, resource_group_name=group_name)) else: raise('Unexpected resourceType: {}'.format(resource.type)) for bucket_name in ip_buckets.keys(): if bucket_name in resource.name: for n in nics: for config in n.ip_configurations: ip_buckets[bucket_name].append(config.private_ip_address) print('Detected IP configuration: {}'.format(ip_buckets)) with Tunnel(get_value('linuxAdminUsername'), 'ssh_key', master_lb, port=2200) as t: integration_test( tunnel=t, test_dir='/home/{}'.format(get_value('linuxAdminUsername')), dcos_dns=ip_buckets['master'][0], master_list=ip_buckets['master'], agent_list=ip_buckets['private'], public_agent_list=ip_buckets['public'], provider='azure', test_dns_search=False, add_env=get_test_config(), pytest_cmd=os.getenv('DCOS_PYTEST_CMD', "py.test -vv -s -rs -m 'not ccm' ") + os.getenv('CI_FLAGS', '')) test_successful = True except Exception as ex: traceback.print_exc() print("ERROR: exception {}".format(ex)) raise finally: if os.getenv('AZURE_CLEANUP') == 'false': print("Cluster must be cleaned up manually") print("Cluster details: {}".format(azure_cluster)) else: # Send a delete request # TODO(cmaloney): The old code had a retry around this: # @retry(wait_exponential_multiplier=1000, wait_exponential_max=60*1000, stop_max_delay=(30*60*1000)) poller = rmc.resource_groups.delete(group_name) # poll for the delete to complete print("Deleting resource group: {} ...".format(group_name)) @retry(wait_fixed=(5 * 1000), stop_max_delay=(60 * 60 * 1000)) def wait_for_delete(): assert poller.done(), "Timed out waiting for delete" print("Waiting for delete ...") wait_for_delete() print("Clean up successful") if test_successful: print("Azure test deployment succeeded") else: print("ERROR: Azure test deployment failed", file=sys.stderr) sys.exit(2)