Line 0
Link Here
|
|
|
1 |
#!/bin/bash |
2 |
# Copyright 1999-2015 Gentoo Foundation |
3 |
# Distributed under the terms of the GNU General Public License v2 |
4 |
# Author: Ralph Sennhauser <sera@gentoo.org> |
5 |
|
6 |
die() { |
7 |
echo "${@}" |
8 |
exit 1 |
9 |
} |
10 |
|
11 |
dir_is_empty() { |
12 |
# usage: |
13 |
# dir_is_empty <some-dir> |
14 |
# |
15 |
# returns 2 if the dir does not even exist |
16 |
# returns 1 if the dir is not empty |
17 |
# returns 0 (success) if the dir exists and is empty |
18 |
|
19 |
local dir=$1 |
20 |
local files |
21 |
|
22 |
if [[ ! -e ${dir} ]] ; then |
23 |
return 2 |
24 |
fi |
25 |
|
26 |
shopt -s nullglob dotglob # To include hidden files |
27 |
files=( "${dir}"/* ) |
28 |
shopt -u nullglob dotglob |
29 |
|
30 |
if [[ ${#files[@]} -eq 0 ]]; then |
31 |
return 0 |
32 |
else |
33 |
return 1 |
34 |
fi |
35 |
|
36 |
} |
37 |
|
38 |
usage() { |
39 |
cat <<EOL |
40 |
Usage: ${BASH_SOURCE} <--create|--remove|--help> [--suffix s][--user u][--group g] |
41 |
|
42 |
Options: |
43 |
--help: |
44 |
show this text. |
45 |
--create: |
46 |
create a new instance |
47 |
--remove: |
48 |
remove an existing instance. |
49 |
--suffix SUFFIX: |
50 |
a suffix for this instance. the suffix may not collide with an already |
51 |
existing instance, defaults to empty. |
52 |
--user USER: |
53 |
the user for which to configure this instance for. The user needs to |
54 |
exist already. defaults to tomcat. |
55 |
--group GROUP: |
56 |
the group for which to configure this instance for. The group needs to |
57 |
exist already. defaults to tomcat. |
58 |
|
59 |
Examples: |
60 |
${BASH_SOURCE} --create --suffix testing --user tacmot --group tacmot |
61 |
${BASH_SOURCE} --remove --suffix testing |
62 |
EOL |
63 |
} |
64 |
|
65 |
parse_argv() { |
66 |
action="not specified" |
67 |
instance_name="tomcat-@SLOT@" |
68 |
instance_systemd=${instance_name} |
69 |
instance_user="tomcat" |
70 |
instance_group="tomcat" |
71 |
|
72 |
while [[ -n $1 ]]; do |
73 |
case $1 in |
74 |
--help) |
75 |
usage |
76 |
exit 0;; |
77 |
--suffix) |
78 |
instance_name+="-$2" |
79 |
instance_systemd+="@${2}.service.d" |
80 |
shift; shift;; |
81 |
--user) |
82 |
instance_user="$2" |
83 |
shift; shift;; |
84 |
--group) |
85 |
instance_group="$2" |
86 |
shift; shift;; |
87 |
--create) |
88 |
action=create |
89 |
shift;; |
90 |
--remove) |
91 |
action=remove |
92 |
shift;; |
93 |
--backup) |
94 |
action=backup |
95 |
shift;; |
96 |
--restore) |
97 |
action=restore |
98 |
shift;; |
99 |
--update) |
100 |
action=update |
101 |
shift;; |
102 |
*) |
103 |
echo "Invalid option '$1'" |
104 |
usage |
105 |
exit 2;; |
106 |
esac |
107 |
done |
108 |
|
109 |
tomcat_home="/@GENTOO_PORTAGE_EPREFIX@usr/share/tomcat-@SLOT@" |
110 |
instance_base="/@GENTOO_PORTAGE_EPREFIX@var/lib/${instance_name}" |
111 |
instance_conf="/@GENTOO_PORTAGE_EPREFIX@etc/${instance_name}" |
112 |
instance_logs="/@GENTOO_PORTAGE_EPREFIX@var/log/${instance_name}" |
113 |
instance_temp="/@GENTOO_PORTAGE_EPREFIX@var/tmp/${instance_name}" |
114 |
|
115 |
if [[ -x /usr/bin/systemctl ]] && ([[ "${instance_user}" != "tomcat" ]] || [[ "${instance_group}" != "tomcat" ]]); then |
116 |
systemd_overlay="/@GENTOO_PORTAGE_EPREFIX@etc/systemd/system/${instance_systemd}" |
117 |
fi |
118 |
|
119 |
if [[ -d "/@GENTOO_PORTAGE_EPREFIX@etc/systemd/system/${instance_systemd}" ]]; then |
120 |
all_targets=( |
121 |
"${instance_base}" |
122 |
"${instance_logs}" |
123 |
"${instance_temp}" |
124 |
"/@GENTOO_PORTAGE_EPREFIX@etc/${instance_name}" |
125 |
"/@GENTOO_PORTAGE_EPREFIX@etc/init.d/${instance_name}" |
126 |
"/@GENTOO_PORTAGE_EPREFIX@etc/conf.d/${instance_name}" |
127 |
"/@GENTOO_PORTAGE_EPREFIX@etc/systemd/system/${instance_systemd}" |
128 |
) |
129 |
else |
130 |
all_targets=( |
131 |
"${instance_base}" |
132 |
"${instance_logs}" |
133 |
"${instance_temp}" |
134 |
"/@GENTOO_PORTAGE_EPREFIX@etc/${instance_name}" |
135 |
"/@GENTOO_PORTAGE_EPREFIX@etc/init.d/${instance_name}" |
136 |
"/@GENTOO_PORTAGE_EPREFIX@etc/conf.d/${instance_name}" |
137 |
) |
138 |
fi |
139 |
} |
140 |
|
141 |
test_can_deploy() { |
142 |
local no_deploy target |
143 |
for target in "${all_targets[@]}"; do |
144 |
if [[ -e "${target}" ]]; then |
145 |
if ! dir_is_empty "${target}" ; then |
146 |
echo "Error: '${target}' already exists and is not empty." |
147 |
no_deploy=yes |
148 |
fi |
149 |
fi |
150 |
done |
151 |
if [[ -n "${no_deploy}" ]]; then |
152 |
cat <<-EOL |
153 |
To protect an existing installation no new instance was deployed. You can use |
154 |
'${BASH_SOURCE} --remove' |
155 |
to remove an existing instance first or run |
156 |
'${BASH_SOURCE} --create --suffix <instance_suffix>' |
157 |
to deploy an instance under a different name |
158 |
EOL |
159 |
usage |
160 |
exit 1 |
161 |
fi |
162 |
|
163 |
if ! getent passwd | cut -d: -f1 | grep -Fx "${instance_user}" > /dev/null; then |
164 |
echo "Error: user '${instance_user}' doesn't exist." |
165 |
exit 1 |
166 |
fi |
167 |
|
168 |
if ! getent group | cut -d: -f1 | grep -Fx "${instance_group}" > /dev/null; then |
169 |
echo "Error: group '${instance_group}' doesn't exist." |
170 |
exit 1 |
171 |
fi |
172 |
} |
173 |
|
174 |
deploy_instance() { |
175 |
test_can_deploy |
176 |
if [[ ! -z "${systemd_overlay}" ]]; then |
177 |
mkdir -p "${systemd_overlay}" || die |
178 |
cat > "${systemd_overlay}"/00-gentoo.conf <<-'EOF' |
179 |
[Service] |
180 |
User=@INSTANCE_USER@ |
181 |
Group=@INSTANCE_GROUP@ |
182 |
EOF |
183 |
sed -i -e "s|@INSTANCE_USER@|${instance_user}|g" \ |
184 |
-e "s|@INSTANCE_GROUP@|${instance_group}|g" \ |
185 |
"${systemd_overlay}"/00-gentoo.conf || die |
186 |
fi |
187 |
|
188 |
mkdir -p "${instance_base}"/{work,webapps} || die |
189 |
mkdir -p "${instance_logs}" || die |
190 |
mkdir -p "${instance_temp}" || die |
191 |
mkdir -p "${instance_conf}" || die |
192 |
|
193 |
cp -r "${tomcat_home}"/webapps/ROOT "${instance_base}"/webapps || die |
194 |
|
195 |
chown -R "${instance_user}":"${instance_group}" \ |
196 |
"${instance_base}" "${instance_logs}" "${instance_temp}" || die |
197 |
|
198 |
find "${instance_base}"/webapps -type d -exec chmod 750 {} + || die |
199 |
find "${instance_base}"/webapps -type f -exec chmod 640 {} + || die |
200 |
|
201 |
# initial config # |
202 |
|
203 |
cp -r "${tomcat_home}"/conf/* "${instance_conf}"/ || die |
204 |
|
205 |
sed -i -e "s|\${catalina.base}/logs|${instance_logs}|" \ |
206 |
"${instance_conf}"/logging.properties || die |
207 |
sed -i -e "s|directory=\"logs\"|directory=\"${instance_logs}\"|" \ |
208 |
"${instance_conf}"/server.xml || die |
209 |
|
210 |
mkdir -p "${instance_conf}"/Catalina/localhost || die |
211 |
cat > "${instance_conf}"/Catalina/localhost/host-manager.xml <<-'EOF' |
212 |
<?xml version="1.0" encoding="UTF-8"?> |
213 |
<Context docBase="${catalina.home}/webapps/host-manager" |
214 |
antiResourceLocking="false" privileged="true" /> |
215 |
EOF |
216 |
|
217 |
cat > "${instance_conf}"/Catalina/localhost/manager.xml <<-'EOF' |
218 |
<?xml version="1.0" encoding="UTF-8"?> |
219 |
<Context docBase="${catalina.home}/webapps/manager" |
220 |
antiResourceLocking="false" privileged="true" /> |
221 |
EOF |
222 |
|
223 |
if [[ -d "${tomcat_home}"/webapps/docs ]]; then |
224 |
cat > "${instance_conf}"/Catalina/localhost/docs.xml <<-'EOF' |
225 |
<?xml version="1.0" encoding="UTF-8"?> |
226 |
<Context docBase="${catalina.home}/webapps/docs" /> |
227 |
EOF |
228 |
fi |
229 |
|
230 |
if [[ -d "${tomcat_home}"/webapps/examples ]]; then |
231 |
cat > "${instance_conf}"/Catalina/localhost/examples.xml <<-'EOF' |
232 |
<?xml version="1.0" encoding="UTF-8"?> |
233 |
<Context docBase="${catalina.home}/webapps/examples" /> |
234 |
EOF |
235 |
fi |
236 |
|
237 |
chown -R "${instance_user}":"${instance_group}" "${instance_conf}" || die |
238 |
find "${instance_conf}" -type d -exec chmod 750 {} + || die |
239 |
find "${instance_conf}" -type f -exec chmod 640 {} + || die |
240 |
|
241 |
# rc script # |
242 |
|
243 |
cp "${tomcat_home}"/gentoo/tomcat.init \ |
244 |
"/@GENTOO_PORTAGE_EPREFIX@etc/init.d/${instance_name}" || die |
245 |
|
246 |
sed -e "s|@INSTANCE_NAME@|${instance_name}|g" \ |
247 |
-e "s|@INSTANCE_USER@|${instance_user}|g" \ |
248 |
-e "s|@INSTANCE_GROUP@|${instance_group}|g" \ |
249 |
"${tomcat_home}"/gentoo/tomcat.conf \ |
250 |
> "/@GENTOO_PORTAGE_EPREFIX@etc/conf.d/${instance_name}" || die |
251 |
|
252 |
# some symlinks for tomcat and netbeans # |
253 |
|
254 |
ln -s "${instance_conf}" "${instance_base}"/conf || die |
255 |
ln -s "${instance_temp}" "${instance_base}"/temp || die |
256 |
|
257 |
# a note to update the default configuration # |
258 |
|
259 |
cat <<-EOL |
260 |
Successfully created instance '${instance_name}' |
261 |
It's strongly recommended for production systems to go carefully through the |
262 |
configuration files at '${instance_conf}'. |
263 |
The generated initial configuration is close to upstreams default which |
264 |
favours the demo aspect over hardening. |
265 |
EOL |
266 |
} |
267 |
|
268 |
remove_instance() { |
269 |
echo "The following files will be removed permanently:" |
270 |
local target; for target in "${all_targets[@]}"; do |
271 |
find ${target} |
272 |
done |
273 |
|
274 |
echo "Type 'yes' to continue" |
275 |
read |
276 |
if [[ ${REPLY} == yes ]]; then |
277 |
rm -rv "${all_targets[@]}" |
278 |
else |
279 |
echo "Aborting as requested ..." |
280 |
fi |
281 |
} |
282 |
|
283 |
parse_argv "$@" |
284 |
|
285 |
if [[ ${action} == create ]]; then |
286 |
deploy_instance |
287 |
elif [[ ${action} == remove ]]; then |
288 |
remove_instance |
289 |
elif [[ ${action} == "not specified" ]]; then |
290 |
echo "No action specified!" |
291 |
usage |
292 |
exit 1 |
293 |
else |
294 |
echo "${action} not yet implemented!" |
295 |
usage |
296 |
exit 1 |
297 |
fi |