Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / adm / common-support.sh @ master

History | View | Annotate | Download (10.8 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 a4a0e09a Eric Allman
# Configure various system path/parameter defaults.
214
# All of the paths will be relative to $DESTDIR.
215 a848016d Eric Allman
configure_defaults() {
216
    : ${GDP_ROOT:=/usr}
217
    if [ "$GDP_ROOT" = "/usr" ]
218
    then
219
	    : ${GDP_ETC:=/etc/gdp}
220
    elif [ "$GDP_ROOT" = "/usr/local" -o "$GDP_ROOT" = "/opt/local" ]
221
    then
222
	    : ${GDP_ETC:=$GDP_ROOT/etc/gdp}
223
    else
224
	    : ${GDP_ETC:=$GDP_ROOT/etc}
225
    fi
226 a4a0e09a Eric Allman
    GDP_ROOT=$DESTDIR$GDP_ROOT	# make relative to $DESTDIR
227
    GDP_ETC=$DESTDIR$GDP_ETC	# make relative to $DESTDIR
228 9e62cb26 Eric Allman
    : ${EP_PARAMS:=$GDP_ETC/params}
229 a848016d Eric Allman
    : ${GDP_USER:=gdp}
230
    : ${GDP_GROUP:=$GDP_USER}
231 a4a0e09a Eric Allman
    : ${GDP_LOG_DIR:=$DESTDIR/var/log/gdp}
232
    : ${GDP_VAR:=$DESTDIR/var/swarm/gdp}
233 a848016d Eric Allman
    : ${GDP_KEYS_DIR:=$GDP_ETC/keys}
234 d17c1801 Eric Allman
    : ${GDPLOGD_DATADIR:=$GDP_VAR/glogs}
235 a4a0e09a Eric Allman
    : ${GDPLOGD_DBDIR:=$GDP_VAR/db}
236 a848016d Eric Allman
}
237 3756d4ac Eric Allman
238 c77f65be Eric Allman
239
dot_to_int() {
240
	full=$1
241
	major=`echo $full | sed 's/\..*//'`
242
	full=`echo $full | sed -e 's/[^.]*//' -e 's/^\.//'`
243
	minor=`echo $full | sed 's/\..*//'`
244
	full=`echo $full | sed -e 's/[^.]*//' -e 's/^\.//'`
245
	patch=`echo $full | sed 's/\..*//'`
246
	test -z "$minor" && minor=0
247
	test -z "$patch" && patch=0
248
	printf "%02d%02d%02d" "$major" "$minor" "$patch"
249 3a404465 Eric Allman
}
250 c77f65be Eric Allman
251
###
252
###  figure out operating system and version number
253
###
254 a848016d Eric Allman
OS=""
255
OSVER=""
256 c77f65be Eric Allman
257 4770b39a Eric Allman
fatal_osver() {
258
	fatal "Must be running $1 or later (have $VERSION_ID)"
259
}
260
261
warn_unsupported() {
262
	msg=${1:-"try anyway"}
263
	warn "$OS is not a supported platform, but I'll $msg"
264
}
265
266 a848016d Eric Allman
set_os() {
267
    if [ -f "/etc/os-release" ]; then
268
	. /etc/os-release
269
	OS="${ID-}"
270
	OSVER="${VERSION_ID-}"
271
    fi
272
    if [ -z "$OS" -a -f "/etc/lsb-release" ]; then
273
	. /etc/lsb-release
274
	OS="${DISTRIB_ID-}"
275
	OSVER="${DISTRIB_VERSION-}"
276
    fi
277
    if [ "$OS" ]; then
278
	# it is set --- do nothing
279
	true
280
    elif [ -f "/etc/centos-release" ]; then
281
	OS="centos"
282
	#OSVER=???
283
    elif [ -f "/etc/redhat-release" ]; then
284
	OS="redhat"
285
	OSVER=`sed -e 's/.* release //' -e 's/ .*//' /etc/redhat-release`
286
    else
287
	OS=`uname -s`
288
    fi
289
    OS=`echo $OS | tr '[A-Z]' '[a-z]'`
290
    if [ "$OS" = "linux" ]; then
291
	OS=`head -1 /etc/issue | sed 's/ .*//' | tr '[A-Z]' '[a-z]'`
292
    elif [ "$OS" = "darwin" ]; then
293
	    OSVER=`sw_vers |
294
		    sed -e '/ProductVersion:/!d' -e 's/^.*[ 	][ 	]*//'`
295
    elif [ "$OS" = "freebsd" ]; then
296
	    _major=`uname -r | sed -e 's/\..*//'`
297
	    _minor=`uname -r | sed -e 's/[0-9]*\.//' -e 's/-.*//'`
298
	    OSVER="$_major.$_minor"
299
    fi
300
301
    if [ -z "$OSVER" ]; then
302
	OSVER="0"
303
    else
304
	# clean up OSVER to make it a single integer
305
	OSVER=`dot_to_int $OSVER`
306
    fi
307
}
308
309 ae040ccc Eric Allman
check_os() {
310
    case "$OS" in
311
	"debian")
312 a848016d Eric Allman
	    if expr $OSVER \< 80000 > /dev/null
313
	    then
314
		    fatal_osver "Debian 8 (Jessie)"
315
	    fi
316
	    ;;
317
318 ae040ccc Eric Allman
	"ubuntu")
319 38721d9e Eric Allman
	    if expr $OSVER \< 160400 > /dev/null
320 a848016d Eric Allman
	    then
321 38721d9e Eric Allman
		    fatal_osver "Ubuntu 16.04"
322 a848016d Eric Allman
	    fi
323 ae040ccc Eric Allman
	    ;;
324
325
	"raspbian")
326
	    warn_unsupported "assume it is debian-based"
327
	    ;;
328
329
	"centos" | "freebsd" | "darwin" | "redhat" | "gentoo")
330
	    warn_unsupported
331
	    ;;
332
333
	*)
334
	    fatal "Oops, we don't support $OS"
335
    esac
336
}
337
338
339
# check to make sure we understand this OS and OSVER; choose PKGMGR & INITsys
340
set_pkgmgr() {
341 dd24741f Eric Allman
    : ${PKGMGR:=$OS}
342 ae040ccc Eric Allman
    INITguess=""
343 dd24741f Eric Allman
    case $PKGMGR in
344 57e502fb Eric Allman
      "ubuntu")
345 ae040ccc Eric Allman
	    PKGMGR=debian
346 a848016d Eric Allman
	    if expr $OSVER \>= 160400 > /dev/null
347
	    then
348
		    INITguess=systemd
349
	    else
350
		    INITguess=upstart
351
	    fi
352
	    ;;
353
354 57e502fb Eric Allman
      "debian")
355
	    INITguess=systemd
356
	    ;;
357
358 a848016d Eric Allman
      "raspbian")
359
	    PKGMGR=debian
360
	    ;;
361
362
      "redhat")
363
	    PKGMGR=yum
364
	    if expr $OSVER \< 070000 > /dev/null
365
	    then
366
		    INITguess=systemd
367
	    else
368
		    INITguess=upstart
369
	    fi
370
	    ;;
371
372
      "darwin")
373 dd24741f Eric Allman
	    if type port > /dev/null 2>&1 && port installed | grep -q .; then
374
		    PKGMGR=macports
375
	    fi
376
	    if type brew > /dev/null 2>&1 && [ ! -z "`brew config`" ]
377
	    then
378
		if [ "$PKGMGR" = "macports" ]; then
379 42f4d4a4 Eric Allman
		    PKGMGR=brewports
380 dd24741f Eric Allman
		    warn "You seem to have both macports and homebrew installed."
381
		    warn "They conflict with each other, and you may break all your"
382
		    warn "packages if you try to use them at the same time."
383
		    warn "Please choose one or the other.  Macports seems to work better."
384
		    fatal "Set envar PKGMGR to 'brew' or 'macports' to choose."
385 42f4d4a4 Eric Allman
		else
386
		    PKGMGR=brew
387 a848016d Eric Allman
		fi
388
	    fi
389
	    if [ "$PKGMGR" = "darwin" ]; then
390
		warn "You must install macports or homebrew."
391
		fatal "See README-compiling.md (Operating System Quirks) for details."
392 4770b39a Eric Allman
	    fi
393 a848016d Eric Allman
	    ;;
394
395
      "centos")
396
	    PKGMGR=yum
397
	    ;;
398
    esac
399 4770b39a Eric Allman
400 a848016d Eric Allman
    # determine what init system we are using (heuristic!)
401
    if [ -z "$INITguess" ]
402
    then
403
	    case $OS in
404
	      "debian" | "ubuntu" | "centos" | "redhat" | "gentoo")
405
		    # some linux variant; see if we can figure out systemd
406
		    proc1exe=`sudo stat /proc/1/exe | grep 'File: '`
407
		    if echo "$proc1exe" | grep -q "systemd"
408
		    then
409
			    INITguess="systemd"
410
		    fi
411
		    ;;
412
	    esac
413
    fi
414
}
415 4770b39a Eric Allman
416 c77f65be Eric Allman
417 a848016d Eric Allman
#################### END OF FUNCTIONS ####################
418
419
configure_defaults
420
set_os
421 ae040ccc Eric Allman
check_os
422
if [ -z "$PKGMGR" ]
423
then
424
    set_pkgmgr
425
fi
426 c77f65be Eric Allman
427
# see if we should use our guess
428 ae040ccc Eric Allman
test -z "$INITSYS" && INITSYS=${INITguess:-unknown}
429 c77f65be Eric Allman
430 a848016d Eric Allman
if ! $quiet
431
then
432 a4a0e09a Eric Allman
    info "System Info:  OS=$OS, OSVER=$OSVER, PKGMGR=$PKGMGR, INITSYS=$INITSYS"
433
	info "Install Dirs: DESTDIR=$DESTDIR, GDP_ROOT=$GDP_ROOT, GDP_ETC=$GDP_ETC"
434 a848016d Eric Allman
fi