168 lines
6.3 KiB
Bash
Executable File
168 lines
6.3 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
|
|
set -e
|
|
|
|
initialise_if_needed() {
|
|
cd /srv/www/obs/api/
|
|
if [ -z "$(mysql -u "${OBS_DB_USER}" -p"${OBS_DB_PASSWD}" -h "${OBS_DB_HOST}" -P "${OBS_DB_PORT}" -e "SELECT table_name FROM information_schema.tables WHERE table_name = 'projects';")" ]; then
|
|
RAILS_ENV="production" /usr/bin/bundle.ruby3.4 exec /usr/lib64/obs-api/ruby/3.4.0/bin/rake db:setup
|
|
RAILS_ENV="production" /usr/bin/bundle.ruby3.4 exec /usr/lib64/obs-api/ruby/3.4.0/bin/rake writeconfiguration
|
|
else
|
|
RAILS_ENV="production" /usr/bin/bundle.ruby3.4 exec /usr/lib64/obs-api/ruby/3.4.0/bin/rake db:migrate:with_data
|
|
fi
|
|
touch /srv/www/obs/api/log/backend_access.log
|
|
chown -R wwwrun.www /srv/www/obs/api/log /srv/www/obs/api/tmp
|
|
}
|
|
|
|
prepare_frontend() {
|
|
cat << EOF > /srv/www/obs/api/config/database.yml
|
|
production:
|
|
adapter: mysql2
|
|
database: ${OBS_DB_DATABASE}
|
|
username: ${OBS_DB_USER}
|
|
password: ${OBS_DB_PASSWD}
|
|
encoding: utf8mb4
|
|
collation: utf8mb4_unicode_ci
|
|
timeout: 15
|
|
pool: 30
|
|
host: ${OBS_DB_HOST}
|
|
port: ${OBS_DB_PORT}
|
|
EOF
|
|
|
|
cat << EOF > /etc/apache2/vhosts.d/obs.conf
|
|
# Passenger defaults
|
|
PassengerSpawnMethod "smart"
|
|
PassengerMaxPoolSize 20
|
|
PassengerFriendlyErrorPages on
|
|
|
|
# allow long request urls and being part of headers
|
|
LimitRequestLine 20000
|
|
LimitRequestFieldsize 20000
|
|
# unlimited request body (default would be 1GB)
|
|
LimitRequestBody 0
|
|
|
|
# OBS WEBUI & API
|
|
<VirtualHost *:80>
|
|
ServerName ${OBS_FRONTEND_HOSTNAME}
|
|
|
|
# General setup for the virtual host
|
|
DocumentRoot "/srv/www/obs/api/public"
|
|
ErrorLog /srv/www/obs/api/log/apache_error.log
|
|
TransferLog /srv/www/obs/api/log/apache_access.log
|
|
|
|
# Enable maintenance mode. All requests will be redirected
|
|
# to the maintenance page and return 503 as http status.
|
|
# Start your apache with -D MAINTENANCE to enable this.
|
|
# On (open)SUSE you can do this by setting
|
|
# APACHE_SERVER_FLAGS="MAINTENANCE" in /etc/sysconfig/apache
|
|
<IfDefine MAINTENANCE>
|
|
ErrorDocument 503 /503.html
|
|
RewriteEngine on
|
|
RewriteCond %{REQUEST_URI} !=/503.html
|
|
RewriteRule ^ - [R=503,L]
|
|
</IfDefine>
|
|
|
|
<Directory /srv/www/obs/api/public>
|
|
AllowOverride all
|
|
Options -MultiViews
|
|
|
|
# This requires mod_xforward loaded in apache
|
|
# Enable the usage via options.yml
|
|
# This will decrease the load due to long running requests a lot (unloading from rails stack)
|
|
XForward on
|
|
|
|
Require all granted
|
|
</Directory>
|
|
|
|
SetEnvIf User-Agent ".*MSIE [1-5].*" \
|
|
nokeepalive ssl-unclean-shutdown \
|
|
downgrade-1.0 force-response-1.0
|
|
|
|
|
|
# from http://guides.rubyonrails.org/asset_pipeline.html
|
|
<LocationMatch "^/assets/.*$">
|
|
Header unset ETag
|
|
FileETag None
|
|
# RFC says only cache for 1 year
|
|
ExpiresActive On
|
|
ExpiresDefault "access plus 1 year"
|
|
</LocationMatch>
|
|
|
|
SetEnvIf User-Agent ".*MSIE [1-5].*" \
|
|
nokeepalive ssl-unclean-shutdown \
|
|
downgrade-1.0 force-response-1.0
|
|
|
|
## Older firefox versions needs this, otherwise it wont cache anything over SSL.
|
|
Header append Cache-Control "public"
|
|
|
|
ProxyPass /v2 ${OBS_SRC_SERVER}/registry
|
|
ProxyPassReverse /v2 ${OBS_SRC_SERVER}/registry
|
|
|
|
</VirtualHost>
|
|
PassengerMinInstances 2
|
|
PassengerPreStart http://${OBS_FRONTEND_HOSTNAME}
|
|
EOF
|
|
|
|
sed -i 's/#use_xforward: true/use_xforward: true/' /srv/www/obs/api/config/options.yml
|
|
sed -i "s/source_host: localhost/source_host: $(echo "${OBS_SRC_SERVER}" | sed -r 's/https?:\/\/(.+):(.+)\/?/\1/')/" /srv/www/obs/api/config/options.yml
|
|
sed -i "s/source_port: localhost/source_port: $(echo "${OBS_SRC_SERVER}" | sed -r 's/https?:\/\/(.+):(.+)\/?/\2/')/" /srv/www/obs/api/config/options.yml
|
|
|
|
# Force setting local bind port
|
|
sed -i "s/frontend_port: 443/frontend_port: 80/" /srv/www/obs/api/config/options.yml
|
|
sed -i "s/frontend_protocol: https/frontend_protocol: http/" /srv/www/obs/api/config/options.yml
|
|
# Adapt for proxies
|
|
sed -i "s/#external_frontend_host: api.opensuse.org/external_frontend_host: ${OBS_FRONTEND_HOSTNAME}/" /srv/www/obs/api/config/options.yml
|
|
sed -i "s/#external_frontend_port: api.opensuse.org/external_frontend_port: ${OBS_FRONTEND_PORT}/" /srv/www/obs/api/config/options.yml
|
|
sed -i "s/#external_frontend_protocol: api.opensuse.org/external_frontend_protocol: ${OBS_FRONTEND_PROTO}/" /srv/www/obs/api/config/options.yml
|
|
|
|
initialise_if_needed
|
|
}
|
|
|
|
generate_supervisor() {
|
|
defined_services="$(sed -rn 's/Wants = (.+)/\1/p' /usr/lib/systemd/system/obs-api-support.target)"
|
|
for service in ${defined_services}; do
|
|
if printf "%s" "${service}" | grep -E '.*@[0-9]+.service' > /dev/null; then
|
|
service_file="/usr/lib/systemd/system/$(printf "%s" "${service}" | sed -nr 's/(.*)@[0-9]+.service/\1@.service/p')"
|
|
service_instance="$(printf "%s" "${service}" | sed -nr 's/.*@([0-9]+).service/\1/p')"
|
|
else
|
|
service_file="/usr/lib/systemd/system/${service}"
|
|
service_instance=""
|
|
fi
|
|
|
|
cat << EOF >> /obs/supervisord.conf.d/obs_api_server.conf
|
|
[program:${service}]
|
|
redirect_stderr=1
|
|
EOF
|
|
if grep "WorkingDirectory" "${service_file}" > /dev/null; then
|
|
cat << EOF >> /obs/supervisord.conf.d/obs_api_server.conf
|
|
directory=$(cat "${service_file}" | sed -nr 's/WorkingDirectory *= *(.*)/\1/p')
|
|
EOF
|
|
fi
|
|
if grep "User" "${service_file}" > /dev/null; then
|
|
cat << EOF >> /obs/supervisord.conf.d/obs_api_server.conf
|
|
user=$(cat "${service_file}" | sed -nr 's/User *= *(.*)/\1/p')
|
|
EOF
|
|
fi
|
|
|
|
if grep "Type = forking" "${service_file}" > /dev/null; then
|
|
command="$(cat "${service_file}" | sed -nr 's/ExecStart = (.*)/\1/p')"
|
|
pidfile="$(cat "${service_file}" | sed -nr 's/PIDFile = (.*)/\1/p')"
|
|
if [ -n "${service_instance}" ]; then
|
|
command="$(printf "%s" "${command}" | sed -nr "s/%i/${service_instance}/p")"
|
|
pidfile="$(printf "%s" "${pidfile}" | sed -nr "s/%i/${service_instance}/p")"
|
|
fi
|
|
cat << EOF >> /obs/supervisord.conf.d/obs_api_server.conf
|
|
command=/obs/pidfile_wrapper.sh ${pidfile} ${command}
|
|
|
|
EOF
|
|
else
|
|
echo "NOT SUPPORTED SYSTEMD SERVICE TYPE"
|
|
exit 1
|
|
fi
|
|
done
|
|
}
|
|
|
|
prepare_frontend
|
|
generate_supervisor
|
|
supervisord -c /obs/supervisord.conf.d/obs_api_server.conf
|