Page MenuHomeDevCentral

D2202.diff
No OneTemporary

D2202.diff

diff --git a/.arclint b/.arclint
--- a/.arclint
+++ b/.arclint
@@ -22,6 +22,9 @@
},
"python": {
"type": "flake8",
+ "severity": {
+ "F821": "advice"
+ },
"include": [
"(\\.py$)"
]
diff --git a/_modules/nano.py b/_modules/nano.py
new file mode 100644
--- /dev/null
+++ b/_modules/nano.py
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+
+# -------------------------------------------------------------
+# Salt — Nano execution module
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Project: Nasqueron
+# Created: 2020-02-09
+# Description: Allow to generate nano configuration
+# License: BSD-2-Clause
+# -------------------------------------------------------------
+
+
+import subprocess
+
+
+def _get_rc_includes(nanorc_dir):
+ process = subprocess.run(["find", nanorc_dir, "-type", "f"], check=True,
+ stdout=subprocess.PIPE, universal_newlines=True)
+ return ["include " + file for file in process.stdout.split()]
+
+
+def _get_rc_content(nanorc_dir):
+ nano_rc_includes = _get_rc_includes(nanorc_dir)
+
+ return "\n".join(nano_rc_includes) + "\n"
+
+
+def check_rc_up_to_date(name="/etc/nanorc", nanorc_dir="/usr/share/nano"):
+ expected_content = _get_rc_content(nanorc_dir)
+
+ try:
+ fd = open(name)
+ except OSError:
+ return False
+
+ actual_content = "".join(fd.readlines())
+ fd.close()
+
+ return actual_content == expected_content
+
+
+def config_autogenerate(name="/etc/nanorc", nanorc_dir="/usr/share/nano"):
+ nano_rc_content = _get_rc_content(nanorc_dir)
+
+ fd = open(name, "w")
+ fd.write(nano_rc_content)
+ fd.close()
diff --git a/_states/nano.py b/_states/nano.py
new file mode 100644
--- /dev/null
+++ b/_states/nano.py
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+
+# -------------------------------------------------------------
+# Salt — Nano state
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Project: Nasqueron
+# Created: 2020-02-09
+# Description: Allow to generate nano configuration
+# License: BSD-2-Clause
+# -------------------------------------------------------------
+
+
+def config_autogenerated(name="/etc/nanorc", nanorc_dir="/usr/share/nano"):
+ ret = {'name': name,
+ 'result': False,
+ 'changes': {},
+ 'comment': ''}
+
+ if __salt__['nano.check_rc_up_to_date'](name=name, nanorc_dir=nanorc_dir):
+ ret['result'] = True
+ ret['comment'] = '{0} is already up to date'.format(name)
+ return ret
+
+ if __opts__['test']:
+ ret['result'] = None
+ ret['comment'] = 'State nano will write config file {0}'.format(name)
+ return ret
+
+ try:
+ __salt__['nano.config_autogenerate'](name=name, nanorc_dir=nanorc_dir)
+ except Exception as e:
+ ret['comment'] = e
+ return ret
+
+ ret['comment'] = "Configuration written"
+ ret['result'] = True
+ return ret
diff --git a/_tests/data/nanorc_dir/bar.nanorc b/_tests/data/nanorc_dir/bar.nanorc
new file mode 100644
diff --git a/_tests/data/nanorc_dir/foo.nanorc b/_tests/data/nanorc_dir/foo.nanorc
new file mode 100644
diff --git a/_tests/data/nanorc_not_ok b/_tests/data/nanorc_not_ok
new file mode 100644
--- /dev/null
+++ b/_tests/data/nanorc_not_ok
@@ -0,0 +1 @@
+include data/nanorc_dir/bar.nanorc
diff --git a/_tests/data/nanorc_ok b/_tests/data/nanorc_ok
new file mode 100644
--- /dev/null
+++ b/_tests/data/nanorc_ok
@@ -0,0 +1,2 @@
+include data/nanorc_dir/bar.nanorc
+include data/nanorc_dir/foo.nanorc
diff --git a/_tests/modules/test_nano.py b/_tests/modules/test_nano.py
new file mode 100644
--- /dev/null
+++ b/_tests/modules/test_nano.py
@@ -0,0 +1,42 @@
+from importlib.machinery import SourceFileLoader
+import unittest
+
+
+salt_test_case = SourceFileLoader(
+ 'salt_test_case', "salt_test_case.py").load_module()
+nano = SourceFileLoader('nano', "../_modules/nano.py").load_module()
+
+
+DATA_DIR = "data/nanorc_dir"
+EXPECTED_INCLUDES = [
+ 'include data/nanorc_dir/bar.nanorc',
+ 'include data/nanorc_dir/foo.nanorc',
+]
+
+
+class Testinstance(unittest.TestCase, salt_test_case.SaltTestCase):
+
+ def test_get_rc_contents(self):
+ actual_includes = nano._get_rc_content(DATA_DIR)
+ self.assertEqual(EXPECTED_INCLUDES,
+ sorted(actual_includes.strip().split("\n")))
+
+ def test_get_rc_includes(self):
+ self.assertEqual(EXPECTED_INCLUDES,
+ sorted(nano._get_rc_includes(DATA_DIR)))
+
+ def check_rc_up_to_date_when_it_is(self):
+ self.assertTrue(nano.check_rc_up_to_date(name="data/nanorc_ok",
+ nanorc_dir=DATA_DIR))
+
+ def check_rc_up_to_date_when_it_is_not(self):
+ self.assertFalse(nano.check_rc_up_to_date(name="data/nanorc_not_ok",
+ nanorc_dir=DATA_DIR))
+
+ def check_rc_up_to_date_when_it_is_does_not_exist(self):
+ self.assertFalse(nano.check_rc_up_to_date(name="/not/existing",
+ nanorc_dir=DATA_DIR))
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/roles/shellserver/userland-software/base.sls b/roles/shellserver/userland-software/base.sls
--- a/roles/shellserver/userland-software/base.sls
+++ b/roles/shellserver/userland-software/base.sls
@@ -280,3 +280,13 @@
- gcal
- pdftk
- qpdf
+
+# -------------------------------------------------------------
+# Nano configuration
+# -------------------------------------------------------------
+
+{% set nano_rc_files = salt['cmd.shell']('find ' + dirs['share'] + '/nano/ -type f') %}
+
+{{ dirs.etc }}/nanorc:
+ nano.config_autogenerated:
+ - nanorc_dir: {{ dirs.share }}/nano
diff --git a/roles/shellserver/userland-software/files/nanorc b/roles/shellserver/userland-software/files/nanorc
new file mode 100644
--- /dev/null
+++ b/roles/shellserver/userland-software/files/nanorc
@@ -0,0 +1,3 @@
+{%- for include in includes %}
+include {{ include }}
+{% endfor -%}

File Metadata

Mime Type
text/plain
Expires
Thu, Dec 19, 23:18 (18 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2307013
Default Alt Text
D2202.diff (6 KB)

Event Timeline