def _create(self): push_file = '.push.json' with open(push_file, 'r') as f: push_data = json.load(f) if sync_helpers.get_sync_spec() is not None: print("Syncing spec has been already created for this app") sys.exit(1) app_run_id = push_data.get('app_run_id', "") job_name = '-'.join([self.config["name"], app_run_id]) namespace = self.config['namespace'] user_env = dict(os.environ, SYNC_SPEC=job_name, NAMESPACE=namespace, JOB_NAME=job_name) try: subprocess.check_output(["make", "sync-create"], env=user_env, stderr=subprocess.STDOUT) with open('.sync.json', 'w') as json_file: sync_data = {'sync_spec': job_name} json.dump(sync_data, json_file, indent=2) print("Syncing spec is created successfully") except subprocess.CalledProcessError as e: if "No rule to make target `sync-create'" in str(e.output): # TODO: when we have a template updating capability, add a # note recommending that he user update's their template to # get the sync create command print("This app does not support the `mlt sync create` " "command. No `sync-create` target was found in the " "Makefile.") else: print("Error while creating sync spec: {}".format(e.output))
def _reload(self): sync_spec = sync_helpers.get_sync_spec() if sync_spec is None: print("No syncing spec has been created for this app yet") sys.exit(1) user_env = dict(os.environ, SYNC_SPEC=sync_spec) try: subprocess.check_output(["make", "sync-reload"], env=user_env, stderr=subprocess.STDOUT) print("Sync agent is restarted") except subprocess.CalledProcessError as e: if "No rule to make target `sync-reload'" in str(e.output): # TODO: when we have a template updating capability, add a # note recommending that he user update's their template to # get the sync reload command print("This app does not support the `mlt sync reload` " "command. No `sync-reload` target was found in the " "Makefile.") elif "{} does not exist. Did you mean something else".format( sync_spec) in str(e.output): print("Syncing spec has not been created for this app yet") else: print("Error while reloading sync agent: {}".format(e.output))
def action(self): """deletes current kubernetes namespace""" if sync_helpers.get_sync_spec() is not None: error_handling.throw_error( "This app is currently being synced, please run " "`mlt sync delete` to unsync first", "red") namespace = self.config['namespace'] jobs = files.get_deployed_jobs(job_names_only=True) if not jobs: error_handling.throw_error("This app has not been deployed yet.") else: if self.args.get('--job-name'): job_name = self.args['--job-name'] if job_name in jobs: self._undeploy_jobs(namespace, job_name) else: error_handling.throw_error( 'Job name {} not found in: {}'.format(job_name, jobs)) elif self.args.get('--all') or len(jobs) == 1: self._undeploy_jobs(namespace, jobs, all_jobs=True) else: error_handling.throw_error( "Multiple jobs are found under this application, " "please try `mlt undeploy --all` or specify a single " "job to undeploy using " "`mlt undeploy --job-name <job-name>`")
def _delete(self): sync_spec = sync_helpers.get_sync_spec() if sync_spec is None: print("No syncing spec has been created for this app yet") sys.exit(1) user_env = dict(os.environ, SYNC_SPEC=sync_spec) try: subprocess.check_output(["make", "sync-delete"], env=user_env, stderr=subprocess.STDOUT) with open('.sync.json', 'r+') as json_file: sync_data = json.load(json_file) if 'sync_spec' in sync_data.keys(): del (sync_data['sync_spec']) json_file.seek(0) json.dump(sync_data, json_file, indent=2) json_file.truncate() print("Syncing spec is successfully deleted") except subprocess.CalledProcessError as e: if "No rule to make target `sync-delete'" in str(e.output): # TODO: when we have a template updating capability, add a # note recommending that he user update's their template to # get the sync delete command print("This app does not support the `mlt sync delete` " "command. No `sync-delete` target was found in the " "Makefile.") elif "{} does not exist. Did you mean something else".format( sync_spec) in str(e.output): print("No syncing spec has been created for this app yet") else: print("Error while deleting syncing spec: {}".format(e.output))
def action(self): push_file = '.push.json' if os.path.isfile(push_file): with open(push_file, 'r') as f: data = json.load(f) else: print("This app has not been deployed yet") sys.exit(1) app_run_id = data.get('app_run_id', "") job_name = "-".join([self.config["name"], app_run_id]) namespace = self.config['namespace'] user_env = dict(os.environ, NAMESPACE=namespace, JOB_NAME=job_name) try: output = subprocess.check_output(["make", "status"], env=user_env, stderr=subprocess.STDOUT) print(output.decode("utf-8").strip()) if sync_helpers.get_sync_spec() is not None: # format the string before passing it print so that the string # is evaluated correctly print("\nSYNC STATUS\n{}".format( 'This app is being watched by sync')) except subprocess.CalledProcessError as e: if "No rule to make target `status'" in str(e.output): # TODO: when we have a template updating capability, add a # note recommending that he user update's their template to # get the status command print("This app does not support the `mlt status` command. " "No `status` target was found in the Makefile.") else: print("Error while getting app status: {}".format(e.output))
def action(self): """deletes current kubernetes namespace""" if sync_helpers.get_sync_spec() is not None: print(colored("This app is currently being synced, please run " "`mlt sync delete` to unsync first", 'red')) sys.exit(1) namespace = self.config['namespace'] jobs_list = files.get_deployed_jobs() if not jobs_list: print("This app has not been deployed yet.") sys.exit(1) else: if self.args.get('--all'): self._undeploy_all(namespace, jobs_list) elif self.args.get('--job-name'): job_name = self.args['--job-name'] if job_name in jobs_list: self._undeploy_job(namespace, job_name) else: print('Job-name {} not found in: {}' .format(job_name, jobs_list)) sys.exit(1) elif len(jobs_list) == 1: self._undeploy_job(namespace, jobs_list.pop()) else: print("Multiple jobs are found under this application, " "please try `mlt undeploy --all` or specify a single" " job to undeploy using " "`mlt undeploy --job-name <job-name>`") sys.exit(1)
def test_get_sync_spec_no_json(isfile_mock, open_mock, json_mock, get_sync_spec_mock): """ Tests if no .sync.json file is present """ isfile_mock.return_value = False output = get_sync_spec() assert output is None
def test_get_sync_spec_empty_json(isfile_mock, open_mock, json_mock, get_sync_spec_mock): """ Tests if .sync.json file is empty """ isfile_mock.return_value = True json.load.return_value = {} output = get_sync_spec() assert output is None
def action(self): schema.validate() if sync_helpers.get_sync_spec() is not None: print(colored("This folder is currently being synced, please run " "`mlt sync delete {}` to delete sync spec " "manually".format(sync_helpers.get_sync_spec()), 'yellow')) skip_crd_check = self.args['--skip-crd-check'] if not skip_crd_check: kubernetes_helpers.check_crds(exit_on_failure=True) if self.args['--no-push']: print("Skipping image push") else: self._push() self._deploy_new_container() if self.args["--logs"]: self._tail_logs()
def _custom_status(self, job, namespace, job_type): """runs `make status` on any special deployment Special deployment is defined as any one of the following: 1. Doesn't have deployment yaml 2. Has deployment yaml but many deployments of different kinds """ user_env = dict(os.environ, NAMESPACE=namespace, JOB_NAME=job) output = subprocess.check_output(["make", "status"], env=user_env, stderr=subprocess.STDOUT) print(output.decode("utf-8").strip()) if sync_helpers.get_sync_spec() is not None: # format the string before passing it print so that the string # is evaluated correctly print("\nSYNC STATUS\n{}".format( 'This app is being watched by sync'))
def test_get_sync_spec_valid_spec(isfile_mock, open_mock, json_mock, get_sync_spec_mock): """tests a valid sync_spec contents that have `sync_spec` in json""" json_mock.load.return_value = {'sync_spec': 'spec'} output = get_sync_spec() assert output == 'spec'