Class: JenkinsLauncher::APIInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins_launcher/api_interface.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ APIInterface

Returns a new instance of APIInterface.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jenkins_launcher/api_interface.rb', line 30

def initialize(options)
  if options[:username] && options[:server_ip] && (options[:password] || options[:password_base64])
    creds = options
  elsif options[:creds_file]
    creds = YAML.load_file(File.expand_path(options[:creds_file], __FILE__))
  elsif File.exist?("#{ENV['HOME']}/.jenkins_api_client/login.yml")
    creds = YAML.load_file(File.expand_path("#{ENV['HOME']}/.jenkins_api_client/login.yml", __FILE__))
  else
    puts "Credentials are not set. Please pass them as parameters or set them in the default credentials file"
    exit 1
  end
  @client = JenkinsApi::Client.new(creds)
end

Instance Method Details

#build_job(name) ⇒ Object



48
49
50
# File 'lib/jenkins_launcher/api_interface.rb', line 48

def build_job(name)
  @client.job.build(name) unless @client.job.get_current_build_status(name) == 'running'
end

#create_job(params) ⇒ Object



44
45
46
# File 'lib/jenkins_launcher/api_interface.rb', line 44

def create_job(params)
  @client.job.create_freestyle(params) unless @client.job.exists?(params[:name])
end

#delete_job(name) ⇒ Object



64
65
66
# File 'lib/jenkins_launcher/api_interface.rb', line 64

def delete_job(name)
  @client.job.delete(name)
end

#display_progressive_console_output(name, refresh_rate) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jenkins_launcher/api_interface.rb', line 72

def display_progressive_console_output(name, refresh_rate)
  debug_changed = false
  if @client.debug == true
    @client.debug = false
    debug_changed = true
  end

  response = @client.job.get_console_output(name)
  puts response['output'] unless response['more']
  while response['more']
    size = response['size']
    puts response['output'] unless response['output'].chomp.empty?
    sleep refresh_rate
    response = @client.job.get_console_output(name, 0, size)
  end
  # Print the last few lines
  puts response['output'] unless response['output'].chomp.empty?
  @client.toggle_debug if debug_changed
end

#get_job_status(name) ⇒ Object



68
69
70
# File 'lib/jenkins_launcher/api_interface.rb', line 68

def get_job_status(name)
  @client.job.get_current_build_status(name)
end

#job_building?(name) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/jenkins_launcher/api_interface.rb', line 60

def job_building?(name)
  @client.job.get_current_build_status(name) == 'running'
end

#job_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/jenkins_launcher/api_interface.rb', line 56

def job_exists?(name)
  @client.job.exists?(name)
end

#stop_job(name) ⇒ Object



52
53
54
# File 'lib/jenkins_launcher/api_interface.rb', line 52

def stop_job(name)
  @client.job.stop_build(name)
end

#use_node(job_name, node_name) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/jenkins_launcher/api_interface.rb', line 92

def use_node(job_name, node_name)
  if @client.node.list.include?(node_name)
    @client.job.restrict_to_node(job_name, node_name)
  else
    puts "Node '#{node_name}' is not found on Jenkins server. Skipping...".yellow
  end
end