def get_container_meta(self, param, default, container_json): """Get the container metadata from the container""" confidx = "" if "config" in container_json: confidx = "config" elif "container_config" in container_json: confidx = "container_config" if container_json[confidx] and param in container_json[confidx]: if container_json[confidx][param] is None: pass elif (is_genstr(container_json[confidx][param]) and (isinstance(default, (list, tuple)))): return container_json[confidx][param].strip().split() elif (is_genstr(default) and (isinstance(container_json[confidx][param], (list, tuple)))): return " ".join(container_json[confidx][param]) elif (is_genstr(default) and (isinstance(container_json[confidx][param], dict))): return self._dict_to_str(container_json[confidx][param]) elif (isinstance(default, list) and (isinstance(container_json[confidx][param], dict))): return self._dict_to_list(container_json[confidx][param]) else: return container_json[confidx][param] return default
def _check_executable(self): """Check if executable exists and has execute permissions""" if self.opt["entryp"] and is_genstr(self.opt["entryp"]): self.opt["entryp"] = self.opt["entryp"].strip().split(' ') if isinstance(self.opt["entryp"], list): if self.opt["cmd"]: # and cmd cmd_args = self.opt["cmd"] self.opt["cmd"] = self.opt["entryp"] self.opt["cmd"].extend(cmd_args) # cmd=args entryp else: self.opt["cmd"] = self.opt["entryp"] if not self.opt["cmd"]: self.opt["cmd"] = Config.conf['cmd'] Msg().err("Warning: no command assuming:", self.opt["cmd"], l=Msg.WAR) exec_name = self.opt["cmd"][0] # exec pathname without args if exec_name.startswith("./") or exec_name.startswith("../"): exec_name = self.opt["cwd"] + '/' + exec_name path = self.opt["env"].getenv("PATH") # DEBUG exec_name = FileUtil(exec_name).find_exec(path, self.container_root, self.opt["vol"], self.opt["cwd"]) if exec_name: return self.container_root + '/' + exec_name Msg().err("Error: command not found or has no execute bit set: ", self.opt["cmd"]) return ""
def _get_mirrors(self, mirrors): """Get shuffled list of tarball mirrors""" if is_genstr(mirrors): mirrors = mirrors.split(' ') try: random.shuffle(mirrors) except NameError: pass return mirrors
def is_container_id(self, obj): """Verify if the provided object matches the format of a local container id. """ if not is_genstr(obj): return False match = re.match("^[a-z0-9]+-[a-z0-9]+-[a-z0-9]+-[a-z0-9]+-[a-z0-9]+$", obj) if match: return True return False
def cmd_has_option(self, executable, search_option, arg=None): """Check if executable has a given cli option""" if not executable: return False arg_list = [] if arg and is_genstr(arg): arg_list = [arg] elif isinstance(arg, list): arg_list = arg out = Uprocess().get_output([executable] + arg_list + ["--help"]) if out and search_option in re.split(r"[=|\*\[\]\n,; ]+", out): return True return False
def find_exec(self, path="", rootdir="", volumes="", workdir="", cont2host=False): """Find an executable pathname""" if not path: path = os.getenv("PATH") + ":" + Config.conf['root_path'] if rootdir: rootdir += "/" if is_genstr(path): if "=" in path: path = "".join(path.split("=", 1)[1:]) path = path.split(":") if not isinstance(path, (list, tuple)): return "" return self._find_exec(path, rootdir, volumes, workdir, cont2host)
def _mkcurlcmd(self, *args, **kwargs): """Prepare curl command line according to invocation options""" self._files["url"] = str(args[0]) if "follow" in kwargs and kwargs["follow"]: self._opts["follow"] = ["-L"] if "post" in kwargs: self._opts["post"] = [ "-X", "POST", "-H", "Content-Type: application/json" ] self._opts["post"] += ["-d", json.dumps(kwargs["post"])] if "ctimeout" in kwargs: self._opts["ctimeout"] = [ "--connect-timeout", str(kwargs["ctimeout"]) ] if "timeout" in kwargs: self._opts["timeout"] = ["-m", str(kwargs["timeout"])] if "proxy" in kwargs and kwargs["proxy"]: self._opts["proxy"] = ["--proxy", str(kwargs["proxy"])] elif self.http_proxy: self._opts["proxy"] = ["--proxy", self.http_proxy] if "header" in kwargs: for header_item in kwargs["header"]: if str(header_item).startswith("Authorization: Bearer"): if "Signature=" in self._files["url"]: continue if "redirect" in kwargs: continue self._opts["header"] += ["-H", str(header_item)] if 'v' in kwargs and kwargs['v']: self._opts["verbose"] = ["-v"] if "nobody" in kwargs and kwargs["nobody"]: self._opts["nobody"] = ["--head"] if "ofile" in kwargs: FileUtil(self._files["output_file"]).remove() self._files["output_file"] = kwargs["ofile"] + ".tmp" self._opts["timeout"] = ["-m", str(self.download_timeout)] if "resume" in kwargs and kwargs["resume"]: self._opts["resume"] = ["-C", "-"] cmd = ["curl"] if self._curl_exec and is_genstr(self._curl_exec): cmd = [self._curl_exec] for opt in self._opts.values(): cmd += opt cmd.extend([ "-D", self._files["header_file"], "-o", self._files["output_file"], "--stderr", self._files["error_file"], self._files["url"] ]) return cmd
def find_inpath(self, filename, path, rootdir=""): """Find file in a path set such as PATH=/usr/bin:/bin""" if not (filename and path): return "" basename = os.path.basename(filename) if is_genstr(path): if "=" in path: path = "".join(path.split("=", 1)[1:]) path = path.split(":") if isinstance(path, (list, tuple)): for directory in path: full_path = rootdir + directory + "/" + basename if os.path.lexists(full_path): return directory + "/" + basename return "" return ""
def _validate_user_str(self, user): """Parse string with uid:gid or username""" user_id = dict() if not is_genstr(user): return user_id if re.match("^[a-zA-Z_][a-zA-Z0-9_-]*$", user): user_id["user"] = user return user_id match = re.match("^(\\d+)(:(\\d+)){0,1}$", user) if match: user_id["uid"] = match.group(1) if match.group(3): user_id["gid"] = match.group(3) return user_id
def get_pair(envstr): """Split env=var into key and val""" if not (is_genstr(envstr) and envstr): return ("", "") if '=' in envstr: try: (key, val) = envstr.split('=', 1) except (ValueError, NameError, AttributeError): return ("", "") key = key.strip() val = val.strip() #for quote in ("'", '"'): # if quote == val[0]: # val = val.strip(quote) # break else: key = envstr.strip() val = os.getenv(envstr, "") if not key or ' ' in key or key[0] in string.digits: return ("", "") return (key, val)
def get_patch_last_path(self): """get last host pathname to the patched container""" last_path = FileUtil(self._container_patch_path).getdata('r') if last_path and is_genstr(last_path): return last_path.strip() return ""
def select_fakechroot_so(self): """Select fakechroot sharable object library""" image_list = [] if Config.conf['fakechroot_so']: if isinstance(Config.conf['fakechroot_so'], list): image_list = Config.conf['fakechroot_so'] elif is_genstr(Config.conf['fakechroot_so']): image_list = [ Config.conf['fakechroot_so'], ] if "/" in Config.conf['fakechroot_so']: if os.path.exists(Config.conf['fakechroot_so']): return os.path.realpath(Config.conf['fakechroot_so']) elif os.path.exists(self.container_dir + "/libfakechroot.so"): return self.container_dir + "/libfakechroot.so" else: lib = "libfakechroot" deflib = "libfakechroot.so" image_list = [ deflib, ] guest = OSInfo(self.container_root) arch = guest.arch() (distro, version) = guest.osdistribution() if "Alpine" not in distro: version = version.split(".")[0] if arch == "amd64": image_list = [ "%s-%s-%s-x86_64.so" % (lib, distro, version), "%s-%s-x86_64.so" % (lib, distro), "%s-x86_64.so" % (lib), deflib ] elif arch == "i386": image_list = [ "%s-%s-%s-x86.so" % (lib, distro, version), "%s-%s-x86.so" % (lib, distro), "%s-x86.so" % (lib), deflib ] elif arch == "arm64": image_list = [ "%s-%s-%s-arm64.so" % (lib, distro, version), "%s-%s-arm64.so" % (lib, distro), "%s-arm64.so" % (lib), deflib ] elif arch == "arm": image_list = [ "%s-%s-%s-arm.so" % (lib, distro, version), "%s-%s-arm.so" % (lib, distro), "%s-arm.so" % (lib), deflib ] f_util = FileUtil(self.localrepo.libdir) fakechroot_so = f_util.find_file_in_dir(image_list) if not os.path.exists(fakechroot_so): Msg().err("Error: no libfakechroot found", image_list) sys.exit(1) Msg().out("Info: fakechroot_so:", fakechroot_so, l=Msg.DBG) return fakechroot_so