Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.48 KB)

1 191945e3 Eric Allman
#!/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 89c9f65e Eric Allman
#	(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 191945e3 Eric Allman
#
11
12 b2cae72c Eric Allman
debug=false
13 89c9f65e Eric Allman
install_mariadb=false
14 b2cae72c Eric Allman
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 89c9f65e Eric Allman
		install_mariadb=true
28 b2cae72c Eric Allman
		;;
29
	  --)
30
		shift
31
		break;;
32
	esac
33
	shift
34
done
35
36
set -e
37 191945e3 Eric Allman
cd `dirname $0`/..
38
root=`pwd`
39
. $root/adm/common-support.sh
40
41 b2cae72c Eric Allman
info "Installing Human-Oriented Name to GDPname Directory Service (HONGD)."
42 191945e3 Eric Allman
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 89c9f65e Eric Allman
install_mariadb_packages() {
73
	info "Installing MariaDB packages"
74 191945e3 Eric Allman
	case "$OS" in
75
	   "ubuntu" | "debian" | "raspbian")
76
		sudo apt-get update
77
		sudo apt-get clean
78
		package mariadb-server
79 99a12823 Eric Allman
		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 51da15df Eric Allman
		;;
86 191945e3 Eric Allman
87
	   "darwin")
88
		sudo port selfupdate
89 89c9f65e Eric Allman
		: ${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 191945e3 Eric Allman
		;;
94
95
	   "freebsd")
96
		sudo pkg update
97 89c9f65e Eric Allman
		: ${GDP_MARIADB_VERSION:="102"}
98
		package mariadb${GDP_MARIADB_VERSION}-server
99 191945e3 Eric Allman
		package base64
100
		;;
101
102
	   *)
103
		fatal "%0: unknown OS $OS"
104
		;;
105
	esac
106
}
107
108
109 1fc7fa41 Eric Allman
# needs to be customized for other OSes
110 191945e3 Eric Allman
control_service() {
111
	cmd=$1
112
	svc=$2
113 1fc7fa41 Eric Allman
	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 191945e3 Eric Allman
}
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 d953d8ef Eric Allman
	gdp_user_name="gdp_user"
167 620d2c18 Eric Allman
	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 191945e3 Eric Allman
	hongd_sql=$root/adm/gdp-hongd.sql.template
181 620d2c18 Eric Allman
	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 b2cae72c Eric Allman
	if sed \
186 d953d8ef Eric Allman
		-e "s@CREATION_SERVICE_NAME@$creation_service_name" \
187 620d2c18 Eric Allman
		-e "s@CREATION_SERVICE_PASSWORD@$creation_service_pw" \
188 d953d8ef Eric Allman
		-e "s@GDP_USER_NAME@$gdp_user_name" \
189 620d2c18 Eric Allman
		-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 191945e3 Eric Allman
	then
194 620d2c18 Eric Allman
		action "Copy $creation_service_pw_file to ${GDP_ETC}/$creation_service_pw_file"
195 b2cae72c Eric Allman
		action "  on the system running the log creation service."
196
		action "  It should be owned by gdp:gdp, mode 640."
197 620d2c18 Eric Allman
		info "Save $hongd_admin_pw_file someplace safe (human use only)"
198 b2cae72c Eric Allman
	else
199 191945e3 Eric Allman
		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 ebdc89b3 Eric Allman
$install_mariadb && install_mariadb_packages
211 191945e3 Eric Allman
create_hongd_db
212
213 620d2c18 Eric Allman
action "Please read the following instructions:"
214 191945e3 Eric Allman
215
cat <<- EOF
216 620d2c18 Eric Allman
	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 0c195bbd Eric Allman
	   swarm.gdp.hongdb.host=$fqdn
222 191945e3 Eric Allman
	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."