Skip to content

Instantly share code, notes, and snippets.

@Udara-Dananjaya
Last active August 15, 2023 09:24
Show Gist options
  • Save Udara-Dananjaya/7387172664a5fdc6f38a40aec2c18382 to your computer and use it in GitHub Desktop.
Save Udara-Dananjaya/7387172664a5fdc6f38a40aec2c18382 to your computer and use it in GitHub Desktop.
Automate Colab setup: create users, install RDP, mount Google Drive, set up SSH. Utilize GPU and resources efficiently.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/Udara-Dananjaya/7387172664a5fdc6f38a40aec2c18382\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iLh_9SkSut4u"
},
"source": [
"# **Colab RDP** : Remote Desktop to Colab Instance\n",
"\n",
"> **Warning : Not for Cryptocurrency Mining<br></br>** \n",
">**Why are hardware resources such as T4 GPUs not available to me?** The best available hardware is prioritized for users who use Colaboratory interactively rather than for long-running computations. Users who use Colaboratory for long-running computations may be temporarily restricted in the type of hardware made available to them, and/or the duration that the hardware can be used for. We encourage users with high computational needs to use Colaboratory’s UI with a local runtime. Please note that using Colaboratory for cryptocurrency mining is disallowed entirely, and may result in being banned from using Colab altogether.\n",
"\n",
"Google Colab can give you Instance with 12GB of RAM and GPU for 12 hours (Max.) for Free users. Anyone can use it to perform Heavy Tasks.\n",
"\n",
"To use other similiar Notebooks use my Repository **[Colab Hacks](https://github.com/PradyumnaKrishna/Colab-Hacks)**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "t4yNp3KmLtZ6",
"outputId": "5d6a5480-7ca4-4c59-deae-8fcbd8c715aa"
},
"outputs": [],
"source": [
"#@title **Create User**\n",
"#@markdown Enter Username and Password\n",
"\n",
"import os\n",
"\n",
"username = \"user\" #@param {type:\"string\"}\n",
"password = \"root\" #@param {type:\"string\"}\n",
"\n",
"print(\"Creating User and Setting it up\")\n",
"\n",
"# Creation of user\n",
"os.system(f\"useradd -m {username}\")\n",
"\n",
"# Add user to sudo group\n",
"os.system(f\"adduser {username} sudo\")\n",
" \n",
"# Set password of user to 'root'\n",
"os.system(f\"echo '{username}:{password}' | sudo chpasswd\")\n",
"\n",
"# Change default shell from sh to bash\n",
"os.system(\"sed -i 's/\\/bin\\/sh/\\/bin\\/bash/g' /etc/passwd\")\n",
"\n",
"print(\"User Created and Configured\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Q6bl1b0EifVG",
"outputId": "dcc60306-bb4c-4cc6-8efd-2dce481103ec"
},
"outputs": [],
"source": [
"#@title **RDP**\n",
"#@markdown It takes 4-5 minutes for installation\n",
"\n",
"import os\n",
"import subprocess\n",
"\n",
"#@markdown Visit http://remotedesktop.google.com/headless and Copy the command after authentication\n",
"\n",
"CRP = \"DISPLAY= /opt/google/chrome-remote-desktop/start-host --code=\\\"4/0AX4XfWjzDxEc7jsUUHZq7ibuGDyEheVYrIa1bi7H3HNBVsLTY_MpNDs7Fgwc5ysg7Vhe3A\\\" --redirect-url=\\\"https://remotedesktop.google.com/_/oauthredirect\\\" --name=$(hostname)\" #@param {type:\"string\"}\n",
"\n",
"#@markdown Enter a pin more or equal to 6 digits\n",
"Pin = 123456 #@param {type: \"integer\"}\n",
"\n",
"\n",
"class CRD:\n",
" def __init__(self):\n",
" os.system(\"apt update\")\n",
" self.installCRD()\n",
" self.installDesktopEnvironment()\n",
" self.installGoogleChorme()\n",
" self.finish()\n",
"\n",
" @staticmethod\n",
" def installCRD():\n",
" print(\"Installing Chrome Remote Desktop\")\n",
" subprocess.run(['wget', 'https://dl.google.com/linux/direct/chrome-remote-desktop_current_amd64.deb'], stdout=subprocess.PIPE)\n",
" subprocess.run(['dpkg', '--install', 'chrome-remote-desktop_current_amd64.deb'], stdout=subprocess.PIPE)\n",
" subprocess.run(['apt', 'install', '--assume-yes', '--fix-broken'], stdout=subprocess.PIPE)\n",
"\n",
" @staticmethod\n",
" def installDesktopEnvironment():\n",
" print(\"Installing Desktop Environment\")\n",
" os.system(\"export DEBIAN_FRONTEND=noninteractive\")\n",
" os.system(\"apt install --assume-yes xfce4 desktop-base xfce4-terminal\")\n",
" os.system(\"bash -c 'echo \\\"exec /etc/X11/Xsession /usr/bin/xfce4-session\\\" > /etc/chrome-remote-desktop-session'\")\n",
" os.system(\"apt remove --assume-yes gnome-terminal\")\n",
" os.system(\"apt install --assume-yes xscreensaver\")\n",
" os.system(\"systemctl disable lightdm.service\")\n",
"\n",
" @staticmethod\n",
" def installGoogleChorme():\n",
" print(\"Installing Google Chrome\")\n",
" subprocess.run([\"wget\", \"https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb\"], stdout=subprocess.PIPE)\n",
" subprocess.run([\"dpkg\", \"--install\", \"google-chrome-stable_current_amd64.deb\"], stdout=subprocess.PIPE)\n",
" subprocess.run(['apt', 'install', '--assume-yes', '--fix-broken'], stdout=subprocess.PIPE)\n",
"\n",
" @staticmethod\n",
" def finish():\n",
" print(\"Finalizing\")\n",
" os.system(f\"adduser {username} chrome-remote-desktop\")\n",
" command = f\"{CRP} --pin={Pin}\"\n",
" os.system(f\"su - {username} -c '{command}'\")\n",
" os.system(\"service chrome-remote-desktop start\")\n",
" print(\"Finished Succesfully\")\n",
"\n",
"\n",
"try:\n",
" if username:\n",
" if CRP == \"\":\n",
" print(\"Please enter authcode from the given link\")\n",
" elif len(str(Pin)) < 6:\n",
" print(\"Enter a pin more or equal to 6 digits\")\n",
" else:\n",
" CRD()\n",
"except NameError as e:\n",
" print(\"username variable not found\")\n",
" print(\"Create a User First\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "vk2qtOTGIFsQ"
},
"outputs": [],
"source": [
"#@title **Google Drive Mount**\n",
"#@markdown Google Drive used as Persistance HDD for files.<br>\n",
"#@markdown Mounted at `user` Home directory inside drive folder\n",
"#@markdown (If `username` variable not defined then use root as default).\n",
"\n",
"\n",
"def MountGDrive():\n",
" from os import environ as env\n",
" from google.colab import drive\n",
"\n",
" config = env['CLOUDSDK_CONFIG']\n",
" addr = env['TBE_CREDS_ADDR']\n",
"\n",
" ! runuser -l $user -c \"yes | python3 -m pip install --user google-colab\" > /dev/null 2>&1\n",
"\n",
" mount = \"\"\"from os import environ as env\n",
"from google.colab import drive\n",
"\n",
"env['CLOUDSDK_CONFIG'] = '{config}'\n",
"env['TBE_CREDS_ADDR'] = '{addr}'\n",
"\n",
"drive.mount('{mountpoint}')\"\"\".format(config=config, addr=addr, mountpoint=mountpoint)\n",
"\n",
" with open('/content/mount.py', 'w') as script:\n",
" script.write(mount)\n",
"\n",
" ! runuser -l $user -c \"python3 /content/mount.py\"\n",
"\n",
"try:\n",
" mountpoint = f\"/home/{username}/drive\"\n",
" user = username\n",
"except NameError:\n",
" print(\"username variable not found, mounting at `/content/drive' using `root'\")\n",
" mountpoint = '/content/drive'\n",
" user = 'root'\n",
"\n",
"MountGDrive()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "8icuQYnyKDLk"
},
"outputs": [],
"source": [
"#@title **SSH**\n",
"\n",
"! pip install colab_ssh --upgrade &> /dev/null\n",
"\n",
"Ngrok = False #@param {type:'boolean'}\n",
"Agro = False #@param {type:'boolean'}\n",
"\n",
"\n",
"#@markdown Copy authtoken from https://dashboard.ngrok.com/auth (only for ngrok)\n",
"ngrokToken = \"\" #@param {type:'string'}\n",
"\n",
"\n",
"def runNGROK():\n",
" from colab_ssh import launch_ssh\n",
" from IPython.display import clear_output\n",
" launch_ssh(ngrokToken, password)\n",
" clear_output()\n",
"\n",
" print(\"ssh\", username, end='@')\n",
" ! curl -s http://localhost:4040/api/tunnels | python3 -c \\\n",
" \"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'][6:].replace(':', ' -p '))\"\n",
"\n",
"\n",
"def runAgro():\n",
" from colab_ssh import launch_ssh_cloudflared\n",
" launch_ssh_cloudflared(password=password)\n",
"\n",
"\n",
"try:\n",
" if username:\n",
" pass\n",
" elif password:\n",
" pass\n",
"except NameError:\n",
" print(\"No user found using username and password as 'root'\")\n",
" username='root'\n",
" password='root'\n",
"\n",
"\n",
"if Agro and Ngrok:\n",
" print(\"You can't do that\")\n",
" print(\"Select only one of them\")\n",
"elif Agro:\n",
" runAgro()\n",
"elif Ngrok:\n",
" if ngrokToken == \"\":\n",
" print(\"No ngrokToken Found, Please enter it\")\n",
" else:\n",
" runNGROK()\n",
"else:\n",
" print(\"Select one of them\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "UoeBdz6_KE6a"
},
"outputs": [],
"source": [
"#@title **Colab Shutdown**\n",
"\n",
"#@markdown To Kill NGROK Tunnel\n",
"NGROK = False #@param {type:'boolean'}\n",
"\n",
"#@markdown To Unmount GDrive\n",
"GDrive = False #@param {type:'boolean'}\n",
"\n",
"#@markdown To Sleep Colab\n",
"Sleep = True #@param {type:'boolean'}\n",
"\n",
"if NGROK:\n",
" ! killall ngrok\n",
"\n",
"if GDrive:\n",
" with open('/content/unmount.py', 'w') as unmount:\n",
" unmount.write(\"\"\"from google.colab import drive\n",
"drive.flush_and_unmount()\"\"\")\n",
" \n",
" try:\n",
" if user:\n",
" ! runuser $user -c 'python3 /content/unmount.py'\n",
" except NameError:\n",
" print(\"Google Drive not Mounted\")\n",
"\n",
"if Sleep:\n",
" from time import sleep\n",
" sleep(43200)"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"include_colab_link": true,
"name": "Colab RDP.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment