]> git.openstreetmap.org Git - chef.git/commitdiff
Install grafana as an interface to prometheus
authorTom Hughes <tom@compton.nu>
Tue, 15 Sep 2020 19:54:16 +0000 (19:54 +0000)
committerTom Hughes <tom@compton.nu>
Tue, 15 Sep 2020 20:19:54 +0000 (21:19 +0100)
cookbooks/apt/recipes/default.rb
cookbooks/prometheus/attributes/default.rb
cookbooks/prometheus/metadata.rb
cookbooks/prometheus/recipes/server.rb
cookbooks/prometheus/templates/default/apache.erb [new file with mode: 0644]
cookbooks/prometheus/templates/default/grafana.ini.erb [new file with mode: 0644]
test/data_bags/prometheus/passwords.json [new file with mode: 0644]
test/integration/prometheus-server/serverspec/apache_spec.rb [new file with mode: 0644]
test/integration/prometheus-server/serverspec/grafana_spec.rb [new file with mode: 0644]

index d0a26fae7aa0a755afc6cb8a93353f0855f3180d..06b81b89819329a52755f5667a64b82c03949417 100644 (file)
@@ -174,6 +174,14 @@ apt_repository "docker" do
   key "https://download.docker.com/linux/ubuntu/gpg"
 end
 
+apt_repository "grafana" do
+  action repository_actions["grafana"]
+  uri "https://packages.grafana.com/enterprise/deb"
+  distribution "stable"
+  components ["main"]
+  key "https://packages.grafana.com/gpg.key"
+end
+
 package "unattended-upgrades"
 
 if Dir.exist?("/usr/share/unattended-upgrades")
index bea00dfc6ce9378638fd879d9ab3936812c30e38..56781b4dcb9c72a8657ac253f569cadf3d96a8bf 100644 (file)
@@ -1 +1,5 @@
 default[:prometheus][:exporters] = {}
+
+if node[:recipes].include?("prometheus::server")
+  default[:apt][:sources] |= ["grafana"]
+end
index ee2a797b7c40dd4e01a391ca0bf93d0d1c0915b7..d3167f888ddf8f90dee851f06ae1cd6649bf7cdc 100644 (file)
@@ -6,4 +6,6 @@ description       "Installs and configures prometheus"
 
 version           "1.0.0"
 supports          "ubuntu"
+depends           "apache"
+depends           "apt"
 depends           "networking"
index 297070b338952ac533224b8c483c2d8d2a8c7009..2fcaddf475ed56005da50340578baef0a5a7f439 100644 (file)
 # limitations under the License.
 #
 
+include_recipe "apache"
+include_recipe "apt"
+
+passwords = data_bag_item("prometheus", "passwords")
+
 package "prometheus"
 
 clients = search(:node, "recipes:prometheus\\:\\:default").sort_by(&:name)
@@ -40,3 +45,29 @@ service "prometheus" do
   action [:enable, :start]
   subscribes :reload, "template[/etc/prometheus/prometheus.yml]"
 end
+
+package "grafana-enterprise"
+
+template "/etc/grafana/grafana.ini" do
+  source "grafana.ini.erb"
+  owner "root"
+  group "grafana"
+  mode "640"
+  variables :passwords => passwords
+end
+
+service "grafana-server" do
+  action [:enable, :start]
+end
+
+apache_module "alias"
+apache_module "proxy_http"
+
+ssl_certificate "prometheus.openstreetmap.org" do
+  domains ["prometheus.openstreetmap.org", "prometheus.osm.org"]
+  notifies :reload, "service[apache2]"
+end
+
+apache_site "prometheus.openstreetmap.org" do
+  template "apache.erb"
+end
diff --git a/cookbooks/prometheus/templates/default/apache.erb b/cookbooks/prometheus/templates/default/apache.erb
new file mode 100644 (file)
index 0000000..b4c7c55
--- /dev/null
@@ -0,0 +1,28 @@
+# DO NOT EDIT - This file is being maintained by Prometheus
+
+<VirtualHost *:80>
+       ServerName prometheus.openstreetmap.org
+       ServerAlias prometheus.osm.org
+       ServerAdmin webmaster@openstreetmap.org
+
+       CustomLog /var/log/apache2/prometheus.openstreetmap.org-access.log combined
+       ErrorLog /var/log/apache2/prometheus.openstreetmap.org-error.log
+
+       RedirectPermanent /.well-known/acme-challenge/ http://acme.openstreetmap.org/.well-known/acme-challenge/
+       Redirect permanent / https://prometheus.openstreetmap.org/
+</VirtualHost>
+
+<VirtualHost *:443>
+       ServerName prometheus.openstreetmap.org
+       ServerAdmin webmaster@openstreetmap.org
+
+       CustomLog /var/log/apache2/prometheus.openstreetmap.org-access.log combined
+       ErrorLog /var/log/apache2/prometheus.openstreetmap.org-error.log
+
+       SSLEngine on
+       SSLCertificateFile /etc/ssl/certs/prometheus.openstreetmap.org.pem
+       SSLCertificateKeyFile /etc/ssl/private/prometheus.openstreetmap.org.key
+
+       ProxyPass / http://localhost:3000/
+       ProxyPreserveHost on
+</VirtualHost>
diff --git a/cookbooks/prometheus/templates/default/grafana.ini.erb b/cookbooks/prometheus/templates/default/grafana.ini.erb
new file mode 100644 (file)
index 0000000..bdc1437
--- /dev/null
@@ -0,0 +1,8 @@
+# DO NOT EDIT - This file is being maintained by Chef
+
+[server]
+root_url = https://prometheus.openstreetmap.org/
+
+[security]
+admin_user = admin
+admin_password = <%= @passwords[:grafana_admin] %>
diff --git a/test/data_bags/prometheus/passwords.json b/test/data_bags/prometheus/passwords.json
new file mode 100644 (file)
index 0000000..81a2ce6
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "id": "passwords",
+  "grafana_admin": "grafana_admin"
+}
diff --git a/test/integration/prometheus-server/serverspec/apache_spec.rb b/test/integration/prometheus-server/serverspec/apache_spec.rb
new file mode 100644 (file)
index 0000000..446d3b9
--- /dev/null
@@ -0,0 +1,21 @@
+require "serverspec"
+
+# Required by serverspec
+set :backend, :exec
+
+describe package("apache2") do
+  it { should be_installed }
+end
+
+describe service("apache2") do
+  it { should be_enabled }
+  it { should be_running }
+end
+
+describe port(80) do
+  it { should be_listening.with("tcp") }
+end
+
+describe port(443) do
+  it { should be_listening.with("tcp") }
+end
diff --git a/test/integration/prometheus-server/serverspec/grafana_spec.rb b/test/integration/prometheus-server/serverspec/grafana_spec.rb
new file mode 100644 (file)
index 0000000..48f8945
--- /dev/null
@@ -0,0 +1,17 @@
+require "serverspec"
+
+# Required by serverspec
+set :backend, :exec
+
+describe package("grafana-enterprise") do
+  it { should be_installed }
+end
+
+describe service("grafana-server") do
+  it { should be_enabled }
+  it { should be_running }
+end
+
+describe port(3000) do
+  it { should be_listening.with("tcp6") }
+end