Exemplo n.º 1
0
def setup_stack_deps(stack):
    for srv in stack['srv']:
        srv = Resource.get_resource(srv.value)
        for pkg in stack['pkg']:
            pkg = Resource.get_resource(pkg.value)
            print(pkg)
            if pkg.id not in srv.requires:
                srv.requires.add(pkg.id)
                print(pkg.id + " toegevoegd aan " + srv)
        for cfg in stack['cfg']:
            cfg = Resource.get_resource(cfg.value)
            if cfg.id not in srv.requires:
                srv.requires.add(cfg.id)
                print(cfg.id + " toegevoegd aan " + srv)
    for cfg in stack[cfg]:
        cfg = Resource.get_resource(cfg.value)
        for pkg in stack['pkg']:
            pkg = Resource.get_resource(pkg.value)
            if pkg.id not in cfg.requires:
                cfg.requires.add(pkg.id)
                print(pkg.id + " toegevoegd aan " + cfg)
Exemplo n.º 2
0
def dir_before_file(model, resources):
    """
        If a file is defined on a host, then make the file depend on its parent directory
    """
    # loop over all resources to find files
    for _id, resource in resources.items():
        res_class = resource.model.__class__
        if resource.model.__module__ == "std" and res_class.__name__ == "File":
            model = resource.model
            host = model.host


            for dir in host.directories:
                dir_res = Resource.get_resource(dir)
                if dir_res is not None and os.path.dirname(resource.path) == dir_res.path:
                    #Make the File resource require the directory
                    resource.requires.add(dir_res.id)
Exemplo n.º 3
0
def file_before_service(model, resources):
    """
        If a service is defined on a host, then make the service depend on all packages
        A brute force way of defining pkg->srv dependencies.
        Optional better way:
            1. somehow ask the package manager what service a package installs
            2. see if that service is described in the model
            3. if so: get the resource of that service and make it depend on the original package
    """
    # loop over all resources to find services
    for _id, resource in resources.items():
        res_class = resource.model.__class__
        if resource.model.__module__ == "std" and res_class.__name__ == "Service":
            model = resource.model
            host = model.host

            # now find all packages on the same host as the service and add the packages as a requirement
            for file in host.files:
                file_res = Resource.get_resource(file)
                if file_res is not None:
                        #Make the Service resource require the package
                        resource.requires.add(file_res.id)
Exemplo n.º 4
0
def bootstrap(config):
    """
        Bootstrap IMP on a remote server by provisioning a VM, configuring it
        and starting the IMP server there.
    """
    console = logging.StreamHandler()
    LOGGER.addHandler(console)
    LOGGER.setLevel(logging.INFO)

    root_scope = compile_model(config)

    mgmt_server = get_mgmt_server_from_model(root_scope)

    # provision the mgmt server
    iaas_agent = Agent(config, False, mgmt_server.iaas.name, offline=True, deploy=True)
    export = Exporter(config)
    export.run(root_scope, offline=True)

    mgmt_vm_resource = Resource.get_resource(mgmt_server)
    iaas_agent.update(mgmt_vm_resource)

    print("Bootstrapping IaaS config and booting management server")
    while iaas_agent._queue.size() > 0:
        iaas_agent.deploy_config()

    # wait for the vm to come online get its ip
    print("Waiting for %s to become available" % mgmt_server.name)
    facts = None
    while facts is None:
        all_facts = iaas_agent.get_facts(mgmt_vm_resource)
        for vm_key in all_facts.keys():
            if vm_key == mgmt_vm_resource.id.resource_str() and "ip_address" in all_facts[vm_key]:
                facts = all_facts[vm_key]

        if facts is None:
            print("No response, waiting 5s for retry")
            time.sleep(5)

    # wait for the server to respond to ssh
    while True:
        result = run("/usr/bin/ssh", ARGS + ["ec2-user@" + facts["ip_address"], "echo 'OK'"])
        if result[0] == "OK":
            break
        else:
            time.sleep(5)

    # now add our ssh key to the root user
    deploy_key(facts["ip_address"], mgmt_vm_resource.key_value)

    # now recompile the model for the mgmt server and do a remote deploy
    Offline.get().set_facts(str(mgmt_vm_resource.id.resource_str()), facts)
    root_scope = compile_model(config)
    deploy(config, root_scope, remote=mgmt_server.name, dry_run=False, ip_address=facts["ip_address"])

    ## now boot all other servers, these are already available in the root_scope of the previous compile
    servers = set(root_scope.get_variable("Host", ["std"]).value)
    vm_servers = []
    for server in servers:
        vm_resource = Resource.get_resource(server)
        if vm_resource is not None:
            vm_servers.append(vm_resource)
            iaas_agent.update(vm_resource)

    print("Booting all other servers")
    while iaas_agent._queue.size() > 0:
        iaas_agent.deploy_config()

    # collect facts about all server
    print("Waiting for servers to become available")
    facts = {}
    while len(vm_servers) > len(facts):
        for vm in vm_servers:
            if vm.id.resource_str() not in facts:
                all_facts = iaas_agent.get_facts(vm)

                for vm_key in all_facts.keys():
                    if vm_key not in facts and "ip_address" in all_facts[vm_key]:
                        Offline.get().set_facts(vm_key, all_facts[vm_key])
                        facts[vm_key] = all_facts[vm_key]

        if len(vm_servers) > len(facts):
            print("No response, waiting 5s for retry")
            time.sleep(5)

    # now recompile the model once again with all facts and deploy to all servers
    root_scope = compile_model(config)

    # wait for the server to come online, deploy the ssh key and deploy the config
    print("Waiting for the server to come online, add our key to the root user and deploy the configuration")
    vm_todo = list(vm_servers)
    while len(vm_todo) > 0:
        for vm in list(vm_todo):
            ip = facts[vm.id.resource_str()]["ip_address"]

            result = run("/usr/bin/ssh", ARGS + ["ec2-user@" + ip, "echo 'OK'"])
            if result[0] == "OK":
                print("%s up" % vm.id.attribute_value)
                deploy_key(ip, vm.key_value)
                deploy(config, root_scope, remote=vm.name, dry_run=False, ip_address=ip)
                print("%s done" % vm.id.attribute_value)
                vm_todo.remove(vm)

        time.sleep(5)

    # now a final run with bootstrap off
    # now recompile the model once again with all facts and deploy to all servers
    root_scope = compile_model(config, bootstrap="false")

    # wait for the server to come online, deploy the ssh key and deploy the config
    print("Deploying final non-bootstrap configuration")
    vm_todo = list(vm_servers)
    for vm in list(vm_todo):
        ip = facts[vm.id.resource_str()]["ip_address"]

        deploy(config, root_scope, remote=vm.name, dry_run=False, ip_address=ip)
        vm_todo.remove(vm)