def decode(self): buff = self.decode_key(self.options.key) if buff is None: raise ex.Error("could not decode the secret key '%s'" % self.options.key) try: sys.stdout.buffer.write(buff) except (TypeError, AttributeError): # buff is not binary, .buffer is not supported sys.stdout.write(buff)
def state_changing_action(self, cmd, timeout=10): """ State changing action can be denied by a peer node during commits. This method implement a retry loop waiting for the action to be not-denied. """ self.log.info(" ".join(cmd)) for i in range(timeout): out, err, ret = justcall(cmd) if ret == 11: # cluster-wide drbd state change in-progress time.sleep(1) continue elif ret != 0: call_log(buff=err, log=self.log, level="error") raise ex.Error() call_log(buff=out, log=self.log, level="info") return out, err, ret raise ex.Error("timeout waiting for action non-denied by peer")
def get_volume_mappings(self, volume=None, **kwargs): params = {"full": 1} uri = "/lun-maps" if volume is None: raise ex.Error("--volume is mandatory") data = self.get_volumes(volume=volume) vol_name = data["content"]["name"] params["filter"] = "vol-name:eq:"+vol_name data = self.get(uri, params=params) return data
def purge_mountpoint(self): if self.mount_point is None: return if os.path.exists( self.mount_point) and not protected_dir(self.mount_point): self.log.info("rm -rf %s" % self.mount_point) try: shutil.rmtree(self.mount_point) except Exception as e: raise ex.Error(str(e))
def start(self): lockfile = os.path.join(Env.paths.pathlock, "disk.loop") if self.is_up(): self.log.info("%s is already up" % self.label) return try: with cmlock(timeout=30, delay=1, lockfile=lockfile): cmd = ['lofiadm', '-a', self.loopfile] ret, out, err = self.vcall(cmd) except Exception as exc: raise ex.Error(str(exc)) if ret != 0: raise ex.Error self.loop = utilities.devices.sunos.file_to_loop(self.loopfile) if len(self.loop) == 0: raise ex.Error("loop device did not appear or disappeared") time.sleep(1) self.log.info("%s now loops to %s" % (self.loop, self.loopfile)) self.can_rollback = True
def failoverestablish(self): cmd = [ '/usr/symcli/bin/symrdf', '-g', self.symdg, '-rdfg', str(self.rdfg), '-noprompt', 'failover', '-establish', '-i', '15', '-c', '4' ] (ret, out, err) = self.vcall(cmd) if ret != 0: raise ex.Error("Failed to run command %s" % ' '.join(cmd)) self.flush_cache()
def start(self): self.unset_lazy("containerid") self.unset_lazy("netns") try: with utilities.lock.cmlock(lockfile=self.lockfile, timeout=20): self.add_netns() self.add_cni() except utilities.lock.LOCK_EXCEPTIONS as exc: raise ex.Error("cni lock acquire: %s" % str(exc)) self.wait_dns_records()
def wait_for_fn(func, tmo, delay, errmsg="waited too long for startup"): """ A helper function to execute a test function until it returns True or the number of retries is exhausted. """ for tick in range(tmo // delay): if func(): return time.sleep(delay) raise ex.Error(errmsg)
def prefix(self): prefixes = [ os.path.join(os.sep), os.path.join(os.sep, 'usr'), os.path.join(os.sep, 'usr', 'local') ] for prefix in prefixes: if os.path.exists(os.path.join(prefix, 'bin', 'lxc-start')): return prefix raise ex.Error("lxc install prefix not found")
def provisioner(self): if not self.pvs: # lazy reference self.pvs = self.oget("pvs") if self.pvs is None: # lazy reference not resolvable raise ex.Error("%s.pvs value is not valid" % self.rid) self.pvs = self.pvs.split() l = [] for pv in self.pvs: if os.sep not in pv: _l = self.vxname_glob(pv) else: _l = self.sysname_glob(pv) if _l: self.log.info("expand %s to %s" % (pv, ', '.join(_l))) l += _l self.pvs = l if len(self.pvs) == 0: raise ex.Error("no pvs specified") for pv in self.pvs: if self.has_pv(pv): continue cmd = ["/opt/VRTS/bin/vxdisksetup", "-i", pv] ret, out, err = self.vcall(cmd) if ret != 0: raise ex.Error if self.prov_has_it(): self.log.info("vg %s already exists") return cmd = ["vxdg", "init", self.name] + self.pvs ret, out, err = self.vcall(cmd) if ret != 0: raise ex.Error self.can_rollback = True self.svc.node.unset_lazy("devtree")
def add_zvol(self, name=None, size=None, volume=None, compression="inherit", dedup="off", sparse=False, **kwargs): for key in ["name", "size", "volume"]: if locals()[key] is None: raise ex.Error("'%s' key is mandatory" % key) size = convert_size(size, _to="MiB") d = { "name": name, "volsize": str(size)+"MiB", "compression": compression, "sparse": sparse, "dedup": dedup, } buff = self.post('/storage/volume/%s/zvols/' % volume, d) try: return json.loads(buff) except ValueError: raise ex.Error(buff)
def add_diskinfo(self, data, size=None, storagepool=None): if self.node is None: return try: result = self.node.collector_rest_post( "/disks", { "disk_id": data["WWN"], "disk_devid": data["ID"], "disk_name": data["NAME"], "disk_size": convert_size(size, _to="MB"), "disk_alloc": 0, "disk_arrayid": self.name, "disk_group": storagepool, }) except Exception as exc: raise ex.Error(str(exc)) if "error" in data: raise ex.Error(result["error"]) return result
def fix_ownership_group(self, path): if self.group is None: return if self.gid is None: raise ex.Error("group %s does not exist" % str(self.group)) if self.gid and not self.check_gid(path): self.vcall(['chgrp', str(self.gid), path]) else: self.log.info("%s has correct group ownership (%s)" % (path, str(self.gid)))
def fix_ownership_user(self, path): if self.user is None: return if self.uid is None: raise ex.Error("user %s does not exist" % str(self.user)) if not self.check_uid(path): self.vcall(['chown', str(self.uid), path]) else: self.log.info("%s has correct user ownership (%s)" % (path, str(self.uid)))
def get_hostid(self): if self.hostid: return if self.prkey: self.hostid = self.prkey return try: self.hostid = self.svc.node.get_prkey() except Exception as e: raise ex.Error(str(e))
def startrcopygroup(self): data = self.showrcopy() if data['rcg']['Status'] == "Started": self.log.info("rcopy group %s is already started. skip startrcopygroup" % self.rcg) return if data['rcg']['Role'] != 'Primary': self.log.error("rcopy group %s role is not Primary. refuse to start rcopy" % self.rcg) raise ex.Error() self._cmd("startrcopygroup %s" % self.rcg, log=True) self.clear_caches()
def find_editor(): if "EDITOR" in os.environ: editor = os.environ["EDITOR"] elif os.name == "nt": editor = "notepad" else: editor = "vi" if not which(editor): raise ex.Error("%s not found" % editor) return editor
def load_context(): fpath = contexts_config_path() try: with open(fpath, "r") as ofile: data = json.load(ofile) except ValueError as exc: raise ex.Error("invalid context: %s: %s" % (fpath, str(exc))) except (IOError, OSError): data = {} return data
def export_zone_cfg(self): cfg_d = self.zone_cfg_dir() if not os.path.exists(cfg_d): os.makedirs(cfg_d) cfg = self.zone_cfg_path() cmd = [ZONECFG, "-z", self.name, "export", "-f", cfg] ret, out, err = self.vcall(cmd) if ret != 0 and not os.path.exists(cfg): raise ex.Error(err)
def push_diskinfo(self, data, name, size): if self.node is None: return try: ret = self.node.collector_rest_post( "/disks", { "disk_id": self.serial + "." + str(data["devNum"]), "disk_devid": data["devNum"], "disk_name": str(name), "disk_size": convert_size(size, _to="MB"), "disk_alloc": 0, "disk_arrayid": self.name, "disk_group": self.get_pool_by_id(data["dpPoolID"]), }) except Exception as exc: raise ex.Error(str(exc)) if "error" in data: raise ex.Error(ret["error"]) return ret
def check_configure_requirements(self): missing = [] if not self.size: missing.append("size") if missing: raise ex.Error( "missing provisioning keywords required to create " "the volume object: %s. if another svc is responsible " "for creating the volume add a parent relation." "" % ",".join(missing))
def add_diskinfo(self, data, size=None, volume=None): if self.node is None: return try: result = self.node.collector_rest_post( "/disks", { "disk_id": data["naa"].replace("0x", ""), "disk_devid": data["id"], "disk_name": data["name"], "disk_size": convert_size(size, _to="MB"), "disk_alloc": 0, "disk_arrayid": self.name, "disk_group": volume, }) except Exception as exc: raise ex.Error(str(exc)) if "error" in data: raise ex.Error(result["error"]) return result
def lookup(self, ip): if ip is None: raise ex.Error("the ip provisioning keyword is not set") try: int(ip[0]) # already in cidr form return except: pass try: a = socket.getaddrinfo(ip, None) if len(a) == 0: raise Exception ip = a[0][-1][0] return ip except: raise ex.Error("could not resolve %s to an ip address" % ip)
def get_mask(self, ifconfig=None): if ifconfig is None: ifconfig = self.get_ifconfig() if self.netmask is None: intf = ifconfig.interface(self.ipdev) if intf is None: raise ex.Error( "netmask parameter is mandatory with 'noalias' tag") self.netmask = intf.mask if not self.netmask: if "noaction" not in self.tags: self.netmask = None raise ex.Error("No netmask set on parent interface %s" % self.ipdev) if isinstance(self.netmask, list): try: self.netmask = self.netmask[0] except IndexError: self.netmask = None
def allocations(self): data = self.daemon_get_allocations() minors = set() ports = set() try: items = data["nodes"].items() except KeyError: raise ex.Error("unable to get current allocations: %s" % data.get("error")) for node, ndata in items: try: minors |= set(ndata["minors"]) ports |= set(ndata["ports"]) except KeyError: raise ex.Error("node %s invalid allocations report: %s" % (node, ndata.get("error", ""))) data = { "minors": sorted(list(minors)), "ports": sorted(list(ports)), } return data
def do_start(self): if self.uuid is None: raise ex.Error("uuid is not set") self.auto_assemble_disable() if self.is_up(): self.log.info("md %s is already assembled" % self.uuid) return 0 self.can_rollback = True self.assemble() self._create_static_name()
def naviseccli(cmd, scope=None, spa=None, spb=None, username=None, password=None): if which('/opt/Navisphere/bin/naviseccli') is None: raise ex.Error( 'can not find Navicli programs in usual /opt/Navisphere/bin') _cmd = ['/opt/Navisphere/bin/naviseccli', '-h', spa] _cmd += cmd out, err, ret = justcall(_cmd) if "Security file not found" in out: print(_cmd) print(out) raise ex.Error("naviseccli command execution error") return out, err
def container_id(self, refresh=False): if self.container.type in ("container.lxd", "container.lxc"): return self.container.name elif self.container.type in ("container.docker", "container.podman"): return self.container.lib.get_container_id(self.container, refresh=refresh) else: raise ex.Error("unsupported container %s type: %s" % ( self.container.rid, self.container.type, ))
def online_drive(self, driveindex): diskpart_file = diskpartfile_name(self) + '_online_disk_' + str(driveindex) with open(diskpart_file, 'w') as f: f.write("select disk=%s\n" % driveindex) f.write("online disk\n") f.write("exit\n") self.log.info("bring disk %s online" % driveindex) cmd = ['diskpart', '/s', diskpart_file] (ret, out, err) = self.vcall(cmd) if ret != 0: raise ex.Error("Failed to run command %s" % ' '.join(cmd))
def get_instance_id(self): if self.instance_id is not None: return self.instance_id try: import httplib except ImportError: raise ex.Error("the httplib module is required") c = httplib.HTTPConnection("instance-data") c.request("GET", "/latest/meta-data/instance-id") self.instance_id = c.getresponse().read() return self.instance_id