Example #1
0
 def get_gid(self):
     self.gid = self.group
     if is_string(self.gid):
         try:
             info = grp.getgrnam(self.gid)
             self.gid = info[2]
         except:
             pass
Example #2
0
 def get_uid(self):
     self.uid = self.user
     if is_string(self.uid):
         try:
             info = pwd.getpwnam(self.uid)
             self.uid = info[2]
         except:
             pass
Example #3
0
 def gid(self):
     gid = self.group
     if is_string(gid):
         try:
             info = grp.getgrnam(gid)
             gid = info[2]
         except:
             pass
     return gid
Example #4
0
 def uid(self):
     uid = self.user
     if is_string(uid):
         try:
             info = pwd.getpwnam(uid)
             uid = info[2]
         except:
             pass
     return uid
Example #5
0
 def format_rule_val(self, val):
     if is_string(val):
         try:
             tmp = json.loads(val)
             val = json.dumps(tmp)
         except Exception as e:
             pass
         if six.PY2:
             val = val.encode("utf-8")
     else:
         val = str(val)
     return val
Example #6
0
 def get_valid_actions(self, section, action):
     """
     Given a section and an action prefix, return the list of
     valid actions
     """
     valid_actions = []
     for candidate_action in sorted(self.actions[section]):
         if is_string(action) and \
            not candidate_action.startswith(action):
             continue
         if isinstance(action, list) and candidate_action not in action:
             continue
         valid_actions.append(candidate_action)
     return valid_actions
Example #7
0
 def gce_auth(self):
     cmd = ["gcloud", "auth", "list", "--format", "json"]
     out, err, ret = justcall(cmd)
     if ret != 0:
         return False
     self.log.debug(out)
     data = json.loads(out)
     if "active_account" not in data:
         return False
     if not is_string(data["active_account"]):
         return False
     if len(data["active_account"]) == 0:
         return False
     return True
Example #8
0
File: cfg.py Project: sghf/opensvc
 def _add_key(self, key, data):
     if not key:
         raise ex.excError("configuration key name can not be empty")
     if not data:
         raise ex.excError("configuration value can not be empty")
     if not is_string(data):
         data = "base64:"+bdecode(base64.urlsafe_b64encode(data))
     elif "\n" in data:
         data = "base64:"+bdecode(base64.urlsafe_b64encode(bencode(data)))
     else:
         data = "literal:"+data
     self.set_multi(["data.%s=%s" % (key, data)])
     self.log.info("configuration key '%s' added (%s)", key, print_size(len(data), compact=True, unit="b"))
     # refresh if in use
     self.postinstall(key)
Example #9
0
    def _info(self):
        """
        Contribute app resource standard and script-provided key/val pairs
        to the service's resinfo.
        """
        import re
        keyvals = [
            ["script", self.script if self.script else ""],
            ["start", str(self.start_seq) if self.start_seq else ""],
            ["stop", str(self.stop_seq) if self.stop_seq else ""],
            ["check", str(self.check_seq) if self.check_seq else ""],
            ["info", str(self.info_seq) if self.info_seq else ""],
            ["timeout", str(self.timeout) if self.timeout else ""],
            [
                "start_timeout",
                str(self.start_timeout) if self.start_timeout else ""
            ],
            [
                "stop_timeout",
                str(self.stop_timeout) if self.stop_timeout else ""
            ],
            [
                "check_timeout",
                str(self.check_timeout) if self.check_timeout else ""
            ],
            [
                "info_timeout",
                str(self.info_timeout) if self.info_timeout else ""
            ],
        ]
        try:
            cmd = self.get_cmd("info")
        except (ex.excAbortAction, AttributeError):
            return keyvals

        buff = self.run('info', cmd, dedicated_log=False, return_out=True)
        if not is_string(buff) or len(buff) == 0:
            keyvals.append(["Error", "info not implemented in launcher"])
            return keyvals
        for line in buff.splitlines():
            try:
                idx = line.find(':')
            except ValueError:
                continue
            elements = line.split(":", 1)
            keyvals.append([elements[0].strip(), elements[1].strip()])
        return keyvals
Example #10
0
 def eval_condition(self, jsonpath_expr, oper, val, data):
     for match in jsonpath_expr.find(data):
         if oper is None:
             if match.value:
                 return True
             else:
                 continue
         obj_class = type(match.value)
         try:
             if obj_class == bool:
                 val = convert_boolean(val)
             else:
                 val = obj_class(val)
         except Exception as exc:
             raise ex.excError("can not convert to a common type")
         if oper is None:
             if match.value:
                 return True
         if oper == "=":
             if match.value == val:
                 return True
         elif oper == ">":
             if match.value > val:
                 return True
         elif oper == "<":
             if match.value < val:
                 return True
         elif oper == ">=":
             if match.value >= val:
                 return True
         elif oper == "<=":
             if match.value <= val:
                 return True
         elif is_string(match.value) and oper == "~":
             if re.match(val, match.value):
                 return True
         elif oper == " in ":
             try:
                 l = json.loads(val)
             except:
                 l = val.split(",")
             if match.value in l:
                 return True
     return False
Example #11
0
 def create_volume(self):
     volume = factory("vol")(name=self.r.volname,
                             namespace=self.r.svc.namespace,
                             node=self.r.svc.node)
     if volume.exists():
         self.r.log.info("volume %s already exists", self.r.volname)
         data = volume.print_status_data(mon_data=True)
         if not data or "cluster" not in data:
             return volume
         if not self.r.svc.node.get_pool(volume.pool):
             raise ex.excError("pool %s not found on this node" %
                               volume.pool)
         if self.r.svc.options.leader and volume.topology == "failover" and \
            (self.owned() or not self.claimed(volume)) and \
            data["avail"] != "up" and data["cluster"]["avail"] == "up":
             self.r.log.info(
                 "volume %s is up on peer, we are leader: take it over",
                 self.r.volname)
             volume.action("takeover", options={"wait": True, "time": 60})
         return volume
     elif not self.r.svc.options.leader:
         self.r.log.info(
             "volume %s does not exist, we are not leader: wait its propagation",
             self.r.volname)
         self.r.wait_for_fn(
             lambda: volume.exists(), 10, 1,
             "non leader instance waited for too long for the "
             "volume to appear")
         return volume
     pooltype = self.r.oget("type")
     self.r.log.info(
         "create new volume %s (pool name: %s, pool type: %s, "
         "access: %s, size: %s, format: %s, shared: %s)", self.r.volname,
         self.r.pool, pooltype, self.r.access,
         print_size(self.r.size, unit="B",
                    compact=True), self.r.format, self.r.shared)
     pool = self.r.svc.node.find_pool(poolname=self.r.pool,
                                      pooltype=pooltype,
                                      access=self.r.access,
                                      size=self.r.size,
                                      fmt=self.r.format,
                                      shared=self.r.shared)
     if pool is None:
         raise ex.excError("could not find a pool matching criteria")
     pool.log = self.r.log
     try:
         nodes = self.r.svc._get("DEFAULT.nodes")
     except ex.OptNotFound:
         nodes = None
     env = {}
     for mapping in pool.volume_env:
         try:
             src, dst = mapping.split(":", 1)
         except Exception:
             continue
         args = src.split(".", 1)
         val = self.r.svc.oget(*args)
         if val is None:
             raise ex.excError("missing mapped key in %s: %s" %
                               (self.r.svc.path, mapping))
         if is_string(val) and ".." in val:
             raise ex.excError(
                 "the '..' substring is forbidden in volume env keys: %s=%s"
                 % (mapping, val))
         env[dst] = val
     pool.configure_volume(volume,
                           fmt=self.r.format,
                           size=self.r.size,
                           access=self.r.access,
                           nodes=nodes,
                           shared=self.r.shared,
                           env=env)
     volume = factory("vol")(name=self.r.volname,
                             namespace=self.r.svc.namespace,
                             node=self.r.svc.node)
     return volume
Example #12
0
def format_default(d):
    from rcPrintTable import print_table_default
    if "error" in d and is_string(d["error"]):
        print(d["error"], file=sys.stderr)
    print_table_default(d)
Example #13
0
    def sched_get_schedule(self, section, option, schedules=None):
        """
        Return the list of schedule structures for the spec string passed
        as <schedules> or, if not passed, from the <section>.<option> value
        in the configuration file.
        """
        if schedules is None:
            schedules = self.sched_get_schedule_raw(section, option)
        try:
            schedules = json.loads(schedules)
        except:
            pass
        if is_string(schedules):
            schedules = [schedules]

        data = []
        for schedule in schedules:
            schedule_orig = schedule
            schedule = schedule.strip()
            if len(schedule) == 0:
                continue
            if schedule.startswith("!"):
                exclude = True
                schedule = schedule[1:].strip()
            else:
                exclude = False
            if len(schedule) == 0:
                continue
            elements = schedule.split()
            ecount = len(elements)
            if ecount == 1:
                _data = {
                    "timeranges":
                    self._sched_parse_timerange(elements[0], section=section),
                    "day":
                    "*",
                    "week":
                    "*",
                    "month":
                    "*",
                }
            elif ecount == 2:
                _tr, _day = elements
                _data = {
                    "timeranges": self._sched_parse_timerange(_tr,
                                                              section=section),
                    "day": _day,
                    "week": "*",
                    "month": "*",
                }
            elif ecount == 3:
                _tr, _day, _week = elements
                _data = {
                    "timeranges": self._sched_parse_timerange(_tr,
                                                              section=section),
                    "day": _day,
                    "week": _week,
                    "month": "*",
                }
            elif ecount == 4:
                _tr, _day, _week, _month = elements
                _data = {
                    "timeranges": self._sched_parse_timerange(_tr,
                                                              section=section),
                    "day": _day,
                    "week": _week,
                    "month": _month,
                }
            else:
                raise SchedSyntaxError(
                    "invalid number of element, '%d' not in "
                    "(1, 2, 3, 4)" % ecount)
            _data["exclude"] = exclude
            _data["raw"] = schedule_orig
            data.append(_data)
        return data
Example #14
0
 def get_uid(self):
     if is_string(self.user):
         info = pwd.getpwnam(self.user)
         self.uid = info[2]
     else:
         self.uid = int(self.user)
Example #15
0
 def get_gid(self):
     if is_string(self.group):
         info = grp.getgrnam(self.group)
         self.gid = info[2]
     else:
         self.gid = int(self.group)