Class: JenkinsJob::JenkinsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyjobbuilderdsl/jenkins_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_url, username, password) ⇒ JenkinsClient

Returns a new instance of JenkinsClient.



11
12
13
14
15
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 11

def initialize(server_url, username, password)
  @server_url = server_url
  @username = username
  @password = password
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 9

def password
  @password
end

#server_urlObject (readonly)

Returns the value of attribute server_url.



9
10
11
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 9

def server_url
  @server_url
end

#usernameObject (readonly)

Returns the value of attribute username.



9
10
11
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 9

def username
  @username
end

Instance Method Details

#delete_job(name) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 94

def delete_job(name)
  $stdout.puts "deleting job #{name}"
  if job_exists?(name)
    request = Net::HTTP::Post.new("/job/#{name}/doDelete")
    response = send_request(request)
    $stderr.puts response.body if response.code == '200'
  else
    $stdout.puts "job #{name} doesn't exists"
  end
end

#delete_view(name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 83

def delete_view(name)
  $stdout.puts "deleting view #{name}"
  if view_exists?(name)
    request = Net::HTTP::Post.new("/view/#{name}/doDelete")
    response = send_request(request)
    $stderr.puts response.body if response.code == '200'
  else
    $stdout.puts "view #{name} doesn't exists"
  end
end

#disable_job(name) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 115

def disable_job(name)
  $stdout.puts "disabling job #{name}"
  if job_exists?(name)
    request = Net::HTTP::Post.new("/job/#{name}/disable")
    send_request(request)
  else
    $stdout.puts "job #{name} doesn't exists"
  end
end

#enable_job(name) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 125

def enable_job(name)
  $stdout.puts "enabling job #{name}"
  if job_exists?(name)
    request = Net::HTTP::Post.new("/job/#{name}/enable")
    send_request(request)
  else
    $stdout.puts "job #{name} doesn't exists"
  end
end

#groovysh(groovy_script) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 68

def groovysh(groovy_script)
  $stdout.puts 'running groovy script'
  request = Net::HTTP::Post.new('/scriptText')

  request.set_form_data('script' => groovy_script)

  response = send_request(request)

  $stderr.puts response.body if response.code == '200'

  response = send_request(request)

  $stderr.puts response.body if response.code == '200'
end

#jobs(filter) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 145

def jobs(filter)
  if jenkins_xml_doc
    jenkins_xml_doc.xpath('//job/name').map(&:text).select { |name| name =~ /#{filter}/ }
  else
    []
  end
end

#trigger_job(name) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 135

def trigger_job(name)
  $stdout.puts "buiding job #{name}"
  if job_exists?(name)
    request = Net::HTTP::Post.new("/job/#{name}/build")
    send_request(request)
  else
    $stdout.puts "job #{name} doesn't exists"
  end
end

#upload_file(remote_name, content) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 53

def upload_file(remote_name, content)
  base64_content = Base64.strict_encode64(content)

  groovy_script = <<-EOS
  import org.apache.commons.codec.binary.Base64
  Base64 coder = new Base64()
  env = System.getenv()
  new File(env['JENKINS_HOME'] + '/#{remote_name}').withWriter {
    out -> out.write(new String(coder.decode('#{base64_content}')))
  }
  EOS

  groovysh(groovy_script)
end

#upload_job(name, xml) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 17

def upload_job(name, xml)
  if job_exists?(name)
    $stderr.puts "updating job #{name}"
    request_uri = "/job/#{name}/config.xml"
  else
    $stderr.puts "creating job #{name}"
    request_uri = "/createItem?name=#{name}"
  end

  request = Net::HTTP::Post.new(request_uri)
  request.content_type = 'application/xml;charset=UTF-8'
  request.body = xml

  response = send_request(request)

  $stderr.puts response.body if response.code == '200'
end

#upload_view(name, xml) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 35

def upload_view(name, xml)
  if view_exists?(name)
    $stderr.puts "updating view #{name}"
    request_uri = "/view/#{name}/config.xml"
  else
    $stderr.puts "creating view #{name}"
    request_uri = "/createView?name=#{name}"
  end

  request = Net::HTTP::Post.new(request_uri)
  request.content_type = 'application/xml;charset=UTF-8'
  request.body = xml

  response = send_request(request)

  $stderr.puts response.body if response.code == '200'
end

#wipeout_workspace(name) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/rubyjobbuilderdsl/jenkins_client.rb', line 105

def wipeout_workspace(name)
  $stdout.puts "wiping out workspace of #{name}"
  if job_exists?(name)
    request = Net::HTTP::Post.new("/job/#{name}/doWipeOutWorkspace")
    send_request(request)
  else
    $stdout.puts "job #{name} doesn't exists"
  end
end