forked from stove/risotto
68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
#!/usr/bin/python3
|
|
from asyncio import run
|
|
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')
|
|
|
|
|
|
async def build_files(server_name: str,
|
|
just_copy: bool,
|
|
) -> None:
|
|
config = await load()
|
|
await templates(server_name,
|
|
config,
|
|
just_copy=just_copy,
|
|
)
|
|
|
|
|
|
class ActionModule(ActionBase):
|
|
def run(self, tmp=None, task_vars=None):
|
|
super(ActionModule, self).run(tmp, task_vars)
|
|
module_args = self._task.args.copy()
|
|
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
|
|
|
|
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)
|