Example #1
0
def deploy(config, root_scope, remote = None, dry_run = True, ip_address = None):
    deploy_host = None
    all_names = []
    if remote is None:
        hostname = socket.gethostname()
    else:
        hostname = remote
    
    print("Deploying on %s (dry-run = %s)" % (hostname, dry_run))
        
    try:
        servers = root_scope.get_variable("Host", ["std"]).value
        
        for server in servers:
            all_names.append(server.name)
            if server.name == hostname:
                deploy_host = server
        
    except Exception:
        print("The std module is not loaded or does not contain the definition of Host")
        return
        
    if deploy_host is None:
        print("Unable to find a host to deploy on the current machine %s" % hostname)
        print("Host found in model: " + ", ".join(all_names))
        return
    
    export = Exporter(config)
    json_data = export.run(root_scope, offline = True)
    files = export.get_offline_files()
    
    if remote is not None:
        ip_address = remote
    
    agent = Agent(config, False, hostname, offline = True, deploy = not dry_run, remote = ip_address)
    agent._offline_files = files

    host_id = "[%s," % deploy_host.name
    for item in json.loads(json_data.decode("utf-8")):
        if host_id in item["id"]:
            agent.update(Resource.deserialize(item))

    if agent._queue.size() == 0:
        print("No configuration found for host %s" % hostname)
        return
            
    print("Deploying config")
    agent.deploy_config()
    #agent.close()