-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup-grafana-prod.sh
executable file
·322 lines (262 loc) · 9 KB
/
setup-grafana-prod.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/bin/bash
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2018 Joyent, Inc.
#
#
# This tool works to set up grafana on an LX zone in a production environment.
# From the global zone, run ./setup-grafana.sh as root. A prometheus zone must
# already exist.
#
# Afterwards, it will print the Grafana URL. It is provisioned with the
# latest dashboards defined in https://github.com/joyent/triton-grafana.
# It takes a few minutes for the discovery process to complete before
# you'll see any metrics.
#
IMAGE_UUID="7b5981c4-1889-11e7-b4c5-3f3bdfc9b88b" # LX Ubuntu 16.04
PACKAGE_UUID="4769a8f9-de51-4c1e-885f-c3920cc68137" # sdc_1024
GRAFANA_VERSION="5.2.2"
ALIAS=grafana0
PORT=443
NODE_VERSION="8.12.0"
function fatal() {
echo "FATAL: $*" >&2
exit 1
}
function usage() {
echo "usage: ./setup-grafana.sh [-s <non-local server uuid>] [-k <path to ssh key>]" >&2
exit 1
}
# Necessary to ensure compatibility for servers with LACP - see HEAD-2247
function get_admin_ip() {
sysinfo | /usr/node/bin/node -e "
function _getAdminIpSysinfo(sysinfo_object, callback) {
var admin_tag = 'admin';
var interfaces = sysinfo_object['Network Interfaces'];
var adminifaces;
if (sysinfo_object['Admin NIC Tag']) {
admin_tag = sysinfo_object['Admin NIC Tag'];
}
adminifaces = Object.keys(interfaces).filter(function (iface) {
return interfaces[iface]['NIC Names'].indexOf(admin_tag) !== -1;
});
if (adminifaces && adminifaces.length !== 0) {
callback(null, interfaces[adminifaces[0]]['ip4addr']);
return;
}
callback(new Error('No admin NIC found with tag ' +
admin_tag + ' in compute node sysinfo'));
}
var chunks = [];
process.stdin.on('data', function (chunk) { chunks.push(chunk) });
process.stdin.on('close', function () {
var sysinfo = JSON.parse(chunks.join(''));
_getAdminIpSysinfo(sysinfo, function (err, adminIp) {
if (err) {
throw err;
} else {
console.log(adminIp);
}
});
});
"
}
server_uuid=$(sysinfo | json UUID) # local headnode by default
ssh_key_file=
ssh_key=
while getopts ":s:k:" f; do
case $f in
s) server_uuid=$OPTARG
;;
k) ssh_key_file=$OPTARG
;;
\?) usage
;;
esac
done
if [[ $# -gt 4 ]]; then
usage
fi
set -o errexit
set -o pipefail
if [[ -n "${TRACE}" ]]; then
set -o xtrace
fi
if [[ -z ${SSH_OPTS} ]]; then
SSH_OPTS=""
fi
. ~/.bash_profile
# Check that key exists if we passed the flag; then read the key's contents
if [[ -n "${ssh_key_file}" ]]; then
[[ -f "${ssh_key_file}" ]] || fatal "ssh key not found at ${ssh_key_file}"
ssh_key=$(<"${ssh_key_file}")
fi
#
# grafana0 zone creation
#
admin_uuid=$(sdc-useradm get admin | json uuid)
[[ -n $(sdc-server lookup uuid=${server_uuid}) ]] || fatal "Invalid server UUID"
vm_uuid=$(sdc-vmadm list alias=$ALIAS owner_uuid=${admin_uuid} -H -o uuid)
[[ -z "$vm_uuid" ]] || fatal "VM $ALIAS already exists"
if ! sdc-imgadm get ${IMAGE_UUID} >/dev/null 2>&1; then
sdc-imgadm import -S https://images.joyent.com ${IMAGE_UUID} </dev/null
fi
external_network_uuid=$(sdc-napi /networks?name=external | json -H 0.uuid)
admin_network_uuid=$(sdc-napi /networks?name=admin | json -H 0.uuid)
# Find package
[[ -n $(sdc-papi /packages | json -Ha uuid | grep $PACKAGE_UUID) ]] || fatal "missing package"
prometheus_ip=$(sdc-vmadm list alias=prometheus0 owner_uuid=${admin_uuid} -j \
| json 0.nics | json -c 'this.nic_tag === "admin"' 0.ip)
[[ -n "$prometheus_ip" ]] \
|| fatal "could not find prometheus0 zone admin IP: have you setup a prometheus0 zone?"
echo "Admin account: ${admin_uuid}"
echo "Admin network: ${admin_network_uuid}"
echo "Server: ${server_uuid}"
echo "Alias: ${ALIAS}"
[[ -n "${admin_uuid}" ]] || fatal "missing admin UUID"
[[ -n "${admin_network_uuid}" ]] || fatal "missing admin network UUID"
# - networks: Need the 'admin' to access the prometheus0 zone. Need 'external'
# so, in general, an operator can reach it.
# - tags.smartdc_role: So 'sdc-login -l graf' works.
echo "Creating VM ${ALIAS} ..."
vm_uuid=$((sdc-vmapi /vms?sync=true -X POST -d@/dev/stdin | json -H vm_uuid) <<PAYLOAD
{
"alias": "${ALIAS}",
"billing_id": "${PACKAGE_UUID}",
"brand": "lx",
"image_uuid": "${IMAGE_UUID}",
"networks": [{"uuid": "${admin_network_uuid}"}, {"uuid": "${external_network_uuid}", "primary": true}],
"owner_uuid": "${admin_uuid}",
"server_uuid": "${server_uuid}",
"tags": {
"smartdc_role": "grafana"
}
}
PAYLOAD
)
#
# Grafana setup.
#
grafana_ip=$(sdc-vmadm get ${vm_uuid} | json nics.1.ip)
server_ip=$(get_admin_ip)
# Download dashboards, grafana, node into the zone
ssh ${SSH_OPTS} ${server_ip} <<SERVER
cd /zones/${vm_uuid}/root/root
# Download dashboards, grafana, node
curl -Lk -o triton-grafana-master.tgz https://github.com/joyent/triton-grafana/archive/master.tar.gz
tar -zxvf triton-grafana-master.tgz
mv triton-grafana-master triton-grafana
curl -L -kO https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-${GRAFANA_VERSION}.linux-amd64.tar.gz
tar -zxvf grafana-${GRAFANA_VERSION}.linux-amd64.tar.gz
ln -s grafana-${GRAFANA_VERSION} grafana
curl -L -kO https://nodejs.org/download/release/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz
tar -zxvf node-v${NODE_VERSION}-linux-x64.tar.gz
ln -s node-v${NODE_VERSION}-linux-x64 node
ln -s /root/node/bin/node /zones/${vm_uuid}/root/usr/bin/node
cd grafana
# Generate/Register Cert/Key
openssl req -x509 -nodes -subj "/CN=${grafana_ip}" -newkey rsa:2048 \
-keyout grafana_key.pem -out grafana_cert.pem -days 365
cat >./conf/custom.ini <<CONFIGINI
# config file version
apiVersion: 1
[server]
http_port=${PORT}
protocol=https
cert_file=/root/grafana/grafana_cert.pem
cert_key=/root/grafana/grafana_key.pem
CONFIGINI
cat >./conf/provisioning/datasources/triton.yaml <<DATAYML
# config file version
apiVersion: 1
datasources:
- name: Triton
type: prometheus
access: proxy
orgId: 1
url: http://${prometheus_ip}:9090
isDefault: true
editable: true
DATAYML
cat >./conf/provisioning/dashboards/triton.yaml <<DASHYML
# config file version
apiVersion: 1
providers:
- name: Triton
orgId: 1
folder: ''
type: file
options:
path: /root/triton-grafana/dashboards
DASHYML
cat > ./redir.js <<REDIRJS
#!/usr/bin/env node
/*
*
* Copyright 2018 Joyent Inc.
*
* http redirection for grafana.
*
*/
var http = require('http');
if (process.argv.length != 2) {
console.error("Usage: node redir.js")
process.exit(1)
}
http.createServer(function(req, res) {
res.statusCode = 301;
res.setHeader("Location", "https://" + req.headers.host + req.url);
res.end();
}).listen(80);
REDIRJS
chmod 700 ./redir.js
cat > ./wrapper.sh <<WRAPPER
#!/bin/bash
/root/grafana/redir.js &
/root/grafana/bin/grafana-server
WRAPPER
chmod 700 ./wrapper.sh
# Generate grafana systemd manifest
cat > /zones/${vm_uuid}/root/etc/systemd/system/grafana.service <<SYSTEMD
[Unit]
Description=Grafana server
After=network.target
[Service]
WorkingDirectory=/root/grafana
StandardOutput=syslog
ExecStart=/root/grafana/wrapper.sh
User=root
[Install]
WantedBy=multi-user.target
SYSTEMD
zlogin ${vm_uuid} "systemctl daemon-reload && systemctl enable grafana && systemctl start grafana && systemctl status grafana" < /dev/null
# Unrefined method to allow grafana to start and provision the dashboards.
# Would be better to poll the curl attempts below.
sleep 5
# Set the CNAPI dashboard (for now) as the default org dashboard.
cert="/zones/${vm_uuid}/root/root/grafana/grafana_cert.pem"
curl -sSf --cacert \${cert} -u admin:admin \
"https://${grafana_ip}:${PORT}/api/search?type=dash-db&query=cnapi"
dashId=\$(curl -sSf --cacert \${cert} -u admin:admin \
"https://${grafana_ip}:${PORT}/api/search?type=dash-db&query=cnapi" | json 0.id)
curl -sSf --cacert \${cert} -u admin:admin \
"https://${grafana_ip}:${PORT}/api/org/preferences" -H content-type:application/json \
-d '{"theme":"","homeDashboardId":'\$dashId',"timezone":"utc"}' -X PUT
# Change the default password
pw=\$(openssl rand -base64 32 | tr -d "=+/")
echo \$pw > /zones/${vm_uuid}/root/root/grafana/password.txt
curl -sSf --cacert \${cert} -u admin:admin \
"https://${grafana_ip}:${PORT}/api/user/password" -H content-type:application/json \
-d '{"oldPassword":"admin","newPassword":'\"\${pw}\"',"confirmNew":'\"\${pw}\"'}' -X PUT
# Add ssh key, if specified
if [[ -n "${ssh_key_file}" ]]; then
echo "${ssh_key}" >> /zones/${vm_uuid}/root/root/.ssh/authorized_keys
fi
SERVER
echo ""
echo "* * * Successfully setup * * *"
echo "Prometheus: http://${prometheus_ip}:9090/"
echo "Grafana: https://${grafana_ip} (username = admin; password in ${ALIAS} zone in /root/grafana/password.txt)"