diff --git a/lib/nextcloud.sh b/lib/nextcloud.sh index 71ec499..5e84e36 100644 --- a/lib/nextcloud.sh +++ b/lib/nextcloud.sh @@ -10,7 +10,12 @@ config_nextcloud() { SPINPID=$! if [ ! -d "/federated/apps/nextcloud" ]; then + mkdir -p /federated/apps/nextcloud/data/root &> /dev/null + mkdir -p /federated/apps/nextcloud/data/home &> /dev/null mkdir -p /federated/apps/nextcloud/data/var/www/html &> /dev/null + mkdir -p /federated/apps/nextcloud/data/var/www/html/custom_apps &> /dev/null + mkdir -p /federated/apps/nextcloud/data/var/www/config &> /dev/null + mkdir -p /federated/apps/nextcloud/data/var/www/data &> /dev/null fi DOMAIN_ARRAY=(${DOMAIN//./ }) @@ -27,21 +32,25 @@ services: hostname: nextcloud.$DOMAIN domainname: $DOMAIN restart: always - build: - context: . - dockerfile: Dockerfile +# working_dir: /var/www/html networks: federated: ipv4_address: 172.99.0.16 extra_hosts: - "collabora.$DOMAIN:$EXTERNALIP" volumes: + - ./data/root:/root + - ./data/home:/home - ./data/var/www/html:/var/www/html + - ./data/var/www/html/custom_apps:/var/www/html/custom_apps + - ./data/var/www/config:/var/www/config + - ./data/var/www/data:/var/www/data env_file: - ./.env secrets: - federated_psql_password - federated_nextcloud_password + command: [ "/root/nextcloud.sh" ] secrets: federated_psql_password: @@ -53,12 +62,13 @@ networks: external: true EOF -cp /federated/apps/postgresql/.postgresql.secret /federated/apps/nextcloud/ +echo "$NEXTCLOUD_SECRET" > /federated/apps/nextcloud/.postgresql.secret echo "$ADMINPASS" > /federated/apps/nextcloud/.nextcloud.secret chmod 600 /federated/apps/nextcloud/.postgresql.secret /federated/apps/nextcloud/.nextcloud.secret cat > /federated/apps/nextcloud/.env < /federated/apps/nextcloud/supervisord.conf < /federated/apps/nextcloud/data/root/supervisord.conf <<'EOF' [supervisord] nodaemon=true logfile=/var/log/supervisord/supervisord.log @@ -98,26 +108,35 @@ stderr_logfile_maxbytes=0 command=service ssh start EOF -cat > /federated/apps/nextcloud/Dockerfile < /federated/apps/nextcloud/data/root/nextcloud.sh <<'EOF' +#!/bin/sh -x -RUN apt update -y && apt-get install ssh -y \ -&& apt-get install python3 -y && apt-get install sudo -y -RUN echo 'ansible ALL=(ALL:ALL) NOPASSWD:ALL' >> /etc/sudoers \ -&& useradd -m ansible -s /bin/bash \ -&& sudo -u ansible mkdir /home/ansible/.ssh \ -&& mkdir -p /var/run/sshd +PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -RUN apt-get install -y supervisor \ +if [ ! -e /root/.init ]; then + echo "First start and initialization" + apt update -y && apt-get install ssh -y + apt-get install python3 -y && apt-get install sudo -y + + echo 'ansible ALL=(ALL:ALL) NOPASSWD:ALL' >> /etc/sudoers \ + && useradd -m ansible -s /bin/bash \ + && sudo -u ansible mkdir /home/ansible/.ssh \ + && mkdir -p /var/run/sshd + + apt-get install -y supervisor \ && rm -rf /var/lib/apt/lists/* \ && mkdir /var/log/supervisord /var/run/supervisord -COPY supervisord.conf / - -ENV NEXTCLOUD_UPDATE=1 - -CMD ["/usr/bin/supervisord", "-c", "/supervisord.conf"] + touch /root/.init + export NEXTCLOUD_UPDATE=1 + /usr/bin/supervisord -c /root/supervisord.conf +else + echo "Already Initialized, running supervisord" + export NEXTCLOUD_UPDATE=1 + /usr/bin/supervisord -c /root/supervisord.conf +fi EOF +chmod +x /federated/apps/nextcloud/data/root/nextcloud.sh cat > /federated/apps/nextcloud/data/configs.json < /federated/apps/nextcloud/data/configs.json < /federated/apps/nextcloud/data/nextcloud.sh < /federated/apps/nextcloud/data/config.sh < /dev/null echo -ne "done." } start_nextcloud() { - echo -ne "\n* Starting /federated/apps/nextcloud service.." - spin & - SPINPID=$! + # Start service with command to make sure it's up before proceeding + start_service "nextcloud" "nc -z 172.99.0.16 80 &> /dev/null" - if [ $DEBUG ]; then - # Start /federated/apps/nextcloud with output to console for debug - docker-compose -f /federated/apps/nextcloud/docker-compose.yml -p nextcloud up --build - [ $? -eq 0 ] && echo -ne "done.\n" || fail "There was a problem starting service /federated/apps/nextcloud" - else - docker-compose -f /federated/apps/nextcloud/docker-compose.yml -p nextcloud up --build -d &> /dev/null + # Move config.sh and sidemenu config, set config.sh executable + mv /federated/apps/nextcloud/data/config.sh /federated/apps/nextcloud/data/configs.json /federated/apps/nextcloud/data/var/www/html/ + docker exec -it nextcloud chown www-data:root /var/www/html/config.sh /var/www/html/configs.json + docker exec -it nextcloud chmod 755 /var/www/html/config.sh + [ $? -ne 0 ] && fail "Couldn't chown config.sh in /federated/apps/nextcloud container" - # Keep trying nextcloud port 80 to make sure it's up - # before we proceed - RETRY="35" - while [ $RETRY -gt 0 ]; do - nc -z 172.99.0.16 80 &> /dev/null - if [ $? -eq 0 ]; then - break - else - if [ "$RETRY" == 1 ]; then - docker-compose -f /federated/apps/nextcloud/docker-compose.yml -p nextcloud down &> /dev/null - kill -9 $SPINPID &> /dev/null - fail "There was a problem starting service /federated/apps/nextcloud\nCheck the output of 'docker logs nextcloud' or turn on\ndebug with -d" - fi - ((RETRY--)) - sleep 7 - fi - done - fi - - # Move nextcloud.sh and sidemenu config, set nextcloud.sh executable - mv /federated/apps/nextcloud/data/nextcloud.sh /federated/apps/nextcloud/data/configs.json /federated/apps/nextcloud/data/var/www/html/ - docker exec -it nextcloud chown www-data:root /var/www/html/nextcloud.sh /var/www/html/configs.json - docker exec -it nextcloud chmod 755 /var/www/html/nextcloud.sh - [ $? -ne 0 ] && fail "Couldn't chown nextcloud.sh in /federated/apps/nextcloud container" - - # Run nextcloud.sh - Setup LDAP, configuration for nextcloud - docker exec -it -u 33 nextcloud /var/www/html/nextcloud.sh &> /dev/null - [ $? -ne 0 ] && fail "Couldn't run nextcloud.sh inside /federated/apps/nextcloud container" + # Run config.sh - Setup LDAP, configuration for nextcloud + docker exec -it -u 33 nextcloud /var/www/html/config.sh &> /dev/null + [ $? -ne 0 ] && fail "Couldn't run config.sh inside /federated/apps/nextcloud container" kill -9 $SPINPID &> /dev/null echo -ne "done." diff --git a/lib/postgresql.sh b/lib/postgresql.sh index 0e51424..7ca244f 100644 --- a/lib/postgresql.sh +++ b/lib/postgresql.sh @@ -48,7 +48,7 @@ services: -c ssl_cert_file=/var/lib/postgresql/server.crt -c ssl_key_file=/var/lib/postgresql/server.key healthcheck: - test: ["CMD-SHELL", "pg_isready -U nextcloud"] + test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 10s timeout: 5s retries: 5 @@ -63,8 +63,8 @@ EOF cat > /federated/apps/postgresql/.env < /federated/apps/postgresql/.postgresql.secret chmod 600 /federated/apps/postgresql/.postgresql.secret +NEXTCLOUD_SECRET=$(create_password); VAULTWARDEN_SECRET=$(create_password); LISTMONK_SECRET=$(create_password); MATRIX_SECRET=$(create_password); @@ -81,6 +82,9 @@ CALCOM_SECRET=$(create_password); # cat postgresql/data/docker-entrypoint-initdb.d/init.sql cat > /federated/apps/postgresql/data/docker-entrypoint-initdb.d/init.sql < /dev/null echo -ne "done." } - start_postgresql() { - # Start /federated/apps/postgresql with output to /dev/null - echo -ne "\n* Starting /federated/apps/postgresql service.." - spin & - SPINPID=$! - - if [ $DEBUG ]; then - # Start /federated/apps/postgresql with output to console for debug - docker-compose -f /federated/apps/postgresql/docker-compose.yml -p postgresql up - [ $? -eq 0 ] && echo -ne "done.\n" || fail "There was a problem starting service /federated/apps/postgresql" - else - docker-compose -f /federated/apps/postgresql/docker-compose.yml -p postgresql up -d &> /dev/null - - # Keep trying postgresql port 5432 to make sure it's up - # before we proceed - RETRY="30" - while [ $RETRY -gt 0 ]; do - nc -z 172.99.0.11 5432 &> /dev/null - if [ $? -eq 0 ]; then - break - else - if [ "$RETRY" == 1 ]; then - docker-compose -f /federated/apps/postgresql/docker-compose.yml -p postgresql down &> /dev/null - kill -9 $SPINPID &> /dev/null - fail "There was a problem starting service /federated/apps/postgresql\nCheck the output of 'docker logs postgresql' or turn on\ndebug with -d" - fi - ((RETRY--)) - sleep 7 - fi - done - fi + # Start service with command to make sure it's up before proceeding + start_service "postgresql" "nc -z 172.99.0.11 5432 &> /dev/null" kill -9 $SPINPID &> /dev/null echo -ne "done."