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
1diff --git a/Makefile b/Makefile
2index a3bbf95..6ee8f98 100644
3--- a/Makefile
4+++ b/Makefile
5@@ -26,7 +26,11 @@ help:
6
7 clean:
8 @echo "Cleaning files"
9+<<<<<<< Makefile
10 @git clean -ffXd -e '!.idea'
11+=======
12+ @git clean -ffXd -e '!.idea' -e '!lib/vpncommon'
13+>>>>>>> Makefile
14 @echo "Cleaning existing build"
15 @rm -rf ${CHARM_BUILD_DIR}/${CHARM_NAME}
16
17@@ -43,6 +47,7 @@ build:
18 @-git rev-parse --abbrev-ref HEAD > ./repo-info
19 @-git describe --always > ./version
20 @mkdir -p ${CHARM_BUILD_DIR}/${CHARM_NAME}
21+<<<<<<< Makefile
22 @cp -a ./* ${CHARM_BUILD_DIR}/${CHARM_NAME}
23 @echo "Installing/updating env if requirements.txt exists"
24 @mkdir -p ${CHARM_BUILD_DIR}/${CHARM_NAME}/env/
25@@ -51,6 +56,19 @@ build:
26 release: clean build
27 @echo "Charm is built at ${CHARM_BUILD_DIR}/${CHARM_NAME}"
28
29+=======
30+ @tox -e build
31+ @mv ${CHARM_NAME}.charm ${CHARM_BUILD_DIR}/.
32+
33+release: clean build unpack
34+ @echo "Charm is built at ${CHARM_BUILD_DIR}/${CHARM_NAME}"
35+
36+unpack: build
37+ @mkdir -p ${CHARM_BUILD_DIR}/${CHARM_NAME}
38+ @echo "Unpacking built .charm into ${CHARM_BUILD_DIR}/${CHARM_NAME}"
39+ @cd ${CHARM_BUILD_DIR}/${CHARM_NAME} && unzip -q ${CHARM_BUILD_DIR}/${CHARM_NAME}.charm
40+
41+>>>>>>> Makefile
42 lint:
43 @echo "Running lint checks"
44 @tox -e lint
45@@ -59,6 +77,7 @@ black:
46 @echo "Reformat files with black"
47 @tox -e black
48
49+<<<<<<< Makefile
50 proof:
51 @echo "Running charm proof"
52 @-charm proof
53@@ -66,6 +85,15 @@ proof:
54 unittests:
55 @echo "Running unit tests"
56 @-tox -e unit
57+=======
58+proof: unpack
59+ @echo "Running charm proof"
60+ @cd ${CHARM_BUILD_DIR}/${CHARM_NAME} && charm proof
61+
62+unittests:
63+ @echo "Running unit tests"
64+ @tox -e unit
65+>>>>>>> Makefile
66
67 functional: build
68 @echo "Executing functional tests in ${CHARM_BUILD_DIR}"
69@@ -75,4 +103,8 @@ test: lint proof unittests functional
70 @echo "Tests completed for charm ${CHARM_NAME}."
71
72 # The targets below don't depend on a file
73-.PHONY: help submodules submodules-update clean build release lint black proof unittests functional test
74\ No newline at end of file
75+<<<<<<< Makefile
76+.PHONY: help submodules submodules-update clean build release lint black proof unittests functional test
77+=======
78+.PHONY: help submodules submodules-update clean build release lint black proof unittests functional test
79+>>>>>>> Makefile
80diff --git a/config.yaml b/config.yaml
81index 25f8225..695b8d8 100644
82--- a/config.yaml
83+++ b/config.yaml
84@@ -1,4 +1,5 @@
85 options:
86+<<<<<<< config.yaml
87 example_string_config:
88 type: string
89 description: 'An example string configuration item'
90@@ -11,3 +12,13 @@ options:
91 type: boolean
92 description: 'An example boolean configuration item'
93 default: false
94+=======
95+ cron_iptables_save_interval:
96+ type: string
97+ description: '01 01 * * *\nhttps://help.ubuntu.com/community/CronHowto'
98+ default: '01 01 * * *'
99+ cron_iptables_save_retention:
100+ type: int
101+ description: 'Number of days to retain backups'
102+ default: 21
103+>>>>>>> config.yaml
104diff --git a/requirements.txt b/requirements.txt
105new file mode 100644
106index 0000000..1655283
107--- /dev/null
108+++ b/requirements.txt
109@@ -0,0 +1,5 @@
110+charmhelpers
111+jinja2
112+ops
113+distro
114+numpy
115\ No newline at end of file
116diff --git a/src/charm.py b/src/charm.py
117index 80c2c12..6dfa4a7 100755
118--- a/src/charm.py
119+++ b/src/charm.py
120@@ -5,6 +5,7 @@
121
122 """Operator Charm main library."""
123 # Load modules from lib directory
124+<<<<<<< src/charm.py
125 import hashlib
126 import json
127 import logging
128@@ -20,12 +21,29 @@ sys.path.append("lib")
129 import iptc # noqa:E402
130
131 import setuppath # noqa:F401
132+=======
133+import logging
134+import netifaces
135+import os
136+
137+>>>>>>> src/charm.py
138 from ops.charm import CharmBase
139 from ops.framework import StoredState
140 from ops.main import main
141 from ops.model import ActiveStatus, MaintenanceStatus
142 from ops.framework import Object
143
144+<<<<<<< src/charm.py
145+=======
146+from charmhelpers.fetch import apt_install
147+from charmhelpers.fetch.python.packages import pip_install
148+
149+try:
150+ import iptc # noqa:E402
151+except (ImportError, AttributeError):
152+ apt_install(["python3-distutils"])
153+ pip_install(["python-iptables"])
154+>>>>>>> src/charm.py
155
156 class VPNRequires(Object):
157 def __init__(self, charm, relation_name):
158@@ -101,8 +119,46 @@ class VpnHostCharm(CharmBase):
159 logging.info("Stopping for configuration, event handle: {}".format(event.handle))
160 # Configure the software
161 logging.info("Configuring")
162+<<<<<<< src/charm.py
163+ self.state.configured = True
164+
165+=======
166+ self.configure_crontabs()
167 self.state.configured = True
168
169+ def configure_crontabs(self):
170+ save_folder = "/etc/vpnhost-backup/iptables"
171+
172+ if os.path.isdir(save_folder) is False:
173+ logging.info(save_folder + " does not exist, attempting to create")
174+ try:
175+ os.makedirs(save_folder, exist_ok=True)
176+ except OSError:
177+ logging.info("Creation of the directory %s failed" % save_folder)
178+ else:
179+ logging.info("Successfully created the directory %s " % save_folder)
180+
181+ save_cronfile = open("/etc/cron.d/vpn-host-iptables-save", "w")
182+ cron_iptables_save_interval = self.model.config["cron_iptables_save_interval"]
183+ cronline = cron_iptables_save_interval
184+ cronline += " root /sbin/iptables-save > "
185+ cronline += save_folder
186+ cronline += r"/`date +\%Y\%m\%d\%H\%M\%S`-cron.save"
187+ save_cronfile.write(cronline)
188+ save_cronfile.close()
189+
190+ cleanup_cronfile = open("/etc/cron.d/vpn-host-iptables-cleanup", "w")
191+ cron_iptables_save_retention = self.model.config["cron_iptables_save_retention"]
192+ cronline = "0 0 * * *"
193+ cronline += " root /usr/bin/find "
194+ cronline += save_folder
195+ cronline += r" -type f -name \*.save -mtime +"
196+ cronline += str(cron_iptables_save_retention)
197+ cronline += r" -exec rm {} \;"
198+ cleanup_cronfile.write(cronline)
199+ cleanup_cronfile.close()
200+
201+>>>>>>> src/charm.py
202 def _on_relation_changed(self, event):
203 ip = event.relation.data[event.unit].get('ip', None)
204 ports = event.relation.data[event.unit].get('ports', None)
205@@ -161,12 +217,28 @@ class VpnHostCharm(CharmBase):
206 start_port += 1
207
208 # find rules to delete
209+<<<<<<< src/charm.py
210 delete_these = np.setdiff1d(current_rules, new_rules)
211+=======
212+ delete_these = set(current_rules)
213+ for item in new_rules:
214+ if item in delete_these:
215+ delete_these.remove(item)
216+ # remaining is due for deletion
217+>>>>>>> src/charm.py
218 logging.info("delete these:")
219 logging.info(delete_these)
220
221 # find rules to add
222+<<<<<<< src/charm.py
223 add_these = np.setdiff1d(new_rules, current_rules)
224+=======
225+ add_these = set(new_rules)
226+ for item in current_rules:
227+ if item in add_these:
228+ add_these.remove(item)
229+ # remaning new_rules to be added
230+>>>>>>> src/charm.py
231 logging.info("add these:")
232 logging.info(add_these)
233
234diff --git a/tox.ini b/tox.ini
235index 49c6e23..13a79b4 100644
236--- a/tox.ini
237+++ b/tox.ini
238@@ -22,6 +22,13 @@ passenv =
239 SNAP_HTTP_PROXY
240 SNAP_HTTPS_PROXY
241
242+<<<<<<< tox.ini
243+=======
244+[testenv:build]
245+deps = charmcraft
246+commands = charmcraft build
247+
248+>>>>>>> tox.ini
249 [testenv:lint]
250 commands =
251 flake8
252@@ -41,6 +48,10 @@ exclude =
253 .tox,
254 charmhelpers,
255 mod,
256+<<<<<<< tox.ini
257+=======
258+ build,
259+>>>>>>> tox.ini
260 .build
261
262 max-line-length = 88
263@@ -48,18 +59,41 @@ max-complexity = 10
264
265 [testenv:black]
266 commands =
267+<<<<<<< tox.ini
268 black --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod)/" .
269+=======
270+ black --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod|vpncommon)/" .
271+>>>>>>> tox.ini
272 deps =
273 black
274
275 [testenv:unit]
276 commands =
277 coverage run -m unittest discover -s {toxinidir}/tests/unit -v
278+<<<<<<< tox.ini
279 coverage report --omit tests/*,mod/*,.tox/*
280 coverage html --omit tests/*,mod/*,.tox/*
281 deps = -r{toxinidir}/tests/unit/requirements.txt
282+=======
283+ coverage report --omit tests/*,mod/*,.tox/*,lib/vpncommon/*
284+ coverage html --omit tests/*,mod/*,.tox/*,lib/vpncommon/*
285+deps = -r{toxinidir}/tests/unit/requirements.txt
286+ -r{toxinidir}/requirements.txt
287+>>>>>>> tox.ini
288
289 [testenv:func]
290 changedir = {toxinidir}/tests/functional
291 commands = functest-run-suite {posargs}
292-deps = -r{toxinidir}/tests/functional/requirements.txt
293\ No newline at end of file
294+<<<<<<< tox.ini
295+deps = -r{toxinidir}/tests/functional/requirements.txt
296+=======
297+deps = -r{toxinidir}/tests/functional/requirements.txt
298+
299+[testenv:func-noop]
300+basepython = python3
301+commands =
302+ functest-run-suite --help
303+deps = -r{toxinidir}/tests/functional/requirements.txt
304+ -r{toxinidir}/requirements.txt
305+ -r{toxinidir}/tests/unit/requirements.txt
306+>>>>>>> tox.ini

Subscribers

People subscribed via source and target branches

to all changes: