def update_dist_git(self, version, release): with Chdir(self.path): # Add new Dockerfile subprocess.check_call(["git", "add", "Dockerfile"]) # Add the scripts directory if it exists if os.path.exists(os.path.join(self.path, "scripts")): subprocess.check_call(["git", "add", "scripts"]) commit_msg = "Sync" if self.source_repo_name: commit_msg += " with %s" % self.source_repo_name if self.source_repo_commit: commit_msg += ", commit %s" % self.source_repo_commit commit_msg += ", release %s-%s" % (version, release) with Chdir(self.path): # Commit the change self.log.info("Commiting with message: '%s'" % commit_msg) subprocess.check_output(["git", "commit", "-m", commit_msg]) untracked = subprocess.check_output(["git", "ls-files", "--others", "--exclude-standard"]) if untracked: self.log.warn( "There are following untracked files: %s. Please review your commit." % ", ".join(untracked.splitlines()) ) diffs = subprocess.check_output(["git", "diff-files", "--name-only"]) if diffs: self.log.warn( "There are uncommited changes in following files: '%s'. Please review your commit." % ", ".join(diffs.splitlines()) ) with Chdir(self.path): subprocess.call(["git", "status"]) subprocess.call(["read", "-p", "\nPress any key to see the last commit...", "-n1", "-s"]) subprocess.call(["git", "show"]) if Tools.decision("Do you want to review your changes?"): with Chdir(self.path): subprocess.call(["bash"]) if Tools.decision("Do you want to push the commit?"): print("") with Chdir(self.path): self.log.info("Pushing change to the upstream repository...") subprocess.check_output(["git", "push", "-q"]) self.log.info("Change pushed.") if Tools.decision("Do you want to execute a build on OSBS?"): subprocess.call(["rhpkg", "container-build"])
def __init__(self, log, source, path): self.log = log self.path = path self.dockerfile = os.path.join(self.path, "Dockerfile") self.tools = Tools() self.name, self.branch, self.commit = self.tools.repo_info(path) self.source_repo_name, self.source_repo_branch, self.source_repo_commit = self.tools.repo_info( source)
def commit(self): commit_msg = "Sync" if self.source_repo_name: commit_msg += " with %s" % self.source_repo_name if self.source_repo_commit: commit_msg += ", commit %s" % self.source_repo_commit # Commit the change self.log.info("Commiting with message: '%s'" % commit_msg) subprocess.check_output(["git", "commit", "-q", "-m", commit_msg]) untracked = subprocess.check_output(["git", "ls-files", "--others", "--exclude-standard"]) if untracked: self.log.warn("There are following untracked files: %s. Please review your commit." % ", ".join(untracked.splitlines())) diffs = subprocess.check_output(["git", "diff-files", "--name-only"]) if diffs: self.log.warn("There are uncommited changes in following files: '%s'. Please review your commit." % ", ".join(diffs.splitlines())) if not self.noninteractive: subprocess.call(["git", "status"]) subprocess.call(["git", "show"]) if not (self.noninteractive or Tools.decision("Are you ok with the changes?")): subprocess.call(["bash"])
def build(self): if self.args.dist_git_assume_yes or Tools.decision("Do you want to execute a build on OSBS?"): self.log.info("Executing container build on OSBS...") cmd = ["rhpkg", "container-build"] if self.args.dist_git_scratch: cmd.append('--scratch') subprocess.call(cmd)
def __init__(self, log, source, path): self.log = log self.path = path self.dockerfile = os.path.join(self.path, "Dockerfile") self.tools = Tools() self.name, self.branch, self.commit = self.tools.repo_info(path) self.source_repo_name, self.source_repo_branch, self.source_repo_commit = self.tools.repo_info(source)
def prepare(self): self.log.debug("Resetting git repository...") # Reset all changes first - nothing should be done by hand with Chdir(self.path): subprocess.check_output(["git", "reset", "--hard"]) # Just check if the target branch is correct if not Tools.decision("You are currently working on the '%s' branch, is this what you want?" % self.branch): print("") self.switch_branch() self.old_version, self.old_release = self.read_version_and_release()
def _handle_custom_template(self): """ Fetches custom template (if provided) and saves as temporary file. This file is removed later in the process. """ if not self.template: return self.log.info("Using custom provided template file: '%s'" % self.template) if Tools.is_url(self.template): self.template = self._fetch_file(self.template) if not os.path.exists(self.template): raise Error("Template file '%s' could not be found. Please make sure you specified correct path or check if the file was successfully fetched." % self.template)
def _handle_additional_scripts(self): self.log.info("Additional scripts provided, installing them...") output_scripts = os.path.join(self.output, "scripts") if not os.path.exists(output_scripts): os.makedirs(output_scripts) for f in self.additional_scripts: self.log.debug("Handling '%s' file..." % f) if Tools.is_url(f): self._fetch_file(f, os.path.join(output_scripts, os.path.basename(f))) else: if not (os.path.exists(f) and os.path.isfile(f)): raise Error("File '%s' does not exist. Please make sure you specified correct path to a file when specifying additional scripts." % f) self.log.debug("Copying '%s' file to target scripts directory..." % f) shutil.copy(f, output_scripts)
def render_from_template(self): if not self.cfg.get('labels'): self.cfg['labels'] = [] labels = {} for label in self.cfg.get('labels'): labels[label['name']] = label['value'] # https://github.com/jboss-dockerfiles/dogen/issues/129 # https://github.com/jboss-dockerfiles/dogen/issues/137 for label in ['maintainer', 'description']: value = self.cfg.get(label) if value and not label in labels: self.cfg['labels'].append({'name': label, 'value': value}) labels[label] = value # https://github.com/jboss-dockerfiles/dogen/issues/195 if 'summary' not in labels: if 'description' in labels: self.cfg['labels'].append({ 'name': 'summary', 'value': labels.get('description') }) if self.template: template_file = self.template else: self.log.debug("Using dogen provided template file") template_file = os.path.join(self.pwd, "templates", "template.jinja") self.log.info("Rendering Dockerfile...") loader = FileSystemLoader(os.path.dirname(template_file)) env = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True) env.globals['helper'] = TemplateHelper() template = env.get_template(os.path.basename(template_file)) with open(self.dockerfile, 'wb') as f: f.write(template.render(self.cfg).encode('utf-8')) self.log.debug("Done") if self.template and Tools.is_url(self.template): self.log.debug("Removing temporary template file...") os.remove(self.template)
def _download_source(self, artifact, filename, hint=None): if Tools.is_url(artifact): self.log.warn( "Trying to download the '%s' artifact from original location" % artifact) try: self._fetch_file(artifact, filename) except Exception as e: raise Error( "Could not download artifact from orignal location, reason: %s" % str(e)) else: if hint: self.log.info(hint) self.log.info( "Please download the '%s' artifact manually and save it as '%s'" % (artifact, filename)) raise Error("Artifact '%s' could not be fetched!" % artifact)
def _handle_custom_template(self): """ Fetches custom template (if provided) and saves as temporary file. This file is removed later in the process. """ if not self.template: return self.log.info("Using custom provided template file: '%s'" % self.template) if Tools.is_url(self.template): self.template = self._fetch_file(self.template) if not os.path.exists(self.template): raise Error( "Template file '%s' could not be found. Please make sure you specified correct path or check if the file was successfully fetched." % self.template)
def render_from_template(self): if self.template: template_file = self.template else: self.log.debug("Using dogen provided template file") template_file = os.path.join(self.pwd, "templates", "template.jinja") self.log.info("Rendering Dockerfile...") loader = FileSystemLoader(os.path.dirname(template_file)) env = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True) env.globals['helper'] = TemplateHelper() template = env.get_template(os.path.basename(template_file)) with open(self.dockerfile, 'wb') as f: f.write(template.render(self.cfg).encode('utf-8')) self.log.debug("Done") if self.template and Tools.is_url(self.template): self.log.debug("Removing temporary template file...") os.remove(self.template)
def render_from_template(self): if self.template: template_file = self.template else: self.log.debug("Using dogen provided template file") template_file = os.path.join(self.pwd, "templates", "template.jinja") self.log.info("Rendering Dockerfile...") loader = FileSystemLoader(os.path.dirname(template_file)) env = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True) env.globals['helper'] = TemplateHelper() template = env.get_template(os.path.basename(template_file)) with open(self.dockerfile, 'w') as f: f.write(template.render(self.cfg).encode('utf-8')) self.log.debug("Done") if self.template and Tools.is_url(self.template): self.log.debug("Removing temporary template file...") os.remove(self.template)
def _handle_additional_scripts(self): self.log.info("Additional scripts provided, installing them...") output_scripts = os.path.join(self.output, "scripts") if not os.path.exists(output_scripts): os.makedirs(output_scripts) for f in self.additional_scripts: self.log.debug("Handling '%s' file..." % f) if Tools.is_url(f): self._fetch_file( f, os.path.join(output_scripts, os.path.basename(f))) else: if not (os.path.exists(f) and os.path.isfile(f)): raise Error( "File '%s' does not exist. Please make sure you specified correct path to a file when specifying additional scripts." % f) self.log.debug( "Copying '%s' file to target scripts directory..." % f) shutil.copy(f, output_scripts)
def test_local_file(self): self.assertFalse(Tools.is_url("a_file.tmp"))
def push(self): if self.noninteractive or Tools.decision("Do you want to push the commit?"): print("") self.log.info("Pushing change to the upstream repository...") subprocess.check_output(["git", "push", "-q"]) self.log.info("Change pushed.")
class Git(object): """ Git support for target directories """ def __init__(self, log, source, path): self.log = log self.path = path self.dockerfile = os.path.join(self.path, "Dockerfile") self.tools = Tools() self.name, self.branch, self.commit = self.tools.repo_info(path) self.source_repo_name, self.source_repo_branch, self.source_repo_commit = self.tools.repo_info(source) def prepare(self): self.log.debug("Resetting git repository...") # Reset all changes first - nothing should be done by hand with Chdir(self.path): subprocess.check_output(["git", "reset", "--hard"]) # Just check if the target branch is correct if not self.tools.decision("You are currently working on the '%s' branch, is this what you want?" % self.branch): print("") self.switch_branch() self.old_version, self.old_release = self.read_version_and_release() def update(self): new_version, release = self.read_version_and_release() new_release = int(self.old_release) + 1 self.log.info("New release will be: %s-%s." % (new_version, new_release)) # Bump the release environment variable self.update_value("JBOSS_IMAGE_RELEASE", new_release) self.update_dist_git(new_version, new_release) def clean_scripts(self): """ Removes the scripts directory from staging and disk """ with Chdir(self.path): if os.path.exists("scripts"): self.log.info("Removing old scripts directory") subprocess.check_output(["git", "rm", "-rf", "scripts"]) def read_dockerfile(self): with open(self.dockerfile, 'r') as f: return f.read() def read_value(self, dockerfile, exp): pattern = re.compile(exp) match = pattern.search(dockerfile) if match: return match.group(1) raise Exception("Could not find the '%s' pattern in %s" % (exp, dockerfile)) def update_value(self, env, value): """ This fnction updates the value of the selected environment variable or label that is set in the following pattern: env="[TO_REPLACE]". """ # Read Dockerfile dockerfile = self.read_dockerfile() with open(self.dockerfile, 'w') as f: f.write(re.sub("(?<=%s=\")(.*)(?=\")" % env, str(value), dockerfile)) def read_version_and_release(self): # If there is no Dockerfile, there are no old versions if not os.path.exists(self.dockerfile): return None, 0 # Read *already existing* Dockerfile dockerfile = self.read_dockerfile() # Read envs from Dockerfile # Used to bump the release label and fill the commit message later try: version = self.read_value(dockerfile, 'JBOSS_IMAGE_VERSION="([\w\.]+)"') except: version = self.read_value(dockerfile, 'Version="([\w\.]+)"') try: release = self.read_value(dockerfile, 'JBOSS_IMAGE_RELEASE="(\d+)"') except: try: release = self.read_value(dockerfile, 'Release="(\d+)"') except: release = 0 return version, release def switch_branch(self): with Chdir(self.path): branches = subprocess.check_output(["git", "for-each-ref", "--format=%(refname)", "refs/heads"]) branches = [b.strip().split("/")[-1] for b in branches.splitlines()] self.log.info("Available branches:") for branch in branches: self.log.info(branch) target_branch = raw_input("\nTo which branch do you want to switch? ") if not target_branch in branches: print("") self.switch_branch() with Chdir(self.path): # Checkout the correct branch self.log.info("Switching to '%s' branch" % target_branch) subprocess.check_output(["git", "checkout", "-q", "-f", target_branch]) def update_lookaside_cache(self, files): with Chdir(self.path): self.log.info("Updating lookaside cache...") subprocess.check_output(["rhpkg", "new-sources"] + files) self.log.info("Update finished.") def update_dist_git(self, version, release): with Chdir(self.path): # Add new Dockerfile subprocess.check_call(["git", "add", "Dockerfile"]) # Add the scripts directory if it exists if os.path.exists(os.path.join(self.path, "scripts")): subprocess.check_call(["git", "add", "scripts"]) commit_msg = "Sync" if self.source_repo_name: commit_msg += " with %s" % self.source_repo_name if self.source_repo_commit: commit_msg += ", commit %s" % self.source_repo_commit commit_msg += ", release %s-%s" % (version, release) with Chdir(self.path): # Commit the change self.log.info("Commiting with message: '%s'" % commit_msg) subprocess.check_output(["git", "commit", "-m", commit_msg]) untracked = subprocess.check_output(["git", "ls-files", "--others", "--exclude-standard"]) if untracked: self.log.warn("There are following untracked files: %s. Please review your commit." % ", ".join(untracked.splitlines())) diffs = subprocess.check_output(["git", "diff-files", "--name-only"]) if diffs: self.log.warn("There are uncommited changes in following files: '%s'. Please review your commit." % ", ".join(diffs.splitlines())) if self.tools.decision("Do you want to review your changes?"): with Chdir(self.path): subprocess.call(["bash"]) if self.tools.decision("Do you want to push the commit?"): print("") with Chdir(self.path): self.log.info("Pushing change to the upstream repository...") subprocess.check_output(["git", "push", "-q"]) self.log.info("Change pushed.") if self.tools.decision("Do you want to execute a build on OSBS?"): subprocess.call(["rhpkg", "container-build"])
class Git(object): """ Git support for target directories """ def __init__(self, log, source, path): self.log = log self.path = path self.dockerfile = os.path.join(self.path, "Dockerfile") self.tools = Tools() self.name, self.branch, self.commit = self.tools.repo_info(path) self.source_repo_name, self.source_repo_branch, self.source_repo_commit = self.tools.repo_info( source) def prepare(self): self.log.debug("Resetting git repository...") # Reset all changes first - nothing should be done by hand with Chdir(self.path): subprocess.check_output(["git", "reset", "--hard"]) # Just check if the target branch is correct if not self.tools.decision( "You are currently working on the '%s' branch, is this what you want?" % self.branch): print("") self.switch_branch() self.old_version, self.old_release = self.read_version_and_release() def update(self): new_version, release = self.read_version_and_release() new_release = int(self.old_release) + 1 self.log.info("New release will be: %s-%s." % (new_version, new_release)) # Bump the release environment variable self.update_value("JBOSS_IMAGE_RELEASE", new_release) self.update_dist_git(new_version, new_release) def clean_scripts(self): """ Removes the scripts directory from staging and disk """ with Chdir(self.path): if os.path.exists("scripts"): self.log.info("Removing old scripts directory") subprocess.check_output(["git", "rm", "-rf", "scripts"]) def read_dockerfile(self): with open(self.dockerfile, 'r') as f: return f.read() def read_value(self, dockerfile, exp): pattern = re.compile(exp) match = pattern.search(dockerfile) if match: return match.group(1) raise Exception("Could not find the '%s' pattern in %s" % (exp, dockerfile)) def update_value(self, env, value): """ This fnction updates the value of the selected environment variable or label that is set in the following pattern: env="[TO_REPLACE]". """ # Read Dockerfile dockerfile = self.read_dockerfile() with open(self.dockerfile, 'w') as f: f.write( re.sub("(?<=%s=\")(.*)(?=\")" % env, str(value), dockerfile)) def read_version_and_release(self): # If there is no Dockerfile, there are no old versions if not os.path.exists(self.dockerfile): return None, 0 # Read *already existing* Dockerfile dockerfile = self.read_dockerfile() # Read envs from Dockerfile # Used to bump the release label and fill the commit message later try: version = self.read_value(dockerfile, 'JBOSS_IMAGE_VERSION="([\w\.]+)"') except: version = self.read_value(dockerfile, 'Version="([\w\.]+)"') try: release = self.read_value(dockerfile, 'JBOSS_IMAGE_RELEASE="(\d+)"') except: try: release = self.read_value(dockerfile, 'Release="(\d+)"') except: release = 0 return version, release def switch_branch(self): with Chdir(self.path): branches = subprocess.check_output( ["git", "for-each-ref", "--format=%(refname)", "refs/heads"]) branches = [b.strip().split("/")[-1] for b in branches.splitlines()] self.log.info("Available branches:") for branch in branches: self.log.info(branch) target_branch = raw_input("\nTo which branch do you want to switch? ") if not target_branch in branches: print("") self.switch_branch() with Chdir(self.path): # Checkout the correct branch self.log.info("Switching to '%s' branch" % target_branch) subprocess.check_output( ["git", "checkout", "-q", "-f", target_branch]) def update_lookaside_cache(self, files): with Chdir(self.path): self.log.info("Updating lookaside cache...") subprocess.check_output(["rhpkg", "new-sources"] + files) self.log.info("Update finished.") def update_dist_git(self, version, release): with Chdir(self.path): # Add new Dockerfile subprocess.check_call(["git", "add", "Dockerfile"]) # Add the scripts directory if it exists if os.path.exists(os.path.join(self.path, "scripts")): subprocess.check_call(["git", "add", "scripts"]) commit_msg = "Sync" if self.source_repo_name: commit_msg += " with %s" % self.source_repo_name if self.source_repo_commit: commit_msg += ", commit %s" % self.source_repo_commit commit_msg += ", release %s-%s" % (version, release) with Chdir(self.path): # Commit the change self.log.info("Commiting with message: '%s'" % commit_msg) subprocess.check_output(["git", "commit", "-m", commit_msg]) untracked = subprocess.check_output( ["git", "ls-files", "--others", "--exclude-standard"]) if untracked: self.log.warn( "There are following untracked files: %s. Please review your commit." % ", ".join(untracked.splitlines())) diffs = subprocess.check_output( ["git", "diff-files", "--name-only"]) if diffs: self.log.warn( "There are uncommited changes in following files: '%s'. Please review your commit." % ", ".join(diffs.splitlines())) if self.tools.decision("Do you want to review your changes?"): with Chdir(self.path): subprocess.call(["bash"]) if self.tools.decision("Do you want to push the commit?"): print("") with Chdir(self.path): self.log.info("Pushing change to the upstream repository...") subprocess.check_output(["git", "push", "-q"]) self.log.info("Change pushed.") if self.tools.decision( "Do you want to execute a build on OSBS?"): subprocess.call(["rhpkg", "container-build"])
def test_remote_https_file(self): self.assertTrue(Tools.is_url("https://host/file.tmp"))