-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_site.sh
More file actions
executable file
·229 lines (192 loc) · 5.94 KB
/
create_site.sh
File metadata and controls
executable file
·229 lines (192 loc) · 5.94 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env bash
shopt -s extglob
# Default paths
ROOT_PATH="/var/www"
TEMPLATE_PATH=$ROOT_PATH
# Other default variables
DOMAIN="example.com"
TEMPLATE_NAME="template.tar"
# Boolean variables
GIT=false
TEMPLATE=false
# Global variables
SITE=""
SUB_PATH=""
SITE_PATH=""
APACHE_SERVER_NAME=""
# Print usage syntax
function print_syntax() {
echo "Usage: create_site SITE_NAME [SUBPATH|OPTIONAL] [OPTION]"
echo "Try create_site --help' for more information."
exit
}
function print_help() {
cat << EOF
Usage: create_site SITE_NAME [SUBPATH|OPTIONAL] [OPTION]
Creates a folder folder /usr/www and adds a new website to apache
Mandatory arguments to long options are mandatory for short options too.
-g, --git create git repository for the new site
-h, --help displays this help and exit
-t, --template TEMPLATE_NAME adds template to new folder,
defaults to template.tar
if no TEMPLATE_NAME is provided.
supported template extentains:
tar.bz2|tar.gz|bz2|rar|gz|
tar|tbz2|tgz|zip|Z|7z
provided that the correct programs
are installed.
looks by default in /usr/www
-tp, --template-path PATH where to look for templates
Example usage:
create_site new_site
creates folder /usr/www/new_site
adds the site to apache with serverName new_site.domain
create_site new_site dev -t -g
creates folder /usr/www/dev/new_site
extracts template.tar to that folder
initializes git and makes a first commit
adds the site to apache with serverName new_site.dev.domain
create_site new_site dev/play -t new_template.zip -tp /home/user -g
creates folder /usr/www/dev/play/new_site
extracts new_template.zip from /home/user to that folder
initializes git and makes a first commit
adds the site to apache with serverName new_site.play.dev.domain
EOF
}
# Extract file
function extract() {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "Unknown file extention for file $1" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Checks if syntax is valid and if a subpath is provided
function init() {
if [ $# -lt 1 ]; then
print_syntax
fi
__shift_nr=0
case "$1" in
-h | --help)
print_help
exit
;;
-* | *- | *[^a-zA-Z0-9-_]*)
print_syntax
;;
*)
SITE=$1
let __shift_nr+=1
esac
if [ $2 ] && [[ $2 != -* ]]; then
SUB_PATH=$2
let __shift_nr+=1
fi
}
# Handle option parameters
function setup_options() {
while [[ $1 ]]; do
case "$1" in
# Template option
# Sets template to true
# Sets new template name if provided
-t | --template)
TEMPLATE=true
shift
if [ $1 ] && [[ $1 != -* ]]; then
TEMPLATE_NAME=$1
shift
fi
;;
# Template path option
# Sets the Template path if provided else discard
-tp | -template-path)
shift
if [ $1 ] && [[ $1 != -* ]]; then
TEMPLATE_PATH=$1
shift
fi
;;
# GIT option
-g | --git) GIT=true; shift ;;
# Unknown option
*) echo "Unknown paramater: $1"; shift ;;
esac
done
}
# Setup full site path and create and cd to dir
function setup_site_path() {
SITE_PATH=$ROOT_PATH/$SUB_PATH/$SITE
SITE_PATH=${SITE_PATH//\/*(\/)/\/} # Remove multiple / if exists
SITE_PATH=${SITE_PATH%/} # Remove last / if exists
mkdir -p $SITE_PATH
}
# Setup server name site.subpath.domain
function setup_apache_server_name() {
APACHE_SERVER_NAME=$DOMAIN
for i in $(echo "$SUB_PATH/$SITE" | tr "/" "\n")
do
APACHE_SERVER_NAME=$i.$APACHE_SERVER_NAME
done
}
# Add and enable site apache
function add_site_to_apache() {
cat << EOF | sudo tee /etc/apache2/sites-available/$APACHE_SERVER_NAME
<VirtualHost *:80>
ServerName $APACHE_SERVER_NAME
DocumentRoot $SITE_PATH
<Directory $SITE_PATH/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
EOF
sudo a2ensite $APACHE_SERVER_NAME
sudo service apache2 restart
}
# Main
init $@ # Call init and shit array
shift $__shift_nr
setup_options $@
setup_site_path
setup_apache_server_name
add_site_to_apache
cd $SITE_PATH
# Add template to site if true
if $TEMPLATE; then
# TODO add support for http/ftp files, if TEMPLATE_NAME is webfile wget and extract
extract $TEMPLATE_PATH/$TEMPLATE_NAME
fi
# GIT
if $GIT; then
git init
if [ "$(ls -A . 2> /dev/null)" != "" ]; then
git add .
git commit -m "Project created"
fi
fi
echo
echo "----"
echo "Root path: "$ROOT_PATH
echo "Sub path: "$SUB_PATH
echo "Site: "$SITE
echo "Site path: "$SITE_PATH
echo "Apache serverName: "$APACHE_SERVER_NAME
echo "Template: $TEMPLATE, $TEMPLATE_PATH/$TEMPLATE_NAME"
echo "Git: $GIT"