diff --git a/g5k/01_remote_actions_and_variables.ipynb b/g5k/01_remote_actions_and_variables.ipynb index 64914ec3d2ecae341e0f8345f06d4671d80bc2a8..8dff07ec5562f03452ada47dd02422c3243331b9 100644 --- a/g5k/01_remote_actions_and_variables.ipynb +++ b/g5k/01_remote_actions_and_variables.ipynb @@ -23,7 +23,10 @@ "## Prerequisites\n", "\n", "<div class=\"alert alert-block alert-warning\">\n", - " Make sure you've run the one time setup for your environment\n", + " <ul>\n", + " <li>⚠️ Make sure you've run the one time setup for your environment</li>\n", + " <li>⚠️ Make sure you're running this notebook under the right kernel</li>\n", + " </ul>\n", "</div>\n" ] }, @@ -36,6 +39,9 @@ "source": [ "import enoslib as en\n", "\n", + "# Display some general information about the library\n", + "en.check()\n", + "\n", "# Enable rich logging\n", "_ = en.init_logging()" ] @@ -73,19 +79,15 @@ "metadata": {}, "outputs": [], "source": [ - "network = en.G5kNetworkConf(type=\"prod\", roles=[\"my_network\"], site=\"rennes\")\n", - "\n", "conf = (\n", " en.G5kConf.from_settings(job_type=[], job_name=\"rsd-01\")\n", - " .add_network_conf(network)\n", " .add_machine(\n", - " roles=[\"control\"], cluster=\"parasilo\", nodes=1, primary_network=network\n", + " roles=[\"control\"], cluster=\"parasilo\", nodes=1\n", " )\n", " .add_machine(\n", " roles=[\"compute\"],\n", " cluster=\"parasilo\",\n", - " nodes=1,\n", - " primary_network=network,\n", + " nodes=1\n", " )\n", " .finalize()\n", ")\n", @@ -228,6 +230,39 @@ "results" ] }, + { + "cell_type": "markdown", + "id": "c4102290-f5cd-4eb9-a6f4-00e1aed01baa", + "metadata": {}, + "source": [ + "<div class=\"alert alert-info\">\n", + " 💡 The `run_command` function is polymorphic in its `roles` parameter. You can pass a `Roles` object as return by `provider.init`, a list of `Host` or a single `Host`. Check its documentation using SHIFT+TAB for instance.\n", + "</div>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4ac51dc2-c096-46f2-bbaa-34087e111dff", + "metadata": {}, + "outputs": [], + "source": [ + "# a list of hosts\n", + "results = en.run_command(\"whoami\", roles=roles[\"control\"])\n", + "results" + ] + }, + { + "cell_type": "markdown", + "id": "dd0ae419-feeb-4b9a-97da-fe778ce2c3f6", + "metadata": {}, + "source": [ + "<div class=\"alert alert-info\">\n", + " 💡 The results is a list of result. Each result is an object with host, task, status, payload attributes.\n", + " You can filter the result given host, task and or status\n", + "</div>" + ] + }, { "cell_type": "code", "execution_count": null, @@ -235,8 +270,9 @@ "metadata": {}, "outputs": [], "source": [ - "one_result = results.filter(host=roles[\"control\"][0].alias)[0]\n", - "one_result" + "# filter by host\n", + "some_results = results.filter(host=roles[\"control\"][0].alias)\n", + "some_results" ] }, { @@ -246,6 +282,8 @@ "metadata": {}, "outputs": [], "source": [ + "# take the first result\n", + "one_result = some_results[0]\n", "one_result.payload[\"stdout\"]" ] }, @@ -254,7 +292,9 @@ "id": "hollow-bulgaria", "metadata": {}, "source": [ - "There are some specific shortcuts when the remote actions is a remote (shell) command: `.stdout`, `.stderr`, `.rc`" + "<div class=\"alert alert-info\">\n", + "💡 There are some specific shortcuts when the remote actions is a remote (shell) command: `.stdout`, `.stderr`, `.rc`\n", + "</div>" ] }, { @@ -272,8 +312,10 @@ "id": "operating-testimony", "metadata": {}, "source": [ - "By default the user is `root` (this is common to all EnOSlib's provider).\n", - "If you want to run command as your regular Grid'5000 user you can tell the command to `sudo` back to your regular user using `run_as` (the SSH login is still `root` though)" + "<div class=\"alert alert-info\">\n", + "💡 By default the user is `root` (this is common to all EnOSlib's provider).\n", + "If you want to run command as your regular Grid'5000 user you can tell the command to `sudo` back to your regular user using `run_as` (the SSH login is still `root` though)\n", + "</div>" ] }, { @@ -283,6 +325,7 @@ "metadata": {}, "outputs": [], "source": [ + "# get my username on g5k (this line is generic and useful if you share your code with someone else)\n", "my_g5k_login = en.g5k_api_utils.get_api_username()\n", "results = en.run_command(\"whoami\", roles=roles, run_as=my_g5k_login)\n", "results" @@ -420,14 +463,8 @@ "outputs": [], "source": [ "with en.actions(roles=roles) as a:\n", - " # installing the docker daemon\n", - " # prepending with a guard to make the command idempotent\n", - " a.shell(\"which docker || /grid5000/code/bin/g5k-setup-docker\")\n", - " # install the python docker binding on the remote host\n", - " # mandatory by the docker_container module\n", - " a.pip(name=\"docker\", state=\"present\")\n", - " # fire up a container (forward port 80 at the host level)\n", - " a.docker_container(name=\"myserver\", image=\"nginx\", state=\"started\", ports=[\"80:80\"])\n", + " # install nginx on the remote machines\n", + " a.apt(name=\"nginx\", state=\"present\")\n", " # wait for the connection on the port 80 to be ready\n", " a.wait_for(port=80, state=\"started\")\n", " # keep track of the result of each modules\n", @@ -442,7 +479,20 @@ "metadata": {}, "outputs": [], "source": [ - "results.filter(task=\"docker_container\")[0]" + "results.filter(task=\"apt\")[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a44e9b11-dcb2-4eda-a8bd-6e6a851fc03e", + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "from IPython.display import HTML\n", + "response = requests.get(\"http://parasilo-4:80\")\n", + "HTML(response.text)\n" ] }, { @@ -665,6 +715,14 @@ "source": [ "provider.destroy()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "961ea738-c4f5-42f0-a061-0eca5e606b65", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/setup_for_use_in_g5k.ipynb b/setup_for_use_in_g5k.ipynb index fef4c0870e828efe71d856862711fae60d860e25..cf04dc152c725227dfc9350328ebeef7c2e72c1c 100644 --- a/setup_for_use_in_g5k.ipynb +++ b/setup_for_use_in_g5k.ipynb @@ -68,6 +68,24 @@ "python3 -m ipykernel install --user --name my_venv" ] }, + { + "cell_type": "markdown", + "id": "11e5fb48-fd0d-4fa1-8b5a-5e9f892e9d8a", + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, + "source": [ + "## Activate this environment\n", + "\n", + "<div class=\"alert alert-warning\"> \n", + "Goal: Make sure we're using the library installed in the previous virtual environment.\n", + "\n", + "- Refresh the browser\n", + "- Switch the kernel to `my_venv`: `kernel > Change kernel` and select `my_venv`\n", + "- Re-execute this notebook\n", + "</div>" + ] + }, { "cell_type": "markdown", "id": "7d60c757-118c-44e3-8374-051d2e2c2bcd", @@ -116,6 +134,14 @@ "\n", "en.check()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9c6b846d-d580-41c5-bdde-eda9f159ff48", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {