|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +from pick import pick |
| 4 | +from wrappers.azure import AzureClient, AZD_ENVIRONMENT_NAME_RESOURCE_TAG |
| 5 | +from wrappers.github import GithubClient |
| 6 | + |
| 7 | +def prompt_user_for_target_clusters(clusters): |
| 8 | + """ |
| 9 | + Prompts the user to select a cluster from a list of clusters. |
| 10 | +
|
| 11 | + Args: |
| 12 | + clusters (List[ManagedCluster]): A list of AKS clusters. |
| 13 | +
|
| 14 | + Returns: |
| 15 | + ManagedCluster: The selected cluster. |
| 16 | + """ |
| 17 | + title = "Select a Big Bang cluster for your deployment environment" |
| 18 | + |
| 19 | + options = [f"Environment: {cluster.tags[AZD_ENVIRONMENT_NAME_RESOURCE_TAG]} \ |
| 20 | + Cluster: {cluster.name}" for cluster in clusters] |
| 21 | + |
| 22 | + _, index = pick(options, title) |
| 23 | + |
| 24 | + return clusters[index - 1] |
| 25 | + |
| 26 | +def set_azd_env_variable(name, value, export=False): |
| 27 | + """ |
| 28 | + Sets an Azure Developer CLI environment variable with the given name and value. |
| 29 | +
|
| 30 | + Args: |
| 31 | + name (str): The name of the environment variable to set. |
| 32 | + value (str): The value to set for the environment variable. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + None |
| 36 | + """ |
| 37 | + print(f"Setting {name} environment variable...") |
| 38 | + try: |
| 39 | + standard_out = subprocess.check_output(["azd", "env", "set", name, value],\ |
| 40 | + universal_newlines=True) |
| 41 | + print(standard_out) |
| 42 | + |
| 43 | + if export: |
| 44 | + os.environ[name] = value |
| 45 | + |
| 46 | + except subprocess.CalledProcessError as ex: |
| 47 | + print(f" Error: {ex.output}") |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + print("Pre-provisioning hook running...") |
| 51 | + |
| 52 | + # verify that the .azure folder exists |
| 53 | + azure_dir = os.path.join(os.getcwd(), ".azure") |
| 54 | + if not os.path.exists(azure_dir): |
| 55 | + raise ValueError("The .azure folder does not exist. Please run 'azd init' \ |
| 56 | + to setup your environment.") |
| 57 | + |
| 58 | + print("Getting active subscription ID...") |
| 59 | + client = AzureClient() |
| 60 | + subscription_id = client.get_active_subscription_id() |
| 61 | + |
| 62 | + if subscription_id is None: |
| 63 | + raise ValueError\ |
| 64 | + ("No active subscription found. Please run 'az login' to log in to Azure.") |
| 65 | + |
| 66 | + print(f"Active subscription ID: {subscription_id}") |
| 67 | + print("Getting AKS clusters...") |
| 68 | + clusters = client.get_aks_clusters(subscription_id) |
| 69 | + |
| 70 | + # Check if any clusters were found |
| 71 | + if len(clusters) == 0: |
| 72 | + raise ValueError(f"No AKS clusters found. Please create an AKS cluster and set the \ |
| 73 | + {AZD_ENVIRONMENT_NAME_RESOURCE_TAG} tag.") |
| 74 | + |
| 75 | + target_cluster = prompt_user_for_target_clusters(clusters) |
| 76 | + resource_group_name = target_cluster.id.split("/")[4] |
| 77 | + github_pat_token = client.get_gitops_repo_pat_token(\ |
| 78 | + target_cluster.tags[AZD_ENVIRONMENT_NAME_RESOURCE_TAG], \ |
| 79 | + subscription_id, resource_group_name) |
| 80 | + |
| 81 | + registry = client.get_container_registry(subscription_id, resource_group_name) |
| 82 | + |
| 83 | + if registry is None: |
| 84 | + raise ValueError("No container registry found. Please create a container registry \ |
| 85 | + and set the azd-container-registry-name tag.") |
| 86 | + |
| 87 | + if github_pat_token is not None: |
| 88 | + set_azd_env_variable("GITHUB_TOKEN", github_pat_token, True) |
| 89 | + |
| 90 | + set_azd_env_variable("AZURE_AKS_CLUSTER_NAME", target_cluster.name) |
| 91 | + set_azd_env_variable("AZURE_RESOURCE_GROUP", resource_group_name) |
| 92 | + set_azd_env_variable("AZURE_CONTAINER_REGISTRY_ENDPOINT", registry.login_server) |
| 93 | + set_azd_env_variable("GITOPS_REPO_RELEASE_BRANCH", \ |
| 94 | + target_cluster.tags["gitops-release-branch"], True) |
| 95 | + set_azd_env_variable("GITOPS_REPO", target_cluster.tags["gitops-repo"], True) |
| 96 | + |
| 97 | + print("Pre-provisioning hook complete.") |
0 commit comments