File size: 2,814 Bytes
df3ac6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
906a710
 
 
 
 
df3ac6b
 
 
 
 
906a710
dd0e65c
 
 
 
 
 
 
df3ac6b
dd0e65c
 
 
 
 
 
 
 
 
 
 
 
df3ac6b
 
 
 
dd0e65c
 
df3ac6b
 
 
 
 
 
 
 
dd0e65c
 
 
df3ac6b
 
 
 
dd0e65c
df3ac6b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
set -e

PG_DATA="/var/lib/postgresql/data"
RABBIT_DATA="/var/lib/rabbitmq"

# Ensure directories exist
mkdir -p "$PG_DATA"
mkdir -p "$RABBIT_DATA"
mkdir -p /var/log/libretime /var/log/icecast2 /var/log/nginx /var/log/supervisor
mkdir -p /run/php

# 1. Config Generation
if [ ! -f /etc/libretime/config.yml ]; then
    echo "Generating config.yml..."
    cp /etc/libretime/config.yml.template /etc/libretime/config.yml
fi

# 2. Setup Postgres
if [ ! -s "$PG_DATA/PG_VERSION" ]; then
    echo "Initializing PostgreSQL..."
    /usr/lib/postgresql/13/bin/initdb -D "$PG_DATA"
    echo "host all all 127.0.0.1/32 trust" >> "$PG_DATA/pg_hba.conf"
    echo "host all all ::1/128 trust" >> "$PG_DATA/pg_hba.conf"
    
    echo "Starting PG for setup..."
    /usr/lib/postgresql/13/bin/pg_ctl -D "$PG_DATA" -w start
    
    echo "Creating LibreTime DB and User..."
    # Connect to 'postgres' db as current user (who is superuser due to initdb)
    psql -h localhost -d postgres -c "CREATE DATABASE libretime;" || true
    psql -h localhost -d postgres -c "CREATE USER libretime WITH PASSWORD 'libretime';" || true
    psql -h localhost -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE libretime TO libretime;" || true
    psql -h localhost -d postgres -c "ALTER USER libretime CREATEDB;" || true

    echo "Stopping PG..."
    /usr/lib/postgresql/13/bin/pg_ctl -D "$PG_DATA" stop
fi

# 3. Setup RabbitMQ
export RABBITMQ_NODENAME=rabbit@localhost
export RABBITMQ_LOG_BASE=/var/log/rabbitmq
export RABBITMQ_MNESIA_BASE=/var/lib/rabbitmq/mnesia
mkdir -p /var/log/rabbitmq /var/lib/rabbitmq/mnesia
chown -R user:user /var/log/rabbitmq /var/lib/rabbitmq

echo "Starting RabbitMQ for setup..."
/usr/sbin/rabbitmq-server -detached

echo "Waiting for RabbitMQ to be ready..."
for i in {1..30}; do
    if rabbitmqctl status >/dev/null 2>&1; then
        echo "RabbitMQ is ready."
        break
    fi
    echo "Waiting... ($i/30)"
    sleep 2
done

echo "Configuring RabbitMQ users..."
rabbitmqctl add_user libretime libretime || true
rabbitmqctl set_permissions -p / libretime ".*" ".*" ".*" || true
rabbitmqctl add_vhost /libretime || true
rabbitmqctl set_permissions -p /libretime libretime ".*" ".*" ".*" || true

echo "Stopping RabbitMQ..."
rabbitmqctl stop

# 4. Migrations
echo "Starting PostgreSQL for migration..."
/usr/lib/postgresql/13/bin/pg_ctl -D "$PG_DATA" -w start

echo "Running LibreTime Migrations..."
export LIBRETIME_CONFIG_FILEPATH=/etc/libretime/config.yml
# Use python -m to be safe if the script isn't in PATH
python3 -m libretime_api collectstatic --noinput
python3 -m libretime_api migrate --noinput

echo "Stopping PostgreSQL..."
/usr/lib/postgresql/13/bin/pg_ctl -D "$PG_DATA" stop

echo "Setup complete. Starting Supervisor..."
exec supervisord -c /etc/supervisor/conf.d/supervisord.conf