def execute(self, *args, **kwargs): cwd = os.getcwd() project_folder_path = re.findall(r"/src/(.*)", cwd) if len(project_folder_path) == 0: Logger.error("Please place the source code to GOPATH.") return {"project_folder": project_folder_path[0]} return {"project_folder": project_folder_path[0]}
def execute(self, context): if check_application_first_setup() is True: Logger.info( "Your application haven't been initialized,you can run `derrick init`." ) return if check_dockerfile_exists() is False: Logger.info( "Dockerfile is not exists, Maybe you can rerun `derrick init` to resolve it." ) return if is_windows() is True: try: import win32api win32api.WinExec('docker-compose up -d ') except Exception as e: Logger.error( "Can not start your application.Have you installed docker-compose in path?" ) return status = os.system("/bin/bash -i -c 'docker-compose up -d '") if status == 0: Logger.info( "Your application has been up to running! You can run `docker ps` to get exposed ports." ) else: Logger.error( "Can not start your application.Have you installed docker-compose in path?" )
def up(self, *args, **kwargs): need_build = True image_with_tag = ApplicationRecorder().get_record("image_with_tag") if image_with_tag is None: Logger.warnf( "Failed to find your docker image, You should build it by your self." ) need_build = False if is_windows() is True: try: import win32api if need_build is True: win32api.WinExec('docker build -t %s .' % image_with_tag) win32api.WinExec('kubectl apply -f kubernetes-deployment.yaml') except Exception as e: Logger.error( "Can not start your application.Have you installed kubelet in path?" ) return status = os.system("docker build -t %s ." % image_with_tag) if status != 0: Logger.info( "Failed to build docker image, Have you installed docker in path?" ) sys.exit(-1) status = os.system("kubectl apply -f kubernetes-deployment.yaml") if status == 0: Logger.info( "Your application has been up to running! You can run `kubelet get svc` to get exposed ports." ) else: Logger.error( "Can not start your application.Have you installed kubelet in path?" )
def execute(self, *args, **kwargs): try: output = subprocess.check_output(["python", "--version"], shell=False) Logger.debug("NodeJs version detected is %s" % output) version = PythonVersionDetector.get_most_relative_version(output) except: version = PYTHON_2 return {"version": version}
def execute(self): try: output = subprocess.check_output(["node", "--version"], shell=False) Logger.debug("NodeJs version detected is %s" % output) version = NodeVersionDetector.get_most_relative_version(output) except: version = NODEJS_8 return {"version": version}
def check_valid(y): if y["project_name"] == "" or y["base_image"] == "" or y[ "image_with_tag"] == "" or y["jar_file"] == "": Logger.error( "Failed to valid yaml file, " "required keys are project_name: %s base_image: %s image_with_tag: %s jar_file: %s" % y["project_name"], y["base_image"], y["image_with_tag"], y["jar_file"]) return False return True
def execute(self): print("Detecting Java version ...") version = default_version; try: output = subprocess.check_output(["java", "-version"], shell=False, stderr=subprocess.STDOUT) version = JavaVersionDetector.get_most_relative_version(output) except Exception as e: Logger.debug("Failed to detect Java version,because of %s" % e) Logger.debug("Use default Java version:%s instead ." % default_version) return {"version": version}
def load(self): with open(self.config_file, "a+") as f: content = f.read() if content is None or content is "" or len(content) is 0: pass else: try: json_dict = json.loads(content)[0]['datapoints'] self.unmarshal(json_dict) except Exception as e: Logger.debug("Failed to load derrick_conf,because of %s" % e)
def get_most_relative_version(version): try: version_section = version.split(" ")[1] if version_section.startswith("2") is True: return PYTHON_2 if version_section.startswith("3") is True: return PYTHON_3 except Exception as e: Logger.debug("Failed to detect Python version(%s) and use default Python version" % version) Logger.debug("Stacktrace is %s" % e) return PYTHON_2
def load(self): if check_derrick_first_setup() is True: try: self.pre_load() except Exception as e: Logger.error("Failed to create DERRICK_HOME:%s.Because of %s" % (get_derrick_home(), e)) # Logger.debug(traceback.format_exc()) return # Load rigging and commands in disk self.rm.load() self.cm.load()
def load(self): with open(self.config_file, "a+") as f: content = f.read() if content is None or content is "" or len(content) is 0: pass else: try: json_dict = json.loads(content)[0]['datapoints'] self.unmarshal(json_dict) except Exception as e: Logger.error("Failed to load .derrick_application_conf,because of %s" % e) Logger.debug("Stack Information:%s" % traceback.format_exec())
def load(self): if self.config_file is None: raise Exception("You should supply at least one config file.") with open(self.config_file, "r") as f: content = f.read() if content is None or content is "" or len(content) is 0: pass else: try: json_dict = json.loads(content) self.unmarshal(json_dict) except Exception as e: Logger.debug("Failed to load derrick_conf,because of %s" % e)
def run(self): try: self.load() except Exception as e: # TODO add some exception handler Logger.error("Failed to load rigging or commands in disk,because of %s" % e) commands_doc = self.cm.get_commands_doc() arguments = docopt(commands_doc, help=False, version=DERRICK_VERSION) if DEBUG_MODE in arguments and arguments[DEBUG_MODE] == 1: Logger.set_debug_mode() command_context = self.init_commands_context(arguments=arguments) self.cm.run_commands(command_context)
def get_most_relative_version(version): try: version_section = version.split(" ")[1] if version_section.startswith("2") is True: return PYTHON_2 if version_section.startswith("3") is True: return PYTHON_3 except Exception as e: Logger.debug( "Failed to detect Python version(%s) and use default Python version" % version) Logger.debug("Stacktrace is %s" % e) return PYTHON_2
def execute(self, *args, **kwargs): try: questions = [ { 'type': 'input', 'name': 'image_with_tag', 'message': 'What\'s your image repo ', } ] answers = prompt(questions) except Exception as e: Logger.warn("Failed to detect image repo. Windows console don't support prompt action.") Logger.warn("Jenkinsfile and docker-compose.yml may not be generated completely.") answers = {"image_with_tag": "[IMAGE_WITH_TAG]"} return answers
def run(self): try: self.load() except Exception as e: # TODO add some exception handler Logger.error( "Failed to load rigging or commands in disk,because of %s" % e) commands_doc = self.cm.get_commands_doc() arguments = docopt(commands_doc, help=False, version=DERRICK_VERSION) if DEBUG_MODE in arguments and arguments[DEBUG_MODE] == 1: Logger.set_debug_mode() command_context = self.init_commands_context(arguments=arguments) self.cm.run_commands(command_context)
def render_templates(templates_dir=None, dest_dir=None, compile_dict=None): if templates_dir is None or dest_dir is None or compile_dict is None: raise ParamsShortageException("compile templates need some more params") all_success = True for template_name in os.listdir(templates_dir): template_path = os.path.join(templates_dir, template_name) try: Init.render_template(template_path, dest_dir, compile_dict.get(template_name)) except Exception as e: all_success = False Logger.debug("template_path:%s,dest_dir:%s,content:%s" % (template_path, dest_dir, compile_dict.get(template_name))) Logger.debug("Failed to compile template(%s),because of %s" % (template_name, e)) return all_success
def run_commands(self, context): # if you define a command that has the same name with others # the latest registered command will execute commands = self.all() arguments = context.get_arguments() for command_name in commands.keys(): command_name_lower = command_name.lower() if command_name_lower in arguments.keys() and arguments[command_name_lower] is True: command = commands[command_name] try: command.execute(context) # TODO Add command event listener in here # TODO event listener will fire event in the whole lifecycle except Exception as e: Logger.error("Failed to execute command %s,because of %s" % (command_name, e)) Logger.debug(traceback.format_exc())
def execute(self, *args, **kwargs): cwd = os.getcwd() file_path = os.path.join(cwd, "baas.yaml") try: f = open(file_path, "r") res = yaml.safe_load(f) if self.check_valid(res) is not True: Logger.error("You must specific those required keys.") exit(-1) return res except Exception as e: Logger.error("Failed to load yaml file from disk,because of %s" % e.message) exit(-1) finally: f.close()
def get_most_relative_version(version): version_num = str(version)[1:] version_arr = version_num.split(".") detect_version = NODEJS_8 try: base_version = version_arr[0] if base_version == "4": detect_version = NODEJS_4 if base_version == "6": detect_version = NODEJS_6 if base_version == "8": detect_version = NODEJS_8 except Exception as e: Logger.debug("system version is %s,error message is %s" % (version, e)) return detect_version
def execute(self, context): questions = [{ 'type': 'list', 'name': 'engine', 'message': 'Which Orchestration Engine would you like to choose?', 'choices': ["Kubernetes", "Swarm"] }] style = style_from_dict({ Token.Selected: '#00FFFF bold', }) answers = prompt(questions, style=style) recorder = Derrick().get_recorder() try: recorder.record(answers) except Exception as e: Logger.error("Failed to record config, Because of %s" % e)
def execute(self, *args, **kwargs): try: questions = [{ 'type': 'input', 'name': 'image_with_tag', 'message': 'What\'s your image repo ', }] answers = prompt(questions) except Exception as e: Logger.warn( "Failed to detect image repo. Windows console don't support prompt action." ) Logger.warn( "Jenkinsfile and docker-compose.yml may not be generated completely." ) answers = {"image_with_tag": "[IMAGE_WITH_TAG]"} return answers
def execute(self, *args, **kwargs): try: questions = [ { 'type': 'input', 'name': 'image_with_tag', 'message': 'Please input image name with tag (such as "registry.com/user/repo:tag"): ', } ] answers = prompt(questions) if len(answers.keys()) is 0: sys.exit(0) except KeyboardInterrupt: sys.exit(0) except Exception as e: Logger.warn("Failed to detect image repo. Windows console don't support prompt action.") Logger.warn("Jenkinsfile and docker-compose.yml may not be generated completely.") answers = {"image_with_tag": "[IMAGE_WITH_TAG]"} return answers
def execute(self, context): if check_application_first_setup() is True: Logger.info("Your application haven't been initialized,you can run `derrick init`.") return if check_dockerfile_exists() is False: Logger.info("Dockerfile is not exists, Maybe you can rerun `derrick init` to resolve it.") return if is_windows() is True: try: import win32api win32api.WinExec('docker-compose up --build -d ') except Exception as e: Logger.error("Can not start your application.Have you installed docker-compose in path?") return status = os.system("/bin/bash -i -c 'docker-compose up --build -d '") if status == 0: Logger.info("Your application has been up to running! You can run `docker ps` to get exposed ports.") else: Logger.error("Can not start your application.Have you installed docker-compose in path?")
def execute(self, context): if check_application_first_setup() is True: Logger.info( "Your application haven't been initialized,you can run `derrick init`." ) return if check_dockerfile_exists() is False: Logger.info( "Dockerfile is not exists, Maybe you can rerun `derrick init` to resolve it." ) return # if is_windows() is True: # try: # import win32api # win32api.WinExec('docker-compose up --build -d ') # except Exception as e: # Logger.error("Can not start your application.Have you installed docker-compose in path?") # return # status = os.system("/bin/bash -i -c 'docker-compose up --build -d '") # if status == 0: # Logger.info("Your application has been up to running! You can run `docker ps` to get exposed ports.") # else: # Logger.error("Can not start your application.Have you installed docker-compose in path?") engine_manager = Derrick().get_engine_manager() recorder = Derrick().get_recorder() select_engine = recorder.get_record("engine") engines = engine_manager.all() for engine_name in engines.keys(): if engine_name.find(select_engine.lower()) != -1: Logger.info(select_engine + " is the default Orchestration Engine.") engines[engine_name].up()
def pre_load(self): os.mkdir(get_derrick_home()) os.mkdir(get_rigging_home()) os.mkdir(get_commands_home()) Logger.info(DERRICK_LOGO) Logger.info("This is the first time to run Derrick.\n") Logger.info("Successfully create DERRICK_HOME in %s" % (get_derrick_home()))
def execute(self, *args, **kwargs): Logger.info("Detecting PHP version ...") version = DEFAULT_VERSION try: output = subprocess.check_output(["php", "-version"], shell=False, stderr=subprocess.STDOUT) version = PhpVersionDetector.get_most_relative_version(output) except Exception as e: Logger.debug("Failed to detect PHP version,because of %s" % e) Logger.debug("Use default PHP version:%s instead ." % DEFAULT_VERSION) return {"version": version}
def execute(self, context): if check_application_first_setup() is True: Logger.info( "Your application haven't been initialized,you can run `derrick init`." ) return if check_dockerfile_exists() is False: Logger.info( "Dockerfile is not exists, Maybe you can rerun `derrick init` to resolve it." ) return status = os.system("/bin/bash -i -c 'docker-compose up -d '") if status == 0: Logger.info( "Your application has been up to running! You can run `docker ps` to get exposed ports." )
def up(self, *args, **kwargs): if is_windows() is True: try: import win32api win32api.WinExec('docker-compose up --build -d ') except Exception as e: Logger.error( "Can not start your application.Have you installed docker-compose in path?" ) return status = os.system("/bin/bash -i -c 'docker-compose up --build -d '") if status == 0: Logger.info( "Your application has been up to running! You can run `docker ps` to get exposed ports." ) else: Logger.error( "Can not start your application.Have you installed docker-compose in path?" )
def run(self): try: self.load() except Exception as e: # TODO add some exception handler Logger.error( "Failed to load rigging or commands in disk,because of %s" % e) commands_doc = self.cm.get_commands_doc() arguments = docopt(commands_doc, help=False, version=DERRICK_VERSION) if DEBUG_MODE in arguments and arguments[DEBUG_MODE] == 1: Logger.set_debug_mode() # if not config command and check record valid if arguments["config"] is False: if self.recorder.is_valid() is False: Logger.error("Your should run `derrick config` first") return command_context = self.init_commands_context(arguments=arguments) self.cm.run_commands(command_context)
def execute(self, context): # TODO add application recorder to record the application platform and other things. rigging_manager = Derrick().get_rigging_manager() all_rigging = rigging_manager.all() detected = False handled_rigging = [] for rigging_name in all_rigging: rigging = all_rigging.get(rigging_name) try: handled, platform = rigging.detect(context) if handled is True: detected = True handled_rigging.append({"rigging_name": rigging_name, "rigging": rigging, "platform": platform}) except Exception as e: Logger.error("Failed to detect your application's platform with rigging(%s),because of %s" % (rigging_name, e)) if detected is True: if len(handled_rigging) > 1: # TODO when more than one rigging can handle your application. Logger.warn("More than one rigging can handle the application.") rigging_dict = Init.choose_rigging(handled_rigging) if rigging_dict is None: Logger.error("The Rigging you chosen maybe broken.") return else: rigging_dict = handled_rigging[0] rigging = rigging_dict.get("rigging") rdi = RiggingDetectInfo(rigging_dict.get("rigging_name"), rigging_dict.get("platform")) try: results = rigging.compile(context) Logger.debug("The platform is %s,the rigging used is %s" % (rigging_dict.get("platform"), rigging_dict.get("rigging_name"))) Logger.debug("The results is %s" % results) except Exception as e: Logger.error("Failed to compile your application.because of %s" % e) return if type(results) is dict: try: template_dir = rigging.get_template_dir() dest_dir = context.get(WORKSPACE_ENV) Logger.debug("Ready to render templates and template_dir:%s,dest_dir:%s,compile_dict:%s" % ( template_dir, dest_dir, results)) Init.render_templates(templates_dir=template_dir, dest_dir=dest_dir, compile_dict=results) Logger.info("Derrick detect your platform is %s and compile successfully." % rigging_dict.get("platform")) except Exception as e: Logger.error("Failed to render template with rigging(%s),because of %s" % (rigging.get_name(), e)) try: ApplicationRecorder().record(rdi) except Exception as e: Logger.debug("Failed to record detected information.because of %s" % e) else: raise RiggingCompileException("compile results is not a dict.") else: Logger.warn( "Failed to detect your application's platform." "Maybe you can upgrade Derrick to get more platforms supported.") return
def __init__(self): super(FileRecorder, self).__init__() try: self.load() except Exception as e: Logger.debug("File may not exists and will create after config creation.")