|
| 1 | + |
| 2 | +## Background |
| 3 | + |
| 4 | +In this walkthrough, we go through the setup of a webserver using `stackql`. This is useful in itself for development purposes, and we will build on it in more complex examples. |
| 5 | + |
| 6 | +This walkthrough is not at all original; it is an amalgam of materials freely (and redundantly) available elsewehere. It is heavily inspired by: |
| 7 | + |
| 8 | +- [GCP documentation on running VMs with startup scripts](https://cloud.google.com/compute/docs/instances/startup-scripts/linux#rest). |
| 9 | +- [GCP quickstart Apache on VM documentation](https://cloud.google.com/compute/docs/tutorials/basic-webserver-apache). |
| 10 | +- [GCP quickstart Flask on VM documentation](https://cloud.google.com/docs/terraform/deploy-flask-web-server). |
| 11 | +- [F5 Nginx install documentation](https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/). |
| 12 | + |
| 13 | + |
| 14 | +**NOTE** if your LAN / wifi has some firewall blocking connections to port 80, then this demonstration will not work. |
| 15 | + |
| 16 | +## Setup |
| 17 | + |
| 18 | +First, create a google service account key using the GCP Console, per [the GCP documentation](https://cloud.google.com/iam/docs/keys-create-delete). Grant the service account at least requisite compute and firewall mutation privileges, per [the GCP documentation](https://cloud.google.com/iam/docs/create-service-agents#grant-roles); corresponding to [this flask deployment example](https://cloud.google.com/docs/terraform/deploy-flask-web-server#permissions): |
| 19 | + |
| 20 | + |
| 21 | +> - `compute.instances.*` |
| 22 | +> - `compute.firewalls.*` |
| 23 | +
|
| 24 | +Then, do this in bash: |
| 25 | + |
| 26 | +```bash setup stackql-shell credentials_path=cicd/keys/testing/google-rw-credentials.json app_root_path=./test/tmp/.create-google-vm-webserver.stackql |
| 27 | + |
| 28 | +export GOOGLE_CREDENTIALS="$(cat <<credentials_path>>)"; |
| 29 | + |
| 30 | +stackql shell --approot=<<app_root_path>> |
| 31 | +``` |
| 32 | + |
| 33 | +## Method |
| 34 | + |
| 35 | +Do this in the `stackql` shell, replacing `<<project>>` with your GCP project name, '<<region>>', and `<<zone>>` as desired, eg: `australia-southeast1-a`: |
| 36 | + |
| 37 | +```sql stackql-shell input required my_ephemeral_network_name=my-ephemeral-network-01 my_vm_name=my-ephemeral-vm-01 project=stackql-demo region=australia-southeast2 zone=australia-southeast2-a fw_name=ephemeral-http-01 |
| 38 | + |
| 39 | +registry pull google; |
| 40 | + |
| 41 | +insert /*+ AWAIT */ into |
| 42 | +google.compute.networks ( |
| 43 | + project, |
| 44 | + data__name, |
| 45 | + data__autoCreateSubnetworks |
| 46 | +) |
| 47 | +select |
| 48 | + '<<project>>', |
| 49 | + '<<my_ephemeral_network_name>>', |
| 50 | + true |
| 51 | +; |
| 52 | + |
| 53 | +insert /*+ AWAIT */ into |
| 54 | +google.compute.instances ( |
| 55 | + project, |
| 56 | + zone, |
| 57 | + data__name, |
| 58 | + data__machineType, |
| 59 | + data__metadata, |
| 60 | + data__networkInterfaces, |
| 61 | + data__disks |
| 62 | +) |
| 63 | +select |
| 64 | + '<<project>>', |
| 65 | + '<<zone>>', |
| 66 | + '<<my_vm_name>>', |
| 67 | + 'zones/<<zone>>/machineTypes/n1-standard-1', |
| 68 | + '{ |
| 69 | + "items": [ |
| 70 | + { |
| 71 | + "key": "startup-script", |
| 72 | + "value": "#! /bin/bash\\nsudo apt-get update\\nsudo apt-get -y install apache2\\necho ''<!doctype html><html><body><h1>Hello from stackql auto-provisioned.</h1></body></html>'' | sudo tee /var/www/html/index.html" |
| 73 | + } |
| 74 | + ] |
| 75 | + }', |
| 76 | + '[ |
| 77 | + { |
| 78 | + "stackType": "IPV4_ONLY", |
| 79 | + "accessConfigs": [ |
| 80 | + { |
| 81 | + "name": "External NAT", |
| 82 | + "type": "ONE_TO_ONE_NAT", |
| 83 | + "networkTier": "PREMIUM" |
| 84 | + } |
| 85 | + ], |
| 86 | + "subnetwork": "projects/<<project>>/regions/<<region>>/subnetworks/<<my_ephemeral_network_name>>" |
| 87 | + } |
| 88 | + ]', |
| 89 | + '[ |
| 90 | + { |
| 91 | + "autoDelete": true, |
| 92 | + "boot": true, |
| 93 | + "initializeParams": { |
| 94 | + "diskSizeGb": "10", |
| 95 | + "sourceImage": "https://compute.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/family/ubuntu-2004-lts" |
| 96 | + }, |
| 97 | + "mode": "READ_WRITE", |
| 98 | + "type": "PERSISTENT" |
| 99 | + } |
| 100 | + ]' |
| 101 | +; |
| 102 | + |
| 103 | +insert /*+ AWAIT */ into |
| 104 | +google.compute.firewalls ( |
| 105 | + project, |
| 106 | + data__name, |
| 107 | + data__network, |
| 108 | + data__allowed, |
| 109 | + data__direction, |
| 110 | + data__sourceRanges |
| 111 | +) |
| 112 | +select |
| 113 | + '<<project>>', |
| 114 | + '<<fw_name>>', |
| 115 | + 'global/networks/<<my_ephemeral_network_name>>', |
| 116 | + '[ |
| 117 | + { |
| 118 | + "IPProtocol": "tcp", |
| 119 | + "ports": [ |
| 120 | + "80" |
| 121 | + |
| 122 | + ] |
| 123 | + } |
| 124 | + ]', |
| 125 | + 'INGRESS', |
| 126 | + '[ |
| 127 | + "0.0.0.0/0" |
| 128 | + ]' |
| 129 | +; |
| 130 | + |
| 131 | +``` |
| 132 | + |
| 133 | +```bash setup credentials_path=cicd/keys/testing/google-rw-credentials.json app_root_path=./test/tmp/.create-google-vm-webserver.stackql my_vm_name=my-ephemeral-vm-01 project=stackql-demo zone=australia-southeast2 zone=australia-southeast2-a |
| 134 | + |
| 135 | +export GOOGLE_CREDENTIALS="$(cat <<credentials_path>>)"; |
| 136 | + |
| 137 | +publicIpAddress=$(stackql --approot=<<app_root_path>> exec "select json_extract(\"networkInterfaces\", '\$[0].accessConfigs[0].natIP') as public_ipv4_address from google.compute.instances where project = '<<project>>' and zone = '<<zone>>' and instance = '<<my_vm_name>>';" -o json | jq -r '.[0].public_ipv4_address') |
| 138 | + |
| 139 | +echo "publicIpAddress=${publicIpAddress}" |
| 140 | +result="" |
| 141 | +for i in $(seq 1 20); do |
| 142 | + sleep 5; |
| 143 | + result="$(curl http://${publicIpAddress} | grep 'auto-provisioned')"; |
| 144 | + if [ "${result}" != "" ]; then |
| 145 | + break |
| 146 | + fi |
| 147 | +done |
| 148 | + |
| 149 | +echo "${result}"; |
| 150 | + |
| 151 | +``` |
| 152 | + |
| 153 | +## Result |
| 154 | + |
| 155 | + |
| 156 | +You will see exactly this in the output: |
| 157 | + |
| 158 | +```html expectation stdout-contains-all |
| 159 | +<!doctype html><html><body><h1>Hello from stackql auto-provisioned.</h1></body></html> |
| 160 | +``` |
| 161 | + |
| 162 | +## Cleanup |
| 163 | + |
| 164 | +```bash teardown best-effort app_root_path=./test/tmp/.create-google-vm-webserver.stackql credentials_path=cicd/keys/testing/google-rw-credentials.json my_ephemeral_network_name=my-ephemeral-network-01 my_vm_name=my-ephemeral-vm-01 project=stackql-demo region=australia-southeast2 zone=australia-southeast2-a fw_name=ephemeral-http-01 |
| 165 | + |
| 166 | +echo "begin teardown"; |
| 167 | + |
| 168 | +export GOOGLE_CREDENTIALS="$(cat <<credentials_path>>)"; |
| 169 | + |
| 170 | +stackql --approot=<<app_root_path>> exec "delete /*+ AWAIT */ from google.compute.instances where project = '<<project>>' and zone = '<<zone>>' and instance = '<<my_vm_name>>';" |
| 171 | + |
| 172 | +stackql --approot=<<app_root_path>> exec "delete /*+ AWAIT */ from google.compute.firewalls where project = '<<project>>' and firewall= '<<fw_name>>';" |
| 173 | + |
| 174 | +stackql --approot=<<app_root_path>> exec "delete /*+ AWAIT */ from google.compute.networks where project = '<<project>>' and network = '<<my_ephemeral_network_name>>';" |
| 175 | + |
| 176 | +rm -rf <<app_root_path>> ; |
| 177 | + |
| 178 | +echo "conclude teardown"; |
| 179 | + |
| 180 | +``` |
0 commit comments