Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/rb_shift/build.rb
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions lib/rb_shift/build_config.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 7 additions & 2 deletions lib/rb_shift/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions lib/rb_shift/openshift_kind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ 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
patch = obj.to_json
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
Expand Down
6 changes: 6 additions & 0 deletions lib/rb_shift/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative 'secret'
require_relative 'service'
require_relative 'template'
require_relative 'build_config'
require_relative 'role_binding'

module RbShift
Expand Down Expand Up @@ -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
Expand Down