Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / adm / common-support.sh @ master

History | View | Annotate | Download (10.5 KB)

1 3a404465 Eric Allman
#!/bin/sh
2 3756d4ac Eric Allman
{ test -r /usr/local/etc/gdp.conf.sh && . /usr/local/etc/gdp.conf.sh; } ||
3
	{ test -r /etc/gdp.conf.sh && . /etc/gdp.conf.sh; }
4 3a404465 Eric Allman
5
#
6
#  These macros are intended to be sourced into other shell files
7
#
8
9 9200f3a8 Eric Allman
#################### MANIFEST CONSTANTS ####################
10
11 3a404465 Eric Allman
Reset=''    # Text Reset
12
13
# Regular           Bold                Underline           High Intensity      BoldHigh Intens     Background          High Intensity Backgrounds
14
Bla='';     BBla='';    UBla='';    IBla='';    BIBla='';   On_Bla='';    On_IBla='';
15
Red='';     BRed='';    URed='';    IRed='';    BIRed='';   On_Red='';    On_IRed='';
16
Gre='';     BGre='';    UGre='';    IGre='';    BIGre='';   On_Gre='';    On_IGre='';
17
Yel='';     BYel='';    UYel='';    IYel='';    BIYel='';   On_Yel='';    On_IYel='';
18
Blu='';     BBlu='';    UBlu='';    IBlu='';    BIBlu='';   On_Blu='';    On_IBlu='';
19
Pur='';     BPur='';    UPur='';    IPur='';    BIPur='';   On_Pur='';    On_IPur='';
20
Cya='';     BCya='';    UCya='';    ICya='';    BICya='';   On_Cya='';    On_ICya='';
21
Whi='';     BWhi='';    UWhi='';    IWhi='';    BIWhi='';   On_Whi='';    On_IWhi='';
22
23 9200f3a8 Eric Allman
# matches sysexits.h (see man sysexits)
24
EX_OK=0
25 30395b16 Eric Allman
EX_USAGE=64		# command line usage error
26
EX_DATAERR=65		# data format error
27
EX_NOINPUT=66		# cannot open input
28
EX_UNAVAILABLE=69	# service unavailable (a catch-all status)
29
EX_SOFTWARE=70		# internal software error
30
EX_OSERR=71		# system error (e.g., out of memory)
31
EX_OSFILE=72		# system file missing
32
EX_CANTCREATE=73	# cannot create (user) output file
33
EX_IOERR=74		# input/output error
34
EX_TEMPFAIL=75		# temp fail, try again sometime soon
35
EX_PROTOCOL=76		# remote error in protocol
36
EX_NOPERM=77		# permission denied
37
EX_CONFIG=78		# configuration error
38 9200f3a8 Eric Allman
39 3756d4ac Eric Allman
#################### FUNCTIONS ####################
40
41 dd24741f Eric Allman
: ${quiet:=false}
42 3756d4ac Eric Allman
43
# Error/Information messages
44 3a404465 Eric Allman
info() {
45 a848016d Eric Allman
    if ! $quiet; then
46 9200f3a8 Eric Allman
	echo "${Gre}${On_Bla}[INFO] $1${Reset}" 2>&1
47 a848016d Eric Allman
    fi
48 3a404465 Eric Allman
}
49
50
warn() {
51 a848016d Eric Allman
    if ! $quiet; then
52 9200f3a8 Eric Allman
	echo "${Yel}${On_Bla}[WARN] $1${Reset}" 2>&1
53 191945e3 Eric Allman
    fi
54
}
55
56
error() {
57
    if ! $quiet; then
58 9200f3a8 Eric Allman
	echo "${Red}${On_Bla}[ERROR] $1${Reset}" 2>&1
59 a848016d Eric Allman
    fi
60 3a404465 Eric Allman
}
61
62
fatal() {
63 9200f3a8 Eric Allman
	echo "${Whi}${On_Red}[FATAL] $1${Reset}" 2>&1
64
	exit ${2:-$EX_UNAVAILABLE}
65 3a404465 Eric Allman
}
66
67 19ddf5e5 Eric Allman
action() {
68 9200f3a8 Eric Allman
	echo "${Bla}${On_Yel}[ACTION REQUIRED] $1${Reset}" 2>&1
69 19ddf5e5 Eric Allman
}
70
71 191945e3 Eric Allman
# Read a password from the terminal
72
case `echo "foo\c"`,`echo -n bar` in
73
    *c*,-n*)	echo_n=		echo_c=		;;
74
    *c*,*)	echo_n=-n	echo_c=		;;
75
    *)		echo_n=		echo_c='\c'	;;
76
esac
77
78
#
79
#  Read a password without echo.
80
#  The first parameter is the name of the variable to set.
81
#  The second parameter is an optional prompt.
82
#
83
read_passwd() {
84
	local var=$1
85
	local prompt="${2:-Password}"
86
87
	stty -echo
88
	echo $echo_n "${prompt}:" $echo_c
89
	read $var
90
	echo ""
91
	stty echo
92
}
93
94 41926557 Eric Allman
#
95
#  Takes the root name of variables (nominally the name of an account)
96
#  and the name of a file.  If the variable ${acct}_pw is set then uses
97
#  the value of that as the password.  If not set but the indicated
98
#  file name exists, the content of that file is used as the password.
99
#  Otherwise, a new password is created which is stored in the file
100
#  and the ${acct}_pw variable.
101
#
102
set_up_passwd() {
103
	local acct="$1"
104
	local pw_file="$2"
105
	local var="\${${acct}_pw}"
106
	local pw=`eval "echo $var"`
107
108
	# if password not already set, try the file contents
109
	if [ -z "$pw" -a -f "$pw_file" ]; then
110
		pw=`sudo head -1 "$pw_file"`
111
		if [ ! -z "$pw" ]; then
112
			info "Using existing password from $pw_file for $acct."
113
		fi
114
	fi
115
116
	# if password still not set, create a new one
117
	if [ -z "$pw" ]; then
118
		pw=`dd if=/dev/random bs=1 count=9 2>/dev/null | base64`
119
		warn "Created new password '$pw' for $acct."
120
		info "Saving it in $pw_file."
121
		sudo sh -c "umask 0177; echo \"$pw\" > \"$pw_file\"; \
122
				chown ${GDP_USER}:${GDP_GROUP} \"$pw_file\""
123
	fi
124
125
	eval "${acct}_pw=\"$pw\""
126
	eval "${acct}_pw_file=\"$pw_file\""
127
}
128
129 3756d4ac Eric Allman
# Create a directory as the user gdp
130
mkdir_gdp() {
131
	test -d $1 && return
132 39ea10b4 Eric Allman
	if [ -e $1 ]; then
133
		warn "$1 exists but is not a directory"
134
		return
135
	fi
136 3756d4ac Eric Allman
	info "Creating $1 as ${GDP_USER}:${GDP_GROUP}"
137
	sudo mkdir -p $1
138 39ea10b4 Eric Allman
	sudo chmod ${2:-0775} $1
139
	sudo chown ${GDP_USER}:${GDP_GROUP} $1
140
}
141
142
# Create a file as the user gdp
143
mkfile_gdp() {
144
	test -f $1 && return
145
	if [ -e $1 ]; then
146
		warn "$1 exists but is not a file"
147
		return
148
	fi
149
	info "Creating $1 as ${GDP_USER}:${GDP_GROUP}"
150
	sudo cp /dev/null $1
151
	sudo chmod ${2:-0664} $1
152 3756d4ac Eric Allman
	sudo chown ${GDP_USER}:${GDP_GROUP} $1
153
}
154
155 71accc0f Eric Allman
package() {
156
    info "Checking package $1..."
157 4770b39a Eric Allman
    case "${PKGMGR:-unknown}" in
158
	"debian")
159 39adcd09 Eric Allman
	    if dpkg --get-selections | grep -w --quiet $1; then
160 71accc0f Eric Allman
		info "$1 is already installed. skipping."
161
	    else
162
		sudo apt-get install -y $@
163
	    fi
164
	    ;;
165 4770b39a Eric Allman
166
	"yum")
167 71accc0f Eric Allman
	    if rpm -qa | grep --quiet $1; then
168
		info "$1 is already installed. skipping."
169
	    else
170
		sudo yum install -y $@
171
	    fi
172
	    ;;
173 4770b39a Eric Allman
174
	"brew")
175
	    if brew list | grep --quiet $1; then
176
		info "$1 is already installed. skipping."
177
	    else
178
		brew install --build-bottle $@ || brew upgrade $@
179 71accc0f Eric Allman
	    fi
180 4770b39a Eric Allman
	    ;;
181
182
	"macports")
183
	    if port -q installed $1 | grep -q "."; then
184
		info "$1 is already installed. skipping."
185 71accc0f Eric Allman
	    else
186 4770b39a Eric Allman
		sudo port install $1
187 71accc0f Eric Allman
	    fi
188
	    ;;
189 4770b39a Eric Allman
190 71accc0f Eric Allman
	"freebsd")
191
	    export PATH="/sbin:/usr/sbin:$PATH"
192
	    if sudo pkg info -q $1; then
193
		info "$1 is already installed. skipping."
194
	    else
195
		sudo pkg install $@
196
	    fi
197
	    ;;
198 4770b39a Eric Allman
199 71accc0f Eric Allman
	"gentoo")
200
	    if equery list $1 >& /dev/null; then
201
		info "$1 is already installed. skipping."
202
	    else
203
		sudo emerge $1
204
	    fi
205
	    ;;
206 4770b39a Eric Allman
207 71accc0f Eric Allman
	*)
208 4770b39a Eric Allman
	    fatal "unrecognized PKGMGR $PKGMGR"
209 71accc0f Eric Allman
	    ;;
210
    esac
211
}
212
213 3756d4ac Eric Allman
# configure defaults
214 a848016d Eric Allman
configure_defaults() {
215
    : ${GDP_ROOT:=/usr}
216
    if [ "$GDP_ROOT" = "/usr" ]
217
    then
218
	    : ${GDP_ETC:=/etc/gdp}
219
    elif [ "$GDP_ROOT" = "/usr/local" -o "$GDP_ROOT" = "/opt/local" ]
220
    then
221
	    : ${GDP_ETC:=$GDP_ROOT/etc/gdp}
222
    else
223
	    : ${GDP_ETC:=$GDP_ROOT/etc}
224
    fi
225 9e62cb26 Eric Allman
    : ${EP_PARAMS:=$GDP_ETC/params}
226 a848016d Eric Allman
    : ${GDP_USER:=gdp}
227
    : ${GDP_GROUP:=$GDP_USER}
228
    : ${GDP_LOG_DIR:=/var/log/gdp}
229
    : ${GDP_VAR:=/var/swarm/gdp}
230
    : ${GDP_KEYS_DIR:=$GDP_ETC/keys}
231 d17c1801 Eric Allman
    : ${GDPLOGD_DATADIR:=$GDP_VAR/glogs}
232 a848016d Eric Allman
}
233 3756d4ac Eric Allman
234 c77f65be Eric Allman
235
dot_to_int() {
236
	full=$1
237
	major=`echo $full | sed 's/\..*//'`
238
	full=`echo $full | sed -e 's/[^.]*//' -e 's/^\.//'`
239
	minor=`echo $full | sed 's/\..*//'`
240
	full=`echo $full | sed -e 's/[^.]*//' -e 's/^\.//'`
241
	patch=`echo $full | sed 's/\..*//'`
242
	test -z "$minor" && minor=0
243
	test -z "$patch" && patch=0
244
	printf "%02d%02d%02d" "$major" "$minor" "$patch"
245 3a404465 Eric Allman
}
246 c77f65be Eric Allman
247
###
248
###  figure out operating system and version number
249
###
250 a848016d Eric Allman
OS=""
251
OSVER=""
252 c77f65be Eric Allman
253 4770b39a Eric Allman
fatal_osver() {
254
	fatal "Must be running $1 or later (have $VERSION_ID)"
255
}
256
257
warn_unsupported() {
258
	msg=${1:-"try anyway"}
259
	warn "$OS is not a supported platform, but I'll $msg"
260
}
261
262 a848016d Eric Allman
set_os() {
263
    if [ -f "/etc/os-release" ]; then
264
	. /etc/os-release
265
	OS="${ID-}"
266
	OSVER="${VERSION_ID-}"
267
    fi
268
    if [ -z "$OS" -a -f "/etc/lsb-release" ]; then
269
	. /etc/lsb-release
270
	OS="${DISTRIB_ID-}"
271
	OSVER="${DISTRIB_VERSION-}"
272
    fi
273
    if [ "$OS" ]; then
274
	# it is set --- do nothing
275
	true
276
    elif [ -f "/etc/centos-release" ]; then
277
	OS="centos"
278
	#OSVER=???
279
    elif [ -f "/etc/redhat-release" ]; then
280
	OS="redhat"
281
	OSVER=`sed -e 's/.* release //' -e 's/ .*//' /etc/redhat-release`
282
    else
283
	OS=`uname -s`
284
    fi
285
    OS=`echo $OS | tr '[A-Z]' '[a-z]'`
286
    if [ "$OS" = "linux" ]; then
287
	OS=`head -1 /etc/issue | sed 's/ .*//' | tr '[A-Z]' '[a-z]'`
288
    elif [ "$OS" = "darwin" ]; then
289
	    OSVER=`sw_vers |
290
		    sed -e '/ProductVersion:/!d' -e 's/^.*[ 	][ 	]*//'`
291
    elif [ "$OS" = "freebsd" ]; then
292
	    _major=`uname -r | sed -e 's/\..*//'`
293
	    _minor=`uname -r | sed -e 's/[0-9]*\.//' -e 's/-.*//'`
294
	    OSVER="$_major.$_minor"
295
    fi
296
297
    if [ -z "$OSVER" ]; then
298
	OSVER="0"
299
    else
300
	# clean up OSVER to make it a single integer
301
	OSVER=`dot_to_int $OSVER`
302
    fi
303
}
304
305 ae040ccc Eric Allman
check_os() {
306
    case "$OS" in
307
	"debian")
308 a848016d Eric Allman
	    if expr $OSVER \< 80000 > /dev/null
309
	    then
310
		    fatal_osver "Debian 8 (Jessie)"
311
	    fi
312
	    ;;
313
314 ae040ccc Eric Allman
	"ubuntu")
315 38721d9e Eric Allman
	    if expr $OSVER \< 160400 > /dev/null
316 a848016d Eric Allman
	    then
317 38721d9e Eric Allman
		    fatal_osver "Ubuntu 16.04"
318 a848016d Eric Allman
	    fi
319 ae040ccc Eric Allman
	    ;;
320
321
	"raspbian")
322
	    warn_unsupported "assume it is debian-based"
323
	    ;;
324
325
	"centos" | "freebsd" | "darwin" | "redhat" | "gentoo")
326
	    warn_unsupported
327
	    ;;
328
329
	*)
330
	    fatal "Oops, we don't support $OS"
331
    esac
332
}
333
334
335
# check to make sure we understand this OS and OSVER; choose PKGMGR & INITsys
336
set_pkgmgr() {
337 dd24741f Eric Allman
    : ${PKGMGR:=$OS}
338 ae040ccc Eric Allman
    INITguess=""
339 dd24741f Eric Allman
    case $PKGMGR in
340 57e502fb Eric Allman
      "ubuntu")
341 ae040ccc Eric Allman
	    PKGMGR=debian
342 a848016d Eric Allman
	    if expr $OSVER \>= 160400 > /dev/null
343
	    then
344
		    INITguess=systemd
345
	    else
346
		    INITguess=upstart
347
	    fi
348
	    ;;
349
350 57e502fb Eric Allman
      "debian")
351
	    INITguess=systemd
352
	    ;;
353
354 a848016d Eric Allman
      "raspbian")
355
	    PKGMGR=debian
356
	    ;;
357
358
      "redhat")
359
	    PKGMGR=yum
360
	    if expr $OSVER \< 070000 > /dev/null
361
	    then
362
		    INITguess=systemd
363
	    else
364
		    INITguess=upstart
365
	    fi
366
	    ;;
367
368
      "darwin")
369 dd24741f Eric Allman
	    if type port > /dev/null 2>&1 && port installed | grep -q .; then
370
		    PKGMGR=macports
371
	    fi
372
	    if type brew > /dev/null 2>&1 && [ ! -z "`brew config`" ]
373
	    then
374
		if [ "$PKGMGR" = "macports" ]; then
375 42f4d4a4 Eric Allman
		    PKGMGR=brewports
376 dd24741f Eric Allman
		    warn "You seem to have both macports and homebrew installed."
377
		    warn "They conflict with each other, and you may break all your"
378
		    warn "packages if you try to use them at the same time."
379
		    warn "Please choose one or the other.  Macports seems to work better."
380
		    fatal "Set envar PKGMGR to 'brew' or 'macports' to choose."
381 42f4d4a4 Eric Allman
		else
382
		    PKGMGR=brew
383 a848016d Eric Allman
		fi
384
	    fi
385
	    if [ "$PKGMGR" = "darwin" ]; then
386
		warn "You must install macports or homebrew."
387
		fatal "See README-compiling.md (Operating System Quirks) for details."
388 4770b39a Eric Allman
	    fi
389 a848016d Eric Allman
	    ;;
390
391
      "centos")
392
	    PKGMGR=yum
393
	    ;;
394
    esac
395 4770b39a Eric Allman
396 a848016d Eric Allman
    # determine what init system we are using (heuristic!)
397
    if [ -z "$INITguess" ]
398
    then
399
	    case $OS in
400
	      "debian" | "ubuntu" | "centos" | "redhat" | "gentoo")
401
		    # some linux variant; see if we can figure out systemd
402
		    proc1exe=`sudo stat /proc/1/exe | grep 'File: '`
403
		    if echo "$proc1exe" | grep -q "systemd"
404
		    then
405
			    INITguess="systemd"
406
		    fi
407
		    ;;
408
	    esac
409
    fi
410
}
411 4770b39a Eric Allman
412 c77f65be Eric Allman
413 a848016d Eric Allman
#################### END OF FUNCTIONS ####################
414
415
configure_defaults
416
set_os
417 ae040ccc Eric Allman
check_os
418
if [ -z "$PKGMGR" ]
419
then
420
    set_pkgmgr
421
fi
422 c77f65be Eric Allman
423
# see if we should use our guess
424 ae040ccc Eric Allman
test -z "$INITSYS" && INITSYS=${INITguess:-unknown}
425 c77f65be Eric Allman
426 a848016d Eric Allman
if ! $quiet
427
then
428
    info "System Info: OS=$OS, OSVER=$OSVER, PKGMGR=$PKGMGR, INITSYS=$INITSYS"
429
fi