websoft9/apps/roles/role_init/templates/init.sh.jinja2

65 lines
2.7 KiB
Plaintext
Raw Normal View History

2022-07-11 14:03:55 +08:00
#!/bin/bash
2022-08-02 10:19:29 +08:00
# support special char ["`$%()[]{},.*+-:;<>?_~/|]
2022-08-02 10:48:47 +08:00
new_password=$(pwgen -ncCs 14 1)!
2022-07-11 14:03:55 +08:00
sudo sleep 10s
sudo sh -c 'echo "init-password started at" $(date -d now) 1>> /tmp/init_debug.txt'
2022-07-11 14:40:24 +08:00
{% for app_name in applist.stdout_lines %}
2022-07-12 08:43:15 +08:00
2022-08-01 10:49:05 +08:00
dockercompose_up=$(cat /data/apps/{{app_name}}/.env |grep "APP_COMPOSE_UP=false")
2022-07-12 08:43:15 +08:00
#1 init for applist of .env
2022-08-02 14:47:41 +08:00
if [ ! $dockercompose_up ]; then
2022-08-01 10:49:05 +08:00
sudo docker compose -f /data/apps/{{app_name}}/docker-compose.yml down -v
fi
2022-07-12 08:43:15 +08:00
# init password
2022-07-13 10:28:49 +08:00
sudo sed -i "s/POWER_PASSWORD=.*/POWER_PASSWORD=$new_password/g" /data/apps/{{app_name}}/.env
# has_app_encrypt
2022-08-02 17:53:30 +08:00
has_app_encrypt=$(cat /data/apps/{{app_name}}/.env |grep "APP_ENCRYPT_PASSWORD")
2022-08-03 10:06:42 +08:00
if [[ "$has_app_encrypt" =~ "APP_ENCRYPT_PASSWORD" ]]; then
echo "Encrypt password: $has_app_encrypt" 1>> /tmp/init_debug.txt
2022-07-13 10:28:49 +08:00
app_encrypt_password=$(bash /data/apps/{{app_name}}/src/encrypt.sh $new_password)
sudo sed -i "s/APP_ENCRYPT_PASSWORD=.*/APP_ENCRYPT_PASSWORD=$app_encrypt_password/g" /data/apps/{{app_name}}/.env
2022-07-12 10:52:40 +08:00
fi
2022-07-12 18:01:26 +08:00
# change all the variables to real value,such as DB_MYSQL_PASSWORD=$APP_PASSWORD=>DB_MYSQL_PASSWORD=ues983ks9309023!
2022-07-12 18:02:27 +08:00
cd /data/apps/{{app_name}}
2022-07-13 11:26:42 +08:00
export $(cat .env)
envsubst < .env > temp
cat temp > .env
rm -f temp
2022-07-13 11:57:25 +08:00
2022-07-12 08:43:15 +08:00
# init APP_URL
2022-07-12 09:12:42 +08:00
app_url_replace=$(cat /data/apps/{{app_name}}/.env |grep APP_URL_REPLACE)
2022-08-02 14:47:41 +08:00
if [ $app_url_replace = "APP_URL_REPLACE=true" ]; then
2022-07-14 09:17:30 +08:00
echo "Change APP_URL" 1>> /tmp/init_debug.txt
2022-07-12 09:12:42 +08:00
public_ip=`wget -O - https://download.websoft9.com/ansible/get_ip.sh | bash`
sudo sed -i "s/APP_URL=.*/APP_URL=$public_ip/g" /data/apps/{{app_name}}/.env
else
echo "There is not APP_URL"
fi
2022-07-12 08:43:15 +08:00
2022-07-13 10:28:49 +08:00
# delete POWER_PASSWORD
sudo sed -i "s/POWER_PASSWORD=.*//g" /data/apps/{{app_name}}/.env
2022-08-01 10:49:05 +08:00
2022-08-02 14:47:41 +08:00
if [ ! $dockercompose_up ]; then
2022-08-01 10:49:05 +08:00
sudo docker compose -f /data/apps/{{app_name}}/docker-compose.yml up -d
fi
2022-07-11 14:03:55 +08:00
2022-07-11 15:29:11 +08:00
#2 init for applist of special
2022-07-14 10:49:18 +08:00
echo "Executing after_up.sh" 1>> /tmp/init_debug.txt
bash /data/apps/{{app_name}}/src/after_up.sh
2022-07-12 08:43:15 +08:00
2022-08-03 15:13:28 +08:00
#3 create password file for guests
2022-08-03 17:12:09 +08:00
echo "========= credentials for {{app_name}} =========" >> /credentials/password.txt
2022-08-03 15:13:28 +08:00
cat /data/apps/{{app_name}}/.env | grep 'APP_USER' |awk -F"=" '{print $1": "$2}' >> /credentials/password.txt
cat /data/apps/{{app_name}}/.env | grep 'APP_PASSWORD' |awk -F"=" '{print $1": "$2}' >> /credentials/password.txt
cat /data/apps/{{app_name}}/.env | grep 'APP_VERSION' |awk -F"=" '{print $1": "$2}' >> /credentials/password.txt
cat /data/apps/{{app_name}}/.env | grep 'APP_PORT' |awk -F"=" '{print $1": "$2}' >> /credentials/password.txt
2022-08-03 17:12:09 +08:00
cat /data/apps/{{app_name}}/.env | grep 'DB_' |awk -F"=" '{print $1": "$2}' >> /credentials/password.txt
2022-08-03 16:08:15 +08:00
echo " " >> /credentials/password.txt
2022-07-12 08:43:15 +08:00
{% endfor %}
2022-07-11 15:29:11 +08:00
2022-08-03 09:49:23 +08:00
sudo echo "init docker ended at" $(date -d now) 1>> /tmp/init_debug.txt