def _restart_unchanged_resources(self, file_path: str): """ Trigger a restart of all restartable resources. Args: output: List of output lines that kubectl produced when apply -f was run """ cmd = ["kubectl", "rollout", "restart", "-f", file_path] run_shell_command(cmd) logger.info("Restarted all possible resources")
def build_image(docker_file: str, tag: str): """Build the docker image This uses bash to run commands directly. Args: docker_file: The name of the dockerfile to build tag: The docker tag to apply to the image name """ cmd = [ "docker", "build", "--build-arg", f"PIP_EXTRA_INDEX_URL={os.getenv('PIP_EXTRA_INDEX_URL')}", "-t", tag, "-f", f"./{docker_file}", ".", ] logger.info(f"Building docker image for {docker_file} with command \n{' '.join(cmd)}") return_code, _ = run_shell_command(cmd) if return_code != 0: raise ChildProcessError("Could not build the image for some reason!")
def test_validate_yaml(self, _, victim): path = victim._create_image_pull_secret("myapp") cmd = ["kubectl", "apply", "--dry-run", "--validate", "-f", path] code, lines = run_shell_command(cmd) print(lines) assert code == 0
def _await_rollout(self, target: str, target_namespace: str): """Await the rollout of a specified target to complete This function awaits the completion of the rollout of the target in the target_namespace. If it fails, or if it does not complete successfully within the default kubectl timeout, a ChildProcessorError is thrown. NOTE: This may be a bit 'racy', in the sense that if multiple CI pipelines are running simultaneously, the await may not always be correct (it may await a different revision than the one that this step had just deployed). Args: target: The resource to target. This resource should be named according to the <resource_type>/name convention. target_namespace: The namespace of the resource Raises: ChildProcessError: if the rollout of the specified resource did not complete successfully. """ cmd = [ "kubectl", "rollout", "--namespace", target_namespace, "status", target, "--watch=True" ] exit_code, _ = run_shell_command(cmd) if exit_code != 0: raise ChildProcessError( f"Specified deployment {target} in namespace {target_namespace} " "did not successfully rollout.") logger.info("Rollout successful")
def _apply_kubernetes_config_file(self, file_path: str): """ Create/Update the kubernetes resources based on the provided file_path to the configuration. This function assumes that the file does NOT contain any Jinja-templated variables anymore (i.e. it's been rendered) Args: file_path: Path to the kubernetes configuration """ # workaround for some CI runners that override the default k8s namespace cmd = ["kubectl", "config", "set-context", self.cluster_name, "--namespace", "default"] exit_code, _ = run_shell_command(cmd) if exit_code != 0: raise ChildProcessError(f"Couldn't set-context for cluster {self.cluster_name}") cmd = ["kubectl", "apply", "-f", file_path] exit_code, response = run_shell_command(cmd) if exit_code != 0: raise ChildProcessError(f"Couldn't apply Kubernetes config from path {file_path}")
def build_sbt_assembly_jar(self): """Builds an SBT assembly jar This uses bash to run commands directly. Raises: ChildProcessError is the bash command was not successful """ self._remove_old_artifacts("target/") cmd = ["sbt", "clean", "assembly"] return_code, _ = run_shell_command(cmd) if return_code != 0: raise ChildProcessError("Could not build the package for some reason!")
def push_image(tag: str): """Push the docker image This uses bash to run commands directly. Args: tag: The docker tag to upload """ cmd = ["docker", "push", tag] logger.info(f"Uploading docker image {tag}") return_code, _ = run_shell_command(cmd) if return_code != 0: raise ChildProcessError("Could not push image for some reason!")
def build_python_wheel(self): """Builds Python wheel This uses bash to run commands directly. Raises: ChildProcessError is the bash command was not successful """ self._write_version() self._remove_old_artifacts("dist/") cmd = ["python", "setup.py", "bdist_wheel"] return_code, _ = run_shell_command(cmd) if return_code != 0: raise ChildProcessError("Could not build the package for some reason!")
def tag_image(old_tag: str, new_tag: str): """Tag a docker tag with a new tag This uses bash to run commands directly. Args: old_tag: The existing docker tag new_tag: The new docker tag """ cmd = ["docker", "tag", old_tag, new_tag] logger.info(f"Tagging {old_tag} as {new_tag}") return_code, _ = run_shell_command(cmd) if return_code != 0: raise ChildProcessError("Could not tag image for some reason!")
def publish_to_ivy(self): """Uses `sbt` to upload to Ivy. The jar will be build as a result of calling `sbt publish`. This means a prebuilt artifact is NOT required to publish to Ivy. This will be a lean jar, containing only project code, no dependencies. This uses bash to run commands directly. Raises: ChildProcessError is the bash command was not successful """ version = self.env.artifact_tag postfix = "-SNAPSHOT" if not get_tag() else "" cmd = ["sbt", f'set version := "{version}{postfix}"', "publish"] return_code, _ = run_shell_command(cmd) if return_code != 0: raise ChildProcessError( "Could not publish the package for some reason!")