<commit_before>#!/bin/bash

source /etc/environment

mkdir -p $PGDATA /data/shared /data/current
chown -R webapp:webapp /data/shared
chown -R webapp:webapp /data/current
chown -R postgres "$PGDATA"
touch /data/authorized_keys
ln -s /data/authorized_keys /home/webapp/.ssh/authorized_keys
ln -s /data/shared /home/webapp/webapp/shared
ln -s /data/current /home/webapp/webapp/current
rm -rf /var/lib/postgresql/10/main
ln -s $PGDATA /var/lib/postgresql/10/main
chown -R postgres "$PGDATA"
chown -R postgres /var/lib/postgresql/10/main

if [ ! -f $PGDATA/postgresql.conf ]; then #we can assume that we need a database setup
    gosu postgres /usr/lib/postgresql/10/bin/initdb --encoding=UTF-8 --local=en_US.UTF-8 -D $PGDATA
    service postgresql start
    gosu postgres psql -d template1 -c 'CREATE EXTENSION hstore;'
    gosu postgres psql -d template1 -c 'CREATE EXTENSION pg_trgm;'
    gosu postgres psql -c "CREATE USER webapp WITH PASSWORD '<PASSWORD>' LOGIN;"
    gosu postgres psql -c "ALTER USER webapp CREATEDB;"
    gosu postgres psql -c "CREATE DATABASE webapp WITH OWNER webapp ENCODING 'UTF-8' TEMPLATE template1;"
    gosu postgres psql -c "CREATE ROLE readonly LOGIN PASSWORD '<PASSWORD>';"
    gosu postgres psql -c "GRANT CONNECT ON DATABASE webapp TO readonly;"
    gosu postgres psql -c "GRANT USAGE ON SCHEMA public TO readonly;"
    gosu postgres psql -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;"
    gosu postgres psql -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;"
else
    service postgresql start
fi

if [ ! -f "/home/webapp/webapp/shared/.env.$RAILS_ENV" ]; then
    echo Generating secret key base
    echo SECRET_KEY_BASE=`cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 128 | head -n 1` > "/home/webapp/webapp/shared/.env.$RAILS_ENV"
    chown webapp:webapp "/home/webapp/webapp/shared/.env.$RAILS_ENV"
fi

if [ ! -f /data/certs/webapp.crt ]; then
    echo Generating OpenSSL certificate for localhost
    mkdir -p /data/certs/
    openssl req -x509 -newkey rsa:4096 -keyout /data/certs/webapp.key -out /data/certs/webapp.crt -days 2048 -subj '/CN=localhost' -nodes
fi

chown -R webapp:webapp "/home/webapp"

service ssh start
service nginx start
service redis-server start
cron
tail -f /var/log/nginx/access.log<commit_msg>Fixed authorized_keys<commit_after>#!/bin/bash

source /etc/environment

mkdir -p $PGDATA /data/shared /data/current
chown -R webapp:webapp /data/shared
chown -R webapp:webapp /data/current
chown -R postgres "$PGDATA"
ln -s /data/shared /home/webapp/webapp/shared
ln -s /data/current /home/webapp/webapp/current
rm -rf /var/lib/postgresql/10/main
ln -s $PGDATA /var/lib/postgresql/10/main
chown -R postgres "$PGDATA"
chown -R postgres /var/lib/postgresql/10/main

if [ ! -f $PGDATA/postgresql.conf ]; then #we can assume that we need a database setup
    gosu postgres /usr/lib/postgresql/10/bin/initdb --encoding=UTF-8 --local=en_US.UTF-8 -D $PGDATA
    service postgresql start
    gosu postgres psql -d template1 -c 'CREATE EXTENSION hstore;'
    gosu postgres psql -d template1 -c 'CREATE EXTENSION pg_trgm;'
    gosu postgres psql -c "CREATE USER webapp WITH PASSWORD '<PASSWORD>' LOGIN;"
    gosu postgres psql -c "ALTER USER webapp CREATEDB;"
    gosu postgres psql -c "CREATE DATABASE webapp WITH OWNER webapp ENCODING 'UTF-8' TEMPLATE template1;"
    gosu postgres psql -c "CREATE ROLE readonly LOGIN PASSWORD '<PASSWORD>';"
    gosu postgres psql -c "GRANT CONNECT ON DATABASE webapp TO readonly;"
    gosu postgres psql -c "GRANT USAGE ON SCHEMA public TO readonly;"
    gosu postgres psql -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;"
    gosu postgres psql -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;"
else
    service postgresql start
fi

if [ ! -f "/home/webapp/webapp/shared/.env.$RAILS_ENV" ]; then
    echo Generating secret key base
    echo SECRET_KEY_BASE=`cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 128 | head -n 1` > "/home/webapp/webapp/shared/.env.$RAILS_ENV"
    chown webapp:webapp "/home/webapp/webapp/shared/.env.$RAILS_ENV"
fi

if [ ! -f /data/certs/webapp.crt ]; then
    echo Generating OpenSSL certificate for localhost
    mkdir -p /data/certs/
    openssl req -x509 -newkey rsa:4096 -keyout /data/certs/webapp.key -out /data/certs/webapp.crt -days 2048 -subj '/CN=localhost' -nodes
fi

if [ ! -f /data/authorized_keys ]; then
    echo "Creating authorized_keys file"
    touch /data/authorized_keys
    ln -s /data/authorized_keys /home/webapp/.ssh/authorized_keys
    chown webapp:webapp /home/webapp/.ssh/authorized_keys
fi

chown -R webapp:webapp "/home/webapp"

service ssh start
service nginx start
service redis-server start
cron
tail -f /var/log/nginx/access.log
{'fraction_non_alphanumeric': 0.10859063222012183, 'fraction_numerical': 0.013022474270111321, 'mean_word_length': 3.4924528301886792, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '72691', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1718, 'n_words': 534}