parse_ignition.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """Ansible action plugin to decode ignition payloads"""
  2. import base64
  3. import os
  4. import six
  5. from six.moves import urllib
  6. from ansible.plugins.action import ActionBase
  7. # pylint: disable=too-many-function-args
  8. def get_files(files_dict, systemd_dict, dir_list, data):
  9. """parse data to populate file_dict"""
  10. files = data.get('storage', []).get('files', [])
  11. for item in files:
  12. path = item["path"]
  13. dir_list.add(os.path.dirname(path))
  14. # remove prefix "data:,"
  15. encoding, contents = item['contents']['source'].split(',', 1)
  16. if 'base64' in encoding:
  17. contents = base64.b64decode(contents).decode('utf-8')
  18. else:
  19. contents = urllib.parse.unquote(contents)
  20. # convert from int to octal, padding at least to 4 places.
  21. # eg, 420 becomes '0644'
  22. mode = str(format(int(item["mode"]), '04o'))
  23. inode = {"contents": contents, "mode": mode}
  24. files_dict[path] = inode
  25. # get the systemd units files we're here
  26. systemd_units = data.get('systemd', []).get('units', [])
  27. for item in systemd_units:
  28. contents = item['contents']
  29. if six.PY2:
  30. # pylint: disable=redefined-variable-type
  31. contents = contents.decode('unicode-escape')
  32. mode = "0644"
  33. inode = {"contents": contents, "mode": mode}
  34. name = item['name']
  35. path = '/etc/systemd/system/' + name
  36. dir_list.add(os.path.dirname(path))
  37. files_dict[path] = inode
  38. enabled = item.get('enabled') or True
  39. systemd_dict[name] = enabled
  40. # pylint: disable=too-few-public-methods
  41. class ActionModule(ActionBase):
  42. """ActionModule for parse_ignition.py"""
  43. def run(self, tmp=None, task_vars=None):
  44. """Run parse_ignition action plugin"""
  45. result = super(ActionModule, self).run(tmp, task_vars)
  46. result["changed"] = False
  47. result["failed"] = False
  48. result["msg"] = "Parsed successfully"
  49. files_dict = {}
  50. systemd_dict = {}
  51. dir_list = set()
  52. result["files_dict"] = files_dict
  53. result["systemd_dict"] = systemd_dict
  54. # self.task_vars holds all in-scope variables.
  55. # Ignore settting self.task_vars outside of init.
  56. # pylint: disable=W0201
  57. self.task_vars = task_vars or {}
  58. ign_file_contents = self._task.args.get('ign_file_contents')
  59. get_files(files_dict, systemd_dict, dir_list, ign_file_contents)
  60. result["dir_list"] = list(dir_list)
  61. return result