#!/bin/bash
echo LANG=en_US.utf-8 > /etc/environment
echo LC_ALL=en_US.utf-8 >> /etc/environment
mkdir ~ec2-user/stage/ 2>/dev/null
cd ~ec2-user/stage/
yum -y install git awscli httpd mod_ssl
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
REGION_CODE=`curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/placement/region`
INSTANCE_ID=`curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id`
echo export REGION_CODE=${REGION_CODE} > ~ec2-user/cloud-env.sh
echo export INSTANCE_ID=${INSTANCE_ID} >> ~ec2-user/cloud-env.sh
alias cp='cp'
su - ec2-user -c "pip3 install boto3"
chown -R ec2-user:ec2-user ~ec2-user/*
chmod +x ~ec2-user/*.sh
amazon-linux-extras install -y php8.0
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www && find /var/www -type d -exec chmod 2775 {} \;
find /var/www -type f -exec chmod 0664 {} \;
aws s3 cp s3://pe-lab-control/config_v2/pingcap.net.cert /etc/pki/tls/certs/
aws s3 cp s3://pe-lab-control/config_v2/pingcap.net_chain.cert /etc/pki/tls/certs/
aws s3 cp s3://pe-lab-control/config_v2/ca.cert /etc/pki/tls/certs/
chmod 644 /etc/pki/tls/certs/*.cert
aws s3 cp s3://pe-lab-control/config_v2/pingcap.net.key /etc/pki/tls/private/
chmod 600 /etc/pki/tls/private/pingcap.net.key
aws s3 cp s3://pe-lab-control/config_v2/ssl.conf /etc/httpd/conf.d/
aws s3 cp s3://pe-lab-control/config_v2/httpd.conf /etc/httpd/conf/
chmod 644 /etc/httpd/conf.d/ssl.conf
chmod 644 /etc/httpd/conf/httpd.conf
su - ec2-user -c "aws s3 sync s3://pe-lab-control/ui_v2/ /var/www/html/"
rm -f google-api-php-client--PHP8.0.zip
wget https://pingcap-edu.s3.us-west-2.amazonaws.com/google-api-php-client--PHP8.0.zip
unzip -u google-api-php-client--PHP8.0.zip -d /var/www/html/google-api-php-client
rm -f google-api-php-client--PHP8.0.zip
mkdir /var/www/wallet/ 2>/dev/null
aws s3 cp s3://pe-lab-control/config_v2/client_auth_config_lab-control.pingcap.net.json /var/www/wallet/
chown -R ec2-user:apache /var/www/wallet/
rm -f /etc/httpd/conf.d/userdir.conf
rm -f /etc/httpd/conf.d/autoindex.conf
systemctl restart httpd
systemctl enable httpd
aws s3 sync s3://pe-lab-control/lab_control_scripts_v2/ /usr/share/httpd/lab_control_scripts/
chown -R apache /usr/share/httpd/lab_control_scripts/
chmod -R 550 /usr/share/httpd/lab_control_scripts/
chown apache /usr/share/httpd
cp /etc/passwd /etc/passwd.bak
sed -i '' -e "s/Apache:\\/usr\\/share\\/httpd:\\/sbin\\/nologin/Apache:\\/usr\\/share\\/httpd:\\/bin\\/bash/g" /etc/passwd 2>/dev/null
su - apache -c "pip3 install boto3"
mkdir /usr/share/httpd/.ssh/ 2>/dev/null
aws s3 sync s3://pe-lab-control/config_v2/keys /usr/share/httpd/.ssh/
chown -R apache /usr/share/httpd/.ssh/
chmod 400 /usr/share/httpd/.ssh/*/*.pem
mkdir /usr/share/httpd/logs/ 2>/dev/null
chown apache:apache /usr/share/httpd/logs/
chown root:root /var/www/html/.htaccess
chmod 644 /var/www/html/.htaccess
chown root:root /var/www/html/lab-controller/.htaccess
chmod 644 /var/www/html/lab-controller/.htaccess
su - apache -c "curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh"
su - apache -c "tiup install cloud"
su - apache -c "tiup cloud:v1.0.0-beta.1"
aws s3 cp s3://pe-lab-control/config_v2/config.toml /usr/share/httpd/.ticloud/
chown apache:apache /usr/share/httpd/.ticloud/config.toml
chmod 600 /usr/share/httpd/.ticloud/config.toml
su - apache -c "pip3 install requests"
su - apache -c "pip3 install mysql-connector"