Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / adm / gdp-init-hongds.sh @ master

History | View | Annotate | Download (5.48 KB)

1
#!/bin/sh
2

    
3
#
4
#  Set up Human-Oriented Name to GDPname Directory Service (HONGDS)
5
#
6
#	We're assuming MariaDB here, although MySQL can work.  The issue
7
#	(as of this writing) is about licenses, not functionality.  That
8
#	may (probably will) change in the future, since it appears that
9
#	recent versions of MariaDB have better support for replication.
10
#
11

    
12
debug=false
13
install_mariadb=false
14
args=`getopt Di $*`
15
if [ $? != 0 ]; then
16
	echo "Usage: $0 [-D] [-i]" >&2
17
	exit 64
18
fi
19
eval set -- $args
20
while true
21
do
22
	case "$1" in
23
	  -D)
24
		debug=true
25
		;;
26
	  -i)
27
		install_mariadb=true
28
		;;
29
	  --)
30
		shift
31
		break;;
32
	esac
33
	shift
34
done
35

    
36
set -e
37
cd `dirname $0`/..
38
root=`pwd`
39
. $root/adm/common-support.sh
40

    
41
info "Installing Human-Oriented Name to GDPname Directory Service (HONGD)."
42

    
43
#
44
#  We need the Fully Qualified Domain Name because MariaDB/MySQL uses
45
#  it for authentication.  Unfortunately some systems require several
46
#  steps to set it properly, so often it is left unqualified.  We do
47
#  what we can.
48
#
49
set_fqdn() {
50
	fqdn=`hostname -f`
51
	case "$fqdn" in
52
	    *.*)
53
		# hostname is fully qualified (probably)
54
		return 0
55
		;;
56
	    "")
57
		fatal "Hostname not set --- cannot proceed."
58
		;;
59
	    *)
60
		warn "Cannot find domain name for host $fqdn."
61
		warn "Suggest adjusting /etc/hosts on your system."
62
		return 1
63
		;;
64
	esac
65
}
66

    
67

    
68
#
69
#  Install appropriate packages for MariaDB.  On some systems this can
70
#  require additional operations to make sure the package is current.
71
#
72
install_mariadb_packages() {
73
	info "Installing MariaDB packages"
74
	case "$OS" in
75
	   "ubuntu" | "debian" | "raspbian")
76
		sudo apt-get update
77
		sudo apt-get clean
78
		package mariadb-server
79
		sudo cp /dev/stdin /etc/mysql/conf.d/open-tcp.cnf <<- EOF
80
			[mysqld]
81
			# allow connections from any address
82
			bind-address = 0.0.0.0
83
EOF
84
		sudo chmod 644 /etc/mysql/conf.d/open-tcp.cnf
85
		;;
86

    
87
	   "darwin")
88
		sudo port selfupdate
89
		: ${GDP_MARIADB_VERSION:="10.2"}
90
		package mariadb-${GDP_MARIADB_VERSION}-server
91
		sudo port select mysql mariadb-$GDP_MARIADB_VEFRSION
92
		sudo port load mariadb-${GDP_MARIADB_VERSION}-server
93
		;;
94

    
95
	   "freebsd")
96
		sudo pkg update
97
		: ${GDP_MARIADB_VERSION:="102"}
98
		package mariadb${GDP_MARIADB_VERSION}-server
99
		package base64
100
		;;
101

    
102
	   *)
103
		fatal "%0: unknown OS $OS"
104
		;;
105
	esac
106
}
107

    
108

    
109
# needs to be customized for other OSes
110
control_service() {
111
	cmd=$1
112
	svc=$2
113
	case "$OS" in
114
	  "ubuntu" | "debian" | "raspbian")
115
		sudo -s service $cmd $svc
116
		;;
117
	  *)
118
		fatal "%0: unknown OS $OS"
119
		;;
120
	esac
121
}
122

    
123

    
124
#
125
#  Read a new password.
126
#  Uses specific prompts.
127
#
128
read_new_password() {
129
	local var=$1
130
	local prompt="${2:-new password}"
131
	local passwd
132
	read_passwd passwd "Enter $prompt"
133
	local passwd_compare
134
	read_passwd passwd_compare "Re-enter $prompt"
135
	if [ "$passwd" != "$passwd_compare" ]
136
	then
137
		error "Sorry, passwords must match"
138
		return 1
139
	fi
140
	eval "${var}=\$passwd"
141
	return 0
142
}
143

    
144

    
145
#
146
#  This sets up the Human-GDP name database.  If necessary it will
147
#  try to set up the MariaDB system schema using initialize_mariadb.
148
#  It should be OK to call this even if HONGD database is already
149
#  set up, but it will prompt you for a password that won't be needed.
150
#
151
create_hongd_db() {
152
	info "Creating and populating HONGD database"
153

    
154
	# determine if mariadb or mysql are already up and running
155
	if ps -alx | grep mysqld | grep -vq grep
156
	then
157
		# it looks like a server is running
158
		warn "It appears MySQL or MariaDB is already running; I'll use that."
159
	else
160
		# apparently nothing running
161
		info "Starting up MariaDB/MySQL"
162
		control_service start mysql
163
	fi
164

    
165
	info "Setting up Human-Oriented Name to GDPname Directory database."
166
	gdp_user_name="gdp_user"
167
	gdp_user_pw="gdp_user"
168
	if [ -r "${GDP_ETC}/gdp_user_pw.txt" ]; then
169
		gdp_user_pw=`head -1 "${GDP_ETC}/gdp_user_pw.txt"`
170
	fi
171

    
172
	creation_service_name="gdp_creation_service"
173
	creation_service_pw_file="${GDP_ETC}/creation_service_pw.txt"
174
	set_up_passwd creation_service "$creation_service_pw_file"
175

    
176
	hongd_admin_name="hongd_admin"
177
	hongd_admin_pw_file="${GDP_ETC}/hongd_admin_pw.txt"
178
	set_up_passwd hongd_admin "$hongd_admin_pw_file"
179

    
180
	hongd_sql=$root/adm/gdp-hongd.sql.template
181
	mysql_args="-h localhost"
182
	if [ ! -z "${MYSQL_ROOT_PASSWORD-}" ]; then
183
		mysql_args="$mysql_args -u root -p$MYSQL_ROOT_PASSWORD"
184
	fi
185
	if sed \
186
		-e "s@CREATION_SERVICE_NAME@$creation_service_name" \
187
		-e "s@CREATION_SERVICE_PASSWORD@$creation_service_pw" \
188
		-e "s@GDP_USER_NAME@$gdp_user_name" \
189
		-e "s@GDP_USER_PASSWORD@$gdp_user_pw" \
190
		-e "s@HONGD_ADMIN_NAME@$hongd_admin_name" \
191
		-e "s@HONGD_ADMIN_PASSWORD@$hongd_admin_pw" \
192
		$hongd_sql | sudo mysql $mysql_args
193
	then
194
		action "Copy $creation_service_pw_file to ${GDP_ETC}/$creation_service_pw_file"
195
		action "  on the system running the log creation service."
196
		action "  It should be owned by gdp:gdp, mode 640."
197
		info "Save $hongd_admin_pw_file someplace safe (human use only)"
198
	else
199
		error "Unable to initialize HONGD database."
200
	fi
201
}
202

    
203

    
204
#
205
#  Now is the time to make work actually happen.
206
#
207

    
208
set_fqdn
209
$debug && echo fqdn = $fqdn
210
$install_mariadb && install_mariadb_packages
211
create_hongd_db
212

    
213
action "Please read the following instructions:"
214

    
215
cat <<- EOF
216
	All GDP client hosts that want to use Human-Oriented Names (hint: this
217
	will be almost all of them) need to have a pointer to this service in
218
	their runtime GDP configuration.  This will normally be in
219
	${GDP_ETC}/params/gdp or /usr/local/etc/gdp/params/gdp.  There should
220
	be a line in that file that reads:
221
	   swarm.gdp.hongdb.host=$fqdn
222
	Everything else should be automatic.
223

    
224
	We have plans to improve this in the future.
225
EOF
226
echo ${Reset}
227
info "Thank you for your attention."