def get_container_registry_service_connection(organization, project): import subprocess import json subscription_id, subscription_name, tenant_id, _environment_name = get_default_subscription_info( ) logger.warning( "Using your default Azure subscription %s for fetching Azure Container Registries.", subscription_name) acr_list = subprocess.check_output('az acr list -o json', shell=True) acr_list = json.loads(acr_list) if acr_list: registry_choice = 0 registry_choice_list = [] for acr_clusters in acr_list: registry_choice_list.append(acr_clusters['name']) registry_choice = prompt_user_friendly_choice_list( "Which Azure Container Registry do you want to use for this pipeline?", registry_choice_list) selected_registry = acr_list[registry_choice] cix_client = get_new_cix_client(organization=organization) acr_connection_obj = get_container_registry_connection_create_object( subscription_id, subscription_name, tenant_id, selected_registry['id'], selected_registry['name'], selected_registry['loginServer']) acr_container_resource = cix_client.create_resources( creation_parameters=acr_connection_obj, project=project) poll_connection_ready( organization, project, acr_container_resource.resources['containerRegistryConnection'] ['Id']) return acr_container_resource.resources['containerRegistryConnection'] raise CLIError( 'There is no Azure container registry associated with your subscription. ' 'Create an ACR or switch to another subscription, ' 'verify with command \'az acr list\' and try again.')
def get_azure_rm_service_connection(organization, project): logger.debug('Create a new Azure Resource Manager service connection') subscription_id, subscription_name, tenant_id, environment_name = get_default_subscription_info( ) logger.warning( "Using your default Azure subscription %s for creating Azure RM connection.", subscription_name) cix_client = get_new_cix_client(organization=organization) azure_rm_connection_create_obj = get_azure_rm_connection_create_object( subscription_id, subscription_name, environment_name, tenant_id) azure_rm_connection = cix_client.create_resources( creation_parameters=azure_rm_connection_create_obj, project=project) azure_rm_connection_obj = azure_rm_connection.resources[ 'azureRmConnection'] poll_connection_ready(organization, project, azure_rm_connection_obj['Id']) return azure_rm_connection_obj
def get_kubernetes_environment_resource(organization, project, repo_name): logger.debug("Creating a new k8s environment resource.") import subprocess import json subscription_id, subscription_name, tenant_id, environment_name = get_default_subscription_info( ) logger.warning( "Using your default Azure subscription %s for fetching AKS clusters.", subscription_name) aks_list = subprocess.check_output('az aks list -o json', shell=True) aks_list = json.loads(aks_list) if aks_list: cluster_choice = 0 cluster_choice_list = [] for aks_clusters in aks_list: cluster_choice_list.append(aks_clusters['name']) cluster_choice = prompt_user_friendly_choice_list( "Which kubernetes cluster do you want to target for this pipeline?", cluster_choice_list) selected_cluster = aks_list[cluster_choice] create_namespace, namespace = get_kubernetes_namespace( organization, project, selected_cluster, subscription_id, subscription_name, tenant_id, environment_name) kubernetes_connection_obj = get_kubernetes_connection_create_object( subscription_id, subscription_name, selected_cluster['id'], selected_cluster['name'], selected_cluster['fqdn'], tenant_id, namespace, create_namespace, environment_name) cix_client = get_new_cix_client(organization=organization) kubernetes_connection = cix_client.create_resources( creation_parameters=kubernetes_connection_obj, project=project) k8s_connection_obj = kubernetes_connection.resources['k8sConnection'] poll_connection_ready(organization, project, k8s_connection_obj['Id']) kubernetes_env_obj = get_kubernetes_resource_create_object( k8s_connection_obj['Name'], selected_cluster['name'], repo_name, k8s_connection_obj['Id'], namespace) kubernetes_environment_resource = cix_client.create_resources( creation_parameters=kubernetes_env_obj, project=project) return kubernetes_environment_resource.resources['k8sResource'] raise CLIError( 'There are no AKS clusters under your subscription. ' 'Create the clusters or switch to another subscription, verify with ' 'command \'az aks list\' and try again.')
def pipeline_create(name, description=None, repository=None, branch=None, yml_path=None, repository_type=None, service_connection=None, organization=None, project=None, detect=None, queue_id=None, skip_first_run=None): """Create a new Azure Pipeline (YAML based) :param name: Name of the new pipeline :type name: str :param description: Description for the new pipeline :type description: str :param repository: Repository for which the pipeline needs to be configured. Can be clone url of the git repository or name of the repository for a Azure Repos or Owner/RepoName in case of GitHub repository. If omitted it will be auto-detected from the remote url of local git repository. If name is mentioned instead of url, --repository-type argument is also required. :type repository: str :param branch: Branch name for which the pipeline will be configured. If omitted, it will be auto-detected from local repository :type branch: str :param yml_path: Path of the pipelines yaml file in the repo (if yaml is already present in the repo). :type yml_path: str :param repository_type: Type of repository. If omitted, it will be auto-detected from remote url of local repository. 'tfsgit' for Azure Repos, 'github' for GitHub repository. :type repository_type: str :param service_connection: Id of the Service connection created for the repository for GitHub repository. Use command az devops service-endpoint -h for creating/listing service_connections. Not required for Azure Repos. :type service_connection: str :param queue_id: Id of the queue in the available agent pools. Will be auto detected if not specified. :type queue_id: str :param skip_first_run: Specify this flag to prevent the first run being triggered by the command. Command will return a pipeline if run is skipped else it will output a pipeline run. :type skip_first_run: bool """ repository_name = None if repository: organization, project = resolve_instance_and_project( detect=detect, organization=organization, project=project) else: organization, project, repository_name = resolve_instance_project_and_repo( detect=detect, organization=organization, project=project) # resolve repository if local repo for azure repo if repository_name: repository = repository_name repository_type = _AZURE_GIT_REPO_TYPE # resolve repository from local repo for github repo if not repository: repository = _get_repository_url_from_local_repo(detect=detect) if not repository: raise CLIError('The following arguments are required: --repository.') if not repository_type: repository_type = try_get_repository_type(repository) if not repository_type: raise CLIError( 'The following arguments are required: --repository-type. ' 'Check command help for valid values.') if not branch and should_detect(detect): branch = get_current_branch_name() if not branch: raise CLIError('The following arguments are required: --branch.') # repository, repository-type, branch should be set by now if not repository_name and is_valid_url(repository): repository_name = _get_repo_name_from_repo_url(repository) else: repository_name = repository # Validate name availability so user does not face name conflicts after going through the whole process if not validate_name_is_available(name, organization, project): raise CLIError( 'Pipeline with name {name} already exists.'.format(name=name)) # Parse repository information according to repository type repo_id = None api_url = None repository_url = None if repository_type.lower() == _GITHUB_REPO_TYPE: repo_id = repository_name repository_url = 'https://github.com/' + repository_name api_url = get_github_repos_api_url(repository_name) if repository_type.lower() == _AZURE_GIT_REPO_TYPE: repo_id = _get_repository_id_from_name(organization, project, repository_name) if not service_connection and repository_type != _AZURE_GIT_REPO_TYPE: service_connection = get_github_service_endpoint(organization, project) new_cix_client = get_new_cix_client(organization=organization) # No yml path => find or recommend yml scenario queue_branch = branch if not yml_path: yml_path, queue_branch = _create_and_get_yml_path( new_cix_client, repository_type, repo_id, repository_name, branch, service_connection, project, organization) if not queue_id: queue_id = _get_agent_queue_by_heuristic(organization=organization, project=project) if queue_id is None: logger.warning( 'Cannot find a hosted pool queue in the project. Provide a --queue-id in command params.' ) # Create build definition definition = _create_pipeline_build_object( name, description, repo_id, repository_name, repository_url, api_url, branch, service_connection, repository_type, yml_path, queue_id) client = get_new_pipeline_client(organization) created_definition = client.create_definition(definition=definition, project=project) logger.warning('Successfully created a pipeline with Name: %s, Id: %s.', created_definition.name, created_definition.id) if skip_first_run: return created_definition return client.queue_build(build=Build(definition=created_definition, source_branch=queue_branch), project=project)