forked from genba/freenas-plugins
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate_plugin
executable file
·425 lines (362 loc) · 8.75 KB
/
create_plugin
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/bin/sh
plugins_top="$(pwd)"
#
# This is the directory where libsh and pbi-manager get installed
# everytime this tool is ran. This allows plugins to be created on
# systems without pbi-manager and libsh installed.
#
plugins_sbin="${plugins_top}/sbin"
#
# This is the directoy where the PBI world chroot will reside
# ${zfsroot}/FNP/.pbi-world-${ARCH}.
#
# This can be set with -b
#
plugins_build=""
plugins_pbidir=""
#
# This is where source code that the build system uses resides.
# The FreeBSD source code is also checked out here.
#
plugins_src="${plugins_top}/src"
#
# This is where source code for plugins goes. Plugin source code that is
# not part of the freenas-plugins source also needs to go here to build.
#
plugins_dir="${plugins_top}/plugins"
#
# This is the actual directory where FreeBSD gets built.
#
plugins_builddir="${plugins_top}/build"
#
# pbi-manager and libsh locations. These are installed into
# ${plugins_sbin} and used to create the plugins.
#
plugins_pbi_manager="${plugins_src}/pbi-manager"
plugins_libsh="${plugins_src}/libsh"
#
# Where pbi-wrapper source code is located. This is only set because it
# gets copied to the FreeBSD source tree and set with LOCAL_DIRS so it
# can be built with the distribution.
#
plugins_pbi_wrapper="${plugins_src}/pbi-wrapper"
#
# Number of make jobs to use. This can be set with -j.
#
plugins_njobs=$(( 2 * $(sysctl -n kern.smp.cpus) + 1 ))
#
# What ports directory to use
# This can be set with -p.
#
plugins_portsdir="${plugins_builddir}/ports"
if [ -n "${PLUGINS_PORTSDIR}" ]
then
plugins_portsdir="${PLUGINS_PORTSDIR}"
fi
#
# Where the FreeBSD source is
#
# This should really be trueos source, but we don't
# have a 9.0 branch, so use FreeBSD source for now
#
plugins_freebsd_src="${plugins_builddir}/FreeBSD"
if [ -n "${PLUGINS_FREEBSD_SRC}" ]
then
plugins_freebsd_src="${PLUGINS_FREEBSD_SRC}"
fi
#
# Where packages are cached from plugin builds, defaults to
# ${plugins_builddir}/${plugin}/pkgs. This can be set with -P.
#
plugins_pkgdir=""
#
# Where the actual plugin is to be output! Defaults to
# "${plugins_builddir}/${plugin}. This can be set with -o.
#
plugins_outdir=""
#
# Plugin architecture to build, Defaults to $(uname -m).
# This can be set with -a.
#
plugins_arch=""
#
# FreeBSD RELEASE to use, defaults to 9.0-RELEASE.
# This can be set with -r.
#
plugins_osrelease=""
#
# This is for debugging only. Can be set with -n. This will pass
# -DNO_CLEAN to the FreeBSD buildworld command.
#
NO_CLEAN=0
install_pbi_manager()
{
local links="pbi_add pbi_addrepo pbi_browser pbi_autobuild \
pbi_delete pbi_deleterepo pbi_icon pbi_info pbi_indextool \
pbi_listrepo pbi_makepatch pbi_makeport pbi_makerepo \
pbi_metatool pbi_patch pbi_update pbi_update_hashdir pbid"
cp "${plugins_libsh}/functions.sh" "${plugins_sbin}/functions.sh"
cp "${plugins_pbi_manager}/pbi-manager" "${plugins_sbin}/pbi_create"
chmod 755 "${plugins_sbin}/pbi_create"
for l in ${links}
do
rm -f "${plugins_sbin}/${l}"
ln -f "${plugins_sbin}/pbi_create" "${plugins_sbin}/${l}"
done
}
do_create_plugin()
{
local plugin="${1}"
local archflags=""
local buildflags=
if [ ! -d "${plugins_outdir}" ]
then
mkdir -p "${plugins_outdir}"
fi
if [ ! -d "${plugins_pkgdir}" ]
then
mkdir -p "${plugins_pkgdir}"
fi
chown root:wheel "${plugins_outdir}"
chown root:wheel "${plugins_pkgdir}"
local realarch="$(uname -m)"
realarch="$(get_arch "${realarch}")"
if [ "${plugins_arch}" = "x32" ]
then
archflags="-32"
fi
if [ "${plugins_njobs}" -gt "0" ]
then
buildflags="-j ${plugins_njobs}"
fi
if [ "${NO_CLEAN}" = "1" ]
then
buildflags="${buildflags} -DNO_CLEAN"
fi
#
# Lots of secret sauce here!
#
# PBI_BUILDSRC tells pbi-manager where the FreeBSD source code is
# PBI_OSREL tells pbi-manager the FreeBSD OS release to checkout
# PBI_APPDIR tells pbi-mananger where "/usr/pbi" is
# PBI_BINDIR tells pbi-mananger where pbi-mananger is ;-)
# PBI_BUILDLOG tells pbi-manager where to (build|install)world output
# PBI_LOCAL_DIRS tells FreeBSD to use LOCAL_DIRS
# PBI_DELETE_BUILD tells pbi-manager to delete FreeBSD or not
# PBI_BUILDFLAGS is passed to FreeBSD (build|install)world
# MAKEOBJDIRPREFIX tells FreeBSD where to build
#
# pbi_makeport:
#
# -d: The plugins directory to use
# -c: The plugin directory
# -o: Where to place the created plugin
# --pkgdir: Where to save packages created during plugin build
# --tmpfs: Use tmpfs ;-)
#
# ${archflags}: If set (will be -32), create 32-bit plugin
#
env PATH="${plugins_sbin}:${PATH}" \
PBI_OSREL="9.3-RELEASE" \
PBI_APPDIR="${plugins_pbidir}" \
PBI_BINDIR="${plugins_sbin}" \
PBI_BUILDLOG="${plugins_builddir}/buildworld.log" \
PBI_LOCAL_DIRS="${plugins_pbi_wrapper}" \
PBI_DELETE_BUILD=0 \
PBI_BUILDSRC="${plugins_freebsd_src}" \
PBI_BUILDFLAGS="${buildflags}" \
MAKEOBJDIRPREFIX="${plugins_builddir}/obj" \
pbi_makeport \
-d "${plugins_portsdir}" \
-c "${plugins_dir}/${plugin}" \
-o "${plugins_outdir}" \
--pkgdir "${plugins_pkgdir}" \
--tmpfs \
"${archflags}"
}
get_arch()
{
local arch="${1}"
if [ -z "${arch}" ]
then
arch="$(uname -m)"
fi
if [ "${arch}" = "amd64" ]
then
arch="x64"
elif [ "${arch}" = "i386" ]
then
arch="x32"
fi
echo "${arch}"
}
set_defaults()
{
local plugin="${1}"
if [ -z "${plugins_arch}" ]
then
plugins_arch="$(get_arch)"
fi
if [ -z "${plugins_outdir}" ]
then
plugins_outdir="${plugins_builddir}/${plugin}"
fi
if [ -z "${plugins_pkgdir}" ]
then
plugins_pkgdir="${plugins_builddir}/${plugin}/pkgs"
fi
if [ -z "${plugins_osrelease}" ]
then
plugins_osrelease="9.0-RELEASE"
fi
}
list_plugins()
{
local plugins="$(ls "${plugins_dir}")"
for p in ${plugins}
do
echo ${p}
done
}
usage()
{
cat <<- __EOF__
usage: $0 [options] <plugin>
Where option is:
-a <plugins architecture> # Set plugins architecture
-b <plugins build directory> # Directory where plugins are built
-h # Display help message
-j <make jobs> # Number of make jobs
-l # List available plugins
-o <output directory> # Directory where plugins are output
-p <ports directory> # Directory where ports are located
-P <package directory> # Directory where packages are stored
-r <OS release> # FreeBSD OS release
__EOF__
}
main()
{
if [ "$(id -u)" != "0" ]
then
echo "$(basename $0): Must be root"
exit 2
fi
while getopts "a:b:hlno:p:P:r:" opt
do
case "${opt}" in
a)
plugins_arch="$(get_arch "${OPTARG}")"
;;
b)
plugins_build="${OPTARG}"
;;
h)
usage
exit 0
;;
j)
plugins_njobs="${OPTARG}"
;;
l)
list_plugins
exit 0
;;
n)
NO_CLEAN=1
;;
o)
plugins_outdir="${OPTARG}"
;;
p)
plugins_portsdir="${OPTARG}"
;;
P)
plugins_pkgdir="${OPTARG}"
;;
r)
plugins_osrelease="${OPTARG}"
;;
\?)
usage
exit 1
;;
esac
done
shift $(( ${OPTIND} - 1 ))
local plugin="${1}"
if [ -z "${plugin}" ]
then
usage
exit 1
fi
set_defaults "${plugin}"
if [ ! -d "${plugins_sbin}" ]
then
mkdir -p "${plugins_sbin}"
fi
if [ -d "${plugins_sbin}" ]
then
for f in $(ls "${plugins_sbin}")
do
rm -f "${plugins_sbin}/${f}"
done
install_pbi_manager
fi
if [ ! -d "${plugins_builddir}/obj" ]
then
mkdir -p "${plugins_builddir}/obj"
fi
chown root:wheel "${plugins_sbin}"
chown root:wheel "${plugins_builddir}/obj"
if [ -d "${plugins_portsdir}" ]
then
echo "Updating FreeBSD ports"
local here="$(pwd)"
cd "${plugins_portsdir}"
git pull --rebase
cd "${here}"
else
echo "Checking out FreeBSD ports"
git clone "${GIT_PORTS_REPO}" "${plugins_portsdir}" -b "${GIT_PORTS_BRANCH}"
if [ "$?" != "0" ]
then
echo "Couldn't checkout ports!"
exit 3
fi
fi
#
# At this point, pbi-manager and libsh are installed.
# We need libsh so we can determine the dataset we're on
# and use that for the plugin build and pbi directory.
#
. ${plugins_sbin}/functions.sh
local ds="$(getZFSDataset "${plugins_top}")"
local mp="$(getZFSMountpoint "${ds}")"
if ! echo "${mp}" | grep -q '^/'
then
mp=
fi
if [ -z "${mp}" ]
then
local p="$(realpath "${plugins_top}/../")"
plugins_top="${p}/fnp"
plugins_pbidir="${p}/pbi"
fi
if [ -z "${plugins_build}" ]
then
plugins_build="${mp}/fnp"
fi
if [ -z "${plugins_pbidir}" ]
then
plugins_pbidir="${mp}/pbi"
fi
do_create_plugin "${plugin}"
}
if [ "${GIT_LOCATION}" = "EXTERNAL" ] ; then
: ${GIT_FREEBSD_REPO=https://github.com/trueos/trueos}
: ${GIT_PORTS_REPO=https://github.com/freenas/ports.git}
fi
: ${GIT_FREEBSD_REPO=git@gitserver:/git/repos/freenas-build/trueos.git}
: ${GIT_PORTS_REPO=git@gitserver:/git/repos/freenas-build/ports.git}
: ${GIT_PORTS_BRANCH=9.2.1-BRANCH}
main $*