示例#1
0
    def provisioner(self):
        if self.is_provisioned():
            self.r.log.info("already provisioned")
            return
        name = self.r.name
        vdev = self.r.oget("vdev")
        multihost = self.r.oget("multihost")
        create_options = self.r.oget("create_options")

        args = create_options
        args += [name]
        args += vdev
        args = drop_option("-m", args, drop_value=True)
        args = drop_option("-o", args, drop_value="cachefile")
        args = drop_option("-o", args, drop_value="multihost")
        args = [
            "-m",
            "legacy",
            "-o",
            "cachefile=" + self.r.zpool_cache,
        ] + args
        if multihost and rcEnv.sysname == "Linux":
            args = ["-o", "multihost=on"] + args
            self.r.zgenhostid()
        cmd = ["zpool", "create"] + args
        ret, _, _ = self.r.vcall(cmd)
        if ret != 0:
            raise ex.excError
        self.r.can_rollback = True
        self.r.svc.node.unset_lazy("devtree")
示例#2
0
 def full_options(self):
     if self.reset_options:
         options = self.options
     else:
         options = ["-HAXpogDtrlvx", "--stats", "--delete", "--force"] + self.options
     out = self.rsync_version()
     if 'no xattrs' in out:
         options = drop_option("-X", options)
     if 'no ACLs' in out:
         options = drop_option("-A", options)
     options += ["--timeout=%s" % self.timeout]
     return options
示例#3
0
 def volume_options(self, errors="raise"):
     if self.run_args is None:
         args = []
     else:
         args = [] + self.run_args
     volumes = []
     dsts = []
     for volarg in chain(get_options("-v", args),
                         get_options("--volume", args),
                         iter(self.volume_mounts)):
         elements = volarg.split(":")
         if not elements or len(elements) not in (2, 3):
             continue
         if elements[1] in dsts:
             raise ex.excError(
                 "different volume mounts use the same destination "
                 "mount point: %s" % elements[1])
         if not elements[0].startswith(os.sep):
             # vol service
             wants_ro = False
             elements[0], vol = self.replace_volname(elements[0],
                                                     strict=False,
                                                     errors=errors)
             try:
                 options = elements[2].split(",")
                 if 'ro' in options:
                     wants_ro = True
                 options = drop_option("ro", options)
                 options = drop_option("rw", options)
                 del elements[2]
             except Exception:
                 options = []
             if wants_ro or (vol and vol.volsvc.access.startswith("ro")):
                 options.insert(0, "ro")
             else:
                 options.insert(0, "rw")
             elements.append(",".join(options))
             volumes.append(":".join(elements))
         elif not os.path.exists(elements[0]):
             # host path
             raise ex.excError("source dir of mapping %s does not "
                               "exist" % volarg)
         else:
             volumes.append(volarg)
         dsts.append(elements[1])
     return volumes
示例#4
0
 def dns_opts(args):
     if not self.svc.node.dns or "--dns" in args:
         return []
     net = get_option("--net", args, boolean=False)
     if net and net.startswith("container:"):
         return []
     if net == "host":
         return []
     l = []
     for dns in self.svc.node.dns:
         l += ["--dns", dns]
     for search in self.dns_search():
         l += ["--dns-search", search]
     dns_options = [
         o for o in get_options(self.dns_option_option, args)
     ]
     args = drop_option(self.dns_option_option, args, drop_value=True)
     for o in self.dns_options(dns_options):
         l += [self.dns_option_option, o]
     return l
示例#5
0
    def _add_run_args(self, errors="raise"):
        if self.run_args is None:
            args = []
        else:
            args = [] + self.run_args

        args = drop_option("-d", args, drop_value=False)
        args = drop_option("--detach", args, drop_value=False)
        if self.detach:
            args += ["--detach"]

        # drop user specified --name. we set ours already
        args = drop_option("--name", args, drop_value=True)
        args = drop_option("-n", args, drop_value=True)
        args += ['--name=' + self.container_name]
        args += ['--label=' + self.container_label_id]

        args = drop_option("--hostname", args, drop_value=True)
        args = drop_option("-h", args, drop_value=True)
        if not self.netns or (self.netns != "host"
                              and not self.netns.startswith("container#")):
            # only allow hostname setting if the container has a private netns
            if self.vm_hostname:
                args += ["--hostname", self.vm_hostname]
            elif not self.run_args:
                pass
            else:
                hostname = get_option("--hostname",
                                      self.run_args,
                                      boolean=False)
                if not hostname:
                    hostname = get_option("-h", self.run_args, boolean=False)
                if hostname:
                    args += ["--hostname", hostname]

        if self.entrypoint:
            args = drop_option("--entrypoint", args, drop_value=True)
            args += ["--entrypoint", self.entrypoint]

        if self.netns:
            args = drop_option("--net", args, drop_value=True)
            args = drop_option("--network", args, drop_value=True)
            if self.netns.startswith("container#"):
                res = self.svc.get_resource(self.netns)
                if res is not None:
                    args += ["--net=container:" + res.container_name]
                elif errors == "raise":
                    raise ex.excError(
                        "resource %s, referenced in %s.netns, does not exist" %
                        (self.netns, self.rid))
            else:
                args += ["--net=" + self.netns]
        elif not has_option("--net", args):
            args += ["--net=" + self.default_net]

        if self.pidns:
            args = drop_option("--pid", args, drop_value=True)
            if self.pidns.startswith("container#"):
                res = self.svc.get_resource(self.netns)
                if res is not None:
                    args += ["--pid=container:" + res.container_name]
                elif errors == "raise":
                    raise ex.excError(
                        "resource %s, referenced in %s.pidns, does not exist" %
                        (self.pidns, self.rid))
            else:
                args += ["--pid=" + self.pidns]

        if self.ipcns:
            args = drop_option("--ipc", args, drop_value=True)
            if self.ipcns.startswith("container#"):
                res = self.svc.get_resource(self.netns)
                if res is not None:
                    args += ["--ipc=container:" + res.container_name]
                elif errors == "raise":
                    raise ex.excError(
                        "resource %s, referenced in %s.ipcns, does not exist" %
                        (self.ipcns, self.rid))
            else:
                args += ["--ipc=" + self.ipcns]

        if self.utsns == "host":
            args = drop_option("--uts", args, drop_value=True)
            args += ["--uts=host"]

        if self.userns is not None:
            args = drop_option("--userns", args, drop_value=True)
        if self.userns == "host":
            args += ["--userns=host"]

        if self.privileged is not None:
            args = drop_option("--privileged", args, drop_value=False)
        if self.privileged:
            args += ["--privileged"]

        if self.interactive is not None:
            args = drop_option("--interactive", args, drop_value=False)
            args = drop_option("-i", args, drop_value=False)
        if self.interactive:
            args += ["--interactive"]

        if self.tty is not None:
            args = drop_option("--tty", args, drop_value=False)
            args = drop_option("-t", args, drop_value=False)
        if self.tty:
            args += ["--tty"]

        drop_option("--rm", args, drop_value=False)

        drop_option("-v", args, drop_value=True)
        drop_option("--volume", args, drop_value=True)
        for vol in self.volume_options(errors=errors):
            args.append("--volume=%s" % vol)

        drop_option("--device", args, drop_value=True)
        for dev in self.device_options(errors=errors):
            args.append("--device=%s" % dev)

        args += self.cgroup_options()

        def dns_opts(args):
            if not self.svc.node.dns or "--dns" in args:
                return []
            net = get_option("--net", args, boolean=False)
            if net and net.startswith("container:"):
                return []
            if net == "host":
                return []
            l = []
            for dns in self.svc.node.dns:
                l += ["--dns", dns]
            for search in self.dns_search():
                l += ["--dns-search", search]
            dns_options = [
                o for o in get_options(self.dns_option_option, args)
            ]
            args = drop_option(self.dns_option_option, args, drop_value=True)
            for o in self.dns_options(dns_options):
                l += [self.dns_option_option, o]
            return l

        args += dns_opts(args)
        args += self.environment_options()
        return args
示例#6
0
    def action(self, nodename, thr=None, **kwargs):
        options = self.parse_options(kwargs)

        if not options.cmd and not options.action:
            thr.log_request("node action ('action' not set)",
                            nodename,
                            lvl="error",
                            **kwargs)
            return {
                "status": 1,
            }

        for opt in ("node", "server", "daemon"):
            if opt in options.options:
                del options.options[opt]
        if options.action_mode and options.options.get("local"):
            if "local" in options.options:
                del options.options["local"]
        for opt, ropt in (("jsonpath_filter", "filter"), ):
            if opt in options.options:
                options.options[ropt] = options.options[opt]
                del options.options[opt]
        options.options["local"] = True
        pmod = __import__("nodemgr_parser")
        popt = pmod.OPT

        def find_opt(opt):
            for k, o in popt.items():
                if o.dest == opt:
                    return o
                if o.dest == "parm_" + opt:
                    return o

        if options.cmd:
            cmd = [] + options.cmd
            cmd = drop_option("--node", cmd, drop_value=True)
            cmd = drop_option("--server", cmd, drop_value=True)
            cmd = drop_option("--daemon", cmd)
            if options.action_mode and "--local" not in cmd:
                cmd += ["--local"]
        else:
            cmd = [options.action]
            for opt, val in options.options.items():
                po = find_opt(opt)
                if po is None:
                    continue
                if val == po.default:
                    continue
                if val is None:
                    continue
                opt = po._long_opts[0] if po._long_opts else po._short_opts[0]
                if po.action == "append":
                    cmd += [opt + "=" + str(v) for v in val]
                elif po.action == "store_true" and val:
                    cmd.append(opt)
                elif po.action == "store_false" and not val:
                    cmd.append(opt)
                elif po.type == "string":
                    opt += "=" + val
                    cmd.append(opt)
                elif po.type == "integer":
                    opt += "=" + str(val)
                    cmd.append(opt)
            fullcmd = rcEnv.python_cmd + [
                os.path.join(rcEnv.paths.pathlib, "nodemgr.py")
            ] + cmd

        thr.log_request("run '%s'" % " ".join(fullcmd), nodename, **kwargs)
        if options.sync:
            proc = Popen(fullcmd,
                         stdout=PIPE,
                         stderr=PIPE,
                         stdin=None,
                         close_fds=True)
            out, err = proc.communicate()
            result = {
                "status": 0,
                "data": {
                    "out": bdecode(out),
                    "err": bdecode(err),
                    "ret": proc.returncode,
                },
            }
        else:
            proc = Popen(fullcmd, stdin=None, close_fds=True)
            thr.push_proc(proc)
            result = {
                "status": 0,
                "info": "started node action %s" % " ".join(cmd),
            }
        return result