#!/bin/bash
timedatectl set-timezone Europe/Madrid
start_time=`date`
echo "provisioning started: ${start_time}"
echo "provisioning started: ${start_time}"
#### The user must fill these variables - START
# Fully Qualified Name: ex. "nc.localenv.com"
MACHINE_HOSTNAME=
#### The user must fill these variables - END
SYSTEM_USER=usrv
NEXTCLOUD_DB_NAME=nextcloud_db
NEXTCLOUD_DB_USER=nextcloud_usr
NEXTCLOUD_DB_PASSWORD=${NEXTCLOUD_DB_USER}
NEXTCLOUD_DB_PORT=3306
NETWORK_INTERFACE=eth0
#PHP_INI=/etc/php/7.4/apache2/php.ini
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}'`
adduser --disabled-password --gecos GECOS ${SYSTEM_USER}
usermod -p "`openssl passwd -1 -salt 5RPVAd ${SYSTEM_USER}`" ${SYSTEM_USER}
adduser ${SYSTEM_USER} sudo
## Install Nextcloud: https://docs.nextcloud.com/server/stable/admin_manual/installation/example_ubuntu.html
#####
## Step 001
## Check the MD5 of the Nextcloud Installer
##(cd to the folder to avoid a 'File Not Found' error
cd /vagrant/artifacts
## Install the needed packages from apt repositories
apt update
apt install -y mariadb-server
## Replace the MariaDB configuration file and restart MariaDB
cp /vagrant/artifacts/50-server.cnf /etc/mysql/mariadb.conf.d/50-server.cnf
systemctl restart mysql
## Create the database and the user for the Nextcloud backend database
echo "START - Create the database"
mysql -u root -e 'create database nextcloud_db;'
mysql -u root -e "create user nextcloud_usr@'%' identified by 'nextcloud_usr'"
mysql -u root -e "grant all privileges on nextcloud_db.* to 'nextcloud_usr'@'%'"
echo "END - Create the database"
echo "START - Cleanup"
rm -rf /vagrant/artifacts/nextcloud
echo "END - Cleanup"
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:"
echo " You can connect with user ${SYSTEM_USER} via ssh (password ${SYSTEM_USER}): ssh ${SYSTEM_USER}@${MACHINE_HOSTNAME}"
echo " You can connect to this MariaDB instance with the following parameters:"
echo " hostname: ${MACHINE_HOSTNAME}"
echo " database name: ${NEXTCLOUD_DB_NAME}"
echo " username: ${NEXTCLOUD_DB_USER}"
echo " password: ${NEXTCLOUD_DB_PASSWORD}"
echo " port: ${NEXTCLOUD_DB_PORT}"
echo "provisioning started: ${start_time}"
echo "provisioning ended: ${end_time}"