Skip to content

Commit f355284

Browse files
readwrite-walkthrough
Summary: - Webserver VM create walkthrough.
1 parent 73814c3 commit f355284

File tree

7 files changed

+289
-29
lines changed

7 files changed

+289
-29
lines changed

.github/workflows/scenario.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ jobs:
1717
runs-on: ubuntu-latest
1818
timeout-minutes: ${{ vars.DEFAULT_JOB_TIMEOUT_MIN == '' && 120 || vars.DEFAULT_JOB_TIMEOUT_MIN }}
1919
steps:
20+
- name: Install Package dependencies
21+
run: |
22+
sudo apt-get update
23+
sudo apt-get install -y jq
24+
2025
- name: Ref Parse
2126
run: |
2227
_defaultRunType="${{ env.RUNTYPE_READ_ONLY }}"
@@ -69,8 +74,8 @@ jobs:
6974
7075
- name: Persist secrets
7176
run: |
72-
echo "$GCP_RO_SECRET" >> cicd/keys/testing/google-ro-credentials.json
73-
echo "$GCP_RW_SECRET" >> cicd/keys/testing/google-rw-credentials.json
77+
echo "$GCP_RO_SECRET" > cicd/keys/testing/google-ro-credentials.json
78+
echo "$GCP_RW_SECRET" > cicd/keys/testing/google-rw-credentials.json
7479
shell: bash
7580
env:
7681
GCP_RO_SECRET: ${{ secrets.CI_SCENARIO_GCP_RO_SECRET }}

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
"SELECT instance_id FROM aws.ec2.instances WHERE region IN ('us-east-1', 'ap-southeast-2');",
160160
"SELECT instance_id FROM aws.ec2_solid_gold.instances WHERE region IN ('us-east-1', 'ap-southeast-2');",
161161
"SELECT region FROM aws.ec2_nextgen.instances WHERE region IN ('us-east-1', 'ap-southeast-2', 'ap-southeast-1') order by region, instance_id; SELECT region, instance_id, tenancy, security_groups FROM aws.ec2_nextgen.instances WHERE region IN ('us-east-1', 'ap-southeast-2', 'ap-southeast-1') order by region, instance_id; SELECT region, instance_id, tenancy, security_groups FROM aws.ec2_nextgen.instances WHERE region IN ('us-east-1', 'ap-southeast-2', 'ap-southeast-1') order by region, instance_id; SELECT region, instance_id, tenancy, security_groups FROM aws.ec2_nextgen.instances WHERE region IN ('us-east-1', 'ap-southeast-2', 'ap-southeast-1') order by region, instance_id; SELECT region, instance_id, tenancy, security_groups FROM aws.ec2_nextgen.instances WHERE region IN ('us-east-1', 'ap-southeast-2', 'ap-southeast-1') order by region, instance_id; SELECT region, instance_id, tenancy, security_groups FROM aws.ec2_nextgen.instances WHERE region IN ('us-east-1', 'ap-southeast-2', 'ap-southeast-1') order by region, instance_id;",
162+
"insert /*+ AWAIT */ into google.compute.instances ( project, zone, data__name, data__machineType, data__metadata, data__networkInterfaces, data__disks ) select 'stackql-demo', 'australia-southeast2-a', 'my-ephemeral-vm-01', 'zones/australia-southeast2-a/machineTypes/n1-standard-1', '{ \"items\": [ { \"key\": \"startup-script\", \"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\" } ] }', '[ { \"stackType\": \"IPV4_ONLY\", \"accessConfigs\": [ { \"name\": \"External NAT\", \"type\": \"ONE_TO_ONE_NAT\", \"networkTier\": \"PREMIUM\" } ], \"subnetwork\": \"projects/stackql-demo/regions/australia-southeast2/subnetworks/my-ephemeral-network-01\" } ]', '[ { \"autoDelete\": true, \"boot\": true, \"initializeParams\": { \"diskSizeGb\": \"10\", \"sourceImage\": \"https://compute.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/family/ubuntu-2004-lts\" }, \"mode\": \"READ_WRITE\", \"type\": \"PERSISTENT\" } ]' ;",
162163
],
163164
"default": "show providers;"
164165
},

docs/walkthroughs/deploy/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
## Lifecycle managment with `stackql-deploy`
2+
## Lifecycle management with `stackql-deploy`
33

docs/walkthroughs/readonly/list-aws-instances.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Assuming you have chosen regions wisely, you will see something like this includ
4242
4343
```bash teardown best-effort app_root_path=./test/tmp/.list-aws-instances.stackql
4444
45-
rm -rf <<app_root_path>>
45+
rm -rf <<app_root_path>>;
46+
47+
echo "teardown complete";
4648
4749
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
## Lifecycle managment with `stackql`
2+
## Lifecycle management with `stackql`
33

44

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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

Comments
 (0)