-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovienight-jail.sh
executable file
·208 lines (184 loc) · 4.67 KB
/
movienight-jail.sh
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
#!/bin/sh
# Build an iocage jail under FreeNAS 11.3 using the current release of Movie Night
# git clone https://github.com/zorglube/freenas-iocage-movienight
# Check for root privileges
if ! [ $(id -u) = 0 ]; then
echo "This script must be run with root privileges"
exit 1
fi
#####
#
# General configuration
#
#####
# Initialize defaults
JAIL_IP=""
JAIL_INTERFACES=""
DEFAULT_GW_IP=""
INTERFACE="vnet0"
VNET="on"
JAIL_NAME="movienight"
CONFIG_NAME="mn-config"
UID="movien"
GID=${UID}
UID_GID_ID="850"
ENV_VAR_UPDATE="env_var_update.sh"
TARGET=""
ARCH=""
MN_REPO=""
# turn on the colors for the `iocage` command.
IOCAGE_COLOR=true
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "${SCRIPT}")
# Check for mn-config and set configuration
if ! [ -e "${SCRIPTPATH}"/"${CONFIG_NAME}" ]; then
echo "${SCRIPTPATH}/${CONFIG_NAME} must exist."
exit 1
fi
# Load conf vars
. "${SCRIPTPATH}"/"${CONFIG_NAME}"
INCLUDES_PATH="${SCRIPTPATH}"/includes
JAILS_MOUNT=$(zfs get -H -o value mountpoint $(iocage get -p)/iocage)
# FreeNAS/TrueNAS Running instance version
RELEASE=$(freebsd-version | sed "s/STABLE/RELEASE/g" | sed "s/-p[0-9]*//")
# Arbitrary selection of an version of the `iocage`
#RELEASE=12.2-RELEASE
#####
#
# Delete old Jail
#
#####
iocage destroy ${JAIL_NAME} --force --recursive
#####
#
# Input/Config Sanity checks
#
#####
# Check that necessary variables were set by nextcloud-config
if [ -z "${JAIL_IP}" ]; then
echo 'Configuration error: JAIL_IP must be set'
exit 1
fi
if [ -z "${JAIL_INTERFACES}" ]; then
echo 'JAIL_INTERFACES not set, defaulting to: vnet0:bridge0'
JAIL_INTERFACES="vnet0:bridge0"
fi
if [ -z "${DEFAULT_GW_IP}" ]; then
echo 'Configuration error: DEFAULT_GW_IP must be set'
exit 1
fi
if [ -z "${TARGET}" ]; then
echo 'Configuration error: TARGET must be set'
exit 1
fi
if [ -z "${ARCH}" ]; then
echo 'Configuration error: ARCH must be set'
exit 1
fi
if [ -z "${MN_REPO}" ]; then
echo 'Configuration error: ARCH must be set'
exit 1
fi
# Extract IP and netmask, sanity check netmask
IP=$(echo ${JAIL_IP} | cut -f1 -d/)
NETMASK=$(echo ${JAIL_IP} | cut -f2 -d/)
if [ "${NETMASK}" = "${IP}" ]
then
NETMASK="24"
fi
if [ "${NETMASK}" -lt 8 ] || [ "${NETMASK}" -gt 30 ]
then
NETMASK="24"
fi
#####
#
# Jail Creation
#
#####
# List packages to be auto-installed after jail creation
cat <<__EOF__ >/tmp/pkg.json
{
"pkgs":[
"nano","bash","gzip","ca_root_nss","git","lang/go"
]
}
__EOF__
# Create the jail and install previously listed packages
if ! iocage create --name "${JAIL_NAME}" -p /tmp/pkg.json -r "${RELEASE}" interfaces="${JAIL_INTERFACES}" ip4_addr="${INTERFACE}|${JAIL_IP}" defaultrouter="${DEFAULT_GW_IP}" boot="on" host_hostname="${JAIL_NAME}" vnet="${VNET}"
then
echo "Failed to create jail"
exit 1
fi
rm /tmp/pkg.json
##
#
# Create user that run the MN process into the jail
#
##
iocage exec "${JAIL_NAME}" "pw user add ${UID} -c ${GID} -u ${UID_GID_ID} -d /usr/local/movienight/ -s /bin/csh"
#####
#
# GO Download and Setup
#
#####
USR_LOCAL="/usr/local"
ROOT_PROFILE="/root/.profile"
SHELL="/bin/bash"
OS=`uname`
INCLUDE_JAIL="/mnt/includes"
iocage exec "${JAIL_NAME}" mkdir -p ${INCLUDE_JAIL}
iocage fstab -a "${JAIL_NAME}" "${INCLUDES_PATH}" ${INCLUDE_JAIL} nullfs rw 0 0
if ! iocage restart "${JAIL_NAME}"
then
echo "Fail to restart Jail"
exit 1
fi
#####
#
# MovieNight Download and Setup
#
#####
MN_URL=${MN_REPO}
MN_HOME="/usr/local/movienight"
MN_MAKEFILE="${MN_HOME}"/Makefile.BSD
MN_LOG_FILE=/var/log/movienight.log
BUILD_CMD="make TARGET=freebsd ARCH=amd64 -f ${MN_MAKEFILE} -C ${MN_HOME} -D SHELL=/usr/local/bin/bash"
if ! iocage exec "${JAIL_NAME}" mkdir "${MN_HOME}"
then
echo "Failed to create download temp dir"
exit 1
fi
iocage exec "${JAIL_NAME}" cd "${MN_HOME}"
if ! iocage exec "${JAIL_NAME}" git clone "${MN_URL}" "${MN_HOME}"
then
echo "Failed to download Movie Night"
exit 1
fi
if ! iocage exec "${JAIL_NAME}" "${BUILD_CMD}"
then
echo "Failed to make Movie Night"
exit 1
fi
if ! iocage exec ${JAIL_NAME} touch ${MN_LOG_FILE}
then
echo "Cant create log file"
exit 1
fi
if ! iocage exec ${JAIL_NAME} chown ${UID}:${GID} ${MN_LOG_FILE}
then
echo "Can't chown ${MN_LOG_FILE}"
exit 1
fi
if ! iocage exec ${JAIL_NAME} chown -R ${UID}:${GID} ${MN_HOME}
then
echo "Failed to chown ${MN_HOME}"
exit 1
fi
# Copy pre-written config files
iocage exec "${JAIL_NAME}" cp ${INCLUDE_JAIL}/movienight /usr/local/etc/rc.d/
iocage exec "${JAIL_NAME}" chmod +x /usr/local/etc/rc.d/movienight
iocage exec "${JAIL_NAME}" sysrc movienight_enable=YES
iocage restart "${JAIL_NAME}"
# Don't need /mnt/includes any more, so unmount it
iocage fstab -r "${JAIL_NAME}" "${INCLUDES_PATH}" ${INCLUDE_JAIL} nullfs rw 0 0
iocage exec "${JAIL_NAME}" rmdir ${INCLUDE_JAIL}