コード例 #1
0
ファイル: run.py プロジェクト: VisualOps/cli
def run_stack(config, app_dict, force=True):
    config["appname"] = utils.user_param(config, "Enter app name",config["appname"])
    config["dirs"] = {
        "containers": os.path.join(config["config_path"],"docker","containers"),
        "boot2docker": os.path.join(config["config_path"],"docker","boot2docker"),
    }
    for d in config["dirs"]:
        if not os.path.exists(config["dirs"][d]):
            os.makedirs(config["dirs"][d])
    if boot2docker.has():
        print "Starting Boot2docker ... (this may take a while)"
        if not os.path.isfile(os.path.join(config["dirs"]["boot2docker"],"boot2docker.iso")):
            utils.download(config["boot2docker_iso"],os.path.join(config["dirs"]["boot2docker"],"boot2docker.iso"))

        if not boot2docker.gen_config(config, config["appname"]):
            utils.error("Unable to generate boot2docker configuration")
            return False
        boot2docker.delete(config, config["appname"])
        boot2docker.init(config, config["appname"])
        boot2docker.mount(config["appname"], [{
            "volume": "visops_root",
            "hostpath": "/",
        },{
            "volume": "visops_containers",
            "hostpath": config["dirs"]["containers"],
        }])
        if boot2docker.run(config, config["appname"]):
            print "Boot2docker successfully running!"
        else:
            utils.error("Unable to run Boot2docker.")
        config["chroot"] = os.path.join("/mnt/host",config.get("chroot",""))
        config["docker_sock"] = "tcp://%s:2375"%(boot2docker.ip(config,config["appname"]))
    config["hosts_table"] = app_dict.get("hosts_table",{})
    actions = {}
    for hostname in app_dict.get("hosts",{}):
        actions[hostname] = {}
        for state in app_dict["hosts"][hostname]:
            if state == "linux.docker.deploy":
                for container in app_dict["hosts"][hostname][state]:
                    app_dict["hosts"][hostname][state][container]["hostname"] = app_dict["hosts"][hostname][state][container].get("container")
                    app_dict["hosts"][hostname][state][container]["container"] = "%s-%s-%s"%(config["appname"],
                                                                                             hostname,
                                                                                             app_dict["hosts"][hostname][state][container].get("container",""))
                    actions[hostname][container] = (dockervisops.preproc_deploy(config,
                                                                                config["appname"],
                                                                                hostname,
                                                                                app_dict["hosts"][hostname][state][container],
                                                                                "deploy"))
    config["actions"] = actions
    actions = dockervisops.render_all(config, actions)
    config["actions"] = actions
    app = {}
    for hostname in actions:
        for container in actions[hostname]:
            app.update(dockervisops.deploy(config, actions[hostname][container]))
    dockervisops.generate_hosts(config, app)

    #save user input parameter to app_dict
    utils.persist_app(actions,app_dict)
コード例 #2
0
ファイル: dockervisops.py プロジェクト: VisualOps/cli
def render(config, filename, content):
    rendered = ""
    l = 1
    for line in content.split("\n"):
        objs = re.findall("@{(?P<uid>.*?)\.(?P<param>.*?)}",line)
        for obj in objs:
            if obj[1] not in IPS: continue
            hostname = config["hosts_table"].get(obj[0])
            containers = list_containers(config["actions"].get(hostname,{}))
            if len(containers) == 0:
                ip = socket.gethostbyname(socket.getfqdn())
            elif len(containers) == 1:
                ip = containers.pop()
            else:
                # TODO (release 2): render with config
                ip = utils.user_param(config,
                                      '''
Multiple containers found on instance %s.
Note: if you are not sure, correctly binded your ports,
      and have an external access to your machine,
      you can try to use your host IP address (default).

Warning: Using localhost (or 127.0.0.1) won't work in most cases

Context: %s %s, line %s: %s
Please, specify which container to use: %s
                                      '''%(hostname,
                                           ("file" if filename else "parameter"),
                                           filename,
                                           l,
                                           line,
                                           ", ".join(containers)),
                                      socket.gethostbyname(socket.getfqdn()))
            line = re.sub("@{%s\.(.*?)}"%obj[0],ip,line,count=1)
        rendered += "%s\n"%line
        l += 1
    return (rendered[:-1] if (rendered[-1:] == '\n' and content[-1:] != '\n') else rendered)
コード例 #3
0
ファイル: dockervisops.py プロジェクト: VisualOps/cli
def _convert_running(config, appname, hostname, addin):
    addin["port_bindings"] = _replace_params(config, hostname, addin,"port_bindings")
    addin["volumes"] = _replace_params(config, hostname, addin,"volumes")
    addin["mem_limit"] = _replace_params(config, hostname, addin,"mem_limit")
    addin["cpu_shares"] = _replace_params(config, hostname, addin,"cpu_shares")
    addin["count"] = _replace_params(config, hostname, addin,"count")
    if addin.get("port_bindings"):
        ports = []
        pb = {}
        for item in addin["port_bindings"]:
            key = item.get("key",None)
            value = item.get("value",None)
            if not key: continue

            # get user input
            ui = utils.user_param(config,
                                  "Update port binding for %s (host=container)"%addin["container"],
                                  ("%s=%s"%(key,value)
                                   if key not in config["port_bindings"][hostname].get(addin["container"],{})
                                   else "%s=%s"%((key if key else ""),(value if value else ""))))
            # parse result
            if not ui:
                config["port_bindings"][hostname][addin["container"]][key] = value
                continue
            ui = ui.split("=")
            if len(ui) != 2:
                utils.error("Wrong port binding syntax")
                continue
            key, value = ui[1], ui[0]
            # persist
            config["port_bindings"][hostname][addin["container"]][key] = value

            if not key or not value: continue
            v = value.split(":")
            pb[key] = ({
                "HostIp": v[0],
                "HostPort": v[1]
            } if len(v) == 2 else {
                "HostIp": "0.0.0.0",
                "HostPort": v[0]
            })
            ports.append(key)
        addin.pop("port_bindings")
        if pb and ports:
            addin["port_bindings"] = pb
            addin["ports"] = ports
    if addin.get("volumes"):
        volumes = []
        binds = {}
        vol = addin.get("volumes",{})
        for item in vol:
            key = item.get("key")
            value = item.get("value")
            if not key: continue

            # get user input
            ui = utils.user_param(config,
                                  "Update mount point for %s"%addin["container"],
                                  ("%s=%s"%(key,value)
                                   if key not in config["volumes"][hostname].get(addin["container"],{})
                                   else "%s=%s"%((key if key else ""),(value if value else ""))))
            # parse result
            if not ui:
                config["volumes"][hostname][addin["container"]][key] = value
                continue
            ui = ui.split("=")
            if len(ui) != 2:
                utils.error("Wrong volume syntax")
                continue
            key, value = ui[0], ui[1]
            if config.get("chroot"):
                key = os.path.join(config["chroot"],appname,hostname,addin["container"],key)
            # persist
            config["volumes"][hostname][addin["container"]][key] = value




            mp = value.split(":")
            ro = (True if (len(mp) == 2 and mp[1] == "ro") else False)
            value = mp[0]
            volumes.append(value)
            binds[key] = {
                'bind': value,
                'ro': ro
            }
        addin.pop("volumes")
        if volumes and binds:
            addin["volumes"] = volumes
            addin["binds"] = binds
    if addin.get("environment"):
        env = {}
        for item in addin["environment"]:
            key = item.get("key","")
            value = item.get("value","")
            if not key: continue
            env[key] = value
        addin.pop("environment")
        addin["environment"] = env
    if addin.get("links"):
        links = {}
        for item in addin["links"]:
            key = item.get("key","")
            value = item.get("value","")
            if not key or not value: continue
            links[key] = value
        addin.pop("links")
        addin["links"] = links
    if addin.get("cpu_shares"):
        # get user input
        ui = utils.user_param(config,
                              "Update CPU shares for %s"%addin["container"],
                              ("%s"%(addin.get("cpu_shares"))
                               if addin["container"] not in config["cpu_shares"][hostname]
                               else addin.get("cpu_shares")))
        # parse result
        addin["cpu_shares"] = ui
        # persist
        config["cpu_shares"][hostname][addin["container"]] = ui
    if addin.get("mem_limit"):
        # get user input
        ui = utils.user_param(config,
                              "Update memory limit for %s"%addin["container"],
                              ("%s"%(addin.get("mem_limit"))
                               if addin["container"] not in config["mem_limit"][hostname]
                               else addin.get("mem_limit")))
        # parse result
        mem = ui
        # persist
        config["cpu_shares"][hostname][addin["container"]] = ui

        mem_eq={
            'b': lambda x: x,
            'k': lambda x: x << 10,
            'm': lambda x: x << 20,
            'g': lambda x: x << 30,
            't': lambda x: x << 40,
        }
        addin["mem_limit"] = (mem_eq[mem[-1].lower()](int(mem[:-1])) if mem[-1].lower() in mem_eq else int(mem))
    if not addin.get("count"):
        addin["containers"] = [addin["container"]]
    else:
        # get user input
        ui = utils.user_param(config,
                              "Update number of containers for %s"%(addin["container"]),
                              addin["count"])
        # parse result
        addin["count"] = ui
        # persist
        config["count"][hostname][addin["container"]] = ui

        addin["containers"] = []
        count = int(addin["count"])
        i=0
        while i < count:
            addin["containers"].append("%s_%s"%(addin["container"],i+1))
            i += 1
    addin.pop("container")
    return addin