def deploy(self, mode): # every service must be deployable in single-node mode, so this is valid even if there # aren't any services if not mode: mode = "single-node" success = True # clean up the old deployment deploy_path = os.path.join(self.path, "deploy") if os.path.isdir(deploy_path): shutil.rmtree(deploy_path) os.mkdir(deploy_path) services = self.services app_params = self.get_app_params() # load all the template strings templates_path = os.path.join(MainSettings.ROOT, "templates") template_names = ["namespace.json", "pod.json", "service-pod.json", "notebook.json", "controller.json", "service.json"] templates = {} for name in template_names: with open(os.path.join(templates_path, name), 'r') as tf: templates[name] = tf.read() # insert the notebooks container into the pod.json template with open(os.path.join(deploy_path, "notebook.json"), 'w+') as nb_file: nb_string = fill_template_string(templates["notebook.json"], app_params) nb_file.write(nb_string) # insert the namespace file into the deployment folder with open(os.path.join(deploy_path, "namespace.json"), 'w+') as ns_file: ns_string = fill_template_string(templates["namespace.json"], app_params) ns_file.write(ns_string) # write deployment files for every service (by passing app parameters down to each service) for service in services: deployed_service = service.deploy(mode, deploy_path, self, templates) if not deployed_service: success = False # use the cluster manager to deploy each file in the deploy/ folder redirect_url = ClusterManager.get_instance().deploy_app(self.app_id, deploy_path) if not redirect_url: success = False if success: app_id = app_params["app.id"] msg = "Successfully deployed app {0} in {1} mode with ID {2}".format(self.name, mode, app_id) info_log(self.TAG, msg) return redirect_url else: error_log(self.TAG, "Failed to deploy app {0} in {1} mode.".format(self.name, mode)) return None
def deploy(self, mode, deploy_path, app, templates): """ Called from within App.deploy """ success = True deps = self.deployments if mode not in deps: raise Exception("service {0} does not support {1} deployment"\ .format(self.full_name, mode)) service_params = app.get_app_params().copy() service_params.update( namespace_params("service", self.parameters.copy())) dep_json = json.loads(fill_template_string(deps[mode], service_params)) comps = self.components for comp in dep_json["components"]: comp_name = comp["name"] for deployment in comp["deployments"]: dep_type = deployment["type"] dep_params = deployment.get("parameters", {}).copy() dep_params.update(comp.get("parameters", {})) # TODO: perhaps this should be done in a cleaner way? dep_params["name"] = comp_name dep_params[ "image-name"] = MainSettings.REGISTRY_NAME + "/" + self.full_name + "-" + comp_name final_params = dict( service_params.items() + namespace_params("component", dep_params).items()) debug_log(self.TAG, "final_params: {0}".format(final_params)) filled_comp = fill_template_string(comps[comp_name + ".json"], final_params) final_params["containers"] = filled_comp filled_template = fill_template_string( templates[dep_type + ".json"], final_params) with open( os.path.join(deploy_path, comp_name + "-" + dep_type + ".json"), "w+") as df: df.write(filled_template) return success
def _launch_proxy_server(self, token): # TODO the following chunk of code is reused in App.deploy (should be abstracted away) proxy_path = os.path.join(MainSettings.ROOT, "proxy") # clean up the old deployment deploy_path = os.path.join(proxy_path, "deploy") if os.path.isdir(deploy_path): shutil.rmtree(deploy_path) os.mkdir(deploy_path) params = {"token": token} # load all the template strings templates_path = os.path.join(proxy_path, "deployment") template_names = os.listdir(templates_path) templates = {} for name in template_names: with open(os.path.join(templates_path, name), 'r') as tf: templates[name] = tf.read() # insert the notebooks container into the pod.json template for name in template_names: with open(os.path.join(deploy_path, name), 'w+') as p_file: p_string = fill_template_string(templates[name], params) p_file.write(p_string) # launch each component self._create(os.path.join(deploy_path, name))
def deploy(self, mode, deploy_path, app, templates): """ Called from within App.deploy """ success = True deps = self.deployments if mode not in deps: raise Exception("service {0} does not support {1} deployment"\ .format(self.full_name, mode)) service_params = app.get_app_params().copy() service_params.update(namespace_params("service", self.parameters.copy())) dep_json = json.loads(fill_template_string(deps[mode], service_params)) comps = self.components for comp in dep_json["components"]: comp_name = comp["name"] for deployment in comp["deployments"]: dep_type = deployment["type"] dep_params = deployment.get("parameters", {}).copy() dep_params.update(comp.get("parameters", {})) # TODO: perhaps this should be done in a cleaner way? dep_params["name"] = comp_name dep_params["image-name"] = REGISTRY_NAME + "/" + self.full_name + "-" + comp_name final_params = dict(service_params.items() + namespace_params("component", dep_params).items()) print("final_params: {0}".format(final_params)) filled_comp = fill_template_string(comps[comp_name + ".json"], final_params) final_params["containers"] = filled_comp filled_template = fill_template_string(templates[dep_type + ".json"], final_params) with open(os.path.join(deploy_path, comp_name + "-" + dep_type + ".json"), "w+") as df: df.write(filled_template) return success