From 0d114c5bbc70954d7a12991baf1115064b69ab2b Mon Sep 17 00:00:00 2001 From: Timothy Middelkoop Date: Thu, 3 Feb 2022 20:19:52 +0000 Subject: [PATCH] GCP: Simpler use of environment variables. --- content/GCP/02_intro_to_compute.ipynb | 2 +- content/GCP/04_intro_to_cli.ipynb | 149 +++++++++++++------------- 2 files changed, 73 insertions(+), 78 deletions(-) diff --git a/content/GCP/02_intro_to_compute.ipynb b/content/GCP/02_intro_to_compute.ipynb index 7097fbc..fda48cd 100644 --- a/content/GCP/02_intro_to_compute.ipynb +++ b/content/GCP/02_intro_to_compute.ipynb @@ -309,7 +309,7 @@ "Did you \"Follow\" the VM instance by looking at the **activity** page as discussed above?\n", "\n", "Since we care about paying for resources we are not using we review our project by visiting the *compute engine* service and reviewing that we no longer have any *VM instances* running. \n", - " * Go to **Navigation Menu** -> **Compute Engine** (under Compute) -> **Instances** and look for your virtual machine.\n", + " * Go to **Navigation Menu** -> **Compute Engine** (under Compute) -> **Instances** and look for your virtual machine. It should be gone!\n", "\n", "In a later episode we will show how to easily list all the resources in a project." ] diff --git a/content/GCP/04_intro_to_cli.ipynb b/content/GCP/04_intro_to_cli.ipynb index 9136726..6ffab2b 100644 --- a/content/GCP/04_intro_to_cli.ipynb +++ b/content/GCP/04_intro_to_cli.ipynb @@ -62,8 +62,11 @@ "id": "8eb44568-1108-4963-aebf-7f6b98ddaffc", "metadata": {}, "source": [ - "### Advanced call-out\n", - "You can access the cloud shell from your laptop via `gcloud cloud-shell ssh` if you have the [Google Cloud SDK installed](https://cloud.google.com/sdk/docs/install). Installing the Google Cloud SDK will also allow you to run `gcloud` and `gsutil` commands directly from your laptop, workstation, or cluster. *Pro-tip, you cannot use a service account to access the cloud shell.*" + "```{admonition} Tip\n", + ":class: tip\n", + "\n", + "You can access the cloud shell from your laptop via `gcloud cloud-shell ssh` if you have the [Google Cloud SDK installed](https://cloud.google.com/sdk/docs/install). Installing the Google Cloud SDK will also allow you to run `gcloud` and `gsutil` commands directly from your laptop, workstation, or cluster. *Pro-tip, you cannot use a service account to access the cloud shell.*\n", + "```" ] }, { @@ -236,12 +239,18 @@ "\n", "*To make the point even clearer, we use environment variables in the Cloud Essentials notebooks instead of hard coding them, which makes it easy for anyone to run, use, and edit and thus collaborate and contribute to them!*\n", "\n", - "We now demonstrate the basic use environment variables. As a reminder, environment variables are used to store configuration information that can be easily passed between programs and programming languages." + "We now demonstrate the basic use environment variables. As a reminder, environment variables are used to store configuration information that can be easily passed between programs and programming languages.\n", + "\n", + "```{admonition} Advanced Tip\n", + ":class: tip\n", + "\n", + "Although we do not always need to, we should always enclose shell variables in double quotes and enclose the variable name in curly braces (funny things may happen otherwise) when writing scripts, for example `\"${TEST}\"`. In most cases it is easier to do this every time than try to figure out when it is possible to use the short form. We only use the short form (`$TEST`) when it is used frequently and known to always work (most cases here), in our case it is `$PROJECT` and `$BUCKET`. When in doubt, use the explicit form.\n", + "```" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "id": "b8f74fe9-0943-4bd9-a91b-da09c001f8a1", "metadata": {}, "outputs": [], @@ -251,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "id": "b937f6c7-3697-4bc0-8e32-da0b0faec593", "metadata": {}, "outputs": [ @@ -272,14 +281,12 @@ "id": "78fe7013-a5ae-498f-aeab-ba1f3c710de1", "metadata": {}, "source": [ - "Although we do not always need to, we should always enclose shell variables in double quotes and enclose the variable name in curly braces (funny things may happen otherwise). In most cases it is easier to do this every time than try to figure out when it is possible to use the short form. We only use the short form (`$TEST`) when it is used frequently and known to always work, in our case it is `$PROJECT` and `$BUCKET`. When in doubt, use the explicit form.\n", - "\n", "There are a number of environment variables already set that contain useful information, for example:" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "id": "bae0587c-0379-40e3-9c31-16ba5505487a", "metadata": {}, "outputs": [ @@ -287,17 +294,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "student31\n" + "learner\n" ] } ], "source": [ - "echo \"${USER}\"" + "echo $USER" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "52b4add5-0d18-4faf-a304-b7e0ee8ba82e", "metadata": {}, "outputs": [ @@ -305,12 +312,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "cs-730348008320-default-default-wwhtf\n" + "essentials\n" ] } ], "source": [ - "echo \"${HOSTNAME}\"" + "echo $HOSTNAME" ] }, { @@ -323,7 +330,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 12, "id": "776806e4-209f-45fa-b14e-f771f5d5089d", "metadata": {}, "outputs": [ @@ -331,7 +338,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Tue 02 Nov 2021 07:15:33 PM UTC\n" + "Thu Feb 3 20:10:51 UTC 2022\n" ] } ], @@ -341,7 +348,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 13, "id": "560fde63-0eb8-4c52-9787-4debfe35ec23", "metadata": {}, "outputs": [ @@ -349,7 +356,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Date: Tue 02 Nov 2021 07:15:34 PM UTC\n" + "Date: Thu Feb 3 20:10:52 UTC 2022\n" ] } ], @@ -357,6 +364,14 @@ "echo \"Date: $(date)\"" ] }, + { + "cell_type": "markdown", + "id": "4ba54cbd-573e-43f6-ad7c-ccf6dac871b0", + "metadata": {}, + "source": [ + "We use double quotes to make it a single string." + ] + }, { "cell_type": "markdown", "id": "6323f7bf-b2fe-4c94-a87a-2d35e75a1a49", @@ -369,7 +384,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "id": "18f40620-8537-42b5-948f-dd581e604a36", "metadata": {}, "outputs": [ @@ -377,8 +392,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Your active configuration is: [cloudshell-17640]\n", - "just-armor-301114\n" + "essentials-learner\n" ] } ], @@ -388,27 +402,19 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 15, "id": "a959f857-8a20-4356-bc30-dbf8c75be96d", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Your active configuration is: [cloudshell-17640]\n" - ] - } - ], + "outputs": [], "source": [ "PROJECT=\"$(gcloud config get-value project)\"" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 16, "id": "c1d27e89-cc69-4979-82c4-79c3fb36bccb", "metadata": {}, "outputs": [ @@ -416,7 +422,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "just-armor-301114\n" + "essentials-learner\n" ] } ], @@ -437,12 +443,12 @@ "id": "c25e6431-4f92-454a-bd10-e77cb28deec6", "metadata": {}, "source": [ - "Now use the `PROJECT` envrionment variable to get more information about the active project." + "Now use the `PROJECT` environment variable to get more information about the active project (you may be prompted to enable the API)." ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 19, "id": "31b01f78-94b1-44f0-a2af-d657058941b2", "metadata": {}, "outputs": [ @@ -450,11 +456,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "createTime: '2021-01-08T14:17:35.831Z'\n", + "createTime: '2022-01-14T19:03:26.486Z'\n", "lifecycleState: ACTIVE\n", - "name: CLASS Project\n", - "projectId: just-armor-301114\n", - "projectNumber: '1002111293252'\n" + "name: essentials-learner\n", + "parent:\n", + " id: '1072231596131'\n", + " type: folder\n", + "projectId: essentials-learner\n", + "projectNumber: '998995103712'\n" ] } ], @@ -472,7 +481,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 21, "id": "845b2aaf-0032-4043-a40a-d2482693d23e", "metadata": {}, "outputs": [ @@ -480,20 +489,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "[accessibility]\n", - "screen_reader = True\n", - "[component_manager]\n", - "disable_update_check = True\n", - "[compute]\n", - "gce_metadata_read_timeout_sec = 30\n", "[core]\n", - "account = student31@class.internet2.edu\n", + "account = 998995103712-compute@developer.gserviceaccount.com\n", "disable_usage_reporting = True\n", - "project = just-armor-301114\n", - "[metrics]\n", - "environment = devshell\n", + "project = essentials-learner\n", "\n", - "Your active configuration is: [cloudshell-17640]\n" + "Your active configuration is: [default]\n" ] } ], @@ -511,7 +512,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 23, "id": "9714ebe2-2921-498b-9e0e-69ec6a85896a", "metadata": {}, "outputs": [ @@ -519,7 +520,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "just-armor-301114\n" + "essentials-learner\n" ] } ], @@ -537,7 +538,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 25, "id": "7d803ddd-acca-4ad1-933a-4d6e6044def6", "metadata": {}, "outputs": [ @@ -545,12 +546,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "just-armor-301114\n" + "essentials-learner\n" ] } ], "source": [ - "PROJECT=\"$(gcloud config list --format='value(core.project)')\"\n", + "PROJECT=$(gcloud config list --format='value(core.project)')\n", "echo $PROJECT" ] }, @@ -588,7 +589,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 26, "id": "678a1735-8cad-403c-b302-639de1fb0f4b", "metadata": { "tags": [] @@ -598,26 +599,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "\u001b[1;39m{\n", - " \u001b[0m\u001b[34;1m\"accessibility\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[1;39m{\n", - " \u001b[0m\u001b[34;1m\"screen_reader\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"True\"\u001b[0m\u001b[1;39m\n", - " \u001b[1;39m}\u001b[0m\u001b[1;39m,\n", - " \u001b[0m\u001b[34;1m\"component_manager\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[1;39m{\n", - " \u001b[0m\u001b[34;1m\"disable_update_check\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"True\"\u001b[0m\u001b[1;39m\n", - " \u001b[1;39m}\u001b[0m\u001b[1;39m,\n", - " \u001b[0m\u001b[34;1m\"compute\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[1;39m{\n", - " \u001b[0m\u001b[34;1m\"gce_metadata_read_timeout_sec\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"30\"\u001b[0m\u001b[1;39m\n", - " \u001b[1;39m}\u001b[0m\u001b[1;39m,\n", - " \u001b[0m\u001b[34;1m\"core\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[1;39m{\n", - " \u001b[0m\u001b[34;1m\"account\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"student31@class.internet2.edu\"\u001b[0m\u001b[1;39m,\n", - " \u001b[0m\u001b[34;1m\"disable_usage_reporting\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"True\"\u001b[0m\u001b[1;39m,\n", - " \u001b[0m\u001b[34;1m\"project\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"just-armor-301114\"\u001b[0m\u001b[1;39m\n", - " \u001b[1;39m}\u001b[0m\u001b[1;39m,\n", - " \u001b[0m\u001b[34;1m\"metrics\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[1;39m{\n", - " \u001b[0m\u001b[34;1m\"environment\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"devshell\"\u001b[0m\u001b[1;39m\n", - " \u001b[1;39m}\u001b[0m\u001b[1;39m\n", - "\u001b[1;39m}\u001b[0m\n" + "bash: jq: command not found\n" ] + }, + { + "ename": "", + "evalue": "127", + "output_type": "error", + "traceback": [] } ], "source": [ @@ -626,7 +615,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 27, "id": "b0dec5ed-dc7f-4003-b228-f6605119ce47", "metadata": {}, "outputs": [ @@ -634,8 +623,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "just-armor-301114\n" + "bash: jq: command not found\n" ] + }, + { + "ename": "", + "evalue": "127", + "output_type": "error", + "traceback": [] } ], "source": [