-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
·212 lines (167 loc) · 7.41 KB
/
setup.sh
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash
#Virtual Environment Setup
echo -e "\n------- Django Template Project Setup -------\n"
#user input for django config
read -p ">>> Set django 'projectname': " projectname
if [ "$projectname" == "" ]; then
echo -e "\nNo projectname has been given. Exiting setup.\n"; return; fi
read -p ">>> Set django 'appname': " appname
if [ "$appname" == "" ]; then
echo -e "\nNo appname has been given. Exiting setup.\n"; return; fi
read -p ">>> Set django 'local ip', (default. 127.0.0.1): " localip
read -p ">>> Set django external 'ip' or 'url', (leave blank for none): " allowedhost
read -p ">>> Set django 'port', (default. 8000): " port
read -p ">>> Do you want to setup Nginx? [y/N]: " nginx
if [ "$localip" == "" ]; then localip="127.0.0.1"; fi
if [ "$allowedhost" == "" ]; then allowedhost="None"; fi
if [ "$port" == "" ]; then port="8000"; fi
echo -e "\n============================================="
echo -e "\nSettings will be made as following:"
echo -e "----------------------------------------------"
echo -en "
Projectname: $projectname
Appname: $appname
Local IP: $localip
External URL: $allowedhost
Port: $port
Nginx: "
if [ "$nginx" == "y" ] || [ "$nginx" == "Y" ];
then
echo -e "Yes\n"
else
echo -e "No\n"
fi
echo -e "----------------------------------------------\n"
echo -e "A python virtual environment (venv) will be created as well.\n"
echo -e "Note: No input settings are checked for correctness."
echo -e "Any settings you choose will be used regardless of legitimacy.\n"
read -p "Proceed with these settings? [Y/n]: " correctconfig
#User Setting Consent Check
if [ "$correctconfig" == "n" ] || [ "$correctconfig" == "N" ];
then
echo -e "\n>>>>>>>==================================<<<<<<<"
echo -e "Django Project Config was manually interrupted!"
echo -e ">>>>>>>==================================<<<<<<<\n"
return
fi
echo -e "\n============================================="
echo -e "--Setup: Creating virtual environment..."
echo -e "=============================================\n"
mkdir ../$projectname
virtualenv --python=python3 ../$projectname/venv
echo -e "--\nSetup: Starting virtual environment...\n"
source ../$projectname/venv/bin/activate
echo -e "============================================="
echo -e "--Setup: Installing requirements with pip3..."
echo -e "=============================================\n"
pip3 install -r requirements.txt
echo -e "\n--Setup: Dependencies are done.\n"
#Django Config
echo -e ">>>>>>>============Django=============<<<<<<<"
echo -e "--Setup: Django Project Config up next:"
echo -e "============================================="
#django project and app creation
django-admin startproject $projectname
cd $projectname
python3 manage.py startapp $appname
cd .. #brings us back up to document root
echo -e "\n--Setup: Configuring local files...\n"
appdir=$projectname/$appname
projappdir=$projectname/$projectname
settings=$projappdir/settings.py
#Places Static and Template folders into app
cp -r templfiles/static $appdir/static
cp -r templfiles/templates $appdir/templates
#Place views/urls files
cp templfiles/misc/main_urls.py $projappdir/urls.py
cp templfiles/misc/app_urls.py $appdir/urls.py
cp templfiles/misc/app_views.py $appdir/views.py
cp templfiles/misc/app_models.py $appdir/models.py
cp templfiles/misc/app_forms.py $appdir/forms.py
#Add Migration File
cp templfiles/misc/migrate.sh $projectname/migrate.sh
chmod +x $projectname/migrate.sh
#Modify views/urls/settings to route correctly
sed -i "s/appname/$appname/g" $projappdir/urls.py
sed -i "s/'DIRS'\: \[\]/'DIRS'\: \[\'$appname\/templates\'\]/g" $settings
cat templfiles/misc/static-dir-code >> $settings
sed -i "s/appname_example/$appname/g" $settings
sed -i "/'django.contrib.staticfiles',/a # DjangoApps\n '$appname'," $settings
sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \[\'$localip\'\]/g" $settings
if [ "$allowedhost" != "None" ]; then
sed -i "s/ALLOWED_HOSTS = \[/ALLOWED_HOSTS = \['$allowedhost',/g" $settings
fi
#Django migration
cd $projectname
echo -e "--Setup: Django Migration...\n"
python3 manage.py makemigrations
python3 manage.py migrate
#python collect static - redirects output to /dev/null
echo -e "\n--Setup: Collecting Django Static Files...\n"
python3 manage.py collectstatic > /dev/null
#Creating Run File
echo "#!/bin/bash" > run.sh
echo "source venv/bin/activate" >> run.sh
echo "python3 manage.py runserver $localip:$port" >> run.sh
chmod +x run.sh
cd ..
#NGINX config ---------------------------------------------
#User Setting Consent Check
if [ "$nginx" == "y" ] || [ "$nginx" == "Y" ];
then
echo -e "\n============================================="
echo -e "--------------- NGINX Setup -----------------"
echo -e "=============================================\n"
echo -e "--Setup: Installing uWGSI...\n"
pip3 install uwsgi
curdir=$(pwd)
projectdir="$(dirname "$curdir")"
sitedir=$projectdir/$projectname
echo -e "\n--Setup: Moving Nginx files..."
uwsgiini="_uwsgi.ini"
siteconf="_nginx.conf"
uwsgiini=$projectname$uwsgiini
siteconf=$projectname$siteconf
cp templfiles/nginx/uwsgi.ini $sitedir/$uwsgiini
cp templfiles/nginx/uwsgi_params $sitedir/uwsgi_params
cp templfiles/nginx/site_nginx.conf $sitedir/$siteconf
cp templfiles/nginx/nginx-run.sh $sitedir/nginx-run.sh
chmod +x $sitedir/nginx-run.sh
echo -e "\n--Setup: Routing Nginx files to the Django project..."
sed -i "s,/path/to/your/project,$sitedir,g" $sitedir/$siteconf
sed -i "s,project,$projectname,g" $sitedir/$siteconf
sed -i "s,/path/to/your/project,$sitedir,g" $sitedir/$uwsgiini
sed -i "s,project,$projectname,g" $sitedir/$uwsgiini
sed -i "s,project,$projectname,g" $sitedir/nginx-run.sh
fi
#Moving the project out of the django-template directory
echo -e "\n--Setup: Moving Django Project out of the Django Template directory"
deactivate
mv $projectname/* ../$projectname
rmdir $projectname
cd ../$projectname
#Initialization Complete
echo -e "\n============================================="
echo -e "------- Django Project Setup Complete -------"
echo -e "=============================================\n"
if [ "$nginx" == "y" ] || [ "$nginx" == "Y" ];
then
echo -e "\nTo finish the NGINX setup you need to:\nManually move the .conf file to the Nginx available-sites and symlink it to sites-enabled."
echo -e "The reason this script doesn't automatically without asking is because it requires superuser privileges, \nthus it is offered that the user does this last part of the setup manually."
echo -e "\nIn the new django project directory, write the following commands in order with sudo:\n
mv $siteconf /etc/nginx/sites-available/$siteconf
ln -s /etc/nginx/sites-available/$siteconf /etc/nginx/sites-enabled/
systemctl restart nginx\n
If you have another path to your nginx directory, use that instead.\n"
read -p ">>> If you want this program to do it for you, answer 'yes' without quotes, any other input declines: " nginxmove
if [ "$nginxmove" == "yes" ]; then
echo -e "\nMoving $siteconf to /etc/nginx/sites-available/ and symlinking..."
sudo mv $siteconf /etc/nginx/sites-available/$siteconf
sudo ln -s /etc/nginx/sites-available/$siteconf /etc/nginx/sites-enabled/
echo -e "Restarting NGINX..."
sudo systemctl restart nginx
fi
echo -e "\nYou may now run the project with nginx using the 'nginx-run.sh' bash script in the django project directory.\n"
else
echo -e "\nYou may now run the project with the run.sh bash script in the django project directory.\n"
fi