-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from serlo/feat/uberspace-script
feat: set up Uberspace scripts
- Loading branch information
Showing
6 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
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
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
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' |
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
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 |
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
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) |
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
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 |
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
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.`) | ||
} |