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, 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 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 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 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 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 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 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 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, 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): 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 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 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 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