test/lib/panel.sh
2022-12-13 16:56:20 +00:00

132 lines
3.9 KiB
Bash

#!/bin/bash
#
# Federated Computer Control Panel Service
PATH=$HOME/.docker/cli-plugins:/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
config_panel() {
echo -ne "\n* Configuring /federated/apps/panel container.."
spin &
SPINPID=$!
if [ ! -d "/federated/apps/panel" ]; then
mkdir -p /federated/apps/panel
fi
DOMAIN_ARRAY=(${DOMAIN//./ })
DOMAIN_FIRST=${DOMAIN_ARRAY[0]}
DOMAIN_LAST=${DOMAIN_ARRAY[1]}
cat > /federated/apps/panel/ansible_hosts <<EOF
[servers]
nextcloud ansible_host=172.99.0.15
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
EOF
cp /federated/apps/files/panel/new_user.php apps/panel
cat > /federated/apps/panel/Dockerfile <<EOF
FROM wheelybird/ldap-user-manager:latest
RUN apt update -y && apt-get install ssh -y \
&& apt-get install ansible -y && apt-get install sudo -y
RUN echo 'www-data ALL=(ALL:ALL) NOPASSWD:ALL' >> /etc/sudoers \
&& useradd -m ansible -s /bin/bash \
&& sudo -u ansible mkdir /home/ansible/.ssh && mkdir /etc/ansible
RUN sudo -u ansible ssh-keygen -q -t rsa -N '' -f /home/ansible/.ssh/id_rsa
COPY ansible_hosts /etc/ansible/hosts
COPY new_user.php /opt/ldap_user_manager/account_manager/
EOF
cat > /federated/apps/panel/docker-compose.yml <<EOF
version: '3.7'
services:
panel:
image: wheelybird/ldap-user-manager:latest
container_name: panel
hostname: panel.$DOMAIN
domainname: $DOMAIN
restart: always
build:
context: .
dockerfile: Dockerfile
networks:
federated:
ipv4_address: 172.99.0.12
environment:
- VIRTUAL_PROTO=http
- VIRTUAL_PORT=80
- VIRTUAL_HOST=panel.$DOMAIN
- SERVER_HOSTNAME=panel.$DOMAIN
- LDAP_URI=ldap://ldap.$DOMAIN
- LDAP_BASE_DN=dc=$DOMAIN_FIRST,dc=$DOMAIN_LAST
- LDAP_REQUIRE_STARTTLS=true
- LDAP_ADMINS_GROUP=admins
- LDAP_ADMIN_BIND_DN=cn=admin,dc=$DOMAIN_FIRST,dc=$DOMAIN_LAST
- LDAP_ADMIN_BIND_PWD=$ADMINPASS
- LDAP_ACCOUNT_ADDITIONAL_OBJECTCLASSES=PostfixBookMailAccount
- LDAP_ACCOUNT_ADDITIONAL_ATTRIBUTES=mailEnabled:Mail Enabled:TRUE,mailAlias+:Email aliases
- EMAIL_DOMAIN=$DOMAIN
- USERNAME_FORMAT={first_name}.{last_name}
- SITE_NAME=$COMPANY User Manager
- SMTP_HOSTNAME=mail.$DOMAIN
- SMTP_USERNAME=admin
- SMTP_PASSWORD=$ADMINPASS
- EMAIL_FROM_ADDRESS=admin@$DOMAIN
- SMTP_USE_TLS=true
- NO_HTTPS=true
networks:
federated:
external: true
EOF
kill -9 $SPINPID &> /dev/null
echo -ne "done."
}
start_panel() {
# Start /federated/apps/panel with output to /dev/null
echo -ne "\n* Starting /federated/apps/panel service.."
spin &
SPINPID=$!
if [ $DEBUG ]; then
# Start /federated/apps/panel with output to console for debug
docker-compose -f /federated/apps/panel/docker-compose.yml -p panel up --build
[ $? -eq 0 ] && echo -ne "done.\n" || fail "There was a problem starting service /federated/apps/panel"
else
docker-compose -f /federated/apps/panel/docker-compose.yml -p panel up --build -d &> /dev/null
# Keep trying panel port 443 to make sure it's up
# before we proceed
RETRY="30"
while [ $RETRY -gt 0 ]; do
nc -z 172.99.0.12 80 &> /dev/null
if [ $? -eq 0 ]; then
break
else
if [ "$RETRY" == 1 ]; then
docker-compose -f /federated/apps/panel/docker-compose.yml -p panel down &> /dev/null
kill -9 $SPINPID &> /dev/null
fail "There was a problem starting service /federated/apps/panel\nCheck the output of 'docker logs panel' or turn on\ndebug with -d"
fi
((RETRY--))
sleep 7
fi
done
fi
# Insert ansible key into nextcloud
KEY=`docker exec -it panel bash -c "cat /home/ansible/.ssh/id_rsa.pub"`
docker exec -it nextcloud bash -c "echo $KEY > /home/ansible/.ssh/authorized_keys"
kill -9 $SPINPID &> /dev/null
echo -ne "done."
}