Skip to content

Commit

Permalink
Merge pull request #61 from serlo/feat/uberspace-script
Browse files Browse the repository at this point in the history
feat: set up Uberspace scripts
  • Loading branch information
hejtful authored Sep 23, 2024
2 parents 136684d + bfcd414 commit b217aef
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .env-template
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ LTI_PLATFORM_NAME=saltire.lti.app
LTI_PLATFORM_CLIENT_ID=saltire.lti.app
LTI_PLATFORM_AUTHENTICATION_ENDPOINT=https://saltire.lti.app/platform/auth
LTI_PLATFORM_ACCESS_TOKEN_ENDPOINT=https://saltire.lti.app/platform/token/sc24671cd70c6e45554e6c405a2f5d966
LTI_PLATFORM_KEYSET_ENDPOINT=https://saltire.lti.app/platform/jwks/sc24671cd70c6e45554e6c405a2f5d966
LTI_PLATFORM_KEYSET_ENDPOINT=https://saltire.lti.app/platform/jwks/sc24671cd70c6e45554e6c405a2f5d966
79 changes: 79 additions & 0 deletions scripts/setup_uberspace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash

set -e

# Set Node.js version
if ! $(uberspace tools version show node | grep -q '20'); then
uberspace tools version use node 20
fi

# Remove default X-Frame-Options header to allow embedding in iframe
# TODO: X-Frame-Options is deprecated anyway. Maybe restrict embedding only on allowed domains using new headers? See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options instead
uberspace web header suppress / X-Frame-Options

# Create MySQL table
mysql -e 'USE '$USER'; CREATE TABLE IF NOT EXISTS `lti_entity` ( `id` bigint NOT NULL AUTO_INCREMENT, `resource_link_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, `custom_claim_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL, `content` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL, `id_token_on_creation` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL, PRIMARY KEY (`id`), KEY `idx_lti_entity_custom_claim_id` (`custom_claim_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;'
echo 'MySQL table created successfully (or existed already)'

# Generate mongodb password
if ! $(grep MONGODB_PASSWORD ~/.bashrc); then
export MONGODB_PASSWORD=$(pwgen 32 1)
echo "export MONGODB_PASSWORD=$MONGODB_PASSWORD" >> ~/.bashrc
fi

# Pull env into current shell
source ~/.bashrc

# Set up MongoDB
if ! $(uberspace tools version show mongodb | grep -q '6.0'); then
uberspace tools version use mongodb 6.0
echo 'MongoDB version set to 6.0. Waiting a few seconds until it runs.'
fi
mkdir -p ~/mongodb
cp ./uberspace/mongodb/mongodb.ini ~/etc/services.d/
echo $(supervisorctl reread)
echo $(supervisorctl update)
sleep 2
if ! $(supervisorctl status | grep -q 'RUNNING'); then
echo 'MongoDB status is not RUNNING'
exit 1
fi
cp ./uberspace/mongodb/.mongoshrc.js ~/
cp ./uberspace/mongodb/setup.js ~/mongodb/
mongosh admin ~/mongodb/setup.js
echo 'MongoDB set up successfully'

# Set environment variables
cp .env-template .env
mysql_pw=$(grep -oP -m 1 "^password=(.*)" ~/.my.cnf | cut -d '=' -f 2-)
echo "MYSQL_URI=mysql://${USER}:${mysql_pw}@localhost:3306/${USER}" >> .env
echo "MONGODB_CONNECTION_URI=mongodb://${USER}_mongoroot:${MONGODB_PASSWORD}@127.0.0.1:27017/" >> .env
echo 'Updated environment variables'

# Install dependencies
yarn
echo 'Installed dependencies using Yarn'

# Build frontend
yarn build
echo 'Built the frontend app using Yarn'

# Run the backend as an Uberspace service
cp ./uberspace/app.ini ~/etc/services.d/
supervisorctl reread
supervisorctl update
if $(supervisorctl status | grep -q "serlo-app.*RUNNING"); then
supervisorctl restart serlo-app
echo 'Restarted the serlo-app Uberspace service, as it already existed'
else
supervisorctl start serlo-app
echo 'Started the serlo-app Uberspace service for running the backend app'
fi

# Open the LTI backend to the internet
uberspace web backend set / --http --port 3000
if ! $(uberspace web backend list | grep -q 'http:3000 => OK, listening'); then
echo 'Uberspace web backend is not listening'
exit 2
fi
echo 'Backend app opened to the internet'
6 changes: 6 additions & 0 deletions uberspace/app.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[program:serlo-app]
directory=/home/%(ENV_USER)s/serlo-editor-as-lti-tool
command=yarn start:dev
autostart=true
autorestart=true
environment=NODE_ENV=production
5 changes: 5 additions & 0 deletions uberspace/mongodb/.mongoshrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint no-undef: 0 */
const uri = `mongodb://${process.env.USER}_mongoroot:${process.env.MONGODB_PASSWORD}@127.0.0.1:27017/admin`
// eslint-disable-next-line no-console
console.log(`Attempting to connect to ${uri}`)
db = connect(uri)
8 changes: 8 additions & 0 deletions uberspace/mongodb/mongodb.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[program:mongodb]
command=mongod
--dbpath %(ENV_HOME)s/mongodb
--bind_ip 127.0.0.1
--auth
--unixSocketPrefix %(ENV_HOME)s/mongodb
autostart=yes
autorestart=yes
15 changes: 15 additions & 0 deletions uberspace/mongodb/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint no-undef: 0 */
const username = `${process.env.USER}_mongoroot`
const password = process.env.MONGODB_PASSWORD

try {
db.auth(username, password)
print(`User '${username}' already exists.`)
} catch {
db.createUser({
user: username,
pwd: password,
roles: ['root'],
})
print(`User '${username}' created.`)
}

0 comments on commit b217aef

Please sign in to comment.