{
    "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/master/src/sql/media/microsoft-small-logo.png)\n",
                " \n",
                "## Deploy SQL Server 2019 Big Data Cluster on an existing cluster deployed using kubeadm\n",
                " \n",
                "This notebook walks through the process of deploying a <a href=\"https://docs.microsoft.com/sql/big-data-cluster/big-data-cluster-overview?view=sqlallproducts-allversions\">SQL Server 2019 Big Data Cluster</a> on an existing kubeadm cluster."
            ],
            "metadata": {
                "azdata_cell_guid": "23954d96-3932-4a8e-ab73-da605f99b1a4"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "import json,sys,os\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": "26fa8bc4-4b8e-4c31-ae11-50484821cea8",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 1
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Set variables**\n",
                "Generated by Azure Data Studio using the values collected in the Deploy Big Data Cluster wizard"
            ],
            "metadata": {
                "azdata_cell_guid": "e70640d0-6059-4cab-939e-e985a978c0da"
            }
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Set password**"
            ],
            "metadata": {
                "azdata_cell_guid": "7b383b0d-5687-45b3-a16f-ba3b170c796e"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "mssql_password = os.environ[\"AZDATA_NB_VAR_BDC_ADMIN_PASSWORD\"]\n",
                "if mssql_auth_mode == \"ad\":\n",
                "    mssql_domain_service_account_password = os.environ[\"AZDATA_NB_VAR_BDC_AD_DOMAIN_SVC_PASSWORD\"]"
            ],
            "metadata": {
                "azdata_cell_guid": "b5970f2b-cf13-41af-b0a2-5133d840325e",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 3
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Set and show current context**"
            ],
            "metadata": {
                "azdata_cell_guid": "6456bd0c-5b64-4d76-be59-e3a5b32697f5"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "run_command(f'kubectl config use-context {mssql_cluster_context}')\n",
                "run_command('kubectl config current-context')"
            ],
            "metadata": {
                "azdata_cell_guid": "a38f8b3a-f93a-484c-b9e2-4eba3ed99cc2",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 0
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Create deployment configuration files**"
            ],
            "metadata": {
                "azdata_cell_guid": "6d78da36-6af5-4309-baad-bc81bb2cdb7f"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "mssql_target_profile = 'ads-bdc-custom-profile'\n",
                "if not os.path.exists(mssql_target_profile):\n",
                "    os.mkdir(mssql_target_profile)\n",
                "bdcJsonObj = json.loads(bdc_json)\n",
                "controlJsonObj = json.loads(control_json)\n",
                "bdcJsonFile = open(f'{mssql_target_profile}/bdc.json', 'w')\n",
                "bdcJsonFile.write(json.dumps(bdcJsonObj, indent = 4))\n",
                "bdcJsonFile.close()\n",
                "controlJsonFile = open(f'{mssql_target_profile}/control.json', 'w')\n",
                "controlJsonFile.write(json.dumps(controlJsonObj, indent = 4))\n",
                "controlJsonFile.close()\n",
                "print(f'Created deployment configuration folder: {mssql_target_profile}')"
            ],
            "metadata": {
                "azdata_cell_guid": "3110ab23-ecfc-4e36-a1c5-28536b7edebf",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 6
        },
        {
            "cell_type": "markdown",
            "source": [
                "### **Create SQL Server 2019 Big Data Cluster**"
            ],
            "metadata": {
                "azdata_cell_guid": "7d56d262-8cd5-49e4-b745-332c6e7a3cb2"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "print (f'Creating SQL Server 2019 Big Data Cluster: {mssql_cluster_name} using configuration {mssql_target_profile}')\n",
                "os.environ[\"ACCEPT_EULA\"] = 'yes'\n",
                "os.environ[\"AZDATA_USERNAME\"] = mssql_username\n",
                "os.environ[\"AZDATA_PASSWORD\"] = mssql_password\n",
                "if mssql_auth_mode == \"ad\":\n",
                "    os.environ[\"DOMAIN_SERVICE_ACCOUNT_USERNAME\"] = mssql_domain_service_account_username\n",
                "    os.environ[\"DOMAIN_SERVICE_ACCOUNT_PASSWORD\"] = mssql_domain_service_account_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\\tkubectl get pods -n {mssql_cluster_name} ')\n",
                "run_command(f'azdata bdc create -c {mssql_target_profile}')"
            ],
            "metadata": {
                "azdata_cell_guid": "0a743e88-e7d0-4b41-b8a3-e43985d15f2b",
                "tags": [
                    "hide_input"
                ]
            },
            "outputs": [],
            "execution_count": 7
        }
    ]
}