-
Notifications
You must be signed in to change notification settings - Fork 1
/
app-template-py
executable file
·110 lines (84 loc) · 3.7 KB
/
app-template-py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
# Convert a Python application created via the Control Panel using our app template.
# Usage: app-template-py APP_NAME [PY_VERSION]
# Validate script arguments.
[ $# -lt 1 ] && { echo -e "\e[1;37mUsage:\e[0m \e[33m`basename ${BASH_SOURCE[0]}` APP_NAME [PY_VERSION]\e[0m"; exit 1; }
. `dirname ${BASH_SOURCE[0]}`/_py-utils
# Initialization.
APP_NAME=$1
PY_VERSION=$2
[ -z "$PY_VERSION" ] && PY_EXEC=`pyLatestAvailableExec` || PY_EXEC=`pyWhichExec $PY_VERSION`
TEMPLATE_DIR="$HOME/templates/python"
APP_DIR="$HOME/apps/$APP_NAME"
# Check that application exists.
[ -d "$APP_DIR" ] || { echo -e "\e[31mERROR: App '$APP_NAME' does not exist.\e[0m"; exit 1; }
# Install Python version if not available.
if [ -z "$PY_EXEC" ]; then
echo -e "\e[1;37mInstalling Python $PY_VERSION…\e[0m"
py-install-version $PY_VERSION
if [ $? = 0 ]; then
PY_EXEC=`pyWhichExec $PY_VERSION`
[ -z "$PY_EXEC" ] && { echo -e "\e[31mERROR: Python $PY_VERSION executable not found.\e[0m"; exit 1; }
else
echo -e "\e[31mERROR: Installation of Python $PY_VERSION failed.\e[0m"
echo -e "Try installing it manually using: \e[36mpy-install-version $PY_VERSION --force\e[0m"
exit 1
fi
fi
# Converting the app.
cd $APP_DIR
echo -e "\n\e[1;37mConverting \e[36m$APP_DIR\e[1;37m using Python \e[36m`pyRealVersion $PY_EXEC`\e[1;37m.\e[0m\n"
# Retrieve application port.
APP_PORT=`grep -oP "^http(-socket)? = 127\.0\.0\.1:\K[0-9]+$" uwsgi.ini`
[ -z "$APP_PORT" ] && echo -e "\e[0;31mERROR: App port not extracted correctly.\nModify manually 'uwsgi.ini' by retrieving the value in the '.original-files' directory.\e[0m"
# Remove current cron job.
echo -e "\e[1;37mRemoving current cron job.\e[0m"
cron_selector="$APP_DIR/start"
CRON_JOB_SCHEDULE=`crontab -l | grep -oP ".+(?= ${cron_selector}.*)"`
[ -n "$CRON_JOB_SCHEDULE" ] && (crontab -l | grep -v "$cron_selector" | crontab -)
# Stop the current application.
[ -f ./stop ] && ./stop > /dev/null
# Move current files to backup folder.
echo -e "\e[1;37mMoving current files into '.original-files/'.\e[0m"
mkdir -p .original-files && chmod 770 $_
mv * .* .original-files/ 2> /dev/null
# Create files from template.
echo -e "\e[1;37mCopying template files.\e[0m"
cp -r $TEMPLATE_DIR/. .
rm README.md
find . -name .gitkeep -delete
# Create virtual environment.
echo -e "\e[1;37mCreating virtual environment.\e[0m"
$PY_EXEC -m venv venv
[ $? = 0 ] || { echo -e "\e[31mERROR: Virtual environment creation failed.\e[0m"; exit 1; }
# Update configuration files.
sed -i "s#%HOME%#$HOME#g;s/%APP_NAME%/$APP_NAME/g;s/%APP_PORT%/$APP_PORT/" uwsgi.ini
# Ensure permissions are correct.
chmod 750 app tmp venv
setfacl -m u:nginx:r-x app venv
chmod 640 opalstack-requirements.txt uwsgi.ini
setfacl -m u:nginx:r-- app venv
# Install pip packages.
echo -e "\e[1;37mInstalling pip packages…\e[0m"
. venv/bin/activate
pip install --upgrade -r opalstack-requirements.txt
deactivate
# Create new cron job.
echo -e "\e[1;37mCreating new cron job.\e[0m"
[ -z "$CRON_JOB_SCHEDULE" ] && CRON_JOB_SCHEDULE="*/10 * * * *"
(crontab -l; echo "$CRON_JOB_SCHEDULE \$HOME/bin/app-control $APP_NAME cron") | crontab -
# Create symlinks to logs.
mkdir -p logs && chmod 750 $_ && cd $_
ln -s $HOME/logs/apps/$APP_NAME app
ln -s $HOME/logs/apps/$APP_NAME/uwsgi.log
ln -s $HOME/logs/sites/$APP_NAME site
ln -s $HOME/logs/sites/$APP_NAME/access.log
ln -s $HOME/logs/sites/$APP_NAME/error.log
cd ..
# Execution report.
echo -e "\n\e[32mApplication \e[36m$APP_NAME\e[32m successfully converted.\e[0m"
echo -e "\n\e[1;37mNext steps:\e[0m"
echo -e "- Copy your application files in \e[36m$APP_DIR/app/\e[0m"
echo -e "- Modify \e[36m$APP_DIR/uwsgi.ini\e[0m if/as needed"
echo -e "- Start the application: \e[36mapp-control $APP_NAME start\e[0m"
echo