Skip to content

Commit

Permalink
Added an SSH on CHI@Edge notebook tutorial for trovi
Browse files Browse the repository at this point in the history
  • Loading branch information
Soufiane Jounaid committed May 1, 2024
1 parent 6ba9289 commit 061613f
Showing 1 changed file with 274 additions and 0 deletions.
274 changes: 274 additions & 0 deletions SSH_on_chiedge_tutorial.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e9168a8a-3f37-4915-8ef8-0b618572b214",
"metadata": {},
"source": [
"# SSH on CHI@Edge containers"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "357430e8-9faf-41e8-8f1b-0d49ad88cc13",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Now using CHI@Edge:\n",
"URL: https://chi.edge.chameleoncloud.org\n",
"Location: University of Chicago, Chicago, Illinois, USA\n",
"Support contact: help@chameleoncloud.org\n"
]
}
],
"source": [
"import chi\n",
"# Before we go any further, we need to select which Chameleon site we will be using.\n",
"chi.use_site(\"CHI@Edge\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8b4b25e2-0ad4-4a27-a339-f4f543ff59d1",
"metadata": {},
"outputs": [],
"source": [
"chi.set(\"project_name\", \"Chameleon\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6dfc1187-6152-4e4e-bb8b-01b66396d697",
"metadata": {},
"outputs": [],
"source": [
"from chi import container\n",
"from chi import lease"
]
},
{
"cell_type": "markdown",
"id": "6718083a-1db3-4350-9baa-bb044eb0dfba",
"metadata": {},
"source": [
"## Creating a 1-day lease\n",
"\n",
"We are creating a short 1 day lease for a raspberry pi 4 device "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a6e10772-4470-473b-983b-0b9a4db1ec34",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"created lease with name ssh-raspberrypi4-64-2024-05-01 15:05 and uuid 205c57ee-8451-428d-aaa1-0f30bc917ffe, waiting for it to start. This can take up to 60s.\n",
"Done!\n"
]
}
],
"source": [
"# machine name refers to the \"type\" of device\n",
"machine_name = \"raspberrypi4-64\"\n",
"\n",
"# get dates for lease start and end\n",
"start, end = lease.lease_duration(days=1)\n",
"\n",
"# make a unique name for the lease\n",
"lease_name = f\"ssh-{machine_name}-{start}\"\n",
"\n",
"reservations = []\n",
"lease.add_device_reservation(reservations, count=2, machine_name=machine_name)\n",
"container_lease = lease.create_lease(lease_name, reservations)\n",
"lease_id = container_lease[\"id\"]\n",
"\n",
"print(f\"created lease with name {lease_name} and uuid {lease_id}, waiting for it to start. This can take up to 60s.\")\n",
"lease.wait_for_active(lease_id)\n",
"print(\"Done!\")"
]
},
{
"cell_type": "markdown",
"id": "07a9828f-f489-41e7-a8b3-afece9f8e758",
"metadata": {},
"source": [
"## Starting the container\n",
"\n",
"The image we are using is a base [ubuntu 22.04 image](https://github.com/ChameleonCloud/edge_ssh_image) where we install a variety of utilities and apply some modifications to the sshd config before starting the sshd daemon. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1386a403-0d5d-49e7-9679-69d571f053be",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requesting container ... This may take a while as the large container image is being downloaded\n",
"Successfully created container: tutorial-raspberrypi4-64-ssh!\n"
]
}
],
"source": [
"print(\"Requesting container ... This may take a while as the large container image is being downloaded\")\n",
"\n",
"# Set a name for the container. Because CHI@Edge uses Kubernetes, ensure that underscores aren't in the name\n",
"container_name = f\"tutorial-{machine_name}-ssh\".replace(\"_\",\"-\")\n",
"\n",
"try:\n",
" my_container = container.create_container(\n",
" container_name, \n",
" image=\"ghcr.io/chameleoncloud/edge_ssh_image:latest\",\n",
" workdir=\"/home\",\n",
" exposed_ports=[22],\n",
" reservation_id=lease.get_device_reservation(lease_id),\n",
" platform_version=2,\n",
" )\n",
"except RuntimeError as ex:\n",
" print(ex)\n",
" print(f\"please stop and/or delete {container_name} and try again\")\n",
"else:\n",
" print(f\"Successfully created container: {container_name}!\")"
]
},
{
"cell_type": "markdown",
"id": "398f7c93-17ba-4df6-9506-1d8d070832c0",
"metadata": {},
"source": [
"## Adding your public key to the authorized keys within the container\n",
"\n",
"**Important** Please first place your RSA public key in one line in the 'edge_user_public_key' text file. The following cells will then upload the file to the running container, and copy over the key to the authorized keys file after setting the appropriate permissions."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "524eef57-67c6-453f-940f-393e064e41c9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(<Response [200]>, None)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chi.container.upload(my_container.uuid, \"./edge_user_public_key\", \"/root/.ssh/\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "768675d9-32d8-4880-95cf-f622f2e275fb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'output': '', 'exit_code': 0, 'exec_id': None, 'proxy_url': None}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cmd = \"/bin/bash -c \\\"chmod 600 /root/.ssh/authorized_keys\\\"\"\n",
"chi.container.execute(my_container.uuid, cmd)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a2670613-aaa9-48e3-bb6c-e874ef446d26",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'output': '', 'exit_code': 0, 'exec_id': None, 'proxy_url': None}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cmd = \"/bin/bash -c \\\"cat /root/.ssh/edge_user_public_key >> /root/.ssh/authorized_keys\\\"\"\n",
"chi.container.execute(my_container.uuid, cmd)"
]
},
{
"cell_type": "markdown",
"id": "2463df02-ba2d-474c-a3f1-c5f3cd369525",
"metadata": {},
"source": [
"## SSH'ing into the container\n",
"\n",
"if ssh'ing into the device hangs forever when connecting to device part, please release the floating IP via the chi@edge openstack dashboard and rerun the cell below"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "a3f83a2a-b64e-456e-b5d0-5a4271d39c55",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"use the following command to ssh into the container: ssh root@129.114.34.187\n"
]
}
],
"source": [
"ip_address = chi.container.associate_floating_ip(my_container.uuid)\n",
"\n",
"print(\"use the following command to ssh into the container: ssh root@\" + ip_address)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 061613f

Please sign in to comment.