-
Notifications
You must be signed in to change notification settings - Fork 1
/
prep-docker
executable file
·181 lines (155 loc) · 4.52 KB
/
prep-docker
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
#!/usr/bin/env bash
show_help() {
cat <<'EOF'
prep-docker
Usage:
prep-docker [options] <env-file> <out-dir>
Creates a Dockerfile file and a docker-compose.yml file that can be used to
build and run a container hosting an iRODS resource server configured to be
part of the CyVerse Data Store.
Parameters:
<env-file> the file providing the required environment variables, see below
<out-dir> the directory where the docker files will be written: defaults to
current working directory
Options:
-h, --help show help and exit
Environment Variables:
IRODS_CLERVER_USER (optional) the name of the rodsadmin user representing the
resource server within the zone: default 'ipc_admin'
IRODS_HOST_UID (optional) the UID of the hosting server to run iRODS as
instead of the default user defined in the container
IRODS_LOG_DIR (optional) the absolute path on the server hosting the
container where iRODS will write its logs: default
'<home>/log' where <home> is the home directory of the user
starting the docker container
IRODS_RES_SERVER the FQDN or address used by the IES and other resource
servers to communicate with this one
IRODS_RES_VAULT (optional) the host directory where the container will
mount the vault: default '<home>/vault', where <home> is
the home directory of the user starting the dock
container
IRODS_STORAGE_RES the name of the storage resource that will be served
EOF
}
set -o errexit -o nounset -o pipefail
main() {
local opts
if ! opts=$(getopt --name prep-docker --options h --longoptions help -- "$@")
then
show_help >&2
return 1
fi
eval set -- "$opts"
while true
do
case "$1" in
-h|--help)
show_help
return 0
;;
--)
shift
break
;;
*)
show_help >&2
return 1
;;
esac
done
if [ "$#" -lt 1 ]
then
printf 'An environment file is required.\n' >&2
show_help >&2
return 1
fi
local envFile="$1"
local outDir=.
if [ "$#" -ge 2 ]
then
outDir="$2"
if [ ! -e "$outDir" ]
then
mkdir --parents "$outDir"
fi
fi
build "$envFile" "$outDir"
}
build() {
local envFile="$1"
local outDir="$2"
# shellcheck disable=SC1090
. "$envFile"
check_env IRODS_STORAGE_RES
check_env IRODS_RES_SERVER
local defaultRes="$IRODS_STORAGE_RES"Res
local hostVault="${IRODS_RES_VAULT:-\$HOME/vault}"
local hostLog="${IRODS_LOG_DIR:-\$HOME/log}"
local user="${IRODS_CLERVER_USER:-ipc_admin}"
printf 'FROM cyverse/ds-irods-rs-onbuild:latest\n' > "$outDir"/Dockerfile
mk_compose_file "$defaultRes" "$user" "$hostLog" "$hostVault" > "$outDir"/docker-compose.yml
}
#
# Checks for the existence of an environment variable. If the variable doesn't
# exist, it writes a message to standard error and returns 1.
#
# Parameter:
# var the environment variable to check
#
check_env() {
local var="$1"
if [ -z "${!var}" ]
then
printf 'The environment variable %s is not set.\n' "$var" >&2
show_help >&2
return 1
fi
}
#
# Generate a script for starting a container from the newly build image.
#
# Parameters:
# defaultRes the name of the coordinating resource the server will use by
# default
# user the name of the rodsadmin user the server will authenticate as
# hostLog the host directory where teh container will mount
# /var/lib/irods/log
# hostVault the host directory where the container will mount its vault
#
mk_compose_file() {
local defaultRes="$1"
local user="$2"
local hostLog="$3"
local hostVault="$4"
cat <<EOF
---
services:
cyverse-rs:
build:
args:
IRODS_CLERVER_USER: $user
IRODS_DEFAULT_RES: $defaultRes
IRODS_HOST_UID: $IRODS_HOST_UID
IRODS_RES_SERVER: $IRODS_RES_SERVER
IRODS_STORAGE_RES: $IRODS_STORAGE_RES
context: .
env_file: cyverse-secrets.env
hostname: rs
ports:
- "1247:1247/tcp"
- "1248:1248/tcp"
- "20000-20009:20000-20009/tcp"
- "20000-20009:20000-20009/udp"
restart: on-failure
sysctls:
- net.ipv4.tcp_keepalive_time=120
tmpfs: /var/lib/irods/log/proc:uid=$IRODS_HOST_UID
tty: true
volumes:
- $hostVault:/irods_vault/$IRODS_STORAGE_RES
- $hostLog:/var/lib/irods/log
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
EOF
}
main "$@"