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 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_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(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 _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_arm_template_core(cli_ctx, resource_group_name, deployment_name, template, parameters): from azure.mgmt.resource.resources.models import Deployment, DeploymentProperties from azure.cli.core.commands import LongRunningOperation properties = DeploymentProperties(template=template, parameters=parameters, mode="incremental") deployment = Deployment(properties=properties) client = resource_client_factory(cli_ctx) deploy_poll = client.deployments.create_or_update(resource_group_name, deployment_name, deployment, raw=False) result = LongRunningOperation(cli_ctx)(deploy_poll) return result
def deploy(self, template_name, deployment_mode = DeploymentMode.incremental): # Create or Update resource group before Deploying template self.client.resource_groups.create_or_update( self.resource_group_name, dict( location=self.resource_group_location ) ) # build template and parameters as json template_path = pkg_resources.resource_filename( "azure_python_arm_deployer", TEMPLATE_PATH.format(template_name=template_name)) with open(template_path, "r") as template_file_fd: template = json.load(template_file_fd) parameter_path = pkg_resources.resource_filename( "azure_python_arm_deployer", PARAMETER_FILE_PATH.format(template_name=template_name)) with open(parameter_path, "r") as parameter_path_fd: parameters = json.load(parameter_path_fd)["parameters"] deployment_properties = { 'mode': deployment_mode, 'template': template, 'parameters': parameters } # create deployment name dynamically. self.last_deployment_id = f'{template_name}-{datetime.datetime.now().strftime("%m%d%Y")}-{random.getrandbits(32)}' """ returns LROPoller of type `LRO poller class <https://docs.microsoft.com/en-us/python/api/msrest/msrest.polling.lropoller?view=azure-python>`""" deployment_async_operation = self.client.deployments.begin_create_or_update( self.resource_group_name, f"{template_name}-deployment", Deployment(properties=deployment_properties) ) # wait for default time to let the async deployment operation complete. deployment_async_operation.wait() if not deployment_async_operation.done: raise Exception("Deployment not complete after wait period also.") # return result as operation complete return deployment_async_operation.result()
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 deploy_template(self) -> None: 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, subscription_id=self.get_subscription_id()) 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") if self.multi_tenant_domain: # clear the value in the Issuer Url field: # https://docs.microsoft.com/en-us/sharepoint/dev/spfx/use-aadhttpclient-enterpriseapi-multitenant app_func_audience = "https://%s/%s" % ( self.multi_tenant_domain, self.application_name, ) app_func_issuer = "" multi_tenant_domain = {"value": self.multi_tenant_domain} else: app_func_audience = "https://%s.azurewebsites.net" % self.application_name tenant_oid = str(self.cli_config["authority"]).split("/")[-1] app_func_issuer = "https://sts.windows.net/%s/" % tenant_oid multi_tenant_domain = {"value": ""} params = { "app_func_audience": { "value": app_func_audience }, "name": { "value": self.application_name }, "owner": { "value": self.owner }, "clientId": { "value": self.results["client_id"] }, "clientSecret": { "value": self.results["client_secret"] }, "app_func_issuer": { "value": app_func_issuer }, "signedExpiry": { "value": expiry }, "multi_tenant_domain": multi_tenant_domain, "workbookData": { "value": self.workbook_data }, } deployment = Deployment( properties=DeploymentProperties(mode=DeploymentMode.incremental, template=template, parameters=params)) count = 0 tries = 10 error: Optional[Exception] = None while count < tries: count += 1 try: 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 return except Exception as err: error = err as_repr = repr(err) # Modeled after Azure-CLI. See: # https://github.com/Azure/azure-cli/blob/ # 3a2f6009cff788fde3b0170823c9129f187b2812/src/azure-cli-core/ # azure/cli/core/commands/arm.py#L1086 if ("PrincipalNotFound" in as_repr and "does not exist in the directory" in as_repr): logging.info( "application principal not available in AAD yet") if error: raise error else: raise Exception("unknown error deploying")
} for k, v in la_template_parameters.items() } # Deploy the Log Analytics Workspace Template la_deployment_properties = DeploymentProperties( mode=DeploymentMode.incremental, template=la_template_body, parameters=la_template_parameters) print("Deploying Log Analytics Workspace {} in resource group {}".format( la_template_parameters['name']['value'], resource_group)) start_time = time.time() la_deployment_async_operation = client.deployments.create_or_update( resource_group, 'adb-e2-automation-la-deploy', Deployment(properties=la_deployment_properties)) la_deployment_async_operation.wait() end_time = time.time() print("Deployed the Log Analytics Workspace in {} seconds".format( str(int(end_time - start_time)))) # Get the Azure Databricks Workspace Template adb_template_body = None adb_template_path = os.path.join(os.path.dirname(__file__), 'arm_templates', 'azure_databricks_npip_template.json') with open(adb_template_path, 'r') as adb_template_file: adb_template_body = json.load(adb_template_file) # Get the Azure Databricks Workspace Template Parameters adb_template_parameters = None adb_template_params_path = os.path.join(
def deploy_template(self) -> None: 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)) count = 0 tries = 10 error: Optional[Exception] = None while count < tries: count += 1 try: 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 return except Exception as err: error = err as_repr = repr(err) # Modeled after Azure-CLI. See: # https://github.com/Azure/azure-cli/blob/ # 3a2f6009cff788fde3b0170823c9129f187b2812/src/azure-cli-core/ # azure/cli/core/commands/arm.py#L1086 if ("PrincipalNotFound" in as_repr and "does not exist in the directory" in as_repr): logging.info( "application principal not available in AAD yet") if error: raise error else: raise Exception("unknown error deploying")