diff --git a/share/salt/README.md b/share/salt/README.md new file mode 100644 index 0000000..f15d266 --- /dev/null +++ b/share/salt/README.md @@ -0,0 +1,11 @@ +# Opinionated example of deployment via Salt Stack + +## Assumptions: + * user & group srv:srv exist, this is used as a generic service runner + * you want to run the service on port 80, directly exposed to the interwebs (you really want to add a reverse SSL proxy in between) + * You have, or are willing to deploy Salt Stack. + * A bit of assembly is required since you need to move pillar.sls into your saltroot/pillar/ and the rest into saltroot/wttr/ + * You want metric-sm units. Just roll your own wegorc to change this + +## Caveats: + * Doesn't do enough to make a recent master checkout work, i.e. needs further improvement. Latest known working revision is 0d76ba4a3e112694665af6653040807835883b22 diff --git a/share/salt/init.sls b/share/salt/init.sls new file mode 100644 index 0000000..5994262 --- /dev/null +++ b/share/salt/init.sls @@ -0,0 +1,112 @@ +wttr: + service.running: + - enable: True + - watch: + - file: /srv/ephemeral/start.sh + - git: wttr-repo + - require: + - pkg: wttr-dependencies + - git: wttr-repo + - cmd: wego + - archive: geolite-db + +# package names are from Ubuntu 18.04, you may need to adjust if on a different distribution +wttr-dependencies: + pkg.installed: + - pkgs: + - golang + - gawk + - python-setuptools + - python-dev + - python-dnspython + - python-geoip2 + - python-geopy + - python-gevent + - python-flask + - python-pil + - authbind + +wttr-repo: + git.latest: + - name: https://github.com/chubin/wttr.in + - rev: master + - target: /srv/ephemeral/wttr.in + - require: + - /srv/ephemeral + +wttr-start: + file.managed: + - name: /srv/ephemeral/start.sh + - source: salt://wttr/start.sh + - mode: '0770' + - user: srv + - group: srv + +wegorc: + file.managed: + - name: /srv/ephemeral/.wegorc + - user: srv + - group: srv + - source: salt://wttr/wegorc + - template: jinja + - context: + apikey: {{ pillar['wttr']['apikey'] }} + +geolite-db: + archive.extracted: + - name: /srv/ephemeral + - source: http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz + - source_hash: http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz.md5 + - keep_source: True + - options: --strip-components=1 # flatten directory structure + - enforce_toplevel: False + +# Could benefit from improvement, won't get updated automatically at all +wego: + cmd.run: + - onlyif: 'test ! -e /srv/ephemeral/bin/wego' + - env: + - GOPATH: /srv/ephemeral + - name: go get -u github.com/schachmat/wego && go install github.com/schachmat/wego + - cwd: /srv/ephemeral/ + - require: + - pkg: wttr-dependencies + - file: wegorc + +/srv/ephemeral: + file.directory: + - makedirs: True + +{% for dir in '/srv/ephemeral/wttr.in/log','/srv/ephemeral/wttr.in/cache' %} +{{ dir }}: + file.directory: + - user: srv + - group: srv + - makedirs: True + - recurse: + - user + - group + - require_in: + - service: wttr +{% endfor %} + +/etc/systemd/system/wttr.service: + file: + - managed + - source: salt://wttr/wttr.service + - require: + - file: wttr-start + - file: authbind-80 + - require_in: + - service: wttr + +authbind-80: + file: + - managed + - name: /etc/authbind/byport/80 + - user: srv + - group: srv + - mode: 770 + - replace: False + - require: + - pkg: wttr-dependencies diff --git a/share/salt/pillar.sls b/share/salt/pillar.sls new file mode 100644 index 0000000..5b61ecc --- /dev/null +++ b/share/salt/pillar.sls @@ -0,0 +1,2 @@ +wttr: + apikey: insert-api-key-here-and-make-this-pillar-available-to-salt diff --git a/share/salt/start.sh b/share/salt/start.sh new file mode 100755 index 0000000..ef8a298 --- /dev/null +++ b/share/salt/start.sh @@ -0,0 +1,12 @@ +#!/bin/sh +export WEGORC="/srv/ephemeral/.wegorc" +export GOPATH="/srv/ephemeral" + +export WTTR_MYDIR="/srv/ephemeral/wttr.in" +export WTTR_GEOLITE="/srv/ephemeral/GeoLite2-City.mmdb" +export WTTR_WEGO="$GOPATH/bin/wego" + +export WTTR_LISTEN_HOST="0.0.0.0" +export WTTR_LISTEN_PORT="80" + +python $WTTR_MYDIR/bin/srv.py diff --git a/share/salt/wegorc b/share/salt/wegorc new file mode 100644 index 0000000..7f71c57 --- /dev/null +++ b/share/salt/wegorc @@ -0,0 +1,58 @@ +# wego configuration +# +# This config has https://github.com/schachmat/ingo syntax. +# Empty lines or lines starting with # will be ignored. +# All other lines must look like "KEY=VALUE" (without the quotes). +# The VALUE must not be enclosed in quotes as well! + +# aat-frontend: Show geo coordinates (default false) +aat-coords=false + +# aat-frontend: Monochrome output (default false) +aat-monochrome=false + +# BACKEND to be used (default forecast.io) +backend=forecast.io + +# NUMBER of days of weather forecast to be displayed (default 3) +days=3 + +# forecast backend: the api KEY to use (default ) +forecast-api-key={{ apikey }} + +# forecast backend: print raw requests and responses (default false) +forecast-debug=false + +# forecast backend: the LANGUAGE to request from forecast.io (default en) +forecast-lang=en + +# FRONTEND to be used (default ascii-art-table) +frontend=ascii-art-table + +# json frontend: do not indent the output (default false) +jsn-no-indent=false + +# LOCATION to be queried (default 40.748,-73.985) +location=40.748,-73.985 + +# openweathermap backend: the api KEY to use (default ) +owm-api-key= + +# openweathermap backend: print raw requests and responses (default false) +owm-debug=false + +# openweathermap backend: the LANGUAGE to request from openweathermap (default en) +owm-lang=en + +# UNITSYSTEM to use for output. +# Choices are: metric, imperial, si, metric-ms (default metric) +units=metric-ms + +# worldweatheronline backend: the api KEY to use (default ) +wwo-api-key= + +# worldweatheronline backend: print raw requests and responses (default false) +wwo-debug=false + +# worldweatheronline backend: the LANGUAGE to request from worldweatheronline (default en) +wwo-lang=en diff --git a/share/salt/wttr.service b/share/salt/wttr.service new file mode 100644 index 0000000..a912e19 --- /dev/null +++ b/share/salt/wttr.service @@ -0,0 +1,9 @@ +[Unit] +Description=Wttr weather service + +[Service] +ExecStart=/usr/bin/authbind --deep /srv/ephemeral/start.sh +Restart=always + +[Install] +WantedBy=multi-user.target