Skip to content

Commit 989692f

Browse files
authored
Merge pull request #5 from cedwards/master
0.3.20181112 template support
2 parents 117dec2 + 7700b9b commit 989692f

25 files changed

+218
-31
lines changed

README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Available Commands:
2626
start Start a stopped jail.
2727
stop Stop a running jail.
2828
sysrc Safely edit rc files within targeted jail(s).
29+
template Apply Bastille template to running jail(s).
2930
top Display and update information about the top(1) cpu processes.
3031
update Update jail base -pX release.
3132
upgrade Upgrade jail release to X.Y-RELEASE.
@@ -130,7 +131,7 @@ release version as the argument.
130131

131132
```shell
132133
ishmael ~ # bastille bootstrap 11.2-RELEASE
133-
ishmael ~ # bastille bootstrap 10.4-RELEASE
134+
ishmael ~ # bastille bootstrap 12.0-RELEASE
134135
```
135136

136137
This command will ensure the required directory structures are in place and
@@ -440,6 +441,86 @@ Note: jail console logs not destroyed.
440441

441442
```
442443
444+
bastille template
445+
-----------------
446+
Bastille supports a templating system allowing you to apply files, pkgs and
447+
execute commands inside the jail automatically.
448+
449+
Currently supported template hooks are: `PRE`, `CONFIG`, `PKG`, `SYSRC`, `CMD`.
450+
Planned template hooks include: `FSTAB`, `PF`
451+
452+
Templates are created in `${bastille_prefix}/templates` and can leverage any of
453+
the template hooks. Simply create a new directory named after the template. eg;
454+
455+
```shell
456+
mkdir -p /usr/local/bastille/templates/base
457+
```
458+
459+
To leverage a template hook, create an UPPERCASE file in the root of the
460+
template directory named after the hook you want to execute. eg;
461+
462+
```shell
463+
echo "zsh vim-console git-lite htop" > /usr/local/bastille/templates/base/PKG
464+
echo "/usr/bin/chsh -s /usr/local/bin/zsh" > /usr/local/bastille/templates/base/CMD
465+
echo "etc root usr" > /usr/local/bastille/templates/base/CONFIG
466+
```
467+
468+
Template hooks are executed in specific order and require specific syntax to
469+
work as expected. This table outlines those requirements:
470+
471+
| HOOK | format | example |
472+
|---------|------------------|--------------------------------------|
473+
| PRE/CMD | /bin/sh command | /usr/bin/chsh -s /usr/local/bin/zsh |
474+
| CONFIG | path | etc root usr |
475+
| PKG | port/pkg name(s) | vim-console zsh git-lite tree htop |
476+
| SYSRC | sysrc command(s) | nginx_enable="YES" nginx_flags="..." |
477+
478+
In addition to supporting template hooks, Bastille supports overlaying files
479+
into the jail. This is done by placing the files in their full path, using the
480+
template directory as "/".
481+
482+
An example here may help. Think of `/usr/local/bastille/templates/base`, our
483+
example template, as the root of our filesystem overlay. If you create an
484+
`etc/hosts` or `etc/resolv.conf` *inside* the base template directory, these
485+
can be overlayed into your jail.
486+
487+
Note: due to the way FreeBSD segregates user-space, the majority of your
488+
overlayed template files will be in `usr/local`. The few general
489+
exceptions are the `etc/hosts`, `etc/resolv.conf`, and `etc/rc.conf.local`.
490+
491+
After populating `usr/local/` with custom config files that your jail will
492+
use, be sure to include `usr` in the template CONFIG definition. eg;
493+
494+
```shell
495+
echo "etc usr" > /usr/local/bastille/templates/base/CONFIG
496+
```
497+
498+
The above example "etc usr" will include anything under "etc" and "usr" inside
499+
the template. You do not need to list individual files. Just include the
500+
top-level directory name.
501+
502+
Applying Templates
503+
------------------
504+
505+
Jails must be running to apply templates.
506+
507+
Bastille includes a `template` sub-command. This sub-command requires a target
508+
and a template name. As covered in the previous section, template names
509+
correspond to directory names in the `bastille/templates` directory.
510+
511+
```shell
512+
ishmael ~ # bastille template folsom base
513+
[folsom]:
514+
Copying files...
515+
Copy complete.
516+
Installing packages.
517+
...[snip]...
518+
Executing final command(s).
519+
chsh: user information updated
520+
Template Complete.
521+
522+
```
523+
443524
444525
bastille top
445526
------------

bastille/usr/local/bin/bastille renamed to usr/local/bin/bastille

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

31+
SAVED_TERM=$TERM
32+
3133
. /usr/local/share/bastille/colors.pre.sh
3234
. /usr/local/etc/bastille/bastille.conf
3335

3436
## version
35-
BASTILLE_VERSION="0.3.20181107"
37+
BASTILLE_VERSION="0.3.20181112"
3638

3739

3840
usage() {
@@ -58,6 +60,7 @@ Available Commands:
5860
start Start a stopped container.
5961
stop Stop a running container.
6062
sysrc Safely edit rc files within targeted container(s).
63+
template Apply file templates to targeted jail(s).
6164
top Display and update information about the top(1) cpu processes.
6265
update Update container base -pX release.
6366
upgrade Upgrade container release to X.Y-RELEASE.
@@ -73,7 +76,6 @@ EOF
7376

7477
CMD=$1
7578
shift
76-
CMD_ENV=
7779

7880
# Handle special-case commands first.
7981
case "${CMD}" in
@@ -88,22 +90,13 @@ esac
8890

8991
# Filter out all non-commands
9092
case "${CMD}" in
91-
cmd|console|cp|create|destroy|list|pkg|restart|start|stop|sysrc|verify)
93+
cmd|cp|create|destroy|list|pkg|restart|start|stop|sysrc|template|verify)
9294
;;
9395
update|upgrade)
9496
CMD_ENV="${CMD_ENV} PAGER=cat"
9597
;;
9698
console|bootstrap|htop|top)
97-
while read envvar envvalue; do
98-
case "${envvar}" in
99-
TERM)
100-
CMD_ENV="${CMD_ENV} ${envvar}=${envvalue}"
101-
;;
102-
esac
103-
done <<-EOF
104-
$(env | sed -Ee 's,^([^=]*)=(.*),\1 \2,')
105-
EOF
106-
;;
99+
;;
107100
bootstrap|update|upgrade)
108101
while read envvar envvalue; do
109102
case "${envvar}" in
@@ -120,6 +113,7 @@ bootstrap|update|upgrade)
120113
;;
121114
esac
122115

116+
123117
SCRIPTPATH="${bastille_sharedir}/${CMD}.sh"
124118

125119
: ${UMASK:=022}
File renamed without changes.

bastille/usr/local/share/bastille/bootstrap.sh renamed to usr/local/share/bastille/bootstrap.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ case "${RELEASE}" in
120120
bootstrap
121121
echo -e "${COLOR_RED}BETA releases are complete untested.${COLOR_RESET}"
122122
;;
123+
12.0-BETA4)
124+
bootstrap
125+
echo -e "${COLOR_RED}BETA releases are complete untested.${COLOR_RESET}"
126+
;;
123127
*)
124128
echo -e "${COLOR_RED}BETA releases are complete untested.${COLOR_RESET}"
125129
usage

bastille/usr/local/share/bastille/cmd.sh renamed to usr/local/share/bastille/cmd.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,4 @@ fi
5656
for _jail in ${JAILS}; do
5757
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
5858
jexec -l ${_jail} $2
59-
echo -e "${NC}"
6059
done

bastille/usr/local/share/bastille/console.sh renamed to usr/local/share/bastille/console.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,5 @@ fi
5555

5656
for _jail in ${JAILS}; do
5757
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
58-
jexec -l ${_jail} /usr/bin/login -f root
59-
echo -e "${NC}"
58+
jexec -l ${_jail} /usr/bin/login -fp root
6059
done

bastille/usr/local/share/bastille/create.sh renamed to usr/local/share/bastille/create.sh

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ create_jail() {
7070

7171
if [ ! -d "${bastille_jail_base}" ]; then
7272
mkdir -p "${bastille_jail_base}"
73-
mkdir -p "${bastille_jail_path}/usr"
7473
mkdir -p "${bastille_jail_path}/usr/home"
74+
mkdir -p "${bastille_jail_path}/usr/local"
7575
fi
7676

7777
if [ ! -d "${bastille_jail_template}" ]; then
@@ -97,17 +97,17 @@ create_jail() {
9797
## ro
9898
cd "${bastille_jail_path}"
9999
echo
100-
echo -e "${COLOR_GREEN}RELEASE: ${RELEASE}.${COLOR_RESET}"
101100
echo -e "${COLOR_GREEN}NAME: ${NAME}.${COLOR_RESET}"
102101
echo -e "${COLOR_GREEN}IP: ${IP}.${COLOR_RESET}"
102+
echo -e "${COLOR_GREEN}RELEASE: ${RELEASE}.${COLOR_RESET}"
103103
echo
104104

105105
for _link in bin boot lib libexec rescue sbin usr/bin usr/include usr/lib usr/lib32 usr/libdata usr/libexec usr/sbin usr/share usr/src; do
106106
ln -sf /.bastille/${_link} ${_link}
107107
done
108108

109109
## link home properly
110-
ln -sf usr/home home
110+
ln -s usr/home home
111111

112112
## rw
113113
cp -a "${bastille_releasesdir}/${RELEASE}/.cshrc" "${bastille_jail_path}"
@@ -135,8 +135,7 @@ create_jail() {
135135
fi
136136

137137
## TZ: UTC
138-
ln -s "/usr/share/zoneinfo/Etc/UTC ${bastille_jail_root}/etc/localtime"
139-
ln -s "/.template/usr/local ${bastille_jail_root}/usr/local"
138+
ln -s /usr/share/zoneinfo/Etc/UTC etc/localtime
140139
}
141140

142141
# Handle special-case commands first.
@@ -174,12 +173,6 @@ if [ -d "/usr/local/bastille/jails/${NAME}/root/.bastille" ]; then
174173
exit 1
175174
fi
176175

177-
## check for name/root/.template
178-
if [ -d "/usr/local/bastille/jails/${NAME}/root/.template" ]; then
179-
echo -e "${COLOR_RED}Jail: ${NAME} already created. ${NAME}/root/.template exists.${COLOR_RESET}"
180-
exit 1
181-
fi
182-
183176
## check if a running jail matches name
184177
if running_jail ${NAME}; then
185178
echo -e "${COLOR_RED}Running jail matches name.${COLOR_RESET}"

bastille/usr/local/share/bastille/htop.sh renamed to usr/local/share/bastille/htop.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,5 @@ for _jail in ${JAILS}; do
6464
fi
6565
echo -e "${COLOR_RESET}"
6666
done
67+
68+
TERM=${SAVED_TERM}

bastille/usr/local/share/bastille/pkg.sh renamed to usr/local/share/bastille/pkg.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,4 @@ fi
5656
for _jail in ${JAILS}; do
5757
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
5858
jexec -l ${_jail} /usr/sbin/pkg $2
59-
echo -e "${COLOR_RESET}"
6059
done

bastille/usr/local/share/bastille/start.sh renamed to usr/local/share/bastille/start.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ fi
6262
for _jail in ${JAILS}; do
6363
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
6464
jail -f "${bastille_jailsdir}/${_jail}/jail.conf" -c ${_jail}
65-
echo -e "${COLOR_RESET}"
6665
done
6766

6867
## HUP the firewall

bastille/usr/local/share/bastille/stop.sh renamed to usr/local/share/bastille/stop.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ fi
5757
for _jail in ${JAILS}; do
5858
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
5959
jail -f "${bastille_jailsdir}/${_jail}/jail.conf" -r ${_jail}
60-
echo -e "${COLOR_RESET}"
6160
done
6261

6362
## HUP the firewall

usr/local/share/bastille/template.sh

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2018, Christer Edwards <christer.edwards@gmail.com>
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 are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this
10+
# list of conditions and the following disclaimer.
11+
#
12+
# * Redistributions in binary form must reproduce the above copyright notice,
13+
# this list of conditions and the following disclaimer in the documentation
14+
# and/or other materials provided with the distribution.
15+
#
16+
# * Neither the name of the copyright holder nor the names of its
17+
# contributors may be used to endorse or promote products derived from
18+
# this software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
31+
. /usr/local/share/bastille/colors.pre.sh
32+
. /usr/local/etc/bastille/bastille.conf
33+
34+
usage() {
35+
echo -e "${COLOR_RED}Usage: bastille template [ALL|glob] template.${COLOR_RESET}"
36+
exit 1
37+
}
38+
39+
# Handle special-case commands first.
40+
case "$1" in
41+
help|-h|--help)
42+
usage
43+
;;
44+
esac
45+
46+
if [ $# -gt 2 ] || [ $# -lt 2 ]; then
47+
usage
48+
fi
49+
50+
if [ "$1" = 'ALL' ]; then
51+
JAILS=$(jls -N name)
52+
fi
53+
if [ "$1" != 'ALL' ]; then
54+
JAILS=$(jls -N name | grep "$1")
55+
fi
56+
57+
TEMPLATE=$2
58+
bastille_template=${bastille_templatesdir}/${TEMPLATE}
59+
60+
for _jail in ${JAILS}; do
61+
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
62+
63+
## pre
64+
if [ -s "${bastille_template}/PRE" ]; then
65+
echo -e "${COLOR_GREEN}Executing PRE-command(s).${COLOR_RESET}"
66+
bastille_templatepre=$(cat "${bastille_template}/PRE")
67+
jexec -l "${_jail}" "${bastille_templatepre}"
68+
fi
69+
70+
## config
71+
if [ -s "${bastille_template}/CONFIG" ]; then
72+
echo -e "${COLOR_GREEN}Copying files...${COLOR_RESET}"
73+
for _dir in $(cat "${bastille_template}/CONFIG"); do
74+
cp -a "${bastille_template}/${_dir}" "${bastille_jailsdir}/${_jail}/root"
75+
done
76+
echo -e "${COLOR_GREEN}Copy complete.${COLOR_RESET}"
77+
fi
78+
79+
## fstab
80+
if [ -s "${bastille_template}/FSTAB" ]; then
81+
bastille_templatefstab=$(cat "${bastille_template}/FSTAB")
82+
echo -e "${COLOR_GREEN}Updating fstab.${COLOR_RESET}"
83+
fi
84+
85+
## pf
86+
if [ -s "${bastille_template}/PF" ]; then
87+
bastille_templatepf=$(cat "${bastille_template}/PF")
88+
echo -e "${COLOR_GREEN}Generating PF profile.${COLOR_RESET}"
89+
fi
90+
91+
## pkg (bootstrap + pkg)
92+
if [ -s "${bastille_template}/PKG" ]; then
93+
bastille_templatepkg=$(cat "${bastille_template}/PKG")
94+
echo -e "${COLOR_GREEN}Installing packages.${COLOR_RESET}"
95+
jexec -l ${_jail} env ASSUME_ALWAYS_YES="YES" /usr/sbin/pkg bootstrap
96+
jexec -l ${_jail} env ASSUME_ALWAYS_YES="YES" /usr/sbin/pkg audit -F
97+
jexec -l ${_jail} env ASSUME_ALWAYS_YES="YES" /usr/sbin/pkg install -y ${bastille_templatepkg}
98+
fi
99+
100+
## sysrc
101+
if [ -s "${bastille_template}/SYSRC" ]; then
102+
bastille_templatesys=$(cat "${bastille_template}/SYSRC")
103+
echo -e "${COLOR_GREEN}Updating services.${COLOR_RESET}"
104+
jexec -l ${_jail} /usr/sbin/sysrc ${bastille_templatesys}
105+
fi
106+
107+
## cmd
108+
if [ -s "${bastille_template}/CMD" ]; then
109+
bastille_templatecmd=$(cat "${bastille_template}/CMD")
110+
echo -e "${COLOR_GREEN}Executing final command(s).${COLOR_RESET}"
111+
jexec -l ${_jail} ${bastille_templatecmd}
112+
fi
113+
echo -e "${COLOR_GREEN}Template Complete.${COLOR_RESET}"
114+
echo
115+
echo
116+
done

bastille/usr/local/share/bastille/top.sh renamed to usr/local/share/bastille/top.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ for _jail in ${JAILS}; do
5959
jexec -l ${_jail} /usr/bin/top
6060
echo -e "${COLOR_RESET}"
6161
done
62+
63+
TERM=${SAVED_TERM}

0 commit comments

Comments
 (0)