risotto/ansible/action_plugins/rougail.py

69 lines
2.6 KiB
Python
Raw Normal View History

2022-10-01 22:33:11 +02:00
#!/usr/bin/python3
from asyncio import run
2022-12-21 16:35:58 +01:00
from os import readlink
from os.path import join, islink
from risotto.machine import load, templates
try:
from ansible.plugins.action import ActionBase
from ansible.module_utils.basic import AnsibleModule
class FakeModule(AnsibleModule):
def __init__(self):
pass
except:
import traceback
traceback.print_exc()
class ActionBase():
def __init__(self, *args, **kwargs):
raise Exception('works only with ansible')
2022-10-01 22:33:11 +02:00
2022-12-21 16:35:58 +01:00
async def build_files(server_name: str,
just_copy: bool,
) -> None:
config = await load()
2022-10-01 22:33:11 +02:00
await templates(server_name,
2022-12-21 16:35:58 +01:00
config,
just_copy=just_copy,
2022-10-01 22:33:11 +02:00
)
class ActionModule(ActionBase):
def run(self, tmp=None, task_vars=None):
super(ActionModule, self).run(tmp, task_vars)
module_args = self._task.args.copy()
2022-12-21 16:35:58 +01:00
root_local = module_args.pop('root_local')
root_remote= module_args.pop('root_remote')
name = module_args.pop('hostname')
is_host = module_args.pop('is_host')
just_copy = module_args.get('just_copy', False)
module_args['root'] = root_remote
2022-10-01 22:33:11 +02:00
2022-12-21 16:35:58 +01:00
run(build_files(name, just_copy))
#
remote = self._execute_module(module_name='compare', module_args=module_args, task_vars=task_vars)
if remote.get('failed'):
raise Exception(f'error in remote action: {remote["module_stdout"]}')
#
module = FakeModule()
modified_files = []
changed = False
for path in module_args['paths']:
full_path = join(root_local, path['name'][1:])
if remote['compare'].get(path['name']):
if remote['compare'][path['name']]['type'] == 'file':
if remote['compare'][path['name']]['shasum'] == module.digest_from_file(full_path, 'sha256'):
continue
else:
# it's a symlink
if islink(full_path) and remote['compare'][path['name']]['name'] == readlink(full_path):
continue
changed = True
modified_files.append(path['name'])
if not is_host:
for old_file in remote['old_files']:
changed = True
# module_args['path'] = old_file
# module_args['state'] = 'absent'
# self._execute_module(module_name='ansible.builtin.file', module_args=module_args, task_vars=task_vars)
return dict(ansible_facts=dict({}), changed=changed)