aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2019-11-21 20:09:12 +0200
committerOskari Timperi <oskari.timperi@iki.fi>2019-11-21 20:09:12 +0200
commit716ded259615b5517bbed2a8c69b5879dbab4fee (patch)
tree04412324576f2c085fadd4f608a811c53926f98a
parent8935760fb926ed084b14db6327cf4ac633e82d2c (diff)
downloadweather-716ded259615b5517bbed2a8c69b5879dbab4fee.tar.gz
weather-716ded259615b5517bbed2a8c69b5879dbab4fee.zip
Move scripts from config.nims to their own executables
-rw-r--r--config.nims68
-rw-r--r--src/create_database.nim50
-rw-r--r--src/install_cgi_kapsi.nim22
3 files changed, 74 insertions, 66 deletions
diff --git a/config.nims b/config.nims
index e0ea177..e288135 100644
--- a/config.nims
+++ b/config.nims
@@ -1,70 +1,6 @@
-import strformat
-import strutils
-from os import `/`, parentDir
-
task build, "build tools":
mkdir("bin")
exec("nim c -o:bin/update_graphs src/update_graphs.nim")
exec("nim c -o:bin/update_database src/update_database.nim")
-
-task createdb, "create a new database":
- hint("QuitCalled", false)
-
- # This database will store the following data
- # - 1 year worth of samples each 15 minutes
- # - average of each day for 10 years
- # - min of each day for 10 years
- # - max of each day for 10 years
-
- let
- database = thisDir() / "database" / paramStr(2) & ".rrd"
- step = 15 * 60
- heartbeat = 20 * 60
- secondsInDay = 24 * 60 * 60
- samplesInDay = secondsInDay div step
- secondsInYear = 365 * secondsInDay
- samplesInYear = secondsInYear div step
- daysIn10Years = 365 * 10
-
- if fileExists(database):
- echo(&"error: database '{database}' exists")
- quit(1)
-
- echo(&"step = {step} seconds")
- echo(&"heartbeat = {heartbeat} seconds")
- echo(&"samplesInYear = {samplesInYear}")
- echo(&"samplesInDay = {samplesInDay}")
- echo(&"daysIn10Years = {daysIn10Years}")
-
- let args = [
- "create", database,
- "--start", "now",
- "--step", $step,
- &"DS:temperature:GAUGE:{heartbeat}:-40:40",
- &"DS:humidity:GAUGE:{heartbeat}:0:100",
- &"DS:pressure:GAUGE:{heartbeat}:U:U",
- &"DS:battery:GAUGE:{heartbeat}:0:4",
- &"RRA:AVERAGE:0.5:1:{samplesInYear}",
- &"RRA:MIN:0.5:{samplesInDay}:{daysIn10Years}",
- &"RRA:MAX:0.5:{samplesInDay}:{daysIn10Years}",
- &"RRA:AVERAGE:0.5:{samplesInDay}:{daysIn10Years}",
- ]
-
- mkdir(database.parentDir())
-
- echo("rrdtool " & args.join(" "))
- exec("rrdtool " & args.join(" "))
-
-task install_cgi_kapsi, "install cgi script on kapsi.fi":
- proc modifyPath(s: string): string =
- result = s.replace("/home/users", "/var/www/userhome")
- let
- databaseDir = modifyPath(thisDir() / "database")
- updateDatabasePath = modifyPath(thisDir() / "bin" / "update_database")
- scriptPath = paramStr(2)
- script = &"""
-#!{updateDatabasePath}
-databaseDir = "{databaseDir}"
-"""
-
- writeFile(scriptPath, script)
+ exec("nim c -o:bin/install_cgi_kapsi src/install_cgi_kapsi.nim")
+ exec("nim c -o:bin/create_database src/create_database.nim")
diff --git a/src/create_database.nim b/src/create_database.nim
new file mode 100644
index 0000000..f210c6f
--- /dev/null
+++ b/src/create_database.nim
@@ -0,0 +1,50 @@
+import os
+import osproc
+import strformat
+import strutils
+
+# This database will store the following data
+# - 1 year worth of samples each 15 minutes
+# - average of each day for 10 years
+# - min of each day for 10 years
+# - max of each day for 10 years
+
+let
+ database = getAppDir().parentDir() / "database" / paramStr(1) & ".rrd"
+ step = 15 * 60
+ heartbeat = 20 * 60
+ secondsInDay = 24 * 60 * 60
+ samplesInDay = secondsInDay div step
+ secondsInYear = 365 * secondsInDay
+ samplesInYear = secondsInYear div step
+ daysIn10Years = 365 * 10
+
+if fileExists(database):
+ echo(&"error: database '{database}' exists")
+ quit(1)
+
+echo(&"step = {step} seconds")
+echo(&"heartbeat = {heartbeat} seconds")
+echo(&"samplesInYear = {samplesInYear}")
+echo(&"samplesInDay = {samplesInDay}")
+echo(&"daysIn10Years = {daysIn10Years}")
+
+let args = [
+ "create", database,
+ "--start", "now",
+ "--step", $step,
+ &"DS:temperature:GAUGE:{heartbeat}:-40:40",
+ &"DS:humidity:GAUGE:{heartbeat}:0:100",
+ &"DS:pressure:GAUGE:{heartbeat}:U:U",
+ &"DS:battery:GAUGE:{heartbeat}:0:4",
+ &"RRA:AVERAGE:0.5:1:{samplesInYear}",
+ &"RRA:MIN:0.5:{samplesInDay}:{daysIn10Years}",
+ &"RRA:MAX:0.5:{samplesInDay}:{daysIn10Years}",
+ &"RRA:AVERAGE:0.5:{samplesInDay}:{daysIn10Years}",
+]
+
+createDir(database.parentDir())
+
+echo("rrdtool " & args.join(" "))
+
+quit(execCmd("rrdtool " & args.join(" ")))
diff --git a/src/install_cgi_kapsi.nim b/src/install_cgi_kapsi.nim
new file mode 100644
index 0000000..872662a
--- /dev/null
+++ b/src/install_cgi_kapsi.nim
@@ -0,0 +1,22 @@
+## CGI script installation tool for kapsi.fi.
+
+import os
+import strformat
+import strutils
+
+proc modifyPath(s: string): string =
+ result = s.replace("/home/users", "/var/www/userhome")
+
+let
+ updateDatabasePath = modifyPath(getAppDir() / "update_database")
+ scriptPath = paramStr(1)
+ script = &"""
+#!/bin/sh
+{updateDatabasePath}
+"""
+
+writeFile(scriptPath, script)
+
+let fpExec = {fpUserExec, fpGroupExec, fpOthersExec}
+
+setFilePermissions(scriptPath, getFilePermissions(scriptPath) + fpExec)