Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / adm / common-support.sh @ master

History | View | Annotate | Download (10.8 KB)

1
#!/bin/sh
2
{ 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

    
5
#
6
#  These macros are intended to be sourced into other shell files
7
#
8

    
9
#################### MANIFEST CONSTANTS ####################
10

    
11
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
# matches sysexits.h (see man sysexits)
24
EX_OK=0
25
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

    
39
#################### FUNCTIONS ####################
40

    
41
: ${quiet:=false}
42

    
43
# Error/Information messages
44
info() {
45
    if ! $quiet; then
46
	echo "${Gre}${On_Bla}[INFO] $1${Reset}" 2>&1
47
    fi
48
}
49

    
50
warn() {
51
    if ! $quiet; then
52
	echo "${Yel}${On_Bla}[WARN] $1${Reset}" 2>&1
53
    fi
54
}
55

    
56
error() {
57
    if ! $quiet; then
58
	echo "${Red}${On_Bla}[ERROR] $1${Reset}" 2>&1
59
    fi
60
}
61

    
62
fatal() {
63
	echo "${Whi}${On_Red}[FATAL] $1${Reset}" 2>&1
64
	exit ${2:-$EX_UNAVAILABLE}
65
}
66

    
67
action() {
68
	echo "${Bla}${On_Yel}[ACTION REQUIRED] $1${Reset}" 2>&1
69
}
70

    
71
# 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
#
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
# Create a directory as the user gdp
130
mkdir_gdp() {
131
	test -d $1 && return
132
	if [ -e $1 ]; then
133
		warn "$1 exists but is not a directory"
134
		return
135
	fi
136
	info "Creating $1 as ${GDP_USER}:${GDP_GROUP}"
137
	sudo mkdir -p $1
138
	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
	sudo chown ${GDP_USER}:${GDP_GROUP} $1
153
}
154

    
155
package() {
156
    info "Checking package $1..."
157
    case "${PKGMGR:-unknown}" in
158
	"debian")
159
	    if dpkg --get-selections | grep -w --quiet $1; then
160
		info "$1 is already installed. skipping."
161
	    else
162
		sudo apt-get install -y $@
163
	    fi
164
	    ;;
165

    
166
	"yum")
167
	    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

    
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
	    fi
180
	    ;;
181

    
182
	"macports")
183
	    if port -q installed $1 | grep -q "."; then
184
		info "$1 is already installed. skipping."
185
	    else
186
		sudo port install $1
187
	    fi
188
	    ;;
189

    
190
	"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

    
199
	"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

    
207
	*)
208
	    fatal "unrecognized PKGMGR $PKGMGR"
209
	    ;;
210
    esac
211
}
212

    
213
# Configure various system path/parameter defaults.
214
# All of the paths will be relative to $DESTDIR.
215
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
    GDP_ROOT=$DESTDIR$GDP_ROOT	# make relative to $DESTDIR
227
    GDP_ETC=$DESTDIR$GDP_ETC	# make relative to $DESTDIR
228
    : ${EP_PARAMS:=$GDP_ETC/params}
229
    : ${GDP_USER:=gdp}
230
    : ${GDP_GROUP:=$GDP_USER}
231
    : ${GDP_LOG_DIR:=$DESTDIR/var/log/gdp}
232
    : ${GDP_VAR:=$DESTDIR/var/swarm/gdp}
233
    : ${GDP_KEYS_DIR:=$GDP_ETC/keys}
234
    : ${GDPLOGD_DATADIR:=$GDP_VAR/glogs}
235
    : ${GDPLOGD_DBDIR:=$GDP_VAR/db}
236
}
237

    
238

    
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
}
250

    
251
###
252
###  figure out operating system and version number
253
###
254
OS=""
255
OSVER=""
256

    
257
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
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
check_os() {
310
    case "$OS" in
311
	"debian")
312
	    if expr $OSVER \< 80000 > /dev/null
313
	    then
314
		    fatal_osver "Debian 8 (Jessie)"
315
	    fi
316
	    ;;
317

    
318
	"ubuntu")
319
	    if expr $OSVER \< 160400 > /dev/null
320
	    then
321
		    fatal_osver "Ubuntu 16.04"
322
	    fi
323
	    ;;
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
    : ${PKGMGR:=$OS}
342
    INITguess=""
343
    case $PKGMGR in
344
      "ubuntu")
345
	    PKGMGR=debian
346
	    if expr $OSVER \>= 160400 > /dev/null
347
	    then
348
		    INITguess=systemd
349
	    else
350
		    INITguess=upstart
351
	    fi
352
	    ;;
353

    
354
      "debian")
355
	    INITguess=systemd
356
	    ;;
357

    
358
      "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
	    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
		    PKGMGR=brewports
380
		    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
		else
386
		    PKGMGR=brew
387
		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
	    fi
393
	    ;;
394

    
395
      "centos")
396
	    PKGMGR=yum
397
	    ;;
398
    esac
399

    
400
    # 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

    
416

    
417
#################### END OF FUNCTIONS ####################
418

    
419
configure_defaults
420
set_os
421
check_os
422
if [ -z "$PKGMGR" ]
423
then
424
    set_pkgmgr
425
fi
426

    
427
# see if we should use our guess
428
test -z "$INITSYS" && INITSYS=${INITguess:-unknown}
429

    
430
if ! $quiet
431
then
432
    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
fi