]> git.uio.no Git - python-TSD.git/blame - bin/tsd-fxdir-setup
Add support for the upcoming alternate file lock.
[python-TSD.git] / bin / tsd-fxdir-setup
CommitLineData
cacf21d9
DES
1#!/bin/sh
2#-
3# Copyright (c) 2014 Universitetet i Oslo
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14# 3. The name of the author may not be used to endorse or promote
15# products derived from this software without specific prior written
16# permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29#
30
31#
32# This script runs at regular intervals on the inner half of the file
33# exchange to create import / export directories for new projects.
34#
35# Note the extensive use of "return 1" instead of "set -e": DO NOT
36# CHANGE THIS as "set -e" has side effects of which you are clearly
37# not aware if you think using it is a good idea.
38#
39
40introot=/tsd
41extroot=/fx
42verbose=
43
44tmpdir=
45tmpmap=
46
47etcdir=/opt/tsd/etc
48mapfile="${etcdir}/tsdfx.map"
49
50umask 0277
51
52#
53# Verbose message
54#
55verbose() {
56 [ "${verbose}" ] && echo "$@" >&2
57}
58
59#
60# Fatal error
61#
62error() {
63 echo "$@" >&2
64 exit 1
65}
66
67#
68# Notice
69#
70notice() {
71 echo "$@" >&2
72}
73
74#
75# Create one or more directories
76#
77makedir() {
78 for dir ; do
79 if ! [ -d "${dir}" ] ; then
80 verbose "creating ${dir}"
81 mkdir "${dir}" || return 1
82 fi
83 done
84 return 0
85}
86
87#
88# Create the import and export directories for a specific project
89#
90mkp() {
91 local p="$1"
d546a900 92 local suffix="$2"
cacf21d9
DES
93
94 intp="${introot}/${p}/fx"
d546a900
DES
95 extp="${extroot}${suffix}/${p}"
96 extimpdir="${extp}/import${suffix}"
97 extexpdir="${extp}/export${suffix}"
98 intimpdir="${intp}/import${suffix}"
99 intexpdir="${intp}/export${suffix}"
cacf21d9
DES
100 impgrp="${p}-import-group"
101 expgrp="${p}-export-group"
102
103 # If the inside directories do not exist, stop here
104 if ! [ -d "${intimpdir}" -a -d "${intexpdir}" ] ; then
105 verbose "missing ${intimpdir} and / or ${intexpdir}"
106 # SOFT ERROR because project directories are generated
107 # in advance of the projects themselves
108 return 0
109 fi
110
111 # If the required groups do not exist, stop here
112 if ! getent group "${impgrp}" "${expgrp}" >/dev/null ; then
113 verbose "missing ${impgrp} and / or ${expgrp}"
114 # SOFT ERROR because project directories are generated
115 # in advance of the projects themselves
116 return 0
117 fi
118
119 # Create the project directory
120 makedir "${extp}" || return 1
121 chown root:"${p}-member-group" "${extp}" || return 1
122 #chmod u=rwx,g=rx,o= "${extp}" || return 1
123 chmod u=rwx,g=rx,o=rx "${extp}" || return 1
124
125 # Create the import directory
126 makedir "${extimpdir}" || return 1
127 chown root:"${impgrp}" "${extimpdir}" || return 1
128 chmod u=rwx,g=rwxs,o= "${extimpdir}" || return 1
129
130 # Create the export directory
131 makedir "${extexpdir}" || return 1
132 chown root:"${expgrp}" "${extexpdir}" || return 1
133 #chmod u=rwx,g=rx,o= "${extexpdir}" || return 1
134 chmod u=rwx,g=rwxs,o= "${extexpdir}" || return 1
135
136 # Output tsdfx map entries
d546a900
DES
137 echo "${p}-import${suffix}: ${extimpdir} => ${intimpdir}"
138 echo "${p}-export${suffix}: ${intexpdir} => ${extexpdir}"
cacf21d9
DES
139}
140
141cleanup() {
142 [ -n "${tmpmap}" -a -f "${tmpmap}" ] && rm "${tmpmap}"
143 [ -n "${tmpdir}" -a -d "${tmpdir}" ] && rmdir "${tmpdir}"
144}
145
146usage() {
147 notice "usage: tsdfx-mkp [-v]"
148 exit 1
149}
150
151main() {
152 while getopts "v" option ; do
153 case ${option} in
154 v)
155 verbose=1
156 ;;
157 *)
158 usage
159 ;;
160 esac
161 done
162
163 # Create temporary directory
164 tmpdir="$(mktemp -d)"
165 if ! [ -n "${tmpdir}" -a -d "${tmpdir}" ] ; then
166 error "unable to create temporary directory"
167 fi
168 tmpmap="${tmpdir}/tsdfx.map"
169 trap cleanup EXIT
170
171 # Iterate over project directories
172 cd "${introot}"
173 errcnt=0
174 for p in p* ; do
175 expr "${p}" : '^p[0-9]\{2,\}$' >/dev/null || continue
176 if ! mkp "${p}" ; then
177 : $((errcnt += 1))
178 fi
d546a900
DES
179 if ! mkp "${p}" "_alt" ; then
180 : $((errcnt += 1))
181 fi
cacf21d9
DES
182 done >"${tmpmap}"
183 if [ $errcnt -gt 0 ] ; then
184 error "one or more errors encountered"
185 elif [ ! -f "${tmpmap}" ] ; then
186 error "failed to create map file"
187 elif [ ! -s "${tmpmap}" ] ; then
188 error "empty map file"
189 elif ! cmp -s "${tmpmap}" "${mapfile}" ; then
190 verbose "installing new map file"
191 newmap="${mapfile}.$$"
192 mv "${tmpmap}" "${newmap}"
193 chmod 0640 "${newmap}"
194 mv "${newmap}" "${mapfile}"
195 verbose "reloading tsdfx"
196 pkill -HUP -f "tsdfx: master"
197 fi
198}
199
200main "$@"