def action(self): """Update the template instance with new template version if template is updated """ if "template_name" not in self.config or \ "template_git_sha" not in self.config: print("ERROR: mlt.json does not have either template_name " "or template_git_sha. Template update is not possible.") return app_name = self.config["name"] template_name = self.config["template_name"] current_template_git_sha = self.config["template_git_sha"] orig_project_backup_dir = self._get_backup_dir_name(app_name) with git_helpers.clone_repo(self.template_repo) as temp_clone: application_dir = os.getcwd() clone_template_dir = os.path.join(temp_clone, constants.TEMPLATES_DIR, template_name) if not os.path.exists(clone_template_dir): print("Unable to update, template {} does " "not exist in MLT git repo.".format(template_name)) return latest_template_git_sha = \ git_helpers.get_latest_sha(clone_template_dir) if current_template_git_sha == latest_template_git_sha: print("Template is up to date, no need for update.") else: print("Template is not up to date, updating template...") copy_tree(application_dir, orig_project_backup_dir) os.chdir(temp_clone) # create temp-branch using git sha from which template # was initiated and clean un-tracked files cmd = "git checkout -f {} -b temp-branch && git clean -f .". \ format(current_template_git_sha) process_helpers.run_popen(cmd, shell=True) # copy app dir content to temp clone template dir copy_tree(application_dir, clone_template_dir) # if there are any uncommitted changes to temp-branch, # commit them otherwise 'pull' from master will fail. output = process_helpers.run("git status".split(" ")) if "Your branch is up-to-date" not in output: process_helpers.run("git add --all ".split(" ")) commit_command = "git commit --message 'temp-commit'" process_helpers.run(commit_command.split(" ")) # merging latest template changes by pulling from master # into temp-branch try: process_helpers.run("git pull origin master".split(" "), raise_on_failure=True) except CalledProcessError as e: # When auto merge failed do not error out, # let user review and fix conflicts # for other errors exit error_string = "Automatic merge failed; " \ "fix conflicts and then commit the result" if error_string not in e.output: error_handling.throw_error(e.output) # copy content of clone template dir back to app dir copy_tree(clone_template_dir, application_dir) print("Latest template changes have merged using git, " "please review changes for conflicts. ") print("Backup directory path: {}".format( os.path.abspath(orig_project_backup_dir))) os.chdir(application_dir)
def action(self): """Creates a new git repository based on an mlt template in the current working directory. """ template_name = self.args["--template"] template_repo = self.args["--template-repo"] skip_crd_check = self.args["--skip-crd-check"] with git_helpers.clone_repo(template_repo) as temp_clone: templates_directory = os.path.join(temp_clone, constants.TEMPLATES_DIR, template_name) try: # The template configs get pulled into the mlt.json file, so # don't grab a copy of that in this app's directory copytree(templates_directory, self.app_name, ignore=ignore_patterns(constants.TEMPLATE_CONFIG)) # Get the template configs from the template and include them # when building the mlt json file param_file = os.path.join(templates_directory, constants.TEMPLATE_CONFIG) template_params = config_helpers.\ get_template_parameters_from_file(param_file) template_git_sha = git_helpers.get_latest_sha( os.path.join(temp_clone, constants.TEMPLATES_DIR, template_name)) if not skip_crd_check: kubernetes_helpers.check_crds(app_name=self.app_name) if self.args["--enable-sync"]: if localhost_helpers.binary_path('ksync'): # Syncthing uses '.stignore' to ignore files during # sync we also don't want to upload unneeded local data app_ignore_file = os.path.join(self.app_name, ".gitignore") ksync_ignore_file = os.path.join( self.app_name, ".stignore") if self._check_update_yaml_for_sync(): copyfile(app_ignore_file, ksync_ignore_file) with open(ksync_ignore_file, 'a+') as f: f.write("\n.git/**") else: error_handling.throw_error( "This app doesn't support syncing", 'yellow') else: error_handling.throw_error( "ksync is not installed on localhost.", 'red') data = self._build_mlt_json(template_params, template_git_sha) # If the app has option for debugging failures, grab the # Kubernetes debug wrapper file and put it in the app directory if any(param["name"] == "debug_on_fail" for param in template_params): self._enable_debug_on_fail(temp_clone) with open(os.path.join(self.app_name, constants.MLT_CONFIG), 'w') as f: json.dump(data, f, indent=2) self._init_git_repo() except OSError as exc: if exc.errno == 17: error_msg = "Directory '{}' already exists: ".format( self.app_name) + \ "delete before trying to initialize new application" color = 'red' else: error_msg = traceback.format_exc() color = None error_handling.throw_error(error_msg, color)
def test_get_latest_sha(chdir_mock, run_mock): run_mock.return_value = 'nsdf923r89fwejks\n' assert git_helpers.get_latest_sha('hello-world') == 'nsdf923r89fwejks'