def deploy_webapp(self, app_name, group_name, service_plan, storage_account_name): self.log.info("Deploying Function App %s (%s) in group %s" % (app_name, service_plan.location, group_name)) site_config = SiteConfig(app_settings=[]) functionapp_def = Site(location=service_plan.location, site_config=site_config) functionapp_def.kind = 'functionapp,linux' functionapp_def.server_farm_id = service_plan.id site_config.linux_fx_version = CONST_DOCKER_VERSION site_config.always_on = True app_insights_key = self.get_application_insights_key(group_name, service_plan.app_service_plan_name) if app_insights_key: site_config.app_settings.append( NameValuePair('APPINSIGHTS_INSTRUMENTATIONKEY', app_insights_key)) con_string = self.get_storage_connection_string(group_name, storage_account_name) site_config.app_settings.append(NameValuePair('AzureWebJobsStorage', con_string)) site_config.app_settings.append(NameValuePair('AzureWebJobsDashboard', con_string)) site_config.app_settings.append(NameValuePair('FUNCTIONS_EXTENSION_VERSION', CONST_FUNCTIONS_EXT_VERSION)) site_config.app_settings.append(NameValuePair('FUNCTIONS_WORKER_RUNTIME', 'python')) #: :type: azure.mgmt.web.WebSiteManagementClient web_client = self.local_session.client('azure.mgmt.web.WebSiteManagementClient') web_client.web_apps.create_or_update(group_name, app_name, functionapp_def).wait()
def _provision(self, params): site_config = SiteConfig(app_settings=[]) functionapp_def = Site(location=params['location'], site_config=site_config) functionapp_def.kind = 'functionapp,linux' functionapp_def.server_farm_id = params['app_service_plan_id'] site_config.linux_fx_version = CONST_DOCKER_VERSION site_config.always_on = True app_insights_key = params['app_insights_key'] if app_insights_key: site_config.app_settings.append( azure_name_value_pair('APPINSIGHTS_INSTRUMENTATIONKEY', app_insights_key)) con_string = params['storage_account_connection_string'] site_config.app_settings.append(azure_name_value_pair('AzureWebJobsStorage', con_string)) site_config.app_settings.append(azure_name_value_pair('AzureWebJobsDashboard', con_string)) site_config.app_settings.append(azure_name_value_pair('FUNCTIONS_EXTENSION_VERSION', CONST_FUNCTIONS_EXT_VERSION)) site_config.app_settings.append(azure_name_value_pair('FUNCTIONS_WORKER_RUNTIME', 'python')) site_config.app_settings.append( azure_name_value_pair('MACHINEKEY_DecryptionKey', FunctionAppDeploymentUnit.generate_machine_decryption_key())) return self.client.web_apps.create_or_update(params['resource_group_name'], params['name'], functionapp_def).result()
def _provision(self, params): site_config = SiteConfig(app_settings=[]) functionapp_def = Site(location=params['location'], site_config=site_config) functionapp_def.kind = 'functionapp,linux' functionapp_def.server_farm_id = params['app_service_plan_id'] site_config.linux_fx_version = FUNCTION_DOCKER_VERSION site_config.always_on = True app_insights_key = params['app_insights_key'] if app_insights_key: site_config.app_settings.append( azure_name_value_pair('APPINSIGHTS_INSTRUMENTATIONKEY', app_insights_key)) con_string = params['storage_account_connection_string'] site_config.app_settings.append(azure_name_value_pair('AzureWebJobsStorage', con_string)) site_config.app_settings.append(azure_name_value_pair('AzureWebJobsDashboard', con_string)) site_config.app_settings.append(azure_name_value_pair('FUNCTIONS_EXTENSION_VERSION', FUNCTION_EXT_VERSION)) site_config.app_settings.append(azure_name_value_pair('FUNCTIONS_WORKER_RUNTIME', 'python')) site_config.app_settings.append( azure_name_value_pair('MACHINEKEY_DecryptionKey', FunctionAppDeploymentUnit.generate_machine_decryption_key())) return self.client.web_apps.create_or_update(params['resource_group_name'], params['name'], functionapp_def).result()
def enable_local_git(resource_group, name, slot=None): client = web_client_factory() location = _get_location_from_webapp(client, resource_group, name) site_config = SiteConfig(location) site_config.scm_type = 'LocalGit' if slot is None: client.sites.create_or_update_site_config(resource_group, name, site_config) else: client.sites.create_or_update_site_config_slot(resource_group, name, site_config, slot) return {'url' : _get_git_url(client, resource_group, name, slot)}
def enable_local_git(resource_group_name, name, slot=None): client = web_client_factory() location = _get_location_from_webapp(client, resource_group_name, name) site_config = SiteConfig(location) site_config.scm_type = 'LocalGit' if slot is None: client.sites.create_or_update_site_config(resource_group_name, name, site_config) else: client.sites.create_or_update_site_config_slot(resource_group_name, name, site_config, slot) return {'url' : _get_local_git_url(client, resource_group_name, name, slot)}
def _provision(self, params): site_config = SiteConfig(app_settings=[]) functionapp_def = Site(https_only=True, client_cert_enabled=True, location=params['location'], site_config=site_config) # common function app settings functionapp_def.server_farm_id = params['app_service_plan_id'] functionapp_def.reserved = True # This implies Linux for auto-created app plans functionapp_def.identity = self._get_identity(params) # consumption app plan if params['is_consumption_plan']: functionapp_def.kind = 'functionapp,linux' site_config.linux_fx_version = FUNCTION_DOCKER_VERSION # dedicated app plan else: functionapp_def.kind = 'functionapp,linux,container' site_config.linux_fx_version = FUNCTION_DOCKER_VERSION site_config.always_on = True # application insights settings app_insights_key = params['app_insights_key'] if app_insights_key: site_config.app_settings.append( azure_name_value_pair('APPINSIGHTS_INSTRUMENTATIONKEY', app_insights_key)) # Don't generate pycache site_config.app_settings.append( azure_name_value_pair('PYTHONDONTWRITEBYTECODE', 1)) # Enable server side build site_config.app_settings.append( azure_name_value_pair('ENABLE_ORYX_BUILD', 'true')) site_config.app_settings.append( azure_name_value_pair('SCM_DO_BUILD_DURING_DEPLOYMENT', 'true')) # general app settings con_string = params['storage_account_connection_string'] site_config.app_settings.append( azure_name_value_pair('AzureWebJobsStorage', con_string)) site_config.app_settings.append( azure_name_value_pair('FUNCTIONS_EXTENSION_VERSION', FUNCTION_EXT_VERSION)) site_config.app_settings.append( azure_name_value_pair('FUNCTIONS_WORKER_RUNTIME', 'python')) return self.client.web_apps.begin_create_or_update( params['resource_group_name'], params['name'], functionapp_def).result()
def test_update_site_config(self, site_op_mock): site_config = SiteConfig('antarctica') site_op_mock.side_effect = [site_config, None] # action update_site_configs('myRG', 'myweb', java_version='1.8') # assert config_for_set = site_op_mock.call_args_list[1][0][4] self.assertEqual(config_for_set.java_version, '1.8') # point check some unrelated properties should stay at None self.assertEqual(config_for_set.use32_bit_worker_process, None) self.assertEqual(config_for_set.java_container, None)
def update(self, source_function_app): """Update the Site object if there are any changes""" source_app_settings = self.web_client.web_apps.list_application_settings( resource_group_name=self.resource_group, name=self.name) changed, target_app_settings = self.update_app_settings( source_app_settings.properties) source_function_app.site_config = SiteConfig( app_settings=target_app_settings, scm_type='LocalGit') return changed, source_function_app
def create_webapp_slot(resource_group_name, webapp, slot, configuration_source=None): client = web_client_factory() site = client.web_apps.get(resource_group_name, webapp) location = site.location slot_def = Site(server_farm_id=site.server_farm_id, location=location) clone_from_prod = None slot_def.site_config = SiteConfig(location) poller = client.web_apps.create_or_update_slot(resource_group_name, webapp, slot_def, slot) result = AppServiceLongRunningOperation()(poller) if configuration_source: clone_from_prod = configuration_source.lower() == webapp.lower() site_config = get_site_configs( resource_group_name, webapp, None if clone_from_prod else configuration_source) _generic_site_operation(resource_group_name, webapp, 'update_configuration', slot, site_config) # slot create doesn't clone over the app-settings and connection-strings, so we do it here # also make sure slot settings don't get propagated. if configuration_source: slot_cfg_names = client.web_apps.list_slot_configuration_names( resource_group_name, webapp) src_slot = None if clone_from_prod else configuration_source app_settings = _generic_site_operation(resource_group_name, webapp, 'list_application_settings', src_slot) for a in (slot_cfg_names.app_setting_names or []): app_settings.properties.pop(a, None) connection_strings = _generic_site_operation( resource_group_name, webapp, 'list_connection_strings', src_slot) for a in (slot_cfg_names.connection_string_names or []): connection_strings.properties.pop(a, None) _generic_site_operation(resource_group_name, webapp, 'update_application_settings', slot, app_settings) _generic_site_operation(resource_group_name, webapp, 'update_connection_strings', slot, connection_strings) result.name = result.name.split('/')[-1] return result
async def main(): log.info("Loading secrets") secrets = load_secrets() web_client, cosmosdb_client = setup_azure(secrets) db, keys, connection_string = setup_cosmosdb(cosmosdb_client, Azure.cosmosdb_name) configure_collections(Mongo.database_name, Mongo.collections, keys.primary_master_key, db.document_endpoint) app_env = deepcopy(secrets["appEnvironment"]) app_env["MajavashakkiMongoConnectionString"] = connection_string app_env["MajavaMongoPassword"] = keys.primary_master_key log.info("Creating App Service Plan") plan = web_client.app_service_plans.create_or_update( Azure.resource_group, Azure.plan_name, app_service_plan=AppServicePlan(Azure.location, Azure.plan_name, sku=SKU_B1_BASIC)).result() log.info("Creating Web App") env_pairs = [NameValuePair(k, v) for k, v in app_env.items()] site = web_client.web_apps.create_or_update( Azure.resource_group, Azure.site_name, Site(location=Azure.location, site_config=SiteConfig( app_settings=env_pairs, scm_type=ScmType.local_git, web_sockets_enabled=True, always_on=True, ))).result() log.info("Pushing code to App Service") pub_cred = web_client.web_apps.list_publishing_credentials( Azure.resource_group, Azure.site_name).result() git_url = mk_git_url(Azure.site_name, pub_cred) if "CI" in os.environ: await shell("git", "-c", "user.name='Majavashakki Deployer'", "-c", "user.email='*****@*****.**'", "commit", "--allow-empty", "-m", "Empty commit to force app service to redeploy") await shell("git", "push", "--force", git_url, "HEAD:master") log.info("Done")
def exec_module(self, **kwargs): for key in self.module_arg_spec: setattr(self, key, kwargs[key]) if self.app_settings is None: self.app_settings = dict() try: resource_group = self.rm_client.resource_groups.get( self.resource_group) except CloudError: self.fail('Unable to retrieve resource group') self.location = self.location or resource_group.location try: function_app = self.web_client.web_apps.get( resource_group_name=self.resource_group, name=self.name) exists = True except CloudError as exc: exists = False if self.state == 'absent': if exists: if self.check_mode: self.results['changed'] = True return self.results try: self.web_client.web_apps.delete( resource_group_name=self.resource_group, name=self.name) self.results['changed'] = True except CloudError as exc: self.fail( 'Failure while deleting web app: {0}'.format(exc)) else: self.results['changed'] = False else: if not exists: function_app = Site( location=self.location, kind='functionapp', site_config=SiteConfig( app_settings=self.aggregated_app_settings(), scm_type='LocalGit')) self.results['changed'] = True else: self.results['changed'], function_app = self.update( function_app) if self.check_mode: self.results['state'] = function_app.as_dict() elif self.results['changed']: try: new_function_app = self.web_client.web_apps.create_or_update( resource_group_name=self.resource_group, name=self.name, site_envelope=function_app).result() self.results['state'] = new_function_app.as_dict() except CloudError as exc: self.fail( 'Error creating or updating web app: {0}'.format(exc)) return self.results
from azure.common.client_factory import get_client_from_cli_profile RESOURCE_GROUP_NAME = 'sampleWebResourceGroup' SERVICE_PLAN_NAME = 'sampleFreePlanName' WEB_APP_NAME = 'sampleflaskapp123' REPO_URL = 'GitHub_Repository_URL' #log in web_client = get_client_from_cli_profile(WebSiteManagementClient) # get service plan id service_plan = web_client.app_service_plans.get(RESOURCE_GROUP_NAME, SERVICE_PLAN_NAME) # create a web app siteConfiguration = SiteConfig(python_version='3.4', ) site_async_operation = web_client.web_apps.create_or_update( RESOURCE_GROUP_NAME, WEB_APP_NAME, Site(location='eastus', server_farm_id=service_plan.id, site_config=siteConfiguration), ) site = site_async_operation.result() print('created webapp: ' + site.default_host_name) # continuous deployment with GitHub source_control_async_operation = web_client.web_apps.create_or_update_source_control( RESOURCE_GROUP_NAME, WEB_APP_NAME, SiteSourceControl(location='GitHub', repo_url=REPO_URL, branch='master'))
location=rg_location, reserved= True, # Without this set, it acts like a Windows App Service Plan sku=SkuDescription(name='P1v2', capacity=1, tier='Standard'))) service_plan = service_plan_async_operation.result() print("Service Plan ID: {}".format(service_plan.id)) # Create a Python Web App site_async_operation = web_client.web_apps.create_or_update( resource_group_name=rg_name, name=site_name, site_envelope=Site(location=rg_location, server_farm_id=service_plan.id, site_config=SiteConfig( python_version="3.7", linux_fx_version="PYTHON|3.7", app_command_line="startup.txt", scm_type="LocalGit"))) site = site_async_operation.result() print("Site ID: {}".format(site.id)) # Create an Azure Blob Storage Account storage_client = StorageManagementClient(credentials, subscription_id) storage_async_operation = storage_client.storage_accounts.create( rg_name, storage_account_name, StorageAccountCreateParameters(sku=StorageSku(name="standard_lrs"), kind="StorageV2", location=rg_location)) storage_account = storage_async_operation.result() storage_keys = storage_client.storage_accounts.list_keys(
def exec_module(self, **kwargs): for key in self.module_arg_spec: setattr(self, key, kwargs[key]) if self.app_settings is None: self.app_settings = dict() try: resource_group = self.rm_client.resource_groups.get(self.resource_group) except CloudError: self.fail('Unable to retrieve resource group') self.location = self.location or resource_group.location try: function_app = self.web_client.web_apps.get( resource_group_name=self.resource_group, name=self.name ) # Newer SDK versions (0.40.0+) seem to return None if it doesn't exist instead of raising CloudError exists = function_app is not None except CloudError as exc: exists = False if self.state == 'absent': if exists: if self.check_mode: self.results['changed'] = True return self.results try: self.web_client.web_apps.delete( resource_group_name=self.resource_group, name=self.name ) self.results['changed'] = True except CloudError as exc: self.fail('Failure while deleting web app: {0}'.format(exc)) else: self.results['changed'] = False else: kind = 'functionapp' linux_fx_version = None if self.container_settings and self.container_settings.get('name'): kind = 'functionapp,linux,container' linux_fx_version = 'DOCKER|' if self.container_settings.get('registry_server_url'): self.app_settings['DOCKER_REGISTRY_SERVER_URL'] = 'https://' + self.container_settings['registry_server_url'] linux_fx_version += self.container_settings['registry_server_url'] + '/' linux_fx_version += self.container_settings['name'] if self.container_settings.get('registry_server_user'): self.app_settings['DOCKER_REGISTRY_SERVER_USERNAME'] = self.container_settings.get('registry_server_user') if self.container_settings.get('registry_server_password'): self.app_settings['DOCKER_REGISTRY_SERVER_PASSWORD'] = self.container_settings.get('registry_server_password') if not self.plan and function_app: self.plan = function_app.server_farm_id if not exists: function_app = Site( location=self.location, kind=kind, site_config=SiteConfig( app_settings=self.aggregated_app_settings(), scm_type='LocalGit' ) ) self.results['changed'] = True else: self.results['changed'], function_app = self.update(function_app) # get app service plan if self.plan: if isinstance(self.plan, dict): self.plan = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Web/serverfarms/{2}".format( self.subscription_id, self.plan.get('resource_group', self.resource_group), self.plan.get('name') ) function_app.server_farm_id = self.plan # set linux fx version if linux_fx_version: function_app.site_config.linux_fx_version = linux_fx_version if self.check_mode: self.results['state'] = function_app.as_dict() elif self.results['changed']: try: new_function_app = self.web_client.web_apps.create_or_update( resource_group_name=self.resource_group, name=self.name, site_envelope=function_app ).result() self.results['state'] = new_function_app.as_dict() except CloudError as exc: self.fail('Error creating or updating web app: {0}'.format(exc)) return self.results