gen_ssh_config.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:sd2 作者: gae123 项目源码 文件源码
def generate_for_host(host):
    from . import util
    rr = ''
    rr += '''\n########## GENERATED DO NOT MODIFY #####################\n'''
    sshport = 22 if util.is_localhost(host['name']) else 2222
    if not util.is_localhost(host['name']):
        if host.get('match'):
            matches = host.get('match')
            for hostname in [host['name'], host['name'] + '-ports']:
                for match in matches:
                    rr += 'Match originalhost {hostname} exec "{match[condition]}"\n'.format(
                        hostname=hostname, match=match)
                    for key in six.viewkeys(match):
                        if not key in ssh_option_names:
                            continue
                        rr += '    {key} {value}\n'.format(key=key,
                                                           value=match[key])
                rr += '\n'

        rr += 'host {}\n'.format(host['name'])
        for key, val in six.iteritems(host):
            if not key in ssh_option_names:
                continue
            if not isinstance(val, (list, tuple)):
                val = [val]
            for vv in val:
                rr += '    {key} {value}\n'.format(key=key, value=vv)
        rr += '\n'

        if host.get('containers'):
            rr += 'host {}-ports\n'.format(host['name'])
            if not 'HostName' in six.viewkeys(host):
                host['HostName'] = host['name']
            for key, val in six.iteritems(host):
                if not key in ssh_option_names + ['LocalForward']:
                    continue
                if not isinstance(val, (list, tuple)):
                    val = [val]
                for vv in val:
                    rr += '    {key} {value}\n'.format(key=key, value=vv)
                    # rr += '    LocalForward {}-local:2375 localhost:2375\n'.format(host['name'])
            for cont in host.get('containers', []):
                ports = cont['image'].get('ports', [])
                for port in ports + ["{}:22".format(sshport)]:
                    (p1, p2) = port.split(':')
                    rr += (
                        "    LocalForward  {0}:{1} {2}:{1}\n".format(
                            cont['name'], p1, cont['ip']))
            rr += '\n'

    for cont in host.get('containers', []):
        rr += container_entry_template.format(**locals())
        for key, val in six.iteritems(host):
            if key in container_ssh_option_names:
                rr += '    {} {}\n'.format(key, val)
    return rr
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号