Create Instance #108
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create Instance | |
on: | |
issue_comment: | |
types: [created] | |
jobs: | |
create: | |
runs-on: self-hosted | |
if: | |
${{ !github.event.issue.pull_request && ( | |
contains(github.event.comment.body, '/create') ) }} | |
steps: | |
- name: create command | |
id: create_command | |
uses: github/[email protected] | |
with: | |
command: "/create" | |
reaction: "rocket" | |
allowed_contexts: "issue" | |
permissions: "write,maintain,admin" | |
allowlist: "jcfr,muratmaga" | |
- name: Find Progress Comment | |
uses: peter-evans/find-comment@v3 | |
id: fc_progress | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
comment-author: "github-actions[bot]" | |
body-includes: Instance Creation Progress | |
- name: comment (progress) | |
id: couc_progress | |
uses: peter-evans/[email protected] | |
with: | |
comment-id: ${{ steps.fc_progress.outputs.comment-id }} | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Progress ⏳ | |
| Create IP | Create Instance | Associate IP | Setup Instance | Retrieve Connection URL | Send Email | | |
|:---------:|:---------------:|:------------:|:--------------:|:-----------------------:|:----------:| | |
| ⏳ | | | | | | | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
edit-mode: replace | |
- 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: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GH_REPO: ${{ github.repository }} | |
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: Check instance exists | |
id: check_instance | |
run: | | |
export OS_CLOUD=BIO180006_IU # Select openstack auth settings defined in ".config/openstack/clouds.yaml" | |
source ~/venv/bin/activate | |
instance=$(openstack server list -f json | \ | |
jq \ | |
--arg instance_name "$INSTANCE_NAME" \ | |
-c '.[] | select(.Name | contains($instance_name))' | \ | |
jq -r '.Name' | tail -1) | |
[[ $instance == "$INSTANCE_NAME" ]] && exists="true" || exists="false" | |
echo "exists [$exists]" | |
echo "exists=$exists" >> $GITHUB_OUTPUT | |
env: | |
INSTANCE_NAME: ${{ steps.define.outputs.instance_name }} | |
- name: comment (Instance already created) | |
if: ${{ fromJSON(steps.check_instance.outputs.exists) }} | |
uses: peter-evans/[email protected] | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Results ❌ | |
Instance **${{ steps.define.outputs.instance_name }}** already created. | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
- name: instance already exists (failure) | |
if: ${{ fromJSON(steps.check_instance.outputs.exists) }} | |
run: | | |
echo "::error ::Instance $INSTANCE_NAME already created" | |
exit 1 | |
env: | |
INSTANCE_NAME: ${{ steps.define.outputs.instance_name }} | |
- name: Create floating IP | |
id: ip_create | |
run: | | |
export OS_CLOUD=BIO180006_IU # Select openstack auth settings defined in ".config/openstack/clouds.yaml" | |
source ~/venv/bin/activate | |
json_output=$(openstack floating ip create public -f json) | |
echo $json_output | |
floating_ip_uuid=$( | |
echo $json_output | | |
jq -r ".id" | |
) | |
echo "floating_ip_uuid [$floating_ip_uuid]" | |
echo "floating_ip_uuid=$floating_ip_uuid" >> $GITHUB_OUTPUT | |
floating_ip_address=$( | |
echo $json_output | | |
jq -r ".floating_ip_address" | |
) | |
echo "floating_ip_address [$floating_ip_address]" | |
echo "floating_ip_address=$floating_ip_address" >> $GITHUB_OUTPUT | |
- name: comment (floating IP creation failed) | |
if: ${{ steps.ip_create.outcome == 'failure' && failure() }} | |
uses: peter-evans/[email protected] | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Results ❌ | |
Failed to create floating IP for **${{ steps.define.outputs.instance_name }}** instance. | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
- name: comment (progress) | |
uses: peter-evans/[email protected] | |
with: | |
comment-id: ${{ steps.couc_progress.outputs.comment-id }} | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Progress ⏳ | |
| Create IP | Create Instance | Associate IP | Setup Instance | Retrieve Connection URL | Send Email | | |
|:---------:|:---------------:|:------------:|:--------------:|:-----------------------:|:----------:| | |
| ✅ | ⏳ | | | | | | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
edit-mode: replace | |
- name: Create instance | |
id: create_instance | |
run: | | |
export OS_CLOUD=BIO180006_IU # Select openstack auth settings defined in ".config/openstack/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 "antsthings-vgl-gpu-image" \ | |
--key-name "jcfr" \ | |
--property "exoGuac={\"v\":1,\"ssh\":true,\"vnc\":true}" \ | |
--property "exoClientUuid=$exoClientUuid" \ | |
--property "exoServerVersion=$exoServerVersion" \ | |
--property "[email protected]" \ | |
--property "exoFloatingIpOption=useFloatingIp" \ | |
--property "exoFloatingIpReuseOption=$FLOATING_IP_UUID" \ | |
--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 }} | |
FLOATING_IP_UUID: ${{ steps.ip_create.outputs.floating_ip_uuid }} | |
- name: comment (instance creation failed) | |
if: ${{ steps.create_instance.outcome == 'failure' && failure() }} | |
uses: peter-evans/[email protected] | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Results ❌ | |
Failed to create instance **${{ steps.define.outputs.instance_name }}**. | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
- name: comment (progress) | |
uses: peter-evans/[email protected] | |
with: | |
comment-id: ${{ steps.couc_progress.outputs.comment-id }} | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Progress ⏳ | |
| Create IP | Create Instance | Associate IP | Setup Instance | Retrieve Connection URL | Send Email | | |
|:---------:|:---------------:|:------------:|:--------------:|:-----------------------:|:----------:| | |
| ✅ | ✅ | ⏳ | | | | | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
edit-mode: replace | |
- name: Associate floating IP with created instance | |
id: associated_ip | |
run: | | |
export OS_CLOUD=BIO180006_IU # Select openstack auth settings defined in ".config/openstack/clouds.yaml" | |
source ~/venv/bin/activate | |
has_ip=$( | |
openstack server show $INSTANCE_NAME -c addresses -f json | \ | |
jq -r '.addresses.auto_allocated_network[1] != null' | |
) | |
echo "has_ip [$has_ip]" | |
if [[ $has_ip != "true" ]]; then | |
openstack server add floating ip "$INSTANCE_NAME" $FLOATING_IP_ADDRESS | |
fi | |
env: | |
FLOATING_IP_ADDRESS: | |
${{ steps.ip_create.outputs.floating_ip_address }} | |
INSTANCE_NAME: ${{ steps.define.outputs.instance_name }} | |
- name: comment (failed to associate IP with instance) | |
if: ${{ steps.associated_ip.outcome == 'failure' && failure() }} | |
uses: peter-evans/[email protected] | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Results ❌ | |
Failed to associate IP with instance **${{ steps.define.outputs.instance_name }}**. | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
- name: comment (progress) | |
uses: peter-evans/[email protected] | |
with: | |
comment-id: ${{ steps.couc_progress.outputs.comment-id }} | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Progress ⏳ | |
| Create IP | Create Instance | Associate IP | Setup Instance | Retrieve Connection URL | Send Email | | |
|:---------:|:---------------:|:------------:|:--------------:|:-----------------------:|:----------:| | |
| ✅ | ✅ | ✅ | ⏳ | | | | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
edit-mode: replace | |
- name: Poll instance setup status | |
id: instance_poll | |
run: | | |
export OS_CLOUD=BIO180006_IU # Select openstack auth settings defined in ".config/openstack/clouds.yaml" | |
source ~/venv/bin/activate | |
echo Polling "$INSTANCE_NAME" setup status | |
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 console log show $INSTANCE_NAME | \ | |
grep "^\{\"status\":\"" | \ | |
tail -1 | \ | |
jq -r '.status // "pending"') | |
echo -n "setup status [$status]. " | |
if [[ "$status" = "complete" ]]; then | |
echo "Exiting loop." | |
break | |
else | |
echo "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 "::error ::Maximum wait time ($max_wait_time seconds) exceeded." | |
exit 1 | |
fi | |
echo "status=$status" >> $GITHUB_OUTPUT | |
env: | |
INSTANCE_NAME: ${{ steps.define.outputs.instance_name }} | |
- name: comment (maximum wait time exceeded) | |
if: ${{ steps.instance_poll.outcome == 'failure' && failure() }} | |
uses: peter-evans/[email protected] | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Results ❌ | |
Maximum wait time for instance **${{ steps.define.outputs.instance_name }}** setup to complete exceeded. | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
- name: comment (progress) | |
uses: peter-evans/[email protected] | |
with: | |
comment-id: ${{ steps.couc_progress.outputs.comment-id }} | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Progress ⏳ | |
| Create IP | Create Instance | Associate IP | Setup Instance | Retrieve Connection URL | Send Email | | |
|:---------:|:---------------:|:------------:|:--------------:|:-----------------------:|:----------:| | |
| ✅ | ✅ | ✅ | ✅ | ⏳ | | | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
edit-mode: replace | |
- name: Retrieve metadata | |
id: instance_metadata | |
run: | | |
export OS_CLOUD=BIO180006_IU # Select openstack auth settings defined in ".config/openstack/clouds.yaml" | |
source ~/venv/bin/activate | |
echo Retrieving instance "$INSTANCE_NAME" metadata | |
# Get instance IP | |
echo "instance_ip=$FLOATING_IP_ADDRESS" >> $GITHUB_OUTPUT | |
# Get instance password | |
instance_pwd=$( | |
openstack server show $INSTANCE_NAME -c tags -f json | \ | |
jq -r '.tags[] | select(startswith("exoPw")) | sub("^exoPw:"; "")' | |
) | |
if [[ -z "$instance_pwd" ]]; then | |
# Since 'exoPw' tag is not yet set, attempt to directly retrieve the password using | |
# the openstack endpoint local to the instance. | |
instance_pwd=$(ssh \ | |
-o StrictHostKeyChecking=no \ | |
-o UserKnownHostsFile=/dev/null \ | |
-o LogLevel=ERROR \ | |
exouser@$FLOATING_IP_ADDRESS \ | |
'curl --silent http://169.254.169.254/openstack/latest/password') | |
fi | |
echo "::add-mask::$instance_pwd" | |
echo "instance_pwd=$instance_pwd" >> $GITHUB_OUTPUT | |
env: | |
INSTANCE_NAME: ${{ steps.define.outputs.instance_name }} | |
FLOATING_IP_ADDRESS: | |
${{ steps.ip_create.outputs.floating_ip_address }} | |
- name: comment (failed to retrieve instance metadata) | |
if: ${{ steps.instance_metadata.outcome == 'failure' && failure() }} | |
uses: peter-evans/[email protected] | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Results ❌ | |
Failed to retrieve metadata for instance **${{ steps.define.outputs.instance_name }}**. | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
- 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" | |
# | |
# Since the token expires after a few hours, remove "?token=$auth_token" from "connection_url" and require | |
# the user to explicitly authenticate specifying the username and passphrase. | |
# 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=" | |
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: comment (failed to generate connection URL) | |
if: ${{ steps.guacamole.outcome == 'failure' && failure() }} | |
uses: peter-evans/[email protected] | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Results ❌ | |
Failed to generate connection URL for instance **${{ steps.define.outputs.instance_name }}**. | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
- name: comment (progress) | |
uses: peter-evans/[email protected] | |
with: | |
comment-id: ${{ steps.couc_progress.outputs.comment-id }} | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Progress ⏳ | |
| Create IP | Create Instance | Associate IP | Setup Instance | Retrieve Connection URL | Send Email | | |
|:---------:|:---------------:|:------------:|:--------------:|:-----------------------:|:----------:| | |
| ✅ | ✅ | ✅ | ✅ | ✅ | ⏳ | | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
edit-mode: replace | |
- 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: comment (progress) | |
uses: peter-evans/[email protected] | |
with: | |
comment-id: ${{ steps.couc_progress.outputs.comment-id }} | |
issue-number: ${{ github.event.issue.number }} | |
body: | | |
### Instance Creation Progress ✅ | |
| Create IP | Create Instance | Associate IP | Setup Instance | Retrieve Connection URL | Send Email | | |
|:---------:|:---------------:|:------------:|:--------------:|:-----------------------:|:----------:| | |
| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | |
See details at https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
edit-mode: replace | |
- 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"] | |
}) |