Files
ubicloud/rhizome/host/bin/setup-grafana
mohi-kalantari 4958f596e2 Add grafana installation strand
We are planning to improve the monitoring collecting the node_exporter
metrics helping us in the kubernetes CSI logic and maintenance. At this
stage we plan to provision a Grafana on a Vm to view the dashboards.

It can be provisioned anywhere as long as it can reach the VictoriaMetrics
server. It can also be provisioned on the same Vm.

In order to keep the strand self contained, it uses certbot for cert
generation and won't provision Ubicloud lbs and use its certs. Most of
the time Grafana is deployed with a single instance and LBs are not needed.

Also the process is not fully automated because it will be run few times and
we don't need full automation. Because of that some actions like the DNS needs
to be done manually.

This strand will try to install grafana on the provided subject id.
The strand won't create the vm or add the DNS records but takes care
of all the remaining logics which must be run inside the vm like
installing the nginx, certbot, getting the certificate and installing
the grafana.

The API looks like this:

vm = Prog::Vm::Nexus.assemble_with_sshable(project_id,
  sshable_unix_user: "ubi", name: "grafana-0", enable_ip4: true).subject

At this step create an A record, resolving your domain to the vm ip.

st = Prog::SetupGrafana.assemble(vm.id, grafana_domain: "grafana.domain.com",
  certificate_owner_email: "youremail@gmail.com")

You can then read the password of the grafana using this command:
vm.sshable.cmd("sudo cat /etc/grafana/grafana.ini | grep admin_password")
2025-06-25 10:59:23 +02:00

66 lines
2.0 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative "../../common/lib/util"
require "securerandom"
domain = ARGV.shift.to_s.strip
cert_email = ARGV.shift.to_s.strip
if domain.empty? || cert_email.empty?
puts "Error: Both domain and cert_email must be provided."
exit 1
end
admin_password = SecureRandom.hex(16)
grafana_ini = "/etc/grafana/grafana.ini"
r "sudo apt update"
r "sudo apt install -y apt-transport-https software-properties-common wget nginx snapd"
r "sudo snap install certbot --classic"
r "sudo mkdir -p /etc/apt/keyrings/"
r "wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null"
r "echo \"deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main\" | sudo tee -a /etc/apt/sources.list.d/grafana.list"
r "sudo apt update"
r "sudo apt install -y grafana"
r "sudo cp #{grafana_ini} #{grafana_ini}.bak"
r "sudo sed -i \
-e 's/;admin_user = admin/admin_user = admin/' \
-e 's/;admin_password = admin/admin_password = #{admin_password}/' \
#{grafana_ini}"
r "sudo mkdir -p /var/www/html"
r %(
sudo tee /etc/nginx/sites-available/grafana.conf > /dev/null <<'EOF'
server {
listen 80;
server_name {{DOMAIN}};
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
EOF
)
r "sudo sed -i 's|{{DOMAIN}}|'#{domain.shellescape}'|' /etc/nginx/sites-available/grafana.conf"
r "sudo ln -s /etc/nginx/sites-available/grafana.conf /etc/nginx/sites-enabled/"
r "sudo systemctl start nginx"
r "sudo certbot --nginx -d #{domain.shellescape} --non-interactive --agree-tos --email #{cert_email.shellescape}"
r "sudo apt install -y ufw"
r "sudo ufw allow 22"
r "sudo ufw allow 80"
r "sudo ufw allow 443"
r "sudo ufw enable"
r "sudo systemctl enable grafana-server"
r "sudo systemctl start grafana-server"