peertube-app/start.sh

150 lines
7.5 KiB
Bash
Raw Normal View History

2020-06-16 04:47:04 +00:00
#!/bin/bash
set -eu
2023-06-21 15:07:26 +00:00
mkdir -p /app/data/storage /run/peertube/cache /run/peertube/npm /tmp/peertube
2020-06-16 04:47:04 +00:00
2021-12-15 16:41:56 +00:00
# do not rely on WORKDIR
cd /app/code/server
2024-01-15 17:01:40 +00:00
migrate_ldap_to_oidc() {
2024-01-16 13:12:05 +00:00
USERS_TO_MIGRATE=$(PGPASSWORD=${CLOUDRON_POSTGRESQL_PASSWORD} psql -h ${CLOUDRON_POSTGRESQL_HOST} -p ${CLOUDRON_POSTGRESQL_PORT} -U ${CLOUDRON_POSTGRESQL_USERNAME} -d ${CLOUDRON_POSTGRESQL_DATABASE} -AXqtc "SELECT count(*) FROM \"user\" u WHERE \"pluginAuth\"='peertube-plugin-auth-ldap'")
echo "==> Migrating users to OIDC";
echo " Users to migrate: ${USERS_TO_MIGRATE}"
if [ ${USERS_TO_MIGRATE} -gt 0 ]; then
PGPASSWORD=${CLOUDRON_POSTGRESQL_PASSWORD} psql -h ${CLOUDRON_POSTGRESQL_HOST} -p ${CLOUDRON_POSTGRESQL_PORT} -U ${CLOUDRON_POSTGRESQL_USERNAME} -d ${CLOUDRON_POSTGRESQL_DATABASE} -c "UPDATE \"user\" SET \"pluginAuth\"='peertube-plugin-auth-openid-connect' WHERE \"pluginAuth\"='peertube-plugin-auth-ldap'"
fi
LDAP_PLUGIN_INSTALLED=$(PGPASSWORD=${CLOUDRON_POSTGRESQL_PASSWORD} psql -h ${CLOUDRON_POSTGRESQL_HOST} -p ${CLOUDRON_POSTGRESQL_PORT} -U ${CLOUDRON_POSTGRESQL_USERNAME} -d ${CLOUDRON_POSTGRESQL_DATABASE} -AXqtc "SELECT count(*) FROM \"plugin\" WHERE name='auth-ldap'")
if [ ${LDAP_PLUGIN_INSTALLED} -gt 0 ]; then
echo "==> Uninstalling LDAP plugin"
gosu cloudron:cloudron npm run plugin:uninstall -- -n peertube-plugin-auth-ldap
echo "==> Deleting LDAP settings"
PGPASSWORD=${CLOUDRON_POSTGRESQL_PASSWORD} psql -h ${CLOUDRON_POSTGRESQL_HOST} -p ${CLOUDRON_POSTGRESQL_PORT} -U ${CLOUDRON_POSTGRESQL_USERNAME} -d ${CLOUDRON_POSTGRESQL_DATABASE} -c "DELETE FROM \"plugin\" WHERE name='auth-ldap'"
2024-01-15 17:01:40 +00:00
fi
}
install_oidc() {
# https://docs.joinpeertube.org/maintain-tools?id=cli-wrapper . Note that we have to restart peertube when installed this way
if [[ -n "${CLOUDRON_OIDC_ISSUER:-}" ]]; then
echo "==> Installing OIDC plugin"
gosu cloudron:cloudron npm run plugin:install -- -n peertube-plugin-auth-openid-connect -v 0.1.1
update_oidc
fi
}
update_oidc() {
echo "==> Updating OIDC config"
PGPASSWORD=${CLOUDRON_POSTGRESQL_PASSWORD} psql -h ${CLOUDRON_POSTGRESQL_HOST} -p ${CLOUDRON_POSTGRESQL_PORT} -U ${CLOUDRON_POSTGRESQL_USERNAME} -d ${CLOUDRON_POSTGRESQL_DATABASE} \
2024-01-15 17:01:40 +00:00
-c "UPDATE plugin SET settings='{\"scope\": \"openid email profile\", \"client-id\": \"${CLOUDRON_OIDC_CLIENT_ID}\", \"discover-url\": \"${CLOUDRON_OIDC_DISCOVERY_URL}\", \"client-secret\": \"${CLOUDRON_OIDC_CLIENT_SECRET}\", \"mail-property\": \"email\", \"auth-display-name\": \"Cloudron\", \"username-property\": \"preferred_username\", \"signature-algorithm\": \"RS256\", \"display-name-property\": \"name\"}' WHERE name='auth-openid-connect'"
}
2020-10-15 04:45:44 +00:00
first_time_setup() {
2022-03-08 05:08:39 +00:00
echo "==> Starting peertube to run migrations on first run"
gosu cloudron:cloudron npm start & # be wary of https://stackoverflow.com/questions/70738567/why-does-running-npm-run-script-as-root-switch-user
2020-06-24 22:02:16 +00:00
sleep 10
2021-09-13 20:13:22 +00:00
while ! curl --silent --output /dev/null --fail http://localhost:9000/; do
2022-03-08 05:08:39 +00:00
echo "==> Waiting for peertube"
2020-06-24 22:02:16 +00:00
sleep 5
2020-06-16 04:47:04 +00:00
done
2022-12-16 16:22:52 +00:00
killall -SIGTERM peertube # this kills the process group
sleep 5
2022-03-08 05:08:39 +00:00
2020-10-15 04:45:44 +00:00
echo "==> Reset root password"
echo "changeme" | gosu cloudron:cloudron npm run reset-password -- -u root
sleep 5 # the above command seems to spawn a separate process to change password in background
2020-10-15 04:45:44 +00:00
2024-01-15 17:01:40 +00:00
install_oidc
echo "==> First time setup complete"
2020-10-15 04:45:44 +00:00
}
update_config() {
echo "==> Ensure and updating configs"
# version 5 needs this now
if [[ `yq eval '.secrets | has("peertube")' /app/data/production.yaml` != "true" ]]; then
yq eval ".secrets.peertube = \"`openssl rand -hex 32`\"" -i /app/data/production.yaml
fi
yq eval ".webserver.hostname = \"${CLOUDRON_APP_DOMAIN}\"" -i /app/data/production.yaml
# database
yq eval ".database.hostname = \"${CLOUDRON_POSTGRESQL_HOST}\"" -i /app/data/production.yaml
yq eval ".database.port = ${CLOUDRON_POSTGRESQL_PORT}" -i /app/data/production.yaml
yq eval ".database.username = \"${CLOUDRON_POSTGRESQL_USERNAME}\"" -i /app/data/production.yaml
yq eval ".database.password = \"${CLOUDRON_POSTGRESQL_PASSWORD}\"" -i /app/data/production.yaml
yq eval ".database.name = \"${CLOUDRON_POSTGRESQL_DATABASE}\"" -i /app/data/production.yaml
yq eval "del(.database.suffix)" -i /app/data/production.yaml
# redis
yq eval ".redis.hostname = \"${CLOUDRON_REDIS_HOST}\"" -i /app/data/production.yaml
yq eval ".redis.port = ${CLOUDRON_REDIS_PORT}" -i /app/data/production.yaml
yq eval ".redis.auth = \"${CLOUDRON_REDIS_PASSWORD}\"" -i /app/data/production.yaml
# smtp
yq eval ".smtp.hostname = \"${CLOUDRON_MAIL_SMTP_SERVER}\"" -i /app/data/production.yaml
yq eval ".smtp.port = ${CLOUDRON_MAIL_SMTP_PORT}" -i /app/data/production.yaml
yq eval ".smtp.username = \"${CLOUDRON_MAIL_SMTP_USERNAME}\"" -i /app/data/production.yaml
yq eval ".smtp.password = \"${CLOUDRON_MAIL_SMTP_PASSWORD}\"" -i /app/data/production.yaml
yq eval ".smtp.tls = false" -i /app/data/production.yaml
yq eval ".smtp.disable_starttls = true" -i /app/data/production.yaml
yq eval ".smtp.from_address = \"${CLOUDRON_MAIL_FROM}\"" -i /app/data/production.yaml
# ensure settings which were later added
yq eval ".storage.bin = \"/app/data/storage/bin/\"" -i /app/data/production.yaml
yq eval ".storage.well_known = \"/app/data/storage/well_known/\"" -i /app/data/production.yaml
2023-06-21 14:15:56 +00:00
yq eval ".storage.tmp_persistent = \"/app/data/storage/tmp_persistent/\"" -i /app/data/production.yaml
2023-12-24 11:46:54 +00:00
# changes for 6.0.0
yq eval ".storage.storyboards = \"/app/data/storage/storyboards/\"" -i /app/data/production.yaml
if [[ -d "/app/data/storage/videos" ]]; then
echo "==> Migrate videos/ to videos-web/"
mv /app/data/storage/videos /app/data/storage/web-videos
fi
yq eval ".storage.web_videos = \"/app/data/storage/web-videos/\"" -i /app/data/production.yaml
yq eval "del(.storage.videos)" -i /app/data/production.yaml
yq eval "del(.transcoding.webtorrent)" -i /app/data/production.yaml
yq eval ".transcoding.web_videos.enabled = true" -i /app/data/production.yaml
# changes for 6.1.0
yq eval ".storage.original_video_files = \"/app/data/storage/original_video_files/\"" -i /app/data/production.yaml
2020-06-16 04:47:04 +00:00
}
echo "==> Changing ownership"
chown -R cloudron:cloudron /app/data /run/peertube /tmp/peertube
# Set eviction policy to prevent warnings - https://docs.bullmq.io/guide/going-to-production#max-memory-policy
while ! REDISCLI_AUTH="${CLOUDRON_REDIS_PASSWORD}" redis-cli -h "${CLOUDRON_REDIS_HOST}" -p "${CLOUDRON_REDIS_PORT}" ping >/dev/null; do
echo "==> Waiting for redis"
sleep 5
done
REDISCLI_AUTH="${CLOUDRON_REDIS_PASSWORD}" redis-cli -h "${CLOUDRON_REDIS_HOST}" -p "${CLOUDRON_REDIS_PORT}" CONFIG SET maxmemory-policy noeviction
2020-06-16 04:47:04 +00:00
# cd /var/www/peertube/peertube-latest/scripts && sudo -H -u peertube ./upgrade.sh
if [[ ! -f "/app/data/production.yaml" ]]; then
echo "==> First run. creating config"
cp /app/pkg/production.yaml.example /app/data/production.yaml
update_config
2022-03-08 05:08:39 +00:00
first_time_setup
2020-10-15 04:45:44 +00:00
else
update_config
2024-01-16 13:12:05 +00:00
if [[ -n "${CLOUDRON_OIDC_ISSUER:-}" ]]; then
install_oidc
migrate_ldap_to_oidc
update_oidc
fi
fi
2024-01-10 17:04:08 +00:00
echo "==> Configuring nginx"
cp /app/pkg/peertube-nginx.conf /run/peertube-nginx.conf
echo "==> Starting PeerTube"
2024-01-10 17:04:08 +00:00
#exec gosu cloudron:cloudron npm start
exec /usr/bin/supervisord --configuration /etc/supervisor/supervisord.conf --nodaemon -i PeerTube