#!/bin/bash
timedatectl set-timezone Europe/Madrid
start_time=`date`
echo "provisioning started: ${start_time}"
MACHINE_HOSTNAME=minio-storage-server-test.localenv.com
MINIO_STORAGE_FOLDER=/usr/local/share/minio/
SYSTEM_USER=usrv
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}'`
adduser --disabled-password --gecos GECOS ${SYSTEM_USER}
usermod -p "`openssl passwd -1 -salt 5RPVAd ${SYSTEM_USER}`" ${SYSTEM_USER}
adduser ${SYSTEM_USER} sudo
#####################################################################
## Script inspired by https://www.digitalocean.com/community/tutorials/how-to-set-up-an-object-storage-server-using-minio-on-ubuntu-18-04
#####################################################################
cp /vagrant/artifacts/minio /usr/local/bin
chmod +x /usr/local/bin/minio
chown ${SYSTEM_USER}:${SYSTEM_USER} /usr/local/bin/minio
mkdir /etc/minio
cp /vagrant/artifacts/minio.conf /etc/minio
sed -i "s|#IP_ADDRESS#|${IP_ADDRESS}|g" /etc/minio/minio.conf
sed -i "s|#MINIO_STORAGE_FOLDER#|${MINIO_STORAGE_FOLDER}|g" /etc/minio/minio.conf
chown -R ${SYSTEM_USER}:${SYSTEM_USER} /etc/minio
cp /vagrant/artifacts/minio.service /etc/systemd/system/
sed -i "s|#SYSTEM_USER#|${SYSTEM_USER}|g" /etc/systemd/system/minio.service
mkdir ${MINIO_STORAGE_FOLDER}
chown -R ${SYSTEM_USER}:${SYSTEM_USER} ${MINIO_STORAGE_FOLDER}
systemctl daemon-reload
systemctl enable minio
systemctl start minio
systemctl status minio
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 with user minio (password miniostorage) to te Minio Web Console http://${MACHINE_HOSTNAME}:9000. These are also the connection details to be used in config.php."
echo "provisioning started: ${start_time}"
echo "provisioning ended: ${end_time}"