def get_service_list(self): service_list = [] (cmd_out, error) = run_shell_cmd(['/usr/bin/systemctl list-units --type=service --no-pager --plain --no-legend']) if error: raise Exception(error) for service in cmd_out.splitlines(): service_name = service.split()[0] service_file_path = os.path.join('/usr/lib/systemd/system/', service_name) service_list.append(Service(service_name, service_file_path)) return service_list
def get_service_list(self): service_list = [] (cmd_out, error) = run_shell_cmd(['chkconfig']) if error: raise Exception(error) records = cmd_out.splitlines() for record in records: service_name = record.split()[0] service_list.append( Service(service_name, "/etc/init.d/%s" % service_name)) return service_list
def make_wrapper_script(self, args, heap_lim_k, stack_lim_k): """Write the wrapper script. Separate for testing purposes. Returns a pair, the (unique) filename of the wrapper script, and the (unique) filename of where the (temporary) environment log will be written""" # Make a tempfile for the environment log fd, envlog_filename = tempfile.mkstemp(prefix="envlog-", suffix=".env") os.close(fd) # we just need the name lines = [ "#!%s" % DASH, "ENVLOG=`env`", # store in memory to avoid IO prior to benchmark "ulimit -d %s || exit $?" % heap_lim_k, "ulimit -s %s || exit $?" % stack_lim_k, " ".join(args), # quotes around ENVLOG required to have one var per-line "echo \"${ENVLOG}\" > %s" % envlog_filename, "exit $?", ] with NamedTemporaryFile(prefix="krunwrapper-", suffix=".dash", delete=False) as fh: wrapper_filename = fh.name debug("Writing out wrapper script to %s" % wrapper_filename) for line in lines: fh.write(line + "\n") debug("Wrapper script:\n%s" % ("\n".join(lines))) os.chmod(wrapper_filename, BaseVMDef.WRAPPER_SCRIPT_MODE) # Make the file R/W for both users. # We need root to transfer ownership to BENCHMARK_USER. os.chmod(envlog_filename, BaseVMDef.ENVLOG_MODE) if not self.platform.no_user_change: chown_args = self.platform.change_user_args("root") + \ ["chown", BENCHMARK_USER, envlog_filename] util.run_shell_cmd(" ".join(chown_args)) return wrapper_filename, envlog_filename
def main(): "" "" parser = argparse.ArgumentParser(description='A2C Converter for any given Application') parser.add_argument('-c', '--config', required=True, help='Configuration file for contenarization') parser.add_argument('-d', '--dockerendpoint', required=True, help='Dockerendpoint to be used for image creation') parser.add_argument('-i', '--imagename', required=True, help='Name of be assigned to created docekr image') parser.add_argument('-p', '--projectname', required=True, help='Name of the GCP Project') pargs = parser.parse_args() config = yaml.load(open(pargs.config, 'r')) excludes = config['excludes'] + config['volumes'] print "excluding directories %s" % " ".join(excludes) print "creating tar file" tarfilepath = os.path.join('/tmp', '%s.tar.gz' % pargs.imagename) """ # Filter out the volume and excludes path from tar def filter_files(tarinfo): if tarinfo.mode & stat.S_IRUSR == 0: print "Cannot read file %s. Ignoring!" % tarinfo.name return None if tarinfo.name in excludes: print "Excluding %s" % tarinfo.name return None print "Included %s" % tarinfo.name return tarinfo # Create tar from the given includes list of paths tarobject = tarfile.TarFile.gzopen(tarfilepath, 'w', compresslevel=1) for path in config['includes']: tarobject.add(path, filter=filter_files) tarobject.close() """ exclude_cmd = "" for path in excludes: exclude_cmd = exclude_cmd + " --exclude=%s" % path run_shell_cmd(['export GZIP=-1; tar -pczf %s --directory=/ %s /' % (tarfilepath, exclude_cmd)]) print "finished creating tar file" print "creating docker image" client = Client(base_url=pargs.dockerendpoint, timeout=10000) result = client.import_image(src=tarfilepath, repository=pargs.imagename) """ Result comes in the following format {"status":"sha256:7cf833972ae093a33e8ec1a14d2669f62574590511087e2fbf64958d6765e80c"} """ result = ast.literal_eval(result) imageid = result['status'].rsplit("sha256:")[1] print "image created with id %s" % imageid print "Collecting metadata for image" metadata = generate_metadata(config) print metadata filecontent = ''' FROM %s CMD ["%s"] LABEL %s ''' label = "" for key, value in metadata.items(): label = label + ' %s="%s"' % (key, value) filecontent = filecontent % (imageid, metadata['initpath'], label) dockerfile = BytesIO(filecontent.encode('utf-8')) print "Building image from metadata" tag = "us.gcr.io/%s/%s" % (pargs.projectname, pargs.imagename) response = [line for line in client.build(fileobj=dockerfile, tag=tag, rm=True)] print response