Skip to content
Snippets Groups Projects
Select Git revision
  • 1c0e5cf53e3d75fc34a67beb9695c29f953b37d3
  • master default protected
  • ci-bullseye
  • wip/bigtop-3.0.0
  • bio3
  • feature/certificates2
6 results

orchestrate.py

Blame
  • orchestrate.py 4.38 KiB
    #! /usr/bin/python3
    
    import argparse
    import importlib
    import json
    import os
    import subprocess
    import sys
    
    DEFAULT_ACTIONS = ['files', 'init', 'check']
    
    parser = argparse.ArgumentParser(description='terraform cluster orchestrator')
    parser.add_argument('-c', '--config',
                        help='Terraform output for using by orchestrator (default: -)', default='-')
    parser.add_argument('actions', metavar='ACTIONS', nargs='*',
                        help='actions (default: %s)' % ' '.join(DEFAULT_ACTIONS),
                        default=DEFAULT_ACTIONS)
    parser.add_argument('-n', '--dry-run', action='store_true',
                        help='simulated run')
    parser.add_argument('-p', '--parameters',
                        help='orchestration parameters')
    args = parser.parse_args()
    
    j = None
    if args.config == '-':
        j = json.load(sys.stdin)
    else:
        with open(args.config) as f:
            j = json.load(f)
    
    config = j['config']['value']
    n = config['n']
    d = config['domain']
    hosts = j['hosts']['value']
    public_hosts = j['public_hosts']['value']
    master_hostname = config['master_hostname']
    master_ip = public_hosts[master_hostname]
    user = config['image_user']
    t = config.get('type', None)
    
    print('== plugin ==')
    if t and os.path.exists('%s/plugin.py' % t):
        print('-> %s/plugin.py' % t)
        plugin = importlib.import_module(f'%s.plugin' % t)
        Component = getattr(plugin, 'Component')
        component = Component(args, config, hosts)
    else:
        component = None
    
    if 'files' in args.actions:
        print('== files ==')
        print('-> hosts')
        if not args.dry_run:
            with open('hosts', 'w') as f:
                f.write('''\
    127.0.0.1	localhost
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     localhost ip6-localhost ip6-loopback
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    
    ''')
                for h, ip in hosts.items():
                    f.write('%s	%s.%s	%s.%s.	%s\n' % (ip, h, d, h, d, h))
    
        print('-> public_hosts')
        if not args.dry_run:
            with open('public_hosts', 'w') as f:
                for h, ip in public_hosts.items():
                    f.write('%s	%s.%s	%s.%s.	%s\n' % (ip, h, d, h, d, h))
    
        print('-> inventory')
        if not args.dry_run:
            with open('inventory', 'w') as f:
                f.write('''\
    [masters]
    %s ansible_user=%s ansible_become=true ansible_host=%s
    
    [nodes]
    ''' % (master_hostname, user, master_ip))
                for h, ip in hosts.items():
                    if h == master_hostname:
                        continue
                    f.write('\
    %s ansible_user=%s ansible_become=true ansible_host=%s ansible_ssh_common_args=\'\
    -o ForwardAgent=yes -o ProxyCommand="ssh -W %s:22 -q %s@%s"\'\n' % (h, user, ip, ip, user,
                                                                        master_ip))
        if component:
            component.action('files')
    
    if 'init' in args.actions:
        print('== init ==')
    
        h = list(hosts.keys()) + ['%s.%s.' % (host, d) for host in hosts.keys()] + \
            ['%s.%s' % (host, d) for host in hosts.keys()] + list(hosts.values()) + [master_ip]
    
        commands = [
            ['ssh-keygen', '-R', master_ip],
            ['ssh', '-o', 'StrictHostKeyChecking=no', '%s@%s' % (user, master_ip), ':'],
            ['scp', '-p', 'hosts', '%s@%s:/tmp/' % (user, master_ip)],
            ['ssh', '%s@%s' % (user, master_ip), 'sudo mv -v /tmp/hosts /etc/hosts'],
            ['ssh', '-o', 'ForwardAgent=yes', '%s@%s' % (user, master_ip),
             'for h in %s; do ssh -o StrictHostKeyChecking=no %s@$h :; done' % (' '.join(h), user)],
        ]
        for ip in hosts.values():
            commands += [
                ['ssh-keygen', '-R', ip],
                ['ssh', '-o', 'ForwardAgent=yes', '-o', 'ProxyCommand=ssh -W %s:22 -q %s@%s' %
                 (ip, user, master_ip), '-o', 'StrictHostKeyChecking=no', '%s@%s' % (user, ip), ':']
            ]
        commands += [
            ['ansible', '-i', './inventory', '-m', 'copy', '-a', 'src=hosts dest=/etc/hosts', 'nodes'],
        ]
        if component:
            commands += component.init_commands()
    
        for cmd in commands:
            print('-> %s' % ' '.join(cmd))
            if not args.dry_run:
                subprocess.run(cmd)
        if component:
            component.action('init')
    
    if 'check' in args.actions:
        print('== check ==')
    
        commands = [
            ['ansible', '-i', './inventory', '-m', 'command', '-a', 'uname -a', 'all']
        ]
    
        for cmd in commands:
            print('-> %s' % ' '.join(cmd))
            if not args.dry_run:
                subprocess.run(cmd)
        if component:
            component.action('check')