3 # this is a library of functions shared by scripts in the system folder.
5 # Author: Chris Koeritz
7 ############################################################################
11 # removes a full domain from the DNS.
12 function remove_domain_file()
14 local domain_name="$1"; shift
16 local domain_file="/etc/bind/${domain_name}.conf"
17 if [ -f "$domain_file" ]; then
18 # don't destroy, just shuffle.
19 \mv -f "$domain_file" "/tmp/$(basename ${domain_file})-old-${RANDOM}"
20 test_or_die "removing domain file: $domain_file"
22 echo "Did not see a domain file to remove: $domain_file"
26 # creates a totally new domain config file for DNS.
27 function write_new_domain_file()
29 local domain_name="$1"; shift
31 local domain_file="/etc/bind/${domain_name}.conf"
33 echo "adding a totally new domain called $domain_name"
34 echo "using the config file: $domain_file"
36 if [ -f $domain_file ]; then
38 echo "The domain configuration file already exists at:"
40 echo "Since we don't want to tear that down if it has specialized configuration"
41 echo "data in it, we will just leave it in place and consider our job done."
48 @ IN SOA @ ${SERVER_ADMIN}. (
55 IN NS ${MAIN_NAME_SERVER}.
56 IN MX 10 ${MAIL_SERVER}.
58 ${domain_name}. IN A ${IP_ADDRESS}
59 IN HINFO \"linux server\" \"${DISTRO}\"
62 # our personalized configuration approach wants the real owner to own the file.
63 chown "$(logname):$(logname)" $domain_file
64 test_or_die "setting ownership on: $domain_file"
67 # takes a zone back out of the local conf file for bind
68 function remove_zone_for_domain()
70 local domain_name="$1"; shift
72 local domain_file="/etc/bind/${domain_name}.conf"
74 # eat the zone file definition. this will botch up badly if more text was added
75 # or the zone info shrank.
76 create_chomped_copy_of_file "/etc/bind/named.conf.local" "zone.*${domain_name}" 6
79 # hooks up a new config file into bind's list of zones.
80 function add_zone_for_new_domain()
82 local domain_name="$1"; shift
84 local domain_file="/etc/bind/${domain_name}.conf"
86 echo "adding a new domain configured by ${domain_file} into"
87 echo "the named.conf.local configuration file."
89 # append the reference to the new conf file in the zone list.
91 zone \"${domain_name}\" in {
92 file \"${domain_file}\";
97 ////////////////////////////////////////////////////////////////////////////
99 " >> /etc/bind/named.conf.local
101 # keep ownership for the real user.
102 chown "$(logname):$(logname)" /etc/bind/named.conf.local
103 test_or_die "setting ownership on: /etc/bind/named.conf.local"
106 # zaps a subdomain out of the containing domain file.
107 function remove_subdomain()
109 local old_domain="$1"; shift
111 # split up the full domain name into subdomain portion and containing domain.
112 local subdomain="${old_domain%.*.*}"
113 local containing_domain="${old_domain#*.}"
115 echo "removing subdomain $subdomain from containing domain $containing_domain"
117 local domain_file="/etc/bind/${containing_domain}.conf"
118 # see if config file already exists; if not, complain.
119 if [ ! -f "$domain_file" ]; then
120 echo "The domain configuration file for $old_domain is missing."
121 echo "It should already be present in: $domain_file"
122 echo "We cannot remove a subdomain if the containing domain isn't there."
126 # see if subdomain already present in config.
127 if ! grep -q "$old_domain" "$domain_file"; then
128 echo "The subdomain $subdomain is already missing from the domain"
129 echo "configuration file: $domain_file"
130 echo "Our work is apparently done for removing it."
134 create_chomped_copy_of_file "$domain_file" "${old_domain}" 2
137 # adds a new subdomain under a containing domain.
138 function add_new_subdomain()
140 local new_domain="$1"; shift
142 # split up the full domain name into subdomain portion and containing domain.
143 local subdomain="${new_domain%.*.*}"
144 local containing_domain="${new_domain#*.}"
146 echo "adding a subdomain $subdomain to containing domain $containing_domain"
148 local domain_file="/etc/bind/${containing_domain}.conf"
149 # see if config file already exists; if not, complain.
150 if [ ! -f "$domain_file" ]; then
151 echo "The domain configuration file for $new_domain is missing."
152 echo "It should already be present in: $domain_file"
153 echo "Please add the containing domain before trying to add a subdomain."
157 # see if subdomain already present in config.
158 if grep -q "$new_domain" "$domain_file"; then
159 echo "The subdomain $subdomain already seems to exist in the domain"
160 echo "configuration file: $domain_file"
161 echo "We are considering our work done; if you want to modify the subdomain,"
162 echo "then please call remove_domain on it first."
166 # append the new subdomain into the config file.
167 echo "${subdomain}.${containing_domain}. IN A ${IP_ADDRESS}
168 IN HINFO \"linux server\" \"${DISTRO}\"
169 " >> /etc/bind/${containing_domain}.conf
171 # keep ownership for real user.
172 chown "$(logname):$(logname)" "/etc/bind/${containing_domain}.conf"
173 test_or_die "setting ownership on: /etc/bind/${containing_domain}.conf"
176 function restart_bind()
178 echo restarting DNS server.
179 service bind9 restart
180 if [ $? -ne 0 ]; then
181 echo "The bind service did not restart properly. Please check the error logs."
184 echo DNS server restarted.
187 ############################################################################
191 # removes a config file for apache given the app name and site name.
192 function remove_apache_config()
194 local appname="$1"; shift
195 local sitename="$1"; shift
197 local site_config="/etc/apache2/sites-available/${sitename}.conf"
199 if [ -f "$site_config" ]; then
200 # don't destroy, just shuffle.
201 \mv -f "$site_config" "/tmp/$(basename ${site_config})-old-${RANDOM}"
202 test_or_die "removing site config: $site_config"
204 echo "Did not see a site config to remove: $site_config"
208 # this function writes out the new configuration file for the site.
209 function write_apache_config()
211 local appname="$1"; shift
212 local sitename="$1"; shift
213 local site_path="$1"; shift
215 local site_config="/etc/apache2/sites-available/${sitename}.conf"
217 # check if config file already exists and bail if so.
218 if [ -f "$site_config" ]; then
219 echo "The apache configuration file already exists at:"
221 echo "Since apache configuration files can get very complex, we do not want to"
222 echo "assume that this file is removable. Calling the site addition done."
226 echo "Creating a new apache2 site for $sitename with config file:"
229 # if no path, then we default to our standard app storage location. otherwise, we
230 # put the site where they told us to.
231 if [ -z "$site_path" ]; then
232 # path where site gets checked out, in some arcane manner, and which happens to be
233 # above the path where we put webroot (in the storage suffix, if defined).
234 local path_above="${BASE_APPLICATION_PATH}/${appname}"
235 # no slash between appname and suffix, in case suffix is empty.
236 local full_path="${path_above}${STORAGE_SUFFIX}"
237 #echo really full path is $full_path
239 # we'll go with their specification for the site storage.
240 local full_path="$site_path"
244 # set up the user's web folder as an apache user web directory.
246 # set permissions on the actual app folder.
247 <Directory \"$full_path\">
248 Options +ExecCGI +Indexes +FollowSymLinks +Includes +MultiViews
253 ServerName ${sitename}
254 DocumentRoot ${full_path}
255 ErrorLog \${APACHE_LOG_DIR}/${sitename}-error.log
256 CustomLog \${APACHE_LOG_DIR}/${sitename}-access.log combined
257 Include /etc/apache2/conf-library/basic-options.conf
258 Include /etc/apache2/conf-library/rewrite-enabling.conf
262 chown "$(logname):$(logname)" "$site_config"
263 test_or_die "setting ownership on: $site_config"
266 # stops apache from serving up the site.
267 function disable_site()
269 local sitename="$1"; shift
270 local site_config="/etc/apache2/sites-available/${sitename}.conf"
272 if [ ! -f "$site_config" ]; then
273 echo "The site config did not exist and could not be disabled: $site_config"
277 #hmmm: repeated pattern of hidden output file, very useful. abstract it...
278 local outfile="$TMP/apacheout.$RANDOM"
279 a2dissite "$(basename $site_config)" &>$outfile
280 if [ $? -ne 0 ]; then
281 # an error happened, so we show the command's output at least.
284 echo "There was a problem disabling the apache config file in:"
286 echo "Please consult the apache error logs for more details."
292 # turns on the config file we create above for apache.
293 function enable_site()
295 local sitename="$1"; shift
296 local site_config="/etc/apache2/sites-available/${sitename}.conf"
298 local outfile="$TMP/apacheout.$RANDOM"
299 a2ensite "$(basename $site_config)" &>$outfile
300 if [ $? -ne 0 ]; then
301 # an error happened, so we show the command's output at least.
304 echo "There was a problem enabling the apache config file in:"
306 echo "Please consult the apache error logs for more details."
312 # restarts the apache2 service.
313 function restart_apache()
315 service apache2 restart
316 if [ $? -ne 0 ]; then
317 echo "There was a problem restarting the apache2 service."
318 echo "Please consult the apache error logs for more details."
323 # sets up the serverpilot storage location for a user hosted web site.
324 function maybe_create_site_storage()
326 local our_app="$1"; shift
327 # make sure the path for storage this app exists for the user.
328 local full_path="$BASE_APPLICATION_PATH/$our_app"
329 if [ ! -d "$full_path" ]; then
331 test_or_die "The app storage path could not be created.\n Path in question is: $full_path"
334 # now give the web server some access to the folder. this is crucial since the folders
335 # can be hosted in any user folder, and the group permissions will not necessarily be correct already.
336 local chow_path="$full_path"
337 # only the first chmod is recursive; the rest just apply to the specific folder of interest.
338 chmod -R g+rx "$chow_path"
339 # walk backwards up the path and fix perms.
340 while [[ $chow_path != $HOME ]]; do
341 echo chow path is now $chow_path
342 chmod g+rx "$chow_path"
343 test_or_die "Failed to add group permissions on the path: $chow_path"
344 # reassert the user's ownership of any directories we might have just created.
345 chown $(logname) "$chow_path"
346 test_or_die "changing ownership to user failed on the path: $chow_path"
347 chow_path="$(dirname "$chow_path")"
351 ############################################################################