def _unpack_play_application(self, extension): service_data = self.service_data microservice_path = self.context.application.workspace + service_data[ "location"] + "/target/" force_chdir(microservice_path) microservice_filename = service_data["binary"]["artifact"] + extension unpacked_dir = SmPlayService.unzipped_dir_path( self.context, service_data["location"]) remove_folder_if_exists(unpacked_dir) os.makedirs(unpacked_dir) if extension == ".zip": self._unzip_play_application(microservice_filename, unpacked_dir) elif extension == ".tgz": self._untar_play_application(microservice_filename, unpacked_dir) else: print "ERROR: unsupported atrifact extension: " + extension folder = [ name for name in os.listdir(unpacked_dir) if os.path.isdir(os.path.join(unpacked_dir, name)) ][0] target_dir = unpacked_dir + "/" + service_data["binary"][ "destinationSubdir"] shutil.move(unpacked_dir + "/" + folder, target_dir) return target_dir
def start_from_sources(self): sbt_extra_params = self.sbt_extra_params() service_data = self.context.service_data(self.service_name) microservice_path = self.context.application.workspace + service_data[ "location"] force_chdir(microservice_path) env_copy = os.environ.copy() env_copy["SBT_EXTRA_PARAMS"] = " ".join( sbt_extra_params) # TODO: not needed i think anymore... makedirs_if_not_exists("logs") with open("logs/stdout.txt", "wb") as out, open("logs/stderr.txt", "wb") as err: process = Popen(self.get_start_command("SOURCE"), env=env_copy, stdout=out, stderr=err, stdin=subprocess.PIPE) process.stdin.close() if process.returncode == 1: print b.fail + "ERROR: could not start '" + self.service_name + "' " + b.endc return process.pid # Note: This is the parent pid
def _start_from_binary(self): assets_target_path = self.context.get_microservice_target_path(self.service_name) assets_path = self.context.application.workspace + self.service_data["location"] force_chdir(assets_path) remove_if_exists("RUNNING_FROM") force_chdir(assets_target_path) if not self.context.offline: nexus = SmNexus(self.context, self.service_name) if self.version: versions = [ self.version ] elif self.context.assets_versions: versions = self.context.assets_versions self.log("Starting assets versions: %s" % (", ".join(versions))) else: versions = nexus.get_all_versions(self.run_from) for version in versions: nexus.download_jar_if_necessary(self.run_from, version) self._unzip_assets(versions) cmd_with_params = self.service_data["binary"]["cmd"] makedirs_if_not_exists("logs") with open("logs/stdout.txt", "wb") as out, open("logs/stderr.txt", "wb") as err: return subprocess.Popen(cmd_with_params[0].split(), shell=False, env=os.environ.copy(), stdout=out, stderr=err, close_fds=True).pid
def start_from_binary(self): microservice_target_path = self.context.get_microservice_target_path(self.service_name) force_chdir(microservice_target_path) binaryConfig = self.service_data["binary"] if not self.context.offline: artifactRepo = SmArtifactRepoFactory.get_repository(self.context, self.service_name, binaryConfig) if not self.version: self.version = artifactRepo.find_latest_version(self.run_from, binaryConfig["artifact"], binaryConfig["groupId"]) artifactRepo.download_jar_if_necessary(self.run_from, self.version) unzip_dir = self._unpack_play_application(SmArtifactRepoFactory.get_play_app_extension(binaryConfig)) parent, _ = os.path.split(unzip_dir) force_pushdir(parent) if "frontend" in self.service_data and self.service_data["frontend"]: assets_versions = self._get_assets_version(unzip_dir) self.context.assets_versions_to_start(assets_versions) cmd_with_params = self.get_start_command("BINARY") if os.path.exists(cmd_with_params[0]): os.chmod(cmd_with_params[0], stat.S_IRWXU) else: print b.fail + "ERROR: unable to chmod on non existent file '" + parent + cmd_with_params[0] + "'" + b.endc makedirs_if_not_exists("logs") print(cmd_with_params) with open("logs/stdout.txt", "wb") as out, open("logs/stderr.txt", "wb") as err: popen_output = Popen(cmd_with_params, env=os.environ.copy(), stdout=out, stderr=err, close_fds=True) if popen_output.returncode == 1: print b.fail + "ERROR: could not start '" + self.service_name + "' " + b.endc return popen_output.pid
def _start_from_binary(self): assets_target_path = self.context.get_microservice_target_path( self.service_name) assets_path = self.context.application.workspace + self.service_data[ "location"] force_chdir(assets_path) remove_if_exists("RUNNING_FROM") force_chdir(assets_target_path) if not self.context.offline: nexus = SmNexus(self.context, self.service_name) versions = nexus.get_all_versions(self.version, self.run_from) for version in versions: nexus.download_jar_if_necessary(self.run_from, version) self._unzip_assets(versions) cmd_with_params = self.service_data["binary"]["cmd"] makedirs_if_not_exists("logs") with open("logs/stdout.txt", "wb") as out, open("logs/stderr.txt", "wb") as err: return subprocess.Popen(cmd_with_params[0].split(), shell=False, env=os.environ.copy(), stdout=out, stderr=err, close_fds=True).pid
def start_from_binary(self): microservice_target_path = self.context.get_microservice_target_path( self.service_name) force_chdir(microservice_target_path) binaryConfig = self.service_data["binary"] if not self.context.offline: artifactRepo = SmArtifactRepoFactory.get_repository( self.context, self.service_name, binaryConfig) if not self.version: self.version = artifactRepo.find_latest_version( self.run_from, binaryConfig["artifact"], binaryConfig["groupId"]) artifactRepo.download_jar_if_necessary(self.run_from, self.version) unzip_dir = self._unpack_play_application( SmArtifactRepoFactory.get_play_app_extension(binaryConfig)) parent, _ = os.path.split(unzip_dir) force_chdir(parent) if "frontend" in self.service_data and self.service_data["frontend"]: assets_versions = self._get_assets_version(unzip_dir) self.context.assets_versions_to_start(assets_versions) cmd_with_params = self.get_start_command("BINARY") if os.path.exists(cmd_with_params[0]): os.chmod(cmd_with_params[0], stat.S_IRWXU) else: self.context.log(b.fail + "ERROR: unable to chmod on non existent file '" + parent + cmd_with_params[0] + "'" + b.endc) makedirs_if_not_exists("logs") self.context.log( "Starting %s with parameters %s" % (self.service_name, cmd_with_params), True) with open("logs/stdout.txt", "wb") as out, open("logs/stderr.txt", "wb") as err: popen_output = Popen(cmd_with_params, env=os.environ.copy(), stdout=out, stderr=err, close_fds=True) if popen_output.returncode == 1: self.context.log(b.fail + "ERROR: could not start '" + self.service_name + "' " + b.endc) else: self.context.log("'%s' version '%s' started successfully" % (self.service_name, self.version)) return popen_output.pid
def _start_from_binary(self): assets_target_path = self.context.get_microservice_target_path( self.service_name) assets_path = self.context.application.workspace + self.service_data[ "location"] force_chdir(assets_path) remove_if_exists("RUNNING_FROM") force_chdir(assets_target_path) if not self.context.offline: artifactory = SmArtifactory(self.context, self.service_name) if self.version: versions = [self.version] elif self.context.assets_versions: versions = self.context.assets_versions self.log( "Starting assets versions: %s" % (", ".join(versions)), True) else: versions = artifactory.find_all_versions(self.run_from) self.log( "Starting assets versions: %s" % (", ".join(versions)), True) for version in versions: self.context.log("\nStarting assets version: %s" % version) artifactory.download_jar_if_necessary(self.run_from, version) self._unzip_assets(versions) cmd_with_params = self.service_data["binary"]["cmd"] if _is_python_3(): py3cmd = self.service_data["binary"].get("py3_cmd") if py3cmd is not None: cmd_with_params = py3cmd self.log("starting %s..." % cmd_with_params) makedirs_if_not_exists("logs") with open("logs/stdout.txt", "wb") as out, open("logs/stderr.txt", "wb") as err: return subprocess.Popen( cmd_with_params[0].split(), shell=False, env=os.environ.copy(), stdout=out, stderr=err, close_fds=True, universal_newlines=True, ).pid
def start_from_sources(self): sbt_extra_params = self.sbt_extra_params() service_data = self.context.service_data(self.service_name) microservice_path = self.context.application.workspace + service_data["location"] force_chdir(microservice_path) env_copy = os.environ.copy() env_copy["SBT_EXTRA_PARAMS"] = " ".join(sbt_extra_params) # TODO: not needed i think anymore... makedirs_if_not_exists("logs") with open("logs/stdout.txt", "wb") as out, open("logs/stderr.txt", "wb") as err: process = Popen(self.get_start_command("SOURCE"), env=env_copy, stdout=out, stderr=err, stdin=subprocess.PIPE) process.stdin.close() if process.returncode == 1: print b.fail + "ERROR: could not start '" + self.service_name + "' " + b.endc return process.pid # Note: This is the parent pid
def _unzip_assets(self, versions): service_data = self.service_data assets_zip_path = self.context.get_microservice_target_path( self.service_name) force_chdir(assets_zip_path) unzipped_dir = "assets" if not os.path.exists(unzipped_dir): os.makedirs(unzipped_dir) for version in versions: zip_filename = service_data["binary"][ "artifact"] + "-" + version + ".zip" extracted_dir = version if not os.path.exists(assets_zip_path + "/assets/" + extracted_dir): os.makedirs(extracted_dir) zipfile.ZipFile(zip_filename, 'r').extractall(extracted_dir) shutil.move(extracted_dir, unzipped_dir) target_dir = unzipped_dir return target_dir
def start_from_binary(self): microservice_target_path = self.context.get_microservice_target_path(self.service_name) force_chdir(microservice_target_path) if not self.context.offline: nexus = SmNexus(self.context, self.service_name) nexus.download_jar_if_necessary(self.run_from, self.version) filename = self._get_jar_filename() service_data = self.service_data binary_data = service_data["binary"] if "configurationFile" in binary_data: configuration_file = binary_data["configurationFile"] else: print "ERROR: required config 'configurationFile' does not exist" return None binary_to_run = os.path.join(microservice_target_path, filename) if os.path.exists(binary_to_run): force_chdir(self.run_from) os.system("jar xvf " + microservice_target_path + filename + " " + configuration_file + " > /dev/null") force_chdir(microservice_target_path) cmd = self.get_start_command("BINARY") process = Popen(cmd, env=os.environ.copy(), stdout=SmDropwizardServiceStarter.DEV_NULL, stderr=SmDropwizardServiceStarter.DEV_NULL, close_fds=True) if process.returncode == 1: print b.fail + "ERROR: could not start '" + self.service_name + "' " + b.endc else: self.context.log("'%s' version '%s' started successfully" % (self.service_name, self.version)) return process.pid else: print "ERROR: the requested file: '" + binary_to_run + "' does not exist" return None
def _start_from_sources(self): service_data = self.context.service_data(self.service_name) assets_path = self.context.application.workspace + service_data[ "location"] cmd_with_params = self.service_data["sources"]["cmd"] force_chdir(assets_path) run_from_file = open("RUNNING_FROM", "w") run_from_file.write(self.run_from) run_from_file.close() makedirs_if_not_exists("logs") seconds_remaining = SmPythonServiceStarter.PROCESS_STARTUP_TIMEOUT_SECONDS with open("logs/stdout.txt", "wb") as out, open("logs/stderr.txt", "wb") as err: subprocess.Popen( cmd_with_params, shell=False, env=os.environ.copy(), stdout=out, stderr=err, close_fds=True, ) while seconds_remaining > 0 and not len( SmProcess.processes_matching("grunt")) > 0: time.sleep(1) seconds_remaining -= 1 if seconds_remaining < 10 or seconds_remaining % 5 == 0: self.log( "Waiting for Assets service to start: %s second%s before timeout" % (seconds_remaining, "s" if seconds_remaining > 1 else "")) if len(SmProcess.processes_matching("grunt")) == 1: process = SmProcess.processes_matching("grunt") for i, v in enumerate(process): return v.pid
def _unpack_play_application(self, extension): service_data = self.service_data microservice_path = self.context.application.workspace + service_data["location"] + "/target/" force_chdir(microservice_path) microservice_filename = service_data["binary"]["artifact"] + extension unpacked_dir = SmPlayService.unzipped_dir_path(self.context, service_data["location"]) remove_folder_if_exists(unpacked_dir) os.makedirs(unpacked_dir) if extension == ".zip": self._unzip_play_application(microservice_filename, unpacked_dir) elif extension == ".tgz": self._untar_play_application(microservice_filename, unpacked_dir) else: print "ERROR: unsupported atrifact extension: " + extension folder = [ name for name in os.listdir(unpacked_dir) if os.path.isdir(os.path.join(unpacked_dir, name)) ][0] target_dir = unpacked_dir + "/" + service_data["binary"]["destinationSubdir"] shutil.move(unpacked_dir + "/" + folder, target_dir) return target_dir
def start_from_binary(self): microservice_target_path = self.context.get_microservice_target_path( self.service_name) force_chdir(microservice_target_path) if not self.context.offline: nexus = SmNexus(self.context, self.service_name) nexus.download_jar_if_necessary(self.run_from, self.version) filename = self._get_jar_filename() service_data = self.service_data binary_data = service_data["binary"] if "configurationFile" in binary_data: configuration_file = binary_data["configurationFile"] else: print "ERROR: required config 'configurationFile' does not exist" return None binary_to_run = os.path.join(microservice_target_path, filename) if os.path.exists(binary_to_run): force_chdir(self.run_from) os.system("jar xvf " + microservice_target_path + filename + " " + configuration_file + " > /dev/null") force_chdir(microservice_target_path) cmd = self.get_start_command("BINARY") process = Popen(cmd, env=os.environ.copy(), stdout=SmDropwizardServiceStarter.DEV_NULL, stderr=SmDropwizardServiceStarter.DEV_NULL, close_fds=True) if process.returncode == 1: print b.fail + "ERROR: could not start '" + self.service_name + "' " + b.endc else: self.context.log("'%s' version '%s' started successfully" % (self.service_name, self.version)) return process.pid else: print "ERROR: the requested file: '" + binary_to_run + "' does not exist" return None