def gzip_text(text): contents = BytesIO() f = gzip.GzipFile(fileobj=contents, mode='wb') f.write(util.encode_text(text)) f.flush() f.close() return contents.getvalue()
def handle(name, cfg, cloud, log, _args): if "bootcmd" not in cfg: log.debug(("Skipping module named %s," " no 'bootcmd' key in configuration"), name) return with util.ExtendedTemporaryFile(suffix=".sh") as tmpf: try: content = util.shellify(cfg["bootcmd"]) tmpf.write(util.encode_text(content)) tmpf.flush() except Exception: util.logexc(log, "Failed to shellify bootcmd") raise try: env = os.environ.copy() iid = cloud.get_instance_id() if iid: env['INSTANCE_ID'] = str(iid) cmd = ['/bin/sh', tmpf.name] util.subp(cmd, env=env, capture=False) except Exception: util.logexc(log, "Failed to run bootcmd module %s", name) raise
def handle(name, cfg, cloud, log, _args): mycfg = cfg.get('random_seed', {}) seed_path = mycfg.get('file', '/dev/urandom') seed_data = mycfg.get('data', b'') seed_buf = BytesIO() if seed_data: seed_buf.write(_decode(seed_data, encoding=mycfg.get('encoding'))) # 'random_seed' is set up by Azure datasource, and comes already in # openstack meta_data.json metadata = cloud.datasource.metadata if metadata and 'random_seed' in metadata: seed_buf.write(util.encode_text(metadata['random_seed'])) seed_data = seed_buf.getvalue() if len(seed_data): log.debug("%s: adding %s bytes of random seed entropy to %s", name, len(seed_data), seed_path) util.append_file(seed_path, seed_data) command = mycfg.get('command', None) req = mycfg.get('command_required', False) try: env = os.environ.copy() env['RANDOM_SEED_FILE'] = seed_path handle_random_seed_command(command=command, required=req, env=env) except ValueError as e: log.warn("handling random command [%s] failed: %s", command, e) raise e
def install_drivers(cfg, pkg_install_func): if not isinstance(cfg, dict): raise TypeError("'drivers' config expected dict, found '%s': %s" % (type_utils.obj_name(cfg), cfg)) cfgpath = "nvidia/license-accepted" # Call translate_bool to ensure that we treat string values like "yes" as # acceptance and _don't_ treat string values like "nah" as acceptance # because they're True-ish nv_acc = util.translate_bool(util.get_cfg_by_path(cfg, cfgpath)) if not nv_acc: LOG.debug("Not installing NVIDIA drivers. %s=%s", cfgpath, nv_acc) return if not subp.which("ubuntu-drivers"): LOG.debug("'ubuntu-drivers' command not available. " "Installing ubuntu-drivers-common") pkg_install_func(["ubuntu-drivers-common"]) driver_arg = "nvidia" version_cfg = util.get_cfg_by_path(cfg, "nvidia/version") if version_cfg: driver_arg += ":{}".format(version_cfg) LOG.debug( "Installing and activating NVIDIA drivers (%s=%s, version=%s)", cfgpath, nv_acc, version_cfg if version_cfg else "latest", ) # Register and set debconf selection linux/nvidia/latelink = true tdir = temp_utils.mkdtemp(needs_exe=True) debconf_file = os.path.join(tdir, "nvidia.template") debconf_script = os.path.join(tdir, "nvidia-debconf.sh") try: util.write_file(debconf_file, NVIDIA_DEBCONF_CONTENT) util.write_file( debconf_script, util.encode_text(NVIDIA_DRIVER_LATELINK_DEBCONF_SCRIPT), mode=0o755, ) subp.subp([debconf_script, debconf_file]) except Exception as e: util.logexc(LOG, "Failed to register NVIDIA debconf template: %s", str(e)) raise finally: if os.path.isdir(tdir): util.del_dir(tdir) try: subp.subp(["ubuntu-drivers", "install", "--gpgpu", driver_arg]) except subp.ProcessExecutionError as exc: if OLD_UBUNTU_DRIVERS_STDERR_NEEDLE in exc.stderr: LOG.warning("the available version of ubuntu-drivers is" " too old to perform requested driver installation") elif "No drivers found for installation." in exc.stdout: LOG.warning("ubuntu-drivers found no drivers for installation") raise
def handle(name, cfg, cloud, log, _args): if "bootcmd" not in cfg: log.debug(("Skipping module named %s," " no 'bootcmd' key in configuration"), name) return validate_cloudconfig_schema(cfg, schema) with temp_utils.ExtendedTemporaryFile(suffix=".sh") as tmpf: try: content = util.shellify(cfg["bootcmd"]) tmpf.write(util.encode_text(content)) tmpf.flush() except Exception as e: util.logexc(log, "Failed to shellify bootcmd: %s", str(e)) raise try: env = os.environ.copy() iid = cloud.get_instance_id() if iid: env['INSTANCE_ID'] = str(iid) cmd = ['/bin/sh', tmpf.name] util.subp(cmd, env=env, capture=False) except Exception: util.logexc(log, "Failed to run bootcmd module %s", name) raise
def handle(name, cfg, cloud, log, _args): if "bootcmd" not in cfg: log.debug( "Skipping module named %s, no 'bootcmd' key in configuration", name) return validate_cloudconfig_schema(cfg, schema) with temp_utils.ExtendedTemporaryFile(suffix=".sh") as tmpf: try: content = util.shellify(cfg["bootcmd"]) tmpf.write(util.encode_text(content)) tmpf.flush() except Exception as e: util.logexc(log, "Failed to shellify bootcmd: %s", str(e)) raise try: env = os.environ.copy() iid = cloud.get_instance_id() if iid: env["INSTANCE_ID"] = str(iid) cmd = ["/bin/sh", tmpf.name] subp.subp(cmd, env=env, capture=False) except Exception: util.logexc(log, "Failed to run bootcmd module %s", name) raise
def _decode(data, encoding=None): if not data: return b'' if not encoding or encoding.lower() in ['raw']: return util.encode_text(data) elif encoding.lower() in ['base64', 'b64']: return base64.b64decode(data) elif encoding.lower() in ['gzip', 'gz']: return util.decomp_gzip(data, quiet=False, decode=None) else: raise IOError("Unknown random_seed encoding: %s" % (encoding))
def _decode(data, encoding=None): if not data: return b"" if not encoding or encoding.lower() in ["raw"]: return util.encode_text(data) elif encoding.lower() in ["base64", "b64"]: return base64.b64decode(data) elif encoding.lower() in ["gzip", "gz"]: return util.decomp_gzip(data, quiet=False, decode=None) else: raise IOError("Unknown random_seed encoding: %s" % (encoding))