-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbsdfile.subr
120 lines (101 loc) · 2.65 KB
/
cbsdfile.subr
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
if [ ! "$_CBSD_CBSDFILE_SUBR" ]; then
_CBSD_CBSDFILE_SUBR=1
###
ARGS=
CBSDFILE_ARGS=
# exclude jname= and mode= params from args str
for i in $*; do
prefix6=$( substr --pos=0 --len=6 --str="${i}" )
prefix9=$( substr --pos=0 --len=9 --str="${i}" )
[ "${prefix6}" = "jname=" ] && continue
[ "${prefix9}" = "cbsdfile=" ] && continue
# find 'param=value' to pass as environment vars
strpos --str="${i}" --search="="
_pos=$?
if [ ${_pos} -ne 0 ]; then
if [ -z "${CBSDFILE_ARGS}" ]; then
CBSDFILE_ARGS="${i}"
else
CBSDFILE_ARGS="${CBSDFILE_ARGS} ${i}"
fi
continue
fi
if [ -z "${ARGS}" ]; then
ARGS="${i}"
else
ARGS="${ARGS} ${i}"
fi
done
# single jname only
[ -n "${jname}" ] && ARGS="${jname}"
if [ -n "${cbsdfile}" ]; then
# is full path?
_path_pref=$( substr --pos=0 --len=1 --str=${cbsdfile} )
if [ "${_path_pref}" != "/" ]; then
cbsdfile="${workdir}/${cbsdfile}"
fi
Makefile="${cbsdfile}"
myworkdir=$( ${DIRNAME_CMD} ${cbsdfile} )
else
Makefile="${CBSD_PWD}/CBSDfile"
myworkdir="${CBSD_PWD}"
fi
# inherit cwd from args (when run via tasks)
if [ -n "${cwd}" ]; then
CBSD_PWD="${cwd}"
myworkdir="${cwd}"
fi
[ ! -r "${Makefile}" ] && err 1 "${N1_COLOR}no such CBSDMake file: ${N2_COLOR}${Makefile}${N0_COLOR}"
cd ${myworkdir}
all_jail_list=$( ${EGREP_CMD} '^jail_[a-zA-Z0-9_@%:][-a-zA-Z0-9_@%:]*\(\)$' ${Makefile} | ${XARGS_CMD} | ${TR_CMD} -d "()" | ${SED_CMD} s#jail_##g )
all_bhyve_list=$( ${EGREP_CMD} '^bhyve_[a-zA-Z0-9_@%:][-a-zA-Z0-9_@%:]*\(\)$' ${Makefile} | ${XARGS_CMD} | ${TR_CMD} -d "()" | ${SED_CMD} s#bhyve_##g )
jail_list=
jail_num=0
bhyve_list=
bhyve_num=0
if [ -n "${ARGS}" ]; then
# limit jail env by args ?
for i in ${ARGS}; do
for j in ${all_jail_list}; do
if [ "${i}" = "${j}" ]; then
jail_num=$(( jail_num + 1 ))
if [ -n "${jail_list}" ]; then
jail_list="${jail_list} ${i}"
else
jail_list="${i}"
fi
fi
done
for j in ${all_bhyve_list}; do
if [ "${i}" = "${j}" ]; then
bhyve_num=$(( bhyve_num + 1 ))
if [ -n "${bhyve_list}" ]; then
bhyve_list="${bhyve_list} ${i}"
else
bhyve_list="${i}"
fi
fi
done
done
else
jail_list="${all_jail_list}"
for i in ${jail_list}; do
jail_num=$(( jail_num + 1 ))
done
bhyve_list="${all_bhyve_list}"
for i in ${bhyve_list}; do
bhyve_num=$(( bhyve_num + 1 ))
done
fi
[ -z "${jail_list}" -a -z "${bhyve_list}" ] && err 1 "${N1_COLOR}empty env list${N0_COLOR}"
num_env=$(( jail_num + bhyve_num ))
[ ${num_env} -gt 1 ] && ${ECHO} "${N1_COLOR}number of environments: ${N2_COLOR}${num_env}${N0_COLOR}" 1>&2
# read config
. ${Makefile}
## CBSDFile function
fn_exists()
{
LC_ALL=C type ${1} | ${GREP_CMD} -q 'shell function'
}
###
fi