module AutomateSoup

Top level module

Constants

VERSION

Attributes

api[RW]
credentials[RW]
enterprise[RW]
organization[RW]
pipeline[RW]
project[RW]
url[RW]

Public Class Methods

approve_change(enterprise: @enterprise, organization: @organization, project: @project, pipeline: @pipeline, topic: nil, wait: false, timeout: 10, retries: 5) click to toggle source

Approve a change by change topic.

@option enterprise [String] the enterprise to fetch org from, defaults to default. @option organization [String] the organization to fetch from. @option project [String] the project to fetch from. @option pipeline [String] the pipeline to fetch from. @option topic [String] the change topic to approve @option wait [Boolean] to wait for the approval stages to complete. @option timeout [Integer] the time in seconds to wait between requests defaults to 10 @option retries [Integer] the amount of retries to make, defaults to 5

@return [Boolean] true if the change was approved, false otherwise.

# File lib/automate_soup.rb, line 160
def approve_change(enterprise: @enterprise, organization: @organization, project: @project, pipeline: @pipeline, topic: nil, wait: false, timeout: 10, retries: 5)
  o = self.change_by_topic(
    enterprise: enterprise,
    organization: organization,
    project: project,
    pipeline: pipeline,
    topic: topic
  )

  return true if !o.nil? && o.delivered?

  if wait && !o.approvable? && !o.deliverable?
    times = 1
    while times <= retries
      o = self.change_by_topic(
        enterprise: enterprise,
        organization: organization,
        project: project,
        pipeline: pipeline,
        topic: topic
      )
      break if o.approvable?
      return false if o.current_stage.failed?
      puts "Stage #{o.current_stage.stage}: #{o.current_stage.status} retries #{times}/#{retries}"
      sleep timeout
      times += 1
    end
  end

  app = o.approve
  return true if !wait && !app.nil? && o.deliverable?
  if app.nil? && !o.deliverable?
    puts "Could not approve change #{o.current_stage.stage}: #{o.current_stage.status}"
    return false
  end

  times = 1
  while times <= retries
    o = self.change_by_topic(
      enterprise: enterprise,
      organization: organization,
      project: project,
      pipeline: pipeline,
      topic: topic
    )
    break if o.deliverable?
    return false if o.current_stage.failed?
    puts "Stage #{o.current_stage.stage}: #{o.current_stage.status} retries #{times}/#{retries}"
    times += 1
    sleep timeout
  end
  true
end
change_by_topic(enterprise: @enterprise, organization: @organization, project: @project, pipeline: @pipeline, topic: nil) click to toggle source

Find a change by topic.

@option enterprise [String] the enterprise to fetch org from, defaults to default. @option organization [String] the organization to fetch from. @option project [String] the project to fetch from. @option pipeline [String] the pipeline to fetch from. @option topic [String] the topic to fetch a change from.

@return [AutomateSoup::Change] the change coresponding to the given topic.

# File lib/automate_soup.rb, line 133
def change_by_topic(enterprise: @enterprise, organization: @organization, project: @project, pipeline: @pipeline, topic: nil)
  o = self.pipeline(
    enterprise: enterprise,
    organization: organization,
    project: project,
    pipeline: pipeline
  ).select { |p| p.topic.eql?(topic) }
  first = o.first
  raise "Cannot find topic #{topic} in #{o}" if first.nil?
  AutomateSoup::Change.new first
end
deliver_change(enterprise: @enterprise, organization: @organization, project: @project, pipeline: @pipeline, topic: nil, wait: false, timeout: 10, retries: 5) click to toggle source

Delivery a change by a topic

@option enterprise [String] the enterprise to fetch org from, defaults to default. @option organization [String] the organization to fetch from. @option project [String] the project to fetch from. @option pipeline [String] the pipeline to fetch from. @option topic [String] the change topic to approve @option wait [Boolean] to wait for the approval stages to complete. @option timeout [Integer] the time in seconds to wait between requests defaults to 10 @option retries [Integer] the amount of retries to make, defaults to 5

@return [Boolean] true if the change was delivered, false otherwise.

# File lib/automate_soup.rb, line 230
def deliver_change(enterprise: @enterprise, organization: @organization, project: @project, pipeline: @pipeline, topic: nil, wait: false, timeout: 10, retries: 5)
  o = self.change_by_topic(
    enterprise: enterprise,
    organization: organization,
    project: project,
    pipeline: pipeline,
    topic: topic
  )
  return false if !o.deliverable?
  if wait && !o.deliverable?
    times = 1
    while times <= retries
      o = self.change_by_topic(
        enterprise: enterprise,
        organization: organization,
        project: project,
        pipeline: pipeline,
        topic: topic
      )
      break if o.deliverable?
      return false if o.current_stage.failed?
      puts "Stage #{o.current_stage.stage}: #{o.current_stage.status} retries #{times}/#{retries}"
      sleep timeout
      times += 1
    end
  end

  de = o.deliver
  return true if !wait && !de.nil? && o.delivered?

  if de.nil? && !o.deliverable?
    puts "Could not deliver change #{o.current_stage.stage}: #{o.current_stage.status}"
    return false
  end

  times = 1
  while times <= retries
    o = self.change_by_topic(
      enterprise: enterprise,
      organization: organization,
      project: project,
      pipeline: pipeline,
      topic: topic
    )
    break if o.delivered?
    return false if o.current_stage.failed?
    puts "Stage #{o.current_stage.stage}: #{o.current_stage.status} retries #{times}/#{retries}"
    times += 1
    sleep timeout
  end
  true
end
orgs(enterprise = 'default') click to toggle source

Fetch all organizations under an enterprise

# File lib/automate_soup.rb, line 59
def orgs(enterprise = 'default')
  @api.orgs enterprise
end
pipeline_topics(enterprise: 'default', organization: nil, project: nil, pipeline: nil) click to toggle source

Filters out the topics from the pipelines changes .

@option enterprise [String] the enterprise to fetch org from, defaults to default. @option organization [String] the organization to fetch from. @option project [String] the project to fetch from. @option pipeline [String] the pipeline to fetch from.

@return [Array] an array of strings holding change topics.

# File lib/automate_soup.rb, line 113
def pipeline_topics(enterprise: 'default', organization: nil, project: nil, pipeline: nil)
  self.pipeline(
    enterprise: enterprise,
    organization: organization,
    project: project,
    pipeline: pipeline
  ).map { |p| p.topic }
end
pipelines(enterprise: 'default', organization: nil, project: nil) click to toggle source

Fetch all project pipelines under an enterprise, organization pair

@option enterprise [String] the enterprise to fetch org from, defaults to default. @option organization [String] the organization to fetch pipelines from. @option project [String] the project to fetch pipelines from.

# File lib/automate_soup.rb, line 82
def pipelines(enterprise: 'default', organization: nil, project: nil)
  @api.pipelines(enterprise: enterprise, organization: organization, project: project)
end
projects(enterprise: 'default', organization: nil) click to toggle source

Get the projects under and organization given the enterprise.

@option enterprise [String] the enterprise to fetch org from, defaults to default. @option organization [String] the organization to fetch projects from.

# File lib/automate_soup.rb, line 70
def projects(enterprise: 'default', organization: nil)
  @api.projects(enterprise: enterprise, organization: organization)
end
setup( url: nil, username: nil, token: nil, password: nil, enterprise: 'default', organization: nil, project: nil, pipeline: nil ) click to toggle source

Setup Automate Soup client.

@option url [String] The Chef Automate URL. @option username [String] The Chef Automate username. @option token [String] The Chef Automate user token. @option password [String] The Chef Automate user password.

# File lib/automate_soup.rb, line 24
def setup(
  url: nil,
  username: nil,
  token: nil,
  password: nil,
  enterprise: 'default',
  organization: nil,
  project: nil,
  pipeline: nil
)
  @url = url
  @credentials = if token
                   token_credentials(username, token)
                 else
                   password_credentials(username, password)
                 end
  @api = AutomateSoup::API.new(self)
  @enterprise = enterprise
  @organization = organization
  @project = project
  @pipeline = pipeline
  self
end
status() click to toggle source

Check the status of Automate

# File lib/automate_soup.rb, line 51
def status
  o = @api.status
  OpenStruct.new o
end

Private Class Methods

password_credentials(username, password) click to toggle source
# File lib/automate_soup.rb, line 285
def password_credentials(username, password)
  AutomateSoup::Credentials.new(
    username: username,
    password: password
  )
end
token_credentials(username, token) click to toggle source
# File lib/automate_soup.rb, line 292
def token_credentials(username, token)
  AutomateSoup::Credentials.new(
    username: username,
    token: token
  )
end