def network_create_routed_bridge_config(self, name="default", nets=None): config = self.network_data(name, nets=nets)["config"] subnet = str(self.node_subnet(name, config=config)) network = config["network"] brip = self.network_bridge_ip(name, config=config).split("/")[0] cf = os.path.join(self.cni_config, name + ".conf") if os.path.exists(cf): return self.log.info("create %s", cf) conf = { "cniVersion": "0.3.0", "name": name, "type": "bridge", "bridge": "obr_" + name, "isGateway": True, "ipMasq": False, "ipam": { "type": "host-local", "subnet": subnet, "routes": [ { "dst": "0.0.0.0/0" }, { "dst": network, "gw": brip }, ] } } makedirs(self.cni_config) with open(cf, "w") as ofile: json.dump(conf, ofile, indent=4)
def __init__(self, logfile): logdir = os.path.dirname(logfile) makedirs(logdir) logging.handlers.RotatingFileHandler.__init__(self, logfile, maxBytes=1 * 5242880, backupCount=1)
def network_create_bridge_config(self, name="default", nets=None): cf = os.path.join(self.cni_config, name + ".conf") if os.path.exists(cf): return self.log.info("create %s", cf) conf = { "cniVersion": "0.3.0", "name": name, "type": "bridge", "bridge": "obr_" + name, "isGateway": True, "ipMasq": True, "ipam": { "type": "host-local", "routes": [{ "dst": "0.0.0.0/0" }] } } makedirs(self.cni_config) data = self.network_data(name, nets=nets) network = data["config"]["network"] conf["ipam"]["subnet"] = network with open(cf, "w") as ofile: json.dump(conf, ofile, indent=4)
def provisioner_lxc_create(self): template_options = self.r.oget("template_options") cmd = ['lxc-create', '--name', self.r.name, "--dir", self.r.rootfs] if self.r.cf: cmd += ['-f', self.r.cf] if self.r.lxcpath: makedirs(self.r.lxcpath) cmd += self.r.lxcpath_args if not self.r.cf: cmd += ["-f", os.path.join(self.r.lxcpath, self.r.name, "config")] if self.template: cmd += ['--template', self.template] if template_options: cmd += ["--"] + template_options env = { "DEBIAN_FRONTEND": "noninteractive", "DEBIAN_PRIORITY": "critical", } for key in ("http_proxy", "https_proxy", "ftp_proxy", "rsync_proxy"): if key in os.environ: env[key] = os.environ[key] key = key.upper() if key in os.environ: env[key] = os.environ[key] mirror = self.r.oget("mirror") if mirror: env["MIRROR"] = mirror env["SECURITY_MIRROR"] = mirror security_mirror = self.r.oget("security_mirror") if security_mirror: env["SECURITY_MIRROR"] = security_mirror self.r.log.info(" ".join(cmd)) ret = self.r.lcall(cmd, env=env) if ret != 0: raise ex.excError
def install_dir_key(self, data, path): """ Install a key decoded data in the host's volatile storage. """ if path.endswith("/"): dirname = os.path.basename(data["path"]) dirpath = os.path.join(path.rstrip("/"), dirname, "") else: dirpath = path + "/" makedirs(dirpath) for key in data["keys"]: self.install_key(key, dirpath)
def setup_lxc(self): if self.check_lxc(): self.r.log.info("container is already created") return self.setup_lxc_config() with open(os.path.join(rcEnv.paths.pathlog, "%s.console.log"%self.r.name), "a+") as f: f.write("") cmd = ['lxc-create', '-n', self.r.name, '-f', self.config] if self.r.lxcpath: makedirs(self.r.lxcpath) cmd += self.r.lxcpath_args ret, out, err = self.r.vcall(cmd) if ret != 0: raise ex.excError
def network_create_weave_config(self, name="default", nets=None): cf = os.path.join(self.cni_config, name + ".conf") if os.path.exists(cf): return self.log.info("create %s", cf) data = self.network_data(name, nets=nets) network = data["config"]["network"] conf = { "cniVersion": "0.3.0", "name": name, "type": "weave-net", "ipam": { "subnet": network, }, } makedirs(self.cni_config) with open(cf, "w") as ofile: json.dump(conf, ofile, indent=4)
def network_create_loopback_config(self, name="lo", nets=None): cf = os.path.join(self.cni_config, name + ".conf") if os.path.exists(cf): return self.log.info("create %s", cf) conf = { "cniVersion": "0.3.0", "name": name, "type": "loopback", } makedirs(self.cni_config) data = self.network_data(name, nets=nets) try: network = data["config"]["network"] conf["ipam"]["subnet"] = network except KeyError: pass with open(cf, "w") as ofile: json.dump(conf, ofile, indent=4)
def install_file_key(self, key, vpath): """ Install a key decoded data in the host's volatile storage. """ # paranoid checks before rmtree()/unlink() if ".." in vpath: return data = self.decode_key(key) if data is None: raise ex.excError("no data in key %s" % key) if os.path.isdir(vpath): self.log.info("remove %s key %s directory at location %s", self.desc, key, vpath) shutil.rmtree(vpath) vdir = os.path.dirname(vpath) if os.path.isfile(vdir) or os.path.islink(vdir): self.log.info("remove %s key %s file at parent location %s", self.desc, key, vdir) os.unlink(vdir) makedirs(vdir) self.write_key(vpath, data, key=key)
def lxc(self, action): self.find_cf() outf = None if action == 'start': outf = '/var/tmp/svc_' + self.name + '_lxc_' + action + '.log' if self.capable("cgroup_dir"): cmd = [ "lxc-start", "-d", "-n", self.name, "-o", outf, "-s", "lxc.cgroup.dir=" + self.cgroup_dir ] else: cmd = ["lxc-start", "-d", "-n", self.name, "-o", outf] if self.cf: cmd += ['-f', self.cf] if self.lxcpath: makedirs(self.lxcpath) cmd += self.lxcpath_args elif action == 'stop': cmd = ['lxc-stop', '-n', self.name] cmd += self.lxcpath_args elif action == 'kill': cmd = ['lxc-stop', '--kill', '--name', self.name] cmd += self.lxcpath_args else: raise ex.excError("unsupported lxc action: %s" % action) def prex(): os.umask(0o022) begin = datetime.now() ret, _, _ = self.vcall(cmd, preexec_fn=prex) duration = datetime.now() - begin loginfo = '%s done in %s - ret %i' % (action, duration, ret) if outf is not None: loginfo += ' - logs in %s' % outf self.log.info(loginfo) if ret != 0: raise ex.excError
def find_cf(self): if self.cf is not None: self.cf, vol = self.replace_volname(self.cf, strict=False) return if self.lxcpath: d_lxc = self.lxcpath self.cf = os.path.join(d_lxc, self.name, 'config') return d_lxc = os.path.join('var', 'lib', 'lxc') # seen on debian squeeze : prefix is /usr, but containers' # config files paths are /var/lib/lxc/$name/config # try prefix first, fallback to other know prefixes prefixes = [ os.path.join(os.sep), os.path.join(os.sep, 'usr'), os.path.join(os.sep, 'usr', 'local') ] for prefix in [self.prefix ] + [p for p in prefixes if p != self.prefix]: cfg = os.path.join(prefix, d_lxc, self.name, 'config') if os.path.exists(cfg): cfg_d = os.path.dirname(cfg) makedirs(cfg_d) self.cf = cfg return # on Oracle Linux, config is in /etc/lxc cfg = os.path.join(os.sep, 'etc', 'lxc', self.name, 'config') if os.path.exists(cfg): self.cf = cfg return self.cf = None raise ex.excError("unable to find the container configuration file")
def start(self): if not self.has_it(): self.log.info("create flag %s", self.flag_f) makedirs(self.flag_d, mode=0o700) self.touch(self.flag_f) self.can_rollback = True
def install_flag(self): makedirs(os.path.dirname(self.flag_path)) with open(self.flag_path, "w"): pass