/
functions
396 lines (359 loc) · 10.6 KB
/
functions
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/bin/sh
#
# SM Framework Core Module ~ Versioned FileSystem (versionedfs)
#
# API Version: 1.0.0
#
# versionedfs create \
# path "{versionedfs root path}" \
# strategy "{fhs,name}" \
# method "{link,copy}" \
# activate "{newest,first}"
#
# Example: (package module)
#
# versionedfs create \
# path /opt/sm/pkg \
# strategy fhs \
# method link \
# activate first
#
# FileSystem structure:
#
# {versionedfs_path}/
# .versionedfs/
# strategy # contains one of {fhs,name}
# method # contains one of {link,copy}
# activate # contains one of {newest,first}
# retain # contains one of {[[:digit:]],all}
#
# versions/
# {name}/{version}/ # For each {name}:{version} pair added.
#
# If strategy is 'fhs' then,
# active/
# {bin,sbin,lib,libexec,usr,var,tmp}
#
# If strategy is 'name' then,
# active/
# {name} # For each active name.
#
# In both cases if method is 'link' then each entry will be a symlink
# otherwise each entry will be copied files/directories.
#
versionedfs()
{
trace_filter versionedfs || set -o xtrace
typeset _token _path _paths _source _name _version _strategy _method _activate _action
_action="${1:-}"
(( $# )) && shift || __sm.log.fail "Usage: versionedfs path \"{versionedfs root path}\" "\
"strategy \"{fhs,name}\" method \"{link,copy}\" activate \"{newest,first}\""
while (( $# ))
do
_token="${1}" && shift
case "${_token}" in
(path)
_path="${1}"
(( $# )) && shift || __sm.log.fail "versionedfs path must follow keyword 'path'"
;;
(source)
_source="${1}"
(( $# )) && shift || __sm.log.fail "Source path must follow keyword 'source'"
;;
(name)
_name="${1}"
(( $# )) && shift || __sm.log.fail "Name must follow keyword 'name'"
;;
(version)
_version="${1}"
(( $# )) && shift || __sm.log.fail "Version string must follow keyword 'version'"
;;
(strategy)
_strategy="${1}"
(( $# )) && shift || __sm.log.fail "Strategy string must follow keyword 'strategy'"
case "${_strategy}" in
(fhs|name) true ;;
(*) __sm.log.fail "Strategy must be one of {fhs,name}" ;;
esac
;;
(method)
_method="${1}"
(( $# )) && shift || __sm.log.fail "method string must follow keyword 'method'"
case "${_method}" in
(link|copy) true ;;
(*) __sm.log.fail "Method must be one of {link,copy}" ;;
esac
;;
(activate)
_activate="${1}"
(( $# )) && shift || __sm.log.fail "activate string must follow keyword 'activate'"
case "${_activate}" in
(first|newest) true ;;
(*) __sm.log.fail "Activate must be one of {first,newest}" ;;
esac
;;
(keep)
_keep="${1}"
(( $# )) && shift || __sm.log.fail "keep number must follow keyword 'activate'"
case "${_keep}" in
([[:digit:]]##)
(( _keep > 0)) || __sm.log.fail "Keep must be a positive integer." ;;
(*) __sm.log.fail "Keep must be an integer." ;;
esac
;;
(*)
__sm.log.fail "Unknown parameter '${_token}'"
esac
done
[[ -n "${_path}" ]] || __sm.log.fail "A versionedfs root path must be given."
[[ "${_action}" == create || -d "${_path}/.versionedfs" ]] ||
__sm.log.fail "${_path} is not a versionedfs root path; "\
"Use 'versionedfs create path {path}' to initialize."
case "${_action}" in
(create)
__sm.versionedfs.create "${_path}"
;;
(add)
[[ -n "${_name}" && -n "${_source}" ]] ||
__sm.log.fail "Name and Source must be given."
[[ -n "${_version}" ]] || _version="$(date +"%Y%m%d%H%M%S")"
__sm.versionedfs.add "${_path}" "${_name}" "${_version}" "${_source}"
;;
(activate)
[[ -n "${_name}" && -n "${_version}" ]] ||
__sm.log.fail "Name and Version must be given."
__sm.versionedfs.activate "${_path}" "${_name}" "${_version}"
;;
(remove|deactivate)
[[ -n "${_name}" ]] || __sm.log.fail "Name must be given."
__sm.versionedfs.${_action} "${_path}" "${_name}" "${_version}"
;;
(versions|active)
__sm.versionedfs.list "${_action}" "${_path}" "${_name}" "${_version}"
;;
(*)
__sm.log.fail "Unknown versionedfs action '${_action}'"
;;
esac
}
__sm.versionedfs.create()
{
typeset _path
_path="$1"
mkdir -p "${_path}/"{.versionedfs,versions,active} ||
__sm.log.fail "Could not create paths ${_path}/{.versionedfs,versions,active}"
echo "${_strategy}" > "${_path}/.versionedfs/strategy"
echo "${_method}" > "${_path}/.versionedfs/method"
echo "${_activate}" > "${_path}/.versionedfs/activate"
}
__sm.versionedfs.add()
{
typeset _path _name _version _source _version_path
_path="$1"
_name="$2"
_version="${3}"
_source="$4"
_version_path="${_path}/versions/${_name}/${_version}"
if [[ -d "${_version_path}" ]]
then
rm -rf "${_version_path}"
fi
mkdir -p "${_version_path}"
if [[ "${_version_path}" != "${_source}" ]]
then
cp -Rf "${_source}"/* "${_version_path}"
printf "${_source}\n" > "${_version_path}/.uri"
fi
}
__sm.versionedfs.remove()
{
typeset _path _name _version _version_path
_path="$1"
_name="${2}"
_version="${3}"
_version_path="${_path}/versions/${_name}/${_version}"
__sm.versionedfs.deactivate "${_path}" "${_name}" "${_version}"
if [[ -d "${_version_path}" ]]
then
rm -rf "${_version_path}/"
fi
}
__sm.versionedfs.activate()
{
typeset _path _name _version _target _version_path _active_path _activate \
_active_version_path _index _count _files
_path="$1"
_name="${2}"
_version="${3}"
_version_path="${_path}/versions/${_name}/${_version}"
_active_path="${_path}/active"
if [[ -L "${_path}/versions/${_name}/active" ]]
then # Deactivate if active version.
# if [[ "$(readlink "${_path}/versions/${_name}/active")" != "${_version_path}" ]]
# then
__sm.versionedfs.deactivate "${_path}" "${_name}"
# else
# return 0 # Success if the given name/version is already active.
# fi
fi
_index=0
read -r _activate < "${_path}/.versionedfs/activate"
if [[ -L "${_path}/versions/${_name}/active" ]]
then
_active_version_path="$(readlink "${_path}/versions/${_name}/active")"
case "${_activate}" in
(first)
[[ -z "${_active_version_path}" ]] || return 0 # a version is already active
;;
(newest)
true # continue with activation
;;
esac
fi
read -r _strategy < "${_path}/.versionedfs/strategy"
read -r _method < "${_path}/.versionedfs/method"
__sm.log.step "${_name} ${_version}"
case "${_strategy}" in
(fhs)
case "${_method}" in
(link)
_files=( ${_version_path}/**/* )
_count="${#_files[@]}"
printf "\r [ ] ${_name} ${_version} ~ symlinking ${_count} files.."
for _file in "${_files[@]}"
do
: $(( _index++ ))
_target="${_active_path}/${_file##${_version_path}/}"
if [[ -d "${_file}" ]]
then
mkdir -p "${_target}"
else
if [[ -f "${_target}" ]]
then
rm -rf "${_target}"
fi
ln -s "${_file}" "${_target}"
fi
done
printf "\r [o] %-40s" "${_name} ${_version}"
;;
(copy)
cp -Rf "${_version_path}"/* "${_active_path}"
;;
esac
printf "${_version_path}\n" > "${_active_path}/.uri"
;;
(name)
if [[ -d "${_active_path}/${_name}" ]]
then
rm -rf "${_active_path}/${_name}"
fi
mkdir -p "${_active_path}/${_name}"
cp -Rf "${_version_path}"/* "${_active_path}/${_name}"
printf "${_version_path}\n" > "${_active_path}/${_name}/.uri"
;;
esac
__sm.log.step success
ln -sf "${_version_path}" "${_path}/versions/${_name}/active"
# TODO: Add to active 'db'
}
__sm.versionedfs.deactivate()
{
typeset _path _name _version _name_path _active_path _index _files _count \
_version_path _target _targets
_path="$1"
_name="${2}"
_version="${3:-}"
_name_path="${_path}/versions/${_name}"
_active_path="${_path}/active"
[[ -L "${_path}/versions/${_name}/active" ]] || return 0 # No active version
_index=0
_version_path="$(readlink "${_path}/versions/${_name}/active")"
if [[ -n "${_version}" && "${_version_path}" == "${_name_path}/${_version}" ]]
then
__sm.log.debug package "A different version than the one given is active; Skipping deactivation."
return 0 #
fi
read -r _strategy < "${_path}/.versionedfs/strategy"
case "${_strategy}" in
(fhs)
# TODO: Switch this to using shell globbing builtin?
_files=($( find "${_version_path}" -mindepth 1 | sort -r ))
_count="${#_files[@]}"
echo
for _file in "${_files[@]}"
do
: $(( _index++ ))
printf "\r Removing (${_index}/${_count})"
_target="${_active_path}/${_file##${_version_path}}"
if [[ -L "${_target}" ]]
then
rm -f "${_target}"
elif [[ -d "${_target}" ]]
then
_targets=( "${_target}"/* )
if (( ! ${#_targets[@]} ))
then
rm -rf "${_target}"
fi
elif [[ -f "${_target}" ]]
then
__sm.log.fail "Unknown file type for ${_target}."
fi
done
;;
(name)
# NO we do NOT remove the entire path always :(
# TODO: Fix this, only remove the active version if it is actually the
# active one. TEST before pushing once fixed ... this can break sm deploy.
# rm -rf "${_active_path}/${_name}/"
;;
esac
rm -f "${_path}/versions/${_name}/active"
# Remove from active metadata
}
__sm.versionedfs.list()
{
typeset _method _path _name __path __name __version
_method=$1
_path="$2"
_name="${3:-}"
if [[ -n "${_name}" ]]
then
_name="${_name//,/ }"
fi
for __path in ${_path}/versions/*/*
do
__version="${__path##*/}"
case "${_method}" in
(active)
if [[ "${__version}" == "active" && -L "${__path}" ]]
then
__path="$( readlink "${__path}" )"
__version="${__path##*/}"
else
__version=""
fi
;;
(versions)
if [[ "${__version}" == "active" ]]
then
__version=""
fi
;;
esac
if [[ -n "${__version}" ]]
then
__name="${__path%/*}"
__name="${__name##*/}"
if [[ -n "${_name}" ]] && ! __sm.string.contains.word "${__name}" "${_name}"
then
__name=""
fi
if [[ -n "${__name}" ]]
then
printf "${__name}:${__version}\n"
fi
fi
done
}