Merge ~paulgear/influxdb-charm/+git/influxdb-charm:master into influxdb-charm:master

Proposed by Paul Gear
Status: Merged
Merged at revision: c98af2696e352c77a6cfd268a9f2073efcb5bb0e
Proposed branch: ~paulgear/influxdb-charm/+git/influxdb-charm:master
Merge into: influxdb-charm:master
Diff against target: 182 lines (+115/-5)
5 files modified
Makefile (+38/-0)
config.yaml (+9/-1)
reactive/influxdb.py (+36/-3)
templates/influxdb-backup (+31/-0)
templates/influxdb.conf (+1/-1)
Reviewer Review Type Date Requested Status
Barry Price Approve
Review via email: mp+332218@code.launchpad.net

Description of the change

Provides an MVP backup facility for influxdb per RT#106301; fixes lp:1719891

To post a comment you must log in.
Revision history for this message
Barry Price (barryprice) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/Makefile b/Makefile
2new file mode 100644
3index 0000000..5dd0577
4--- /dev/null
5+++ b/Makefile
6@@ -0,0 +1,38 @@
7+#!/usr/bin/make
8+
9+CHARM_DIR := $(PWD)
10+REACTIVE_DIR := $(CHARM_DIR)/reactive
11+CHARM_NAME := influxdb
12+PERSONAL_REPO := $(LOGNAME)
13+CSDEST := cs:~$(LOGNAME)/$(CHARM_NAME)
14+
15+all: clean lint
16+
17+clean:
18+ rm -f $(REACTIVE_DIR)/*.pyc
19+
20+lint:
21+ @flake8 $(REACTIVE_DIR)
22+
23+git:
24+ git push $(PERSONAL_REPO)
25+
26+check-status:
27+ @if [ -n "`git status --porcelain`" ]; then \
28+ echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'; \
29+ echo '!!! There are uncommitted changes !!!'; \
30+ echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'; \
31+ false; \
32+ fi
33+ git clean -fdx
34+
35+build: check-status
36+ @charm build
37+
38+cspush: lint build
39+ cd $(JUJU_REPOSITORY)/builds/$(CHARM_NAME) && \
40+ version=`charm push . $(CSDEST) | awk '/^url:/ {print $$2}'` && \
41+ charm release $$version
42+
43+upgrade: cspush
44+ juju upgrade-charm $(CHARM_NAME)
45diff --git a/config.yaml b/config.yaml
46index eecbd22..88e99ce 100644
47--- a/config.yaml
48+++ b/config.yaml
49@@ -1,6 +1,6 @@
50 options:
51 extra_packages:
52- default: influxdb
53+ default: influxdb jq
54 install_sources:
55 default: |
56 - deb https://repos.influxdata.com/ubuntu xenial stable
57@@ -67,3 +67,11 @@ options:
58 description: "Which port to bind to"
59 type: int
60 default: 8086
61+ backup_dir:
62+ type: string
63+ default: ""
64+ description: |
65+ If set, enable daily backups of the InfluxDB metastore and all
66+ databases to the nominated directory. See
67+ https://docs.influxdata.com/influxdb/v1.3/administration/backup_and_restore/
68+ for more information on InfluxDB backups.
69diff --git a/reactive/influxdb.py b/reactive/influxdb.py
70index 56e7a22..8895dd0 100644
71--- a/reactive/influxdb.py
72+++ b/reactive/influxdb.py
73@@ -1,8 +1,11 @@
74 from charmhelpers.core import hookenv
75 from charmhelpers.core.host import service_start, service_stop
76 from charmhelpers.core.templating import render
77-from charms.reactive import when, when_not
78-from charms.reactive import set_state
79+from charms.reactive import hook, set_state, when, when_any, when_not
80+
81+import os
82+
83+CRONFILE = '/etc/cron.daily/influxdb-charm-backup'
84
85
86 @when('admin.available')
87@@ -25,7 +28,7 @@ def install_influx():
88 set_state('influxdb.configured')
89
90
91-@when('config.changed')
92+@when_any('config.changed.bind_port', 'config.changed.ip_address')
93 def config_changed():
94 config = hookenv.config()
95 port = config.get('bind_port')
96@@ -43,6 +46,36 @@ def config_changed():
97 service_start('influxdb')
98
99
100+@when('config.changed.backup_dir')
101+def create_backup_job():
102+ config = hookenv.config()
103+ backup_dir = config.get('backup_dir')
104+ if backup_dir:
105+ render(source='influxdb-backup',
106+ target=CRONFILE,
107+ perms=0o755,
108+ context={
109+ 'backup_dir': backup_dir,
110+ 'backup_retention': 3,
111+ })
112+ else:
113+ old_backup_dir = config.previous('backup_dir')
114+ hookenv.log('NOT removing previous backup directory {}'.format(old_backup_dir))
115+ try:
116+ os.remove(CRONFILE)
117+ hookenv.log('Removed {}'.format(CRONFILE))
118+ except OSError as e:
119+ hookenv.log('Cannot remove {} - ignoring'.format(CRONFILE), hookenv.ERROR)
120+
121+
122+@hook('upgrade-charm')
123+def upgrade_charm():
124+ # Force config changed on upgrade-charm in case one of the templates changed.
125+ set_state('config.changed.backup_dir')
126+ set_state('config.changed.bind_port')
127+ set_state('config.changed.ip_address')
128+
129+
130 @when('query.api.available')
131 def query_available(query):
132 config = hookenv.config()
133diff --git a/templates/influxdb-backup b/templates/influxdb-backup
134new file mode 100644
135index 0000000..9c3fdf9
136--- /dev/null
137+++ b/templates/influxdb-backup
138@@ -0,0 +1,31 @@
139+#!/bin/bash
140+
141+# This file managed by juju - do not edit here!
142+
143+# Back up influxdb databases & metadata to the specified local directory
144+
145+set -eu
146+
147+BACKUP_DIR="{{backup_dir}}"
148+NUM="{{backup_retention}}"
149+TODAY_DIR="$BACKUP_DIR/$(date -I)"
150+mkdir -p $TODAY_DIR
151+
152+DATABASES=$(influx --execute 'show databases' --format=json | jq --raw-output '.results[0].series[0].values[][0]' | grep -ve '^_internal$')
153+
154+# back up metadata
155+influxd backup $TODAY_DIR/metdata
156+
157+# back up individual databases
158+for d in ${DATABASES}; do
159+ influxd backup -database $d $TODAY_DIR/$d
160+done
161+
162+# remove old backups - to keep NUM backups we need to delete from directory NUM+1
163+(( NUM++ ))
164+cd $BACKUP_DIR
165+for dir in $(ls -t | tail -n +$NUM); do
166+ if [ -d ./$dir ]; then
167+ rm -rf ./$dir && echo "Removed $dir"
168+ fi
169+done
170diff --git a/templates/influxdb.conf b/templates/influxdb.conf
171index e666bcd..675ce90 100644
172--- a/templates/influxdb.conf
173+++ b/templates/influxdb.conf
174@@ -237,7 +237,7 @@
175 # https-private-key = ""
176
177 # The JWT auth shared secret to validate requests using JSON web tokens.
178- # shared-sercret = ""
179+ # shared-secret = ""
180
181 # The default chunk size for result sets that should be chunked.
182 # max-row-limit = 0

Subscribers

People subscribed via source and target branches

to all changes: