From a9ae696fdb1e882a9be767574204bb845023f515 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 6 Jan 2022 03:57:19 +0100 Subject: basic openwrt deploy support --- chaos-at-home/openwrt-deploy.yml | 9 ++++ common/openwrt-deploy.yml | 17 +++++++ dan/openwrt-deploy.yml | 9 ++++ inventory/hosts.ini | 21 ++++++++ openwrt-deploy.sh | 1 + .../deploy/action_plugins/openwrt_sysupgrade.py | 57 ++++++++++++++++++++++ roles/openwrt/deploy/tasks/main.yml | 5 ++ spreadspace/openwrt-deploy.yml | 9 ++++ 8 files changed, 128 insertions(+) create mode 100644 chaos-at-home/openwrt-deploy.yml create mode 100644 common/openwrt-deploy.yml create mode 100644 dan/openwrt-deploy.yml create mode 120000 openwrt-deploy.sh create mode 100644 roles/openwrt/deploy/action_plugins/openwrt_sysupgrade.py create mode 100644 roles/openwrt/deploy/tasks/main.yml create mode 100644 spreadspace/openwrt-deploy.yml diff --git a/chaos-at-home/openwrt-deploy.yml b/chaos-at-home/openwrt-deploy.yml new file mode 100644 index 00000000..2832d6ed --- /dev/null +++ b/chaos-at-home/openwrt-deploy.yml @@ -0,0 +1,9 @@ +--- +- name: generate os image + hosts: "{{ install_hostname }}" + connection: local + gather_facts: no + roles: + - role: openwrt/image + +- import_playbook: ../common/openwrt-deploy.yml diff --git a/common/openwrt-deploy.yml b/common/openwrt-deploy.yml new file mode 100644 index 00000000..eacb243c --- /dev/null +++ b/common/openwrt-deploy.yml @@ -0,0 +1,17 @@ +--- +- name: preparations and sanity checks + hosts: "{{ install_hostname }}" + connection: local + gather_facts: no + tasks: + - name: check if there is only one output image + fail: + msg: "the output_images variable must only contain a single image" + when: + - (output_images | length) != 1 + +- name: deploy openwrt image + hosts: "{{ install_hostname }}" + gather_facts: no + roles: + - role: openwrt/deploy diff --git a/dan/openwrt-deploy.yml b/dan/openwrt-deploy.yml new file mode 100644 index 00000000..2832d6ed --- /dev/null +++ b/dan/openwrt-deploy.yml @@ -0,0 +1,9 @@ +--- +- name: generate os image + hosts: "{{ install_hostname }}" + connection: local + gather_facts: no + roles: + - role: openwrt/image + +- import_playbook: ../common/openwrt-deploy.yml diff --git a/inventory/hosts.ini b/inventory/hosts.ini index bed5319e..9d555778 100644 --- a/inventory/hosts.ini +++ b/inventory/hosts.ini @@ -288,6 +288,27 @@ emc-0[1:3] # host categories ## OS +[openwrt:vars] +ansible_ssh_transfer_method=scp + +[openwrt] +ch-router +ch-alix1d +ch-testvm-openwrt +mz-ap +mz-router +glt-gw-r3 +glt-gw-tug +ele-router +ele-uhrturm +ele-orpheum +ele-tub +[openwrt:children] +accesspoints +chaos-at-home-sensors +ups + + [dellos6:children] chaos-at-home-switches diff --git a/openwrt-deploy.sh b/openwrt-deploy.sh new file mode 120000 index 00000000..91c11375 --- /dev/null +++ b/openwrt-deploy.sh @@ -0,0 +1 @@ +deploy.sh \ No newline at end of file diff --git a/roles/openwrt/deploy/action_plugins/openwrt_sysupgrade.py b/roles/openwrt/deploy/action_plugins/openwrt_sysupgrade.py new file mode 100644 index 00000000..16772937 --- /dev/null +++ b/roles/openwrt/deploy/action_plugins/openwrt_sysupgrade.py @@ -0,0 +1,57 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os + +from ansible.errors import AnsibleError, AnsibleAction, AnsibleActionFail, AnsibleActionSkip, AnsibleConnectionFailure +from ansible.module_utils._text import to_native +from ansible.plugins.action import ActionBase + + +class ActionModule(ActionBase): + TRANSFERS_FILES = True + + def run(self, tmp=None, task_vars=None): + if task_vars is None: + task_vars = dict() + + if self._task.environment and any(self._task.environment): + self._display.warning('openwrt_sysupgrade module does not support the environment keyword') + + result = super(ActionModule, self).run(tmp, task_vars) + del tmp # tmp no longer has any effect + self._cleanup_remote_tmp = False + + try: + if self._play_context.check_mode: + raise AnsibleActionSkip('Check mode is not supported for this task.') + + result['changed'] = True + + try: + image = to_native(self._task.args.get('image', ''), errors='surrogate_or_strict') + image = self._loader.get_real_file(self._find_needle('files', image), decrypt=False) + except AnsibleError as e: + raise AnsibleActionFail(to_native(e)) + + tmp_img = self._connection._shell.join_path(self._connection._shell.tmpdir, os.path.basename(image)) + self._transfer_file(image, tmp_img) + self._fixup_perms2((self._connection._shell.tmpdir, tmp_img), execute=False) + + args = to_native(self._task.args.get('args', ''), errors='surrogate_or_strict') + + script_cmd = ' '.join(['sysupgrade', args, tmp_img]) + script_cmd = self._connection._shell.wrap_for_exec(script_cmd) + + try: + result.update(self._low_level_execute_command(cmd=script_cmd)) + except AnsibleConnectionFailure as e: + result['rc'] = 0 + + if 'rc' in result and result['rc'] != 0: + raise AnsibleActionFail('non-zero return code') + + except AnsibleAction as e: + result.update(e.result) + + return result diff --git a/roles/openwrt/deploy/tasks/main.yml b/roles/openwrt/deploy/tasks/main.yml new file mode 100644 index 00000000..06fb28ad --- /dev/null +++ b/roles/openwrt/deploy/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: copy image and run sysupgrade + openwrt_sysupgrade: + image: "{{ output_images | first }}" + args: -n diff --git a/spreadspace/openwrt-deploy.yml b/spreadspace/openwrt-deploy.yml new file mode 100644 index 00000000..2832d6ed --- /dev/null +++ b/spreadspace/openwrt-deploy.yml @@ -0,0 +1,9 @@ +--- +- name: generate os image + hosts: "{{ install_hostname }}" + connection: local + gather_facts: no + roles: + - role: openwrt/image + +- import_playbook: ../common/openwrt-deploy.yml -- cgit v1.2.3