diff --git a/lib/rb_shift/build.rb b/lib/rb_shift/build.rb new file mode 100644 index 0000000..2d47ea0 --- /dev/null +++ b/lib/rb_shift/build.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require_relative 'openshift_kind' + +module RbShift + # Representation of OpenShift build + class Build < OpenshiftKind + def phase + obj[:status][:phase] + end + + def running? + reload + phase == 'Running' || phase == 'Pending' + end + + def completed? + reload + phase == 'Completed' + end + end +end diff --git a/lib/rb_shift/build_config.rb b/lib/rb_shift/build_config.rb new file mode 100644 index 0000000..38f4399 --- /dev/null +++ b/lib/rb_shift/build_config.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require_relative 'openshift_kind' +require_relative 'build' + +module RbShift + # Representation of OpenShift build config + class BuildConfig < OpenshiftKind + def builds(update = false) + bc_label = 'openshift.io/build-config.name'.to_sym + if update || @_builds.nil? + items = @parent.client + .get('builds', namespace: @parent.name) + .select { |item| item[:metadata][:annotations][bc_label] == name } + + @_builds = items.each_with_object({}) do |item, hash| + resource = Build.new(self, item) + hash[resource.name] = resource + end + end + @_builds + end + + def running?(reload = false) + builds(true) if reload + !builds.values.select(&:running?).empty? + end + + def wait_for_build(timeout: 60, polling: 5) + Timeout.timeout(timeout) do + log.info "Waiting for builds of #{name} to be finished for #{timeout} seconds..." + loop do + log.debug "--> Checking builds after #{polling} seconds..." + sleep polling + break unless running?(true) + end + end + log.info 'Build finished' + end + + def start_build(block: false, timeout: 60, polling: 5, **opts) + log.info "Starting build from BuildConfig #{name} with options #{opts}" + @parent.execute('start-build ', name, **opts) + sleep polling * 2 + builds(true) + wait_for_build(timeout: timeout, polling: polling) if block + end + end +end diff --git a/lib/rb_shift/client.rb b/lib/rb_shift/client.rb index 17bd9fc..ec55761 100644 --- a/lib/rb_shift/client.rb +++ b/lib/rb_shift/client.rb @@ -105,13 +105,18 @@ def execute(command, *args, **opts) log.debug("[EXEC] Executing command #{command} with opts: #{opts}") oc_cmd = oc_command(command, *args, **opts) stdout, stderr, stat = Open3.capture3(oc_cmd) - unless stderr.empty? && stat.success? + unless stat.success? log.error oc_command(command, *args, exclude_token: true, **opts) log.error "Command failed with status #{stat.exitstatus} -->" log.debug "Standard Output: #{stdout}" log.error "Error Output: #{stderr}" raise InvalidCommandError, "ERROR: #{stdout} #{stderr}" end + unless stderr.empty? + log.warn "Command succeeded with status #{stat.exitstatus}, but stderr is not empty -->" + log.debug "Standard Output: #{stdout}" + log.warn "Error Output: #{stderr}" + end end def wait_project_deletion(project_name, timeout = 1) @@ -137,7 +142,7 @@ def make_get_request(client, request_path) # rubocop:disable Metrics/LineLength def oc_command(command, *args, exclude_token: false, **opts) token = exclude_token ? '***' : @token - "oc --server=\"#{@url}\" --token=\"#{token}\" #{command} #{unfold_opts opts} #{unfold_args args}" + "oc --server=\"#{@url}\" --token=\"#{token}\" #{command} #{unfold_args args} #{unfold_opts opts}" end # rubocop:enable Metrics/LineLength diff --git a/lib/rb_shift/openshift_kind.rb b/lib/rb_shift/openshift_kind.rb index cfc222b..c394afa 100644 --- a/lib/rb_shift/openshift_kind.rb +++ b/lib/rb_shift/openshift_kind.rb @@ -38,7 +38,7 @@ def reload(self_only = false) invalidate unless self_only end - def update(patch = nil) + def update(patch = nil, type = 'strategic') if patch @parent.invalidate else @@ -46,11 +46,11 @@ def update(patch = nil) end log.info "Updating #{self.class.class_name} #{name}" - execute "patch #{self.class.class_name} #{name} -p #{patch.shellescape}" + execute "patch #{self.class.class_name} #{name} -p #{patch.shellescape}", :type => type end - def execute(command, **opts) - parent.execute(command, **opts) if parent.respond_to? :execute + def execute(command, *args, **opts) + parent.execute(command, *args, **opts) if parent.respond_to? :execute end def delete diff --git a/lib/rb_shift/project.rb b/lib/rb_shift/project.rb index 63a95a8..17f10db 100644 --- a/lib/rb_shift/project.rb +++ b/lib/rb_shift/project.rb @@ -7,6 +7,7 @@ require_relative 'secret' require_relative 'service' require_relative 'template' +require_relative 'build_config' require_relative 'role_binding' module RbShift @@ -53,6 +54,11 @@ def role_bindings(update = false) @_role_bindings end + def build_configs(update = false) + @_build_configs = init_objects(BuildConfig) if update || @_build_configs.nil? + @_build_configs + end + def routes(update = false) @_routes = init_objects(Route) if update || @_routes.nil? @_routes