#!/bin/bash
timedatectl set-timezone Europe/Madrid
start_time=`date`
echo "provisioning started: ${start_time}"
NETWORK_INTERFACE=eth0
hostnamectl set-hostname ${MACHINE_HOSTNAME}
# Print some information about the container OS
hostnamectl
# Print some information about the container timezone
timedatectl
#####################################################################
## Get the IP address into an environment variable. This command outputs
## an empty variable if the network interface name is not ${NETWORK_INTERFACE}
#####################################################################
ip_address=`ip -4 addr show ${NETWORK_INTERFACE} | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`
export DEBIAN_FRONTEND=noninteractive
apt update
# Quiet installation of OpenLDAP server
# https://apassionatechie.wordpress.com/2017/12/12/automating-slapd-install/
cat /vagrant/artifacts/debconf-slapd.conf | debconf-set-selections
apt install ldap-utils slapd -y
# Install phpldapadmin
# https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-openldap-and-phpldapadmin-on-ubuntu-16-04
apt install -y software-properties-common apt-transport-https
## It seems that phpldap has issues with PHP8.1, so enabling the PHP7.4 repository
add-apt-repository ppa:ondrej/php -y
apt install -y apache2 \
apache2-bin \
apache2-data \
apache2-utils \
file \
libapache2-mod-php7.4 \
libapr1 \
libaprutil1 \
libaprutil1-dbd-sqlite3 \
libaprutil1-ldap \
libcurl4 \
libjansson4 \
liblua5.3-0 \
libmagic-mgc \
libmagic1 \
libxslt1.1 \
mailcap \
mime-support \
php7.4-common \
php7.4-ldap \
php7.4-xml \
php7.4 \
php7.4-cli \
mkcert
apt install phpldapadmin -y
## Patch https://github.com/leenooks/phpLDAPadmin/pull/176
cp /vagrant/artifacts/functions.php /usr/share/phpldapadmin/lib/
cp /vagrant/artifacts/config.php /etc/phpldapadmin
cp /vagrant/artifacts/phpldapadmin.conf /etc/apache2/sites-available/
sed -i "s|#MACHINE_HOSTNAME#|${MACHINE_HOSTNAME}|g" /etc/apache2/sites-available/phpldapadmin.conf
export CAROOT=/vagrant/artifacts/
mkcert -install
mkcert --cert-file /etc/ssl/certs/${MACHINE_HOSTNAME}.pem --key-file /etc/ssl/private/${MACHINE_HOSTNAME}-key.pem "${MACHINE_HOSTNAME}"
a2enmod ssl
a2ensite phpldapadmin.conf
a2dissite 000-default.conf
systemctl restart apache2
end_time=`date`
echo "This container has IP (interface: ${NETWORK_INTERFACE}): ${ip_address}"
echo "If you add this IP to the hostname (${MACHINE_HOSTNAME}) in your hosts file or configure LXD nameserver,"
echo "Your LDAP instance can be managed with phpLDAPadmin, accessible via the following URL: https://${MACHINE_HOSTNAME}/phpldapadmin"
echo "with user 'admin' and password 'admin'"
echo "provisioning started: ${start_time}"
echo "provisioning ended: ${end_time}"