Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / adm / common-support.sh @ master

History | View | Annotate | Download (10.5 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 defaults
214
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
    : ${EP_PARAMS:=$GDP_ETC/params}
226
    : ${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
    : ${GDPLOGD_DATADIR:=$GDP_VAR/glogs}
232
}
233

    
234

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

    
247
###
248
###  figure out operating system and version number
249
###
250
OS=""
251
OSVER=""
252

    
253
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
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
check_os() {
306
    case "$OS" in
307
	"debian")
308
	    if expr $OSVER \< 80000 > /dev/null
309
	    then
310
		    fatal_osver "Debian 8 (Jessie)"
311
	    fi
312
	    ;;
313

    
314
	"ubuntu")
315
	    if expr $OSVER \< 160400 > /dev/null
316
	    then
317
		    fatal_osver "Ubuntu 16.04"
318
	    fi
319
	    ;;
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
    : ${PKGMGR:=$OS}
338
    INITguess=""
339
    case $PKGMGR in
340
      "ubuntu")
341
	    PKGMGR=debian
342
	    if expr $OSVER \>= 160400 > /dev/null
343
	    then
344
		    INITguess=systemd
345
	    else
346
		    INITguess=upstart
347
	    fi
348
	    ;;
349

    
350
      "debian")
351
	    INITguess=systemd
352
	    ;;
353

    
354
      "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
	    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
		    PKGMGR=brewports
376
		    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
		else
382
		    PKGMGR=brew
383
		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
	    fi
389
	    ;;
390

    
391
      "centos")
392
	    PKGMGR=yum
393
	    ;;
394
    esac
395

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

    
412

    
413
#################### END OF FUNCTIONS ####################
414

    
415
configure_defaults
416
set_os
417
check_os
418
if [ -z "$PKGMGR" ]
419
then
420
    set_pkgmgr
421
fi
422

    
423
# see if we should use our guess
424
test -z "$INITSYS" && INITSYS=${INITguess:-unknown}
425

    
426
if ! $quiet
427
then
428
    info "System Info: OS=$OS, OSVER=$OSVER, PKGMGR=$PKGMGR, INITSYS=$INITSYS"
429
fi