{
    "metadata": {
        "kernelspec": {
            "name": "python3",
            "display_name": "Python 3"
        },
        "language_info": {
            "name": "python",
            "version": "3.6.6",
            "mimetype": "text/x-python",
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "pygments_lexer": "ipython3",
            "nbconvert_exporter": "python",
            "file_extension": ".py"
        }
    },
    "nbformat_minor": 2,
    "nbformat": 4,
    "cells": [
        {
            "cell_type": "markdown",
            "source": [
                "![Microsoft](https://raw.githubusercontent.com/microsoft/azuredatastudio/main/extensions/arc/images/microsoft-small-logo.png)\n",
                " \n",
                "## Deploy Azure Arc Data Controller on an existing cluster\n",
                " \n",
                "This notebook walks through the process of deploying a <a href=\"https://azure.microsoft.com/services/azure-arc/\">Azure Arc controller</a> on an existing cluster.\n",
                " \n",
                "* Follow the instructions in the **Prerequisites** cell to install the tools if not already installed.\n",
                "* The **Required information** will check and prompt you for password if it is not set in the environment variable. The password can be used to access the data controller.\n",
                "\n",
                "<span style=\"color:red\"><font size=\"3\">Please press the \"Run All\" button to run the notebook</font></span>"
            ],
            "metadata": {
                "azdata_cell_guid": "82e60c1a-7acf-47ee-877f-9e85e92e11da"
            }
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Prerequisites** \n",
                "Ensure the following tools are installed and added to PATH before proceeding.\n",
                " \n",
                "|Tools|Description|Installation|\n",
                "|---|---|---|\n",
                "|kubectl | Command-line tool for monitoring the underlying Kubernetes cluster | [Installation](https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-binary-using-native-package-management) |\n",
                "|azdata | Command-line tool for installing and managing resources in an Azure Arc cluster |[Installation](https://github.com/microsoft/Azure-data-services-on-Azure-Arc/blob/master/scenarios/001-install-client-tools.md) |"
            ],
            "metadata": {
                "azdata_cell_guid": "714582b9-10ee-409e-ab12-15a4825c9471"
            }
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Setup**"
            ],
            "metadata": {
                "azdata_cell_guid": "e3dd8e75-e15f-44b4-81fc-1f54d6f0b1e2"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "import pandas,sys,os,json,html,getpass,time, tempfile\n",
                "pandas_version = pandas.__version__.split('.')\n",
                "pandas_major = int(pandas_version[0])\n",
                "pandas_minor = int(pandas_version[1])\n",
                "pandas_patch = int(pandas_version[2])\n",
                "if not (pandas_major > 0 or (pandas_major == 0 and pandas_minor > 24) or (pandas_major == 0 and pandas_minor == 24 and pandas_patch >= 2)):\n",
                "    sys.exit('Please upgrade the Notebook dependency before you can proceed, you can do it by running the \"Reinstall Notebook dependencies\" command in command palette (View menu -> Command Palette…).')\n",
                "def run_command(command):\n",
                "    print(\"Executing: \" + command)\n",
                "    !{command}\n",
                "    if _exit_code != 0:\n",
                "        sys.exit(f'Command execution failed with exit code: {str(_exit_code)}.\\n\\t{command}\\n')\n",
                "    print(f'Successfully executed: {command}')"
            ],
            "metadata": {
                "azdata_cell_guid": "d973d5b4-7f0a-4a9d-b204-a16480f3940d",
                "tags": []
            },
            "outputs": [],
            "execution_count": null
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Set variables**\n",
                "Generated by Azure Data Studio using the values collected in the Azure Arc Data controller create wizard"
            ],
            "metadata": {
                "azdata_cell_guid": "4b266b2d-bd1b-4565-92c9-3fc146cdce6d"
            }
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Check dependencies**"
            ],
            "metadata": {
                "azdata_cell_guid": "2544648b-59c9-4ce5-a3b6-87086e214d4c"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "run_command('azdata --version')"
            ],
            "metadata": {
                "azdata_cell_guid": "691671d7-3f05-406c-a183-4cff7d17f83d",
                "tags": []
            },
            "outputs": [],
            "execution_count": null
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Required information**"
            ],
            "metadata": {
                "azdata_cell_guid": "0bb02e76-fee8-4dbc-a75b-d5b9d1b187d0"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "if \"AZDATA_NB_VAR_ARC_DOCKER_PASSWORD\" in os.environ:\n",
                "    arc_docker_password = os.environ[\"AZDATA_NB_VAR_ARC_DOCKER_PASSWORD\"]\n",
                "if \"AZDATA_NB_VAR_ARC_ADMIN_PASSWORD\" in os.environ:\n",
                "    arc_admin_password = os.environ[\"AZDATA_NB_VAR_ARC_ADMIN_PASSWORD\"]\n",
                "else:\n",
                "    if arc_admin_password == \"\":\n",
                "        arc_admin_password = getpass.getpass(prompt = 'Azure Arc Data controller password')\n",
                "        if arc_admin_password == \"\":\n",
                "            sys.exit(f'Password is required.')\n",
                "        confirm_password = getpass.getpass(prompt = 'Confirm password')\n",
                "        if arc_admin_password != confirm_password:\n",
                "            sys.exit(f'Passwords do not match.')"
            ],
            "metadata": {
                "azdata_cell_guid": "e7e10828-6cae-45af-8c2f-1484b6d4f9ac",
                "tags": []
            },
            "outputs": [],
            "execution_count": null
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Set and show current context**"
            ],
            "metadata": {
                "azdata_cell_guid": "127c8042-181f-4862-a390-96e59c181d09"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "os.environ[\"KUBECONFIG\"] = arc_config_file\n",
                "run_command(f'kubectl config use-context {arc_cluster_context}')\n",
                "run_command('kubectl config current-context')"
            ],
            "metadata": {
                "azdata_cell_guid": "7d1a03d4-1df8-48eb-bff0-0042603b95b1",
                "tags": []
            },
            "outputs": [],
            "execution_count": null
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Create Azure Arc Data controller**"
            ],
            "metadata": {
                "azdata_cell_guid": "efe78cd3-ed73-4c9b-b586-fdd6c07dd37f"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "print (f'Creating Azure Arc controller: {arc_data_controller_name} using configuration {arc_cluster_context}')\n",
                "os.environ[\"ACCEPT_EULA\"] = 'yes'\n",
                "os.environ[\"AZDATA_USERNAME\"] = arc_admin_username\n",
                "os.environ[\"AZDATA_PASSWORD\"] = arc_admin_password\n",
                "os.environ[\"DOCKER_USERNAME\"] = arc_docker_username\n",
                "os.environ[\"DOCKER_PASSWORD\"] = arc_docker_password\n",
                "if os.name == 'nt':\n",
                "    print(f'If you don\\'t see output produced by azdata, you can run the following command in a terminal window to check the deployment status:\\n\\t {os.environ[\"AZDATA_NB_VAR_KUBECTL\"]} get pods -A')\n",
                "run_command(f'azdata arc dc create --connectivity-mode {arc_data_controller_connectivity_mode} -n {arc_data_controller_name} -ns {arc_data_controller_namespace} -s {arc_subscription} -g {arc_resource_group} -l {arc_data_controller_location} -p {arc_profile}')\n",
                "print(f'Azure Arc Data controller cluster: {arc_data_controller_name} created.') "
            ],
            "metadata": {
                "azdata_cell_guid": "373947a1-90b9-49ee-86f4-17a4c7d4ca76",
                "tags": []
            },
            "outputs": [],
            "execution_count": null
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Setting context to created Azure Arc Data controller**"
            ],
            "metadata": {
                "azdata_cell_guid": "a3ddc701-811d-4058-b3fb-b7295fcf50ae"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "# Setting context to data controller.\n",
                "#\n",
                "run_command(f'kubectl config set-context --current --namespace {arc_data_controller_namespace}')"
            ],
            "metadata": {
                "azdata_cell_guid": "c974561f-13d0-4e7a-b74b-d781c2e06d68"
            },
            "outputs": [],
            "execution_count": null
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Login to the data controller.**\n"
            ],
            "metadata": {
                "azdata_cell_guid": "9376b2ab-0edf-478f-9e3c-5ff46ae3501a"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "# Login to the data controller.\n",
                "#\n",
                "run_command(f'azdata login -n {arc_data_controller_namespace}')"
            ],
            "metadata": {
                "azdata_cell_guid": "9aed0c5a-2c8a-4ad7-becb-60281923a196"
            },
            "outputs": [],
            "execution_count": null
        }
    ]
}