Authentication-Results: mail-b.sr.ht; dkim=pass header.d=secluded.site header.i=@secluded.site Received: from mx.nixnet.email (mx.nixnet.email [94.16.121.167]) by mail-b.sr.ht (Postfix) with ESMTPS id 36AC511F08C for <~whereswaldon/arbor-dev@lists.sr.ht>; Thu, 21 Jul 2022 03:10:38 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by mx.nixnet.email (Postfix) with ESMTPSA id B21F8202868; Wed, 20 Jul 2022 23:10:33 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secluded.site; s=202002021149; t=1658373034; h=from:from:reply-to:subject:subject:to:to:cc:cc; bh=a42W8yuFhCv2j7bOnJjkLqKc2jRvESHL5I6IPNFH5zQ=; b=N9EUFlZdpR0/X0gu6dQdsNDsYJPxXYiPlcaBIdg0p+LG9tc2kPnwgPbPD/KGTyHX+TcQ3J ZO+xDs2Wkvarr4ze1JgO6/n0/oKvHrMy1cPU6yjcjYRSZykPfsTw5fq8o4fm7tlNdcs/l1 NZ2CPHSqKbt3kPBoYLEgPyaLXsWnYpU= From: Amolith To: ~whereswaldon/arbor-dev@lists.sr.ht Cc: Amolith Subject: [PATCH 04/12] Move setup source, add templating & more configs Date: Wed, 20 Jul 2022 23:09:53 -0400 Message-Id: <20220721031001.97027-5-amolith@secluded.site> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220721031001.97027-1-amolith@secluded.site> References: <20220721031001.97027-1-amolith@secluded.site> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Signed-off-by: Amolith --- deploy/setup.go | 12 ---------- files/Caddyfile | 3 +++ files/arbor-relay.service | 11 +++++++++ files/prometheus.yml | 47 +++++++++++++++++++++++++++++++++++++++ setup.go | 39 ++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 12 deletions(-) delete mode 100644 deploy/setup.go create mode 100644 files/Caddyfile create mode 100644 files/arbor-relay.service create mode 100644 files/prometheus.yml diff --git a/deploy/setup.go b/deploy/setup.go deleted file mode 100644 index bbb2af9..0000000 --- a/deploy/setup.go +++ /dev/null @@ -1,12 +0,0 @@ -//+build mage - -package main - -import ( - "github.com/magefile/mage/sh" -) - -// says hoi >:3 -func Build() error { - return sh.Run("/bin/echo", "hoooooooooi!") -} diff --git a/files/Caddyfile b/files/Caddyfile new file mode 100644 index 0000000..7b790f9 --- /dev/null +++ b/files/Caddyfile @@ -0,0 +1,3 @@ +{{ .Hostname }} { + reverse_proxy localhost:9090 +} diff --git a/files/arbor-relay.service b/files/arbor-relay.service new file mode 100644 index 0000000..49e281b --- /dev/null +++ b/files/arbor-relay.service @@ -0,0 +1,11 @@ +[Unit] +Description=Arbor Chat Relay + +[Service] +User=arbor-relay +Group=arbor-relay +ExecStart=/usr/local/bin/relay --grovepath /usr/local/share/arbor-relay --tls-ip 0.0.0.0 --tls-port 7117 --keypath /var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/{{ .Hostname }}/{{ .Hostname }}.key --certpath /var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/{{ .Hostname }}/{{ .Hostname }}.crt arbor.chat:7117 +Restart=Always + +[Install] +WantedBy=multi-user.target diff --git a/files/prometheus.yml b/files/prometheus.yml new file mode 100644 index 0000000..2e011d6 --- /dev/null +++ b/files/prometheus.yml @@ -0,0 +1,47 @@ +global: + scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. + evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. + # scrape_timeout is set to the global default (10s). + + # Attach these labels to any time series or alerts when communicating with + # external systems (federation, remote storage, Alertmanager). + external_labels: + monitor: '{{ .Hostname }}' + +# Alertmanager configuration +alerting: + alertmanagers: + - static_configs: + - targets: ['localhost:9093'] + +# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. +rule_files: + # - "first_rules.yml" + # - "second_rules.yml" + +# A scrape configuration containing exactly one endpoint to scrape: +# Here it's Prometheus itself. +scrape_configs: + # The job name is added as a label `job=` to any timeseries scraped from this config. + - job_name: 'prometheus' + + # Override the global default and scrape targets from this job every 5 seconds. + scrape_interval: 5s + scrape_timeout: 5s + + # metrics_path defaults to '/metrics' + # scheme defaults to 'http'. + + static_configs: + - targets: ['localhost:9090'] + + - job_name: node + # If prometheus-node-exporter is installed, grab stats about the local + # machine by default. + static_configs: + - targets: ['localhost:9100'] + + - job_name: arbor-relay + static_configs: + - targets: ['localhost:2112'] + diff --git a/setup.go b/setup.go index de63f25..5fcfb18 100644 --- a/setup.go +++ b/setup.go @@ -5,6 +5,10 @@ package main import ( "embed" "fmt" + "log" + "os" + "strings" + "text/template" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" @@ -113,3 +117,38 @@ func Firewall() error { return sh.Run("ufw", "enable") } + +// config() takes source template and destination file arguments and executes +// the template replacing `hostname` with the system's hostname. +func config(rfile string, wfile string) error { + log.Println("Generating config from", rfile, "and writing to", wfile) + + // Create a new template using the rfile (read file) + data, err := files.ReadFile(rfile) + tmpl := template.Must(template.New("temp").Parse(string(data))) + + hostname, err := os.ReadFile("/etc/hostname") + if err != nil { + return err + } + + // Open wfile (write file) for writing generated config + file, err := os.OpenFile(wfile, os.O_CREATE|os.O_RDWR, 0660) + if err != nil { + return err + } + + // Compose config file using the template, substituting Hostname with the + // contents of /etc/hostname and trimming all whitespace + err = tmpl.Execute(file, map[string]string{"Hostname": strings.TrimSpace(string(hostname))}) + if err != nil { + return err + } + + err = file.Close() + if err != nil { + return err + } + + return nil +} -- 2.37.1