Skip to content

Commit

Permalink
GCP: Simpler use of environment variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiddelkoop committed Feb 3, 2022
1 parent df67f49 commit 0d114c5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 78 deletions.
2 changes: 1 addition & 1 deletion content/GCP/02_intro_to_compute.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
Expand Down
149 changes: 72 additions & 77 deletions content/GCP/04_intro_to_cli.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"```"
]
},
{
Expand Down Expand Up @@ -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": [],
Expand All @@ -251,7 +260,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"id": "b937f6c7-3697-4bc0-8e32-da0b0faec593",
"metadata": {},
"outputs": [
Expand All @@ -272,45 +281,43 @@
"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": [
{
"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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cs-730348008320-default-default-wwhtf\n"
"essentials\n"
]
}
],
"source": [
"echo \"${HOSTNAME}\""
"echo $HOSTNAME"
]
},
{
Expand All @@ -323,15 +330,15 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 12,
"id": "776806e4-209f-45fa-b14e-f771f5d5089d",
"metadata": {},
"outputs": [
{
"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"
]
}
],
Expand All @@ -341,22 +348,30 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 13,
"id": "560fde63-0eb8-4c52-9787-4debfe35ec23",
"metadata": {},
"outputs": [
{
"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"
]
}
],
"source": [
"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",
Expand All @@ -369,16 +384,15 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 14,
"id": "18f40620-8537-42b5-948f-dd581e604a36",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your active configuration is: [cloudshell-17640]\n",
"just-armor-301114\n"
"essentials-learner\n"
]
}
],
Expand All @@ -388,35 +402,27 @@
},
{
"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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"just-armor-301114\n"
"essentials-learner\n"
]
}
],
Expand All @@ -437,24 +443,27 @@
"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": [
{
"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"
]
}
],
Expand All @@ -472,28 +481,20 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 21,
"id": "845b2aaf-0032-4043-a40a-d2482693d23e",
"metadata": {},
"outputs": [
{
"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"
]
}
],
Expand All @@ -511,15 +512,15 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 23,
"id": "9714ebe2-2921-498b-9e0e-69ec6a85896a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"just-armor-301114\n"
"essentials-learner\n"
]
}
],
Expand All @@ -537,20 +538,20 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 25,
"id": "7d803ddd-acca-4ad1-933a-4d6e6044def6",
"metadata": {},
"outputs": [
{
"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"
]
},
Expand Down Expand Up @@ -588,7 +589,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 26,
"id": "678a1735-8cad-403c-b302-639de1fb0f4b",
"metadata": {
"tags": []
Expand All @@ -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": [
Expand All @@ -626,16 +615,22 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 27,
"id": "b0dec5ed-dc7f-4003-b228-f6605119ce47",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"just-armor-301114\n"
"bash: jq: command not found\n"
]
},
{
"ename": "",
"evalue": "127",
"output_type": "error",
"traceback": []
}
],
"source": [
Expand Down

0 comments on commit 0d114c5

Please sign in to comment.