def execute_command(command, error=None): """ run command and raise HighlandError if command fails :param command: the command to run :param error: a message to include in the raised error if error is callable it is treated as a function that takes in process output and outputs and error """ try: proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1) except OSError as oerr: if oerr.errno == errno.ENOEXEC: errmsg = "Could not execute hook at '{}'. Is it missing a #! line?".format( command) raise errors.HighlandError(errmsg) else: raise collected_output = '' for line in iter(proc.stdout.readline, b''): if callable(error): collected_output += line public_log.info(line.decode("utf-8", "ignore")) result = proc.wait() if result != 0 and error: if callable(error): raise errors.HighlandError('{} ({})'.format( error(collected_output), result)) raise errors.HighlandError('{} ({})'.format(error, result))
def dockerfile_for_dir_path(build_path, dockerfile_path): dockerfile_path = dockerfile_path or 'Dockerfile' filename = os.path.join(build_path, dockerfile_path) if not os.path.isfile(filename): if os.path.isdir(filename): hint_dockerfile = os.path.join(filename, 'Dockerfile') raise errors.HighlandError( "Dockerfile location '{}' points to a directory." "Perhaps this was supposed to be the build path or " "you meant for '{}' to be the dockerfile location." .format(filename, hint_dockerfile)) else: raise errors.HighlandError("Dockerfile not found at {}".format( filename)) return os.path.abspath(build_path), dockerfile_path
def dockerfile_for_file_path(build_path, dockerfile_path): if dockerfile_path: build_path_dir, build_path_file = os.path.split(build_path) if build_path_file != dockerfile_path: raise errors.HighlandError( "Conflicting desired dockerfiles in {}: {}, {}".format( build_path_dir, build_path_file, dockerfile_path)) build_path, dockerfile_path = os.path.split(build_path) return os.path.abspath(build_path), dockerfile_path
def push(client, repo, tags, retry_count=5): error = None for try_index in range(retry_count): retry_delay(try_index) # delay between retries error = try_push(client, repo, tags) if error is None: break # no error? let's get out of here else: # all retries were extinguished raise errors.HighlandError(error or "Error pushing tags")
def resolve_dockerfile_path(build_path, dockerfile_path): """ Return the real Dockerfile name. """ dockerfile_path = utils.clean_path(dockerfile_path, False) if os.path.isfile(build_path): return dockerfile_for_file_path(build_path, dockerfile_path) elif os.path.isdir(build_path): return dockerfile_for_dir_path(build_path, dockerfile_path) else: raise errors.HighlandError("Build path does not exist: {}".format( build_path))
def get_clone_commands(source_type, source_url, source_branch, source_commit=None): """ Return a list of command parts suitable for Popen that will clone the source of the build context """ delegates = { 'git': clone_commands_for_git, 'hg': clone_commands_for_hg, } delegate = delegates.get(source_type) if not delegate: options = ", ".join(delegates.keys()) msg = "Invalid SCM type: %r must be one of {}" % options raise errors.HighlandError(msg) return delegate(source_url, source_branch, source_commit)
def post_to_url(post_spec, file_path, attempts=5): if not post_spec: return for try_index in range(attempts): with open(file_path, 'rb') as fd: try: response = requests.post(post_spec['url'], data=post_spec['fields'], files={'file': fd}) if response.status_code != 204: private_log.info("post error", code=response.status_code, text=response.text) else: return True except: private_log.exception("post failure", file_path=file_path, post_spec=post_spec) else: raise errors.HighlandError("Could not post to url {}".format( post_spec['url']))
def process_error_line(line): value = line['error'] if isinstance(value, (str, unicode)): raise errors.HighlandError(value)
def test_highland_error(self): self.mock_runner.setup.side_effect = errors.HighlandError() result = runner.BuildRunner._run(self.mock_runner) assert result._mock_new_parent == self.mock_runner.handle_highland_error assert self.mock_runner.cleanup.called
def handle_test_result(result, test_path): if result: msg = 'executing {} ({})'.format(test_path, result) raise errors.HighlandError(msg) else: public_log.info('Tests in {} succeeded'.format(test_path))