Merge ~dmzoneill/+git/vpn-host:dev/crontabs into ~dmzoneill/+git/vpn-host:master

Proposed by David O Neill
Status: Needs review
Proposed branch: ~dmzoneill/+git/vpn-host:dev/crontabs
Merge into: ~dmzoneill/+git/vpn-host:master
Diff against target: 306 lines (+156/-2) (has conflicts)
5 files modified
Makefile (+33/-1)
config.yaml (+11/-0)
requirements.txt (+5/-0)
src/charm.py (+72/-0)
tox.ini (+35/-1)
Conflict in Makefile
Conflict in config.yaml
Conflict in src/charm.py
Conflict in tox.ini
Reviewer Review Type Date Requested Status
David O Neill Pending
Review via email: mp+397662@code.launchpad.net
To post a comment you must log in.

Unmerged commits

d18de48... by David O Neill

Implement the feature to create crontab in /etc/cron.d/xxxx that runs iptables-dave > /x/y/z/$(date)
on given interval.

https://trello.com/b/Fq3KHMtu/bootstack-vpns-sprints-board

02ba69a... by David O Neill

Just some basic code for relation test

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/Makefile b/Makefile
index a3bbf95..6ee8f98 100644
--- a/Makefile
+++ b/Makefile
@@ -26,7 +26,11 @@ help:
2626
27clean:27clean:
28 @echo "Cleaning files"28 @echo "Cleaning files"
29<<<<<<< Makefile
29 @git clean -ffXd -e '!.idea'30 @git clean -ffXd -e '!.idea'
31=======
32 @git clean -ffXd -e '!.idea' -e '!lib/vpncommon'
33>>>>>>> Makefile
30 @echo "Cleaning existing build"34 @echo "Cleaning existing build"
31 @rm -rf ${CHARM_BUILD_DIR}/${CHARM_NAME}35 @rm -rf ${CHARM_BUILD_DIR}/${CHARM_NAME}
3236
@@ -43,6 +47,7 @@ build:
43 @-git rev-parse --abbrev-ref HEAD > ./repo-info47 @-git rev-parse --abbrev-ref HEAD > ./repo-info
44 @-git describe --always > ./version48 @-git describe --always > ./version
45 @mkdir -p ${CHARM_BUILD_DIR}/${CHARM_NAME}49 @mkdir -p ${CHARM_BUILD_DIR}/${CHARM_NAME}
50<<<<<<< Makefile
46 @cp -a ./* ${CHARM_BUILD_DIR}/${CHARM_NAME}51 @cp -a ./* ${CHARM_BUILD_DIR}/${CHARM_NAME}
47 @echo "Installing/updating env if requirements.txt exists"52 @echo "Installing/updating env if requirements.txt exists"
48 @mkdir -p ${CHARM_BUILD_DIR}/${CHARM_NAME}/env/53 @mkdir -p ${CHARM_BUILD_DIR}/${CHARM_NAME}/env/
@@ -51,6 +56,19 @@ build:
51release: clean build56release: clean build
52 @echo "Charm is built at ${CHARM_BUILD_DIR}/${CHARM_NAME}"57 @echo "Charm is built at ${CHARM_BUILD_DIR}/${CHARM_NAME}"
5358
59=======
60 @tox -e build
61 @mv ${CHARM_NAME}.charm ${CHARM_BUILD_DIR}/.
62
63release: clean build unpack
64 @echo "Charm is built at ${CHARM_BUILD_DIR}/${CHARM_NAME}"
65
66unpack: build
67 @mkdir -p ${CHARM_BUILD_DIR}/${CHARM_NAME}
68 @echo "Unpacking built .charm into ${CHARM_BUILD_DIR}/${CHARM_NAME}"
69 @cd ${CHARM_BUILD_DIR}/${CHARM_NAME} && unzip -q ${CHARM_BUILD_DIR}/${CHARM_NAME}.charm
70
71>>>>>>> Makefile
54lint:72lint:
55 @echo "Running lint checks"73 @echo "Running lint checks"
56 @tox -e lint74 @tox -e lint
@@ -59,6 +77,7 @@ black:
59 @echo "Reformat files with black"77 @echo "Reformat files with black"
60 @tox -e black78 @tox -e black
6179
80<<<<<<< Makefile
62proof:81proof:
63 @echo "Running charm proof"82 @echo "Running charm proof"
64 @-charm proof83 @-charm proof
@@ -66,6 +85,15 @@ proof:
66unittests:85unittests:
67 @echo "Running unit tests"86 @echo "Running unit tests"
68 @-tox -e unit87 @-tox -e unit
88=======
89proof: unpack
90 @echo "Running charm proof"
91 @cd ${CHARM_BUILD_DIR}/${CHARM_NAME} && charm proof
92
93unittests:
94 @echo "Running unit tests"
95 @tox -e unit
96>>>>>>> Makefile
6997
70functional: build98functional: build
71 @echo "Executing functional tests in ${CHARM_BUILD_DIR}"99 @echo "Executing functional tests in ${CHARM_BUILD_DIR}"
@@ -75,4 +103,8 @@ test: lint proof unittests functional
75 @echo "Tests completed for charm ${CHARM_NAME}."103 @echo "Tests completed for charm ${CHARM_NAME}."
76104
77# The targets below don't depend on a file105# The targets below don't depend on a file
78.PHONY: help submodules submodules-update clean build release lint black proof unittests functional test
79\ No newline at end of file106\ No newline at end of file
107<<<<<<< Makefile
108.PHONY: help submodules submodules-update clean build release lint black proof unittests functional test
109=======
110.PHONY: help submodules submodules-update clean build release lint black proof unittests functional test
111>>>>>>> Makefile
diff --git a/config.yaml b/config.yaml
index 25f8225..695b8d8 100644
--- a/config.yaml
+++ b/config.yaml
@@ -1,4 +1,5 @@
1options:1options:
2<<<<<<< config.yaml
2 example_string_config:3 example_string_config:
3 type: string4 type: string
4 description: 'An example string configuration item'5 description: 'An example string configuration item'
@@ -11,3 +12,13 @@ options:
11 type: boolean12 type: boolean
12 description: 'An example boolean configuration item'13 description: 'An example boolean configuration item'
13 default: false14 default: false
15=======
16 cron_iptables_save_interval:
17 type: string
18 description: '01 01 * * *\nhttps://help.ubuntu.com/community/CronHowto'
19 default: '01 01 * * *'
20 cron_iptables_save_retention:
21 type: int
22 description: 'Number of days to retain backups'
23 default: 21
24>>>>>>> config.yaml
diff --git a/requirements.txt b/requirements.txt
14new file mode 10064425new file mode 100644
index 0000000..1655283
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,5 @@
1charmhelpers
2jinja2
3ops
4distro
5numpy
0\ No newline at end of file6\ No newline at end of file
diff --git a/src/charm.py b/src/charm.py
index 80c2c12..6dfa4a7 100755
--- a/src/charm.py
+++ b/src/charm.py
@@ -5,6 +5,7 @@
55
6"""Operator Charm main library."""6"""Operator Charm main library."""
7# Load modules from lib directory7# Load modules from lib directory
8<<<<<<< src/charm.py
8import hashlib9import hashlib
9import json10import json
10import logging11import logging
@@ -20,12 +21,29 @@ sys.path.append("lib")
20import iptc # noqa:E40221import iptc # noqa:E402
2122
22import setuppath # noqa:F40123import setuppath # noqa:F401
24=======
25import logging
26import netifaces
27import os
28
29>>>>>>> src/charm.py
23from ops.charm import CharmBase30from ops.charm import CharmBase
24from ops.framework import StoredState31from ops.framework import StoredState
25from ops.main import main32from ops.main import main
26from ops.model import ActiveStatus, MaintenanceStatus33from ops.model import ActiveStatus, MaintenanceStatus
27from ops.framework import Object34from ops.framework import Object
2835
36<<<<<<< src/charm.py
37=======
38from charmhelpers.fetch import apt_install
39from charmhelpers.fetch.python.packages import pip_install
40
41try:
42 import iptc # noqa:E402
43except (ImportError, AttributeError):
44 apt_install(["python3-distutils"])
45 pip_install(["python-iptables"])
46>>>>>>> src/charm.py
2947
30class VPNRequires(Object):48class VPNRequires(Object):
31 def __init__(self, charm, relation_name):49 def __init__(self, charm, relation_name):
@@ -101,8 +119,46 @@ class VpnHostCharm(CharmBase):
101 logging.info("Stopping for configuration, event handle: {}".format(event.handle))119 logging.info("Stopping for configuration, event handle: {}".format(event.handle))
102 # Configure the software120 # Configure the software
103 logging.info("Configuring")121 logging.info("Configuring")
122<<<<<<< src/charm.py
123 self.state.configured = True
124
125=======
126 self.configure_crontabs()
104 self.state.configured = True127 self.state.configured = True
105128
129 def configure_crontabs(self):
130 save_folder = "/etc/vpnhost-backup/iptables"
131
132 if os.path.isdir(save_folder) is False:
133 logging.info(save_folder + " does not exist, attempting to create")
134 try:
135 os.makedirs(save_folder, exist_ok=True)
136 except OSError:
137 logging.info("Creation of the directory %s failed" % save_folder)
138 else:
139 logging.info("Successfully created the directory %s " % save_folder)
140
141 save_cronfile = open("/etc/cron.d/vpn-host-iptables-save", "w")
142 cron_iptables_save_interval = self.model.config["cron_iptables_save_interval"]
143 cronline = cron_iptables_save_interval
144 cronline += " root /sbin/iptables-save > "
145 cronline += save_folder
146 cronline += r"/`date +\%Y\%m\%d\%H\%M\%S`-cron.save"
147 save_cronfile.write(cronline)
148 save_cronfile.close()
149
150 cleanup_cronfile = open("/etc/cron.d/vpn-host-iptables-cleanup", "w")
151 cron_iptables_save_retention = self.model.config["cron_iptables_save_retention"]
152 cronline = "0 0 * * *"
153 cronline += " root /usr/bin/find "
154 cronline += save_folder
155 cronline += r" -type f -name \*.save -mtime +"
156 cronline += str(cron_iptables_save_retention)
157 cronline += r" -exec rm {} \;"
158 cleanup_cronfile.write(cronline)
159 cleanup_cronfile.close()
160
161>>>>>>> src/charm.py
106 def _on_relation_changed(self, event): 162 def _on_relation_changed(self, event):
107 ip = event.relation.data[event.unit].get('ip', None)163 ip = event.relation.data[event.unit].get('ip', None)
108 ports = event.relation.data[event.unit].get('ports', None)164 ports = event.relation.data[event.unit].get('ports', None)
@@ -161,12 +217,28 @@ class VpnHostCharm(CharmBase):
161 start_port += 1217 start_port += 1
162 218
163 # find rules to delete219 # find rules to delete
220<<<<<<< src/charm.py
164 delete_these = np.setdiff1d(current_rules, new_rules)221 delete_these = np.setdiff1d(current_rules, new_rules)
222=======
223 delete_these = set(current_rules)
224 for item in new_rules:
225 if item in delete_these:
226 delete_these.remove(item)
227 # remaining is due for deletion
228>>>>>>> src/charm.py
165 logging.info("delete these:")229 logging.info("delete these:")
166 logging.info(delete_these)230 logging.info(delete_these)
167231
168 # find rules to add232 # find rules to add
233<<<<<<< src/charm.py
169 add_these = np.setdiff1d(new_rules, current_rules)234 add_these = np.setdiff1d(new_rules, current_rules)
235=======
236 add_these = set(new_rules)
237 for item in current_rules:
238 if item in add_these:
239 add_these.remove(item)
240 # remaning new_rules to be added
241>>>>>>> src/charm.py
170 logging.info("add these:")242 logging.info("add these:")
171 logging.info(add_these)243 logging.info(add_these)
172244
diff --git a/tox.ini b/tox.ini
index 49c6e23..13a79b4 100644
--- a/tox.ini
+++ b/tox.ini
@@ -22,6 +22,13 @@ passenv =
22 SNAP_HTTP_PROXY22 SNAP_HTTP_PROXY
23 SNAP_HTTPS_PROXY23 SNAP_HTTPS_PROXY
2424
25<<<<<<< tox.ini
26=======
27[testenv:build]
28deps = charmcraft
29commands = charmcraft build
30
31>>>>>>> tox.ini
25[testenv:lint]32[testenv:lint]
26commands =33commands =
27 flake834 flake8
@@ -41,6 +48,10 @@ exclude =
41 .tox,48 .tox,
42 charmhelpers,49 charmhelpers,
43 mod,50 mod,
51<<<<<<< tox.ini
52=======
53 build,
54>>>>>>> tox.ini
44 .build55 .build
4556
46max-line-length = 8857max-line-length = 88
@@ -48,18 +59,41 @@ max-complexity = 10
4859
49[testenv:black]60[testenv:black]
50commands =61commands =
62<<<<<<< tox.ini
51 black --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod)/" .63 black --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod)/" .
64=======
65 black --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod|vpncommon)/" .
66>>>>>>> tox.ini
52deps =67deps =
53 black68 black
5469
55[testenv:unit]70[testenv:unit]
56commands =71commands =
57 coverage run -m unittest discover -s {toxinidir}/tests/unit -v72 coverage run -m unittest discover -s {toxinidir}/tests/unit -v
73<<<<<<< tox.ini
58 coverage report --omit tests/*,mod/*,.tox/*74 coverage report --omit tests/*,mod/*,.tox/*
59 coverage html --omit tests/*,mod/*,.tox/*75 coverage html --omit tests/*,mod/*,.tox/*
60deps = -r{toxinidir}/tests/unit/requirements.txt76deps = -r{toxinidir}/tests/unit/requirements.txt
77=======
78 coverage report --omit tests/*,mod/*,.tox/*,lib/vpncommon/*
79 coverage html --omit tests/*,mod/*,.tox/*,lib/vpncommon/*
80deps = -r{toxinidir}/tests/unit/requirements.txt
81 -r{toxinidir}/requirements.txt
82>>>>>>> tox.ini
6183
62[testenv:func]84[testenv:func]
63changedir = {toxinidir}/tests/functional85changedir = {toxinidir}/tests/functional
64commands = functest-run-suite {posargs}86commands = functest-run-suite {posargs}
65deps = -r{toxinidir}/tests/functional/requirements.txt
66\ No newline at end of file87\ No newline at end of file
88<<<<<<< tox.ini
89deps = -r{toxinidir}/tests/functional/requirements.txt
90=======
91deps = -r{toxinidir}/tests/functional/requirements.txt
92
93[testenv:func-noop]
94basepython = python3
95commands =
96 functest-run-suite --help
97deps = -r{toxinidir}/tests/functional/requirements.txt
98 -r{toxinidir}/requirements.txt
99 -r{toxinidir}/tests/unit/requirements.txt
100>>>>>>> tox.ini

Subscribers

People subscribed via source and target branches

to all changes: