示例#1
0
def deploy(hubs, masterfiles):
    if masterfiles.startswith(("http://", "https://")):
        urls = [masterfiles]
        paths = _download_urls(urls)
        assert len(paths) == 1
        masterfiles = paths[0]
        log.debug(f"Deploying downloaded: {masterfiles}")
    else:
        masterfiles = os.path.abspath(os.path.expanduser(masterfiles))
        log.debug(f"Deploy path expanded to: {masterfiles}")

    masterfiles = masterfiles.rstrip("/")

    if os.path.isfile(masterfiles):
        return deploy_tarball(hubs, masterfiles)

    if not os.path.isdir(masterfiles):
        log.error(f"'{masterfiles}' must be a directory")
        return 1

    directory = masterfiles

    if not directory.endswith("/masterfiles"):
        log.error(
            "The masterfiles directory to deploy must be called 'masterfiles'")
        return 1

    if os.path.isfile(f"{directory}/autogen.sh"):
        os.system(f"bash -c 'cd {directory} && ./autogen.sh 1>/dev/null 2>&1'")
        if not os.path.isfile(f"{directory}/promises.cf"):
            log.error(
                f"The autogen.sh script did not produce promises.cf in '{directory}'"
            )
            return 1
    elif os.path.isfile(f"{directory}/configure"):
        os.system(f"bash -c 'cd {directory} && ./configure 1>/dev/null 2>&1'")
        if not os.path.isfile(f"{directory}/promises.cf"):
            log.error(
                f"The configure script did not produce promises.cf in '{directory}'"
            )
            return 1
    else:
        log.debug(
            "No autogen.sh / configure found, assuming ready to install directory"
        )
        if not os.path.isfile(f"{directory}/promises.cf"):
            log.error(f"No promises.cf in '{directory}'")
            return 1

    assert (not cf_remote_dir().endswith("/"))
    tarball = cf_remote_dir() + "/masterfiles.tgz"
    above = directory[0:-len("/masterfiles")]
    os.system(f"rm -rf {tarball}")
    os.system(f"tar -czf {tarball} -C {above} masterfiles")
    return deploy_tarball(hubs, tarball)
示例#2
0
def deploy(hubs, directory):
    assert (directory.endswith("/masterfiles"))
    assert (os.path.isfile(directory + "/autogen.sh"))
    os.system(f"bash -c 'cd {directory} && ./autogen.sh 1>/dev/null 2>&1'")
    assert (os.path.isfile(directory + "/promises.cf"))

    assert (not cf_remote_dir().endswith("/"))
    tarball = cf_remote_dir() + "/masterfiles.tgz"
    above = directory[0:-len("/masterfiles")]
    os.system(f"rm -rf {tarball}")
    os.system(f"tar -czf {tarball} -C {above} masterfiles")
    errors = 0
    for hub in hubs:
        errors += deploy_masterfiles(hub, tarball)
    return errors
示例#3
0
def install_def_json(host, *, connection=None, call_collect=False):
    print("Transferring def.json to hub: '{}'".format(host))
    data = json.dumps(def_json(call_collect), indent=2)
    path = os.path.join(cf_remote_dir("json"), "def.json")
    save_file(path, data)
    scp(path, host, connection=connection)
    ssh_sudo(connection, "cp def.json /var/cfengine/masterfiles/def.json")
示例#4
0
文件: demo.py 项目: nickanderson/core
def install_def_json(host, *, connection=None, call_collect=False):
    print("Transferring def.json to hub: '{}'".format(host))
    data = json.dumps(def_json(call_collect), indent=2)
    path = os.path.join(cf_remote_dir("json"), "def.json")
    save_file(path, data)
    scp(path, host, connection=connection)
    ssh_sudo(connection, "cp def.json /var/cfengine/masterfiles/def.json")
示例#5
0
def install_def_json(host):
    print("Transferring def.json to hub: '{}'".format(host))
    data = json.dumps(def_json(), indent=2)
    path = os.path.join(cf_remote_dir("json"), "def.json")
    save_file(path, data)
    remote.scp(path, host)
    with remote.connect(host) as connection:
        connection.sudo("cp def.json /var/cfengine/masterfiles/def.json")
示例#6
0
def install(hubs,
            clients,
            *,
            bootstrap=None,
            package=None,
            hub_package=None,
            client_package=None,
            version=None,
            demo=False,
            call_collect=False,
            edition=None):
    assert hubs or clients
    assert not (hubs and clients and package)
    # These assertions are checked in main.py

    if not hub_package:
        hub_package = package
    if not client_package:
        client_package = package
    if bootstrap:
        if type(bootstrap) is str:
            bootstrap = [bootstrap]
        save_file(os.path.join(cf_remote_dir(), "policy_server.dat"),
                  "\n".join(bootstrap + [""]))
    errors = 0
    if hubs:
        if type(hubs) is str:
            hubs = [hubs]
        for index, hub in enumerate(hubs):
            log.debug("Installing {} hub package on '{}'".format(edition, hub))
            errors += install_host(
                hub,
                hub=True,
                package=hub_package,
                bootstrap=bootstrap[index %
                                    len(bootstrap)] if bootstrap else None,
                version=version,
                demo=demo,
                call_collect=call_collect,
                edition=edition)
    for index, host in enumerate(clients or []):
        log.debug("Installing {} client package on '{}'".format(edition, host))
        errors += install_host(
            host,
            hub=False,
            package=client_package,
            bootstrap=bootstrap[index % len(bootstrap)] if bootstrap else None,
            version=version,
            demo=demo,
            edition=edition)
    if demo and hubs:
        for hub in hubs:
            print(
                "Your demo hub is ready: https://{}/ (Username: admin, Password: password)"
                .format(strip_user(hub)))
    return errors
示例#7
0
def get_json(url):
    r = requests.get(url)
    assert r.status_code >= 200 and r.status_code < 300
    data = r.json()

    filename = os.path.basename(url)
    dir = cf_remote_dir("json")
    path = os.path.join(dir, filename)
    log.debug("Saving '{}' to '{}'".format(url, path))
    write_json(path, data)

    return data
示例#8
0
文件: web.py 项目: cfengine/core
def get_json(url):
    r = requests.get(url)
    assert r.status_code >= 200 and r.status_code < 300
    data = r.json()

    filename = os.path.basename(url)
    dir = cf_remote_dir("json")
    path = os.path.join(dir, filename)
    log.debug("Saving '{}' to '{}'".format(url, path))
    write_json(path, data)

    return data
示例#9
0
文件: commands.py 项目: cfengine/core
def install(
        hubs,
        clients,
        *,
        bootstrap=None,
        package=None,
        hub_package=None,
        client_package=None,
        version=None,
        demo=False,
        call_collect=False):
    assert hubs or clients
    assert not (hubs and clients and package)
    # These assertions are checked in main.py

    if not hub_package:
        hub_package = package
    if not client_package:
        client_package = package
    if bootstrap:
        if type(bootstrap) is str:
            bootstrap = [bootstrap]
        save_file(os.path.join(cf_remote_dir(), "policy_server.dat"), "\n".join(bootstrap + [""]))
    if hubs:
        if type(hubs) is str:
            hubs = [hubs]
        for index, hub in enumerate(hubs):
            log.debug("Installing hub package on '{}'".format(hub))
            install_host(
                hub,
                hub=True,
                package=hub_package,
                bootstrap=bootstrap[index % len(bootstrap)] if bootstrap else None,
                version=version,
                demo=demo,
                call_collect=call_collect)
    for index, host in enumerate(clients or []):
        log.debug("Installing client package on '{}'".format(host))
        install_host(
            host,
            hub=False,
            package=client_package,
            bootstrap=bootstrap[index % len(bootstrap)] if bootstrap else None,
            version=version,
            demo=demo)
    if demo and hubs:
        for hub in hubs:
            print(
                "Your demo hub is ready: https://{}/ (Username: admin, Password: password)".
                format(strip_user(hub)))
示例#10
0
def install(hub,
            clients,
            *,
            bootstrap=None,
            package=None,
            hub_package=None,
            client_package=None,
            version=None,
            demo=False):
    assert hub or clients
    assert not (hub and clients and package)
    # These assertions are checked in main.py

    if not hub_package:
        hub_package = package
    if not client_package:
        client_package = package
    if bootstrap:
        save_file(os.path.join(cf_remote_dir(), "policy_server.dat"),
                  bootstrap + "\n")
    if hub:
        log.debug("Installing hub package on '{}'".format(hub))
        install_host(hub,
                     hub=True,
                     package=hub_package,
                     bootstrap=bootstrap,
                     version=version,
                     demo=demo)
    for host in (clients or []):
        log.debug("Installing client package on '{}'".format(host))
        install_host(host,
                     hub=False,
                     package=client_package,
                     bootstrap=bootstrap,
                     version=version,
                     demo=demo)
    if demo and hub:
        print(
            "Your demo hub is ready: https://{}/ (Username: admin, Password: password)"
            .format(hub))
示例#11
0
def install(hubs,
            clients,
            *,
            bootstrap=None,
            package=None,
            hub_package=None,
            client_package=None,
            version=None,
            demo=False,
            call_collect=False,
            edition=None,
            remote_download=False,
            trust_keys=None):
    assert hubs or clients
    assert not (hubs and clients and package)
    assert (trust_keys is None) or hasattr(trust_keys, "__iter__")
    # These assertions are checked/ensured in main.py

    # If there are URLs in any of the package strings and remote_download is FALSE, download and replace with path:
    packages = (package, hub_package, client_package)
    if remote_download:
        package, hub_package, client_package = _verify_package_urls(packages)
    else:
        package, hub_package, client_package = _download_urls(packages)

    # If any of these are folders, transform them to lists of the files inside those folders:
    package = _maybe_packages_in_folder(package)
    hub_package = _maybe_packages_in_folder(hub_package)
    client_package = _maybe_packages_in_folder(client_package)

    # If --hub-package or --client-pacakge are not specified, use --package argument:
    if not hub_package:
        hub_package = package
    if not client_package:
        client_package = package

    if bootstrap:
        if type(bootstrap) is str:
            bootstrap = [bootstrap]
        save_file(os.path.join(cf_remote_dir(), "policy_server.dat"),
                  "\n".join(bootstrap + [""]))

    hub_jobs = []
    if hubs:
        show_host_info = (len(hubs) == 1)
        if type(hubs) is str:
            hubs = [hubs]
        for index, hub in enumerate(hubs):
            log.debug("Installing {} hub package on '{}'".format(edition, hub))
            hub_jobs.append(
                HostInstaller(
                    hub,
                    hub=True,
                    packages=hub_package,
                    bootstrap=bootstrap[index %
                                        len(bootstrap)] if bootstrap else None,
                    version=version,
                    demo=demo,
                    call_collect=call_collect,
                    edition=edition,
                    show_info=show_host_info,
                    remote_download=remote_download,
                    trust_keys=trust_keys))

    errors = 0
    if hub_jobs:
        with Pool(len(hub_jobs)) as hubs_install_pool:
            hubs_install_pool.map(lambda job: job.run(), hub_jobs)
        errors = sum(job.errors for job in hub_jobs)

    if errors > 0:
        s = "s" if errors > 1 else ""
        log.error(
            f"{errors} error{s} encountered while installing hub packages, aborting..."
        )
        return errors

    client_jobs = []
    show_host_info = (clients and (len(clients) == 1))
    for index, host in enumerate(clients or []):
        log.debug("Installing {} client package on '{}'".format(edition, host))
        client_jobs.append(
            HostInstaller(
                host,
                hub=False,
                packages=client_package,
                bootstrap=bootstrap[index %
                                    len(bootstrap)] if bootstrap else None,
                version=version,
                demo=demo,
                edition=edition,
                show_info=show_host_info,
                remote_download=remote_download,
                trust_keys=trust_keys))

    if client_jobs:
        with Pool(len(client_jobs)) as clients_install_pool:
            clients_install_pool.map(lambda job: job.run(), client_jobs)
        errors += sum(job.errors for job in client_jobs)

    if demo and hubs:
        for hub in hubs:
            print(
                "Your demo hub is ready: https://{}/ (Username: admin, Password: password)"
                .format(strip_user(hub)))

    if errors > 0:
        s = "s" if errors > 1 else ""
        log.error(
            f"{errors} error{s} encountered while installing client packages")

    return errors