-
Notifications
You must be signed in to change notification settings - Fork 1
253 lines (212 loc) · 9.05 KB
/
create-instance.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
name: Create Instance
on:
issues:
types: [labeled]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
create:
runs-on: self-hosted
if: github.event.label.name == 'instance:approved'
steps:
- name: Remove "instance:approved" label
if: ${{ always() }}
uses: actions/github-script@v7
with:
script: |
github.rest.issues.removeLabel({
issue_number: context.payload.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: ["instance:approved"]
})
- name: Issue Forms Body Parser
id: parse
uses: zentered/[email protected]
- name: Display parsed data
run: |
echo ${{ toJSON(steps.parse.outputs.data) }} | jq .
- name: Extract fields
id: extract
run: |
email=$(
echo ${{ toJSON(steps.parse.outputs.data) }} |
jq -r ".email.text"
)
echo "email=$email" >> $GITHUB_OUTPUT
- name: Extract labels
id: labels
run: |
# If there are multiple "flavor:" labels, select the first one
instance_flavor=$(
gh issue view $ISSUE_NUMBER \
--json labels \
--jq '[.labels[].name | select(. | startswith("flavor:")) | split(":")[1]][0]'
)
echo "instance_flavor=$instance_flavor" >> $GITHUB_OUTPUT
env:
ISSUE_NUMBER: ${{ github.event.issue.number }}
- name: Define instance name
id: define
run: |
instance_name="morpho-cloud-portal_instance-$ISSUE_NUMBER"
echo "instance_name=$instance_name" >> $GITHUB_OUTPUT
env:
ISSUE_NUMBER: ${{ github.event.issue.number }}
- uses: actions/checkout@v4
- name: Create instance
run: |
OS_CLOUD=BIO180006_IU # Select openstack auth settings defined in "clouds.yaml"
source ~/venv/bin/activate
echo Creating instance "$INSTANCE_NAME"
# See https://jetstream2.exosphere.app/exosphere/helpabout
exoClientUuid=67296a2e-069b-49ca-9ca4-5dd296869ada
# See "currentExoServerVersion" in exosphere/src/Types/Server.elm
exoServerVersion=5
openstack server create "$INSTANCE_NAME" \
--nic net-id="auto_allocated_network" \
--security-group "default" \
--security-group "exosphere" \
--flavor $INSTANCE_FLAVOR \
--image "Featured-Ubuntu22" \
--key-name "jcfr" \
--property "exoGuac={\"v\":1,\"ssh\":true,\"vnc\":true}" \
--property "exoClientUuid=$exoClientUuid" \
--property "exoServerVersion=$exoServerVersion" \
--property "[email protected]" \
--property "exoFloatingIpOption=automatic" \
--property "exoSetup={\"status\":\"waiting\",\"epoch\":null}" \
--user-data ./cloud-config \
--wait \
--column created \
--column flavor \
--column image \
--column name \
--column status
env:
INSTANCE_NAME: ${{ steps.define.outputs.instance_name }}
INSTANCE_FLAVOR: ${{ steps.labels.outputs.instance_flavor }}
- name: Poll instance status
id: instance_poll
run: |
OS_CLOUD=BIO180006_IU # Select openstack auth settings defined in "clouds.yaml"
source ~/venv/bin/activate
echo Polling instance "$INSTANCE_NAME"
max_wait_time=300 # Maximum wait time in seconds (300s -> 5mins)
wait_interval=5 # Interval between status checks in seconds
total_wait_time=0
while [ $total_wait_time -lt $max_wait_time ]; do
status=$(openstack server show "$INSTANCE_NAME" -f json -c properties | \
jq -r .properties | \
jq -r .exoSetup | \
jq -r .status)
if [ "$status" = "complete" ]; then
echo "Status is complete. Exiting loop."
break
else
echo "Status is $status. Waiting for completion..."
sleep $wait_interval
total_wait_time=$((total_wait_time + wait_interval))
fi
done
if [ $total_wait_time -ge $max_wait_time ]; then
echo "Maximum wait time ($max_wait_time seconds) exceeded. Exiting."
status="polling_timeout"
fi
echo "status=$status" >> $GITHUB_OUTPUT
env:
INSTANCE_NAME: ${{ steps.define.outputs.instance_name }}
- name: Send mail (not completed)
if: ${{ steps.instance_poll.outputs.status != 'complete' }}
uses: dawidd6/action-send-mail@2cea9617b09d79a095af21254fbcb7ae95903dde # v3.12.0
with:
server_address: smtp.gmail.com
server_port: 465
secure: true
username: ${{secrets.MAIL_USERNAME}}
password: ${{secrets.MAIL_PASSWORD}}
from: MorphoCloudPortal
to: ${{ steps.extract.outputs.email }}
subject:
"[MorphoCloudPortal] Instance ${{ steps.define.outputs.instance_name
}} creation failed"
body:
Failed to create instance ${{ steps.define.outputs.instance_name }}
- name: Retrieve metadata
id: instance_metadata
if: ${{ steps.instance_poll.outputs.status == 'complete' }}
run: |
OS_CLOUD=BIO180006_IU # Select openstack auth settings defined in "clouds.yaml"
source ~/venv/bin/activate
echo Retrieving metadata from instance "$INSTANCE_NAME"
# Get instance IP
instance_ip=$(
openstack server show $INSTANCE_NAME -c addresses -f json | \
jq -r .addresses.auto_allocated_network[1]
)
echo "instance_ip=$instance_ip" >> $GITHUB_OUTPUT
# Get instance password
instance_pwd=$(
openstack server show $INSTANCE_NAME -c tags -f json | \
jq -r '.tags[] | select(startswith("exoPw")) | sub("^exoPw:"; "")'
)
echo "::add-mask::$instance_pwd"
echo "instance_pwd=$instance_pwd" >> $GITHUB_OUTPUT
env:
INSTANCE_NAME: ${{ steps.define.outputs.instance_name }}
- name: Generate Guacamole Connection URL
if: ${{ steps.instance_poll.outputs.status == 'complete' }}
id: guacamole
run: |
# See hard-coded value in exosphere/src/Helpers/Interaction.elm
guacamole_port=49528
# See cloud_configs.js (allocation region is "IU")
proxy_hostname=proxy-js2-iu.exosphere.app
# See "buildProxyUrl" in src/Helpers/Url.elm
proxified_instance_ip=${INSTANCE_IP//./-}
# See "stepServerGuacamoleAuth" in exosphere/src/Orchestration/GoalServer.elm
tokens_url="https://http-$proxified_instance_ip-$guacamole_port.$proxy_hostname/guacamole/api/tokens"
auth_token=$(
curl -X POST --silent -d "username=exouser&password=$INSTANCE_PWD" $tokens_url | \
jq -r .authToken
)
echo "::add-mask::$auth_token"
# See hard-coded value in exosphere/src/Helpers/Interaction.elm
client_id=ZGVza3RvcABjAGRlZmF1bHQ
connection_url="https://http-$proxified_instance_ip-$guacamole_port.$proxy_hostname/guacamole/#/client/$client_id=?token=$auth_token"
echo $connection_url
echo "connection_url=$connection_url" >> $GITHUB_OUTPUT
env:
INSTANCE_IP: ${{ steps.instance_metadata.outputs.instance_ip }}
INSTANCE_PWD: ${{ steps.instance_metadata.outputs.instance_pwd }}
- name: Send mail (completed)
if: ${{ steps.instance_poll.outputs.status == 'complete' }}
uses: dawidd6/action-send-mail@2cea9617b09d79a095af21254fbcb7ae95903dde # v3.12.0
with:
server_address: smtp.gmail.com
server_port: 465
secure: true
username: ${{secrets.MAIL_USERNAME}}
password: ${{secrets.MAIL_PASSWORD}}
from: MorphoCloudPortal
to: ${{ steps.extract.outputs.email }}
subject:
"[MorphoCloudPortal] Instance ${{ steps.define.outputs.instance_name
}} created"
body: |
Instance ${{ steps.define.outputs.instance_name }} created
Web connect: ${{ steps.guacamole.outputs.connection_url }}
SSH: ssh exouser@${{ steps.instance_metadata.outputs.instance_ip }}
Passphrase: ${{ steps.instance_metadata.outputs.instance_pwd }}
- name: Add "instance:created" label
if: ${{ success() && steps.instance_poll.outputs.status == 'complete' }}
uses: actions/github-script@v7
with:
script: |
github.rest.issues.addLabels({
issue_number: context.payload.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ["instance:created"]
})