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
diff --git a/Makefile b/Makefile
0new file mode 1006440new file mode 100644
index 0000000..5dd0577
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,38 @@
1#!/usr/bin/make
2
3CHARM_DIR := $(PWD)
4REACTIVE_DIR := $(CHARM_DIR)/reactive
5CHARM_NAME := influxdb
6PERSONAL_REPO := $(LOGNAME)
7CSDEST := cs:~$(LOGNAME)/$(CHARM_NAME)
8
9all: clean lint
10
11clean:
12 rm -f $(REACTIVE_DIR)/*.pyc
13
14lint:
15 @flake8 $(REACTIVE_DIR)
16
17git:
18 git push $(PERSONAL_REPO)
19
20check-status:
21 @if [ -n "`git status --porcelain`" ]; then \
22 echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'; \
23 echo '!!! There are uncommitted changes !!!'; \
24 echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'; \
25 false; \
26 fi
27 git clean -fdx
28
29build: check-status
30 @charm build
31
32cspush: lint build
33 cd $(JUJU_REPOSITORY)/builds/$(CHARM_NAME) && \
34 version=`charm push . $(CSDEST) | awk '/^url:/ {print $$2}'` && \
35 charm release $$version
36
37upgrade: cspush
38 juju upgrade-charm $(CHARM_NAME)
diff --git a/config.yaml b/config.yaml
index eecbd22..88e99ce 100644
--- a/config.yaml
+++ b/config.yaml
@@ -1,6 +1,6 @@
1options:1options:
2 extra_packages:2 extra_packages:
3 default: influxdb3 default: influxdb jq
4 install_sources:4 install_sources:
5 default: |5 default: |
6 - deb https://repos.influxdata.com/ubuntu xenial stable6 - deb https://repos.influxdata.com/ubuntu xenial stable
@@ -67,3 +67,11 @@ options:
67 description: "Which port to bind to"67 description: "Which port to bind to"
68 type: int68 type: int
69 default: 808669 default: 8086
70 backup_dir:
71 type: string
72 default: ""
73 description: |
74 If set, enable daily backups of the InfluxDB metastore and all
75 databases to the nominated directory. See
76 https://docs.influxdata.com/influxdb/v1.3/administration/backup_and_restore/
77 for more information on InfluxDB backups.
diff --git a/reactive/influxdb.py b/reactive/influxdb.py
index 56e7a22..8895dd0 100644
--- a/reactive/influxdb.py
+++ b/reactive/influxdb.py
@@ -1,8 +1,11 @@
1from charmhelpers.core import hookenv1from charmhelpers.core import hookenv
2from charmhelpers.core.host import service_start, service_stop2from charmhelpers.core.host import service_start, service_stop
3from charmhelpers.core.templating import render3from charmhelpers.core.templating import render
4from charms.reactive import when, when_not4from charms.reactive import hook, set_state, when, when_any, when_not
5from charms.reactive import set_state5
6import os
7
8CRONFILE = '/etc/cron.daily/influxdb-charm-backup'
69
710
8@when('admin.available')11@when('admin.available')
@@ -25,7 +28,7 @@ def install_influx():
25 set_state('influxdb.configured')28 set_state('influxdb.configured')
2629
2730
28@when('config.changed')31@when_any('config.changed.bind_port', 'config.changed.ip_address')
29def config_changed():32def config_changed():
30 config = hookenv.config()33 config = hookenv.config()
31 port = config.get('bind_port')34 port = config.get('bind_port')
@@ -43,6 +46,36 @@ def config_changed():
43 service_start('influxdb')46 service_start('influxdb')
4447
4548
49@when('config.changed.backup_dir')
50def create_backup_job():
51 config = hookenv.config()
52 backup_dir = config.get('backup_dir')
53 if backup_dir:
54 render(source='influxdb-backup',
55 target=CRONFILE,
56 perms=0o755,
57 context={
58 'backup_dir': backup_dir,
59 'backup_retention': 3,
60 })
61 else:
62 old_backup_dir = config.previous('backup_dir')
63 hookenv.log('NOT removing previous backup directory {}'.format(old_backup_dir))
64 try:
65 os.remove(CRONFILE)
66 hookenv.log('Removed {}'.format(CRONFILE))
67 except OSError as e:
68 hookenv.log('Cannot remove {} - ignoring'.format(CRONFILE), hookenv.ERROR)
69
70
71@hook('upgrade-charm')
72def upgrade_charm():
73 # Force config changed on upgrade-charm in case one of the templates changed.
74 set_state('config.changed.backup_dir')
75 set_state('config.changed.bind_port')
76 set_state('config.changed.ip_address')
77
78
46@when('query.api.available')79@when('query.api.available')
47def query_available(query):80def query_available(query):
48 config = hookenv.config()81 config = hookenv.config()
diff --git a/templates/influxdb-backup b/templates/influxdb-backup
49new file mode 10064482new file mode 100644
index 0000000..9c3fdf9
--- /dev/null
+++ b/templates/influxdb-backup
@@ -0,0 +1,31 @@
1#!/bin/bash
2
3# This file managed by juju - do not edit here!
4
5# Back up influxdb databases & metadata to the specified local directory
6
7set -eu
8
9BACKUP_DIR="{{backup_dir}}"
10NUM="{{backup_retention}}"
11TODAY_DIR="$BACKUP_DIR/$(date -I)"
12mkdir -p $TODAY_DIR
13
14DATABASES=$(influx --execute 'show databases' --format=json | jq --raw-output '.results[0].series[0].values[][0]' | grep -ve '^_internal$')
15
16# back up metadata
17influxd backup $TODAY_DIR/metdata
18
19# back up individual databases
20for d in ${DATABASES}; do
21 influxd backup -database $d $TODAY_DIR/$d
22done
23
24# remove old backups - to keep NUM backups we need to delete from directory NUM+1
25(( NUM++ ))
26cd $BACKUP_DIR
27for dir in $(ls -t | tail -n +$NUM); do
28 if [ -d ./$dir ]; then
29 rm -rf ./$dir && echo "Removed $dir"
30 fi
31done
diff --git a/templates/influxdb.conf b/templates/influxdb.conf
index e666bcd..675ce90 100644
--- a/templates/influxdb.conf
+++ b/templates/influxdb.conf
@@ -237,7 +237,7 @@
237 # https-private-key = ""237 # https-private-key = ""
238238
239 # The JWT auth shared secret to validate requests using JSON web tokens.239 # The JWT auth shared secret to validate requests using JSON web tokens.
240 # shared-sercret = ""240 # shared-secret = ""
241241
242 # The default chunk size for result sets that should be chunked.242 # The default chunk size for result sets that should be chunked.
243 # max-row-limit = 0243 # max-row-limit = 0

Subscribers

People subscribed via source and target branches

to all changes: