Class: Domo::Core

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/domo/core.rb

Instance Method Summary collapse

Constructor Details

#initializeCore

Returns a new instance of Core.



24
25
26
# File 'lib/domo/core.rb', line 24

def initialize
  @agent = Mechanize.new
end

Instance Method Details

#backup_config(target, dest) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/domo/core.rb', line 123

def backup_config(target, dest)
    list(target).each do |name|
      FileUtils.mkdir_p(dest) unless File.exists?(dest)
      file_to_write = "#{dest}/#{name}.xml"
      File.open(file_to_write, "w") do |f|
        f.write @agent.get_file("#{target}/job/#{url_encode(name)}/config.xml")
      end
      puts "backed up successfully: #{file_to_write}"
    end
end

#clone(from, to, job_list) ⇒ Object

ジョブ名一覧ファイルを元に、Jenkins AからJenkins BへJobをクローンする



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/domo/core.rb', line 92

def clone(from, to, job_list)
  raise "target file unreadable" unless File.readable? job_list

  (from) if auth_required?(from)
  (to) if auth_required?(to)
  File.read(job_list).each_line do |name|
    @agent.get("#{from}/job/#{url_encode(name.strip!)}/config.xml") do |res|
      create_job(to, name, res.body)
    end
  end
end

#create_job(target, name, config) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/domo/core.rb', line 142

def create_job(target, name, config)
  create_api = "#{target}/createItem?name=#{url_encode(name)}"
  @agent.post(create_api, config, {'Content-Type'=>'text/xml'})
  puts "#{name} successfully created at #{target}"

rescue Mechanize::ResponseCodeError => e
  case e.response_code
  when '400'
    puts "#{name} already exists, so just skiped."
  else
    raise e
  end
end

#create_jobs(target, config_dir) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/domo/core.rb', line 134

def create_jobs(target, config_dir)
  (target) if auth_required?(target)
  Dir.glob("#{config_dir}/*.xml").each do |file_name|
    job_name = File.split(file_name)[1].sub(/\.xml$/, "")
    create_job(target, job_name, File.read(file_name))
  end
end

#delete(target, job_list, dryrun = true) ⇒ Object

ジョブ名一覧ファイルをもとに対象のJenkinsからジョブを削除する 破壊的なメソッドなのでdryrunがデフォルト



81
82
83
84
85
86
87
88
89
# File 'lib/domo/core.rb', line 81

def delete(target, job_list, dryrun=true)
  raise "target file unreadable" unless File.readable? job_list
  (target) if auth_required?(target)
  puts "!!dryrun mode!!" if dryrun
  File.read(job_list).each_line do |name|
    @agent.post("#{target}/job/#{url_encode(name.strip!)}/doDelete") unless dryrun
    puts "#{name} deleted"
  end
end

#delete_all(target, dryrun = true) ⇒ Object

対象のJenkinsから全てを削除する 破壊的なメソッドなのでdryrunがデフォルト



71
72
73
74
75
76
77
# File 'lib/domo/core.rb', line 71

def delete_all(target, dryrun=true)
  puts "!!dryrun mode!!" if dryrun
  list(target).each do |name|
    @agent.post("#{target}/job/#{url_encode(name)}/doDelete") unless dryrun
    puts "#{name} deleted"
  end
end

#install(target, plugin_list) ⇒ Object

プラグイン一覧ファイルを元にJenkinsにプラグインをインストールする プラグイン一覧ファイルの内容はインストール可能なプラグインページのチェックボックスのname属性値



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/domo/core.rb', line 106

def install(target, plugin_list)
  (target) if auth_required?(target)
  form = @agent.get("#{target}/pluginManager/available").form_with(:action => 'install')
  File.read(plugin_list).each_line do |plugin|
    form.checkbox_with(:name => plugin.strip!) do |checkbox|
      puts "trying to mark: #{plugin}"
      if checkbox != nil
        puts "  => #{plugin} marked as an install target."
        checkbox.check
      end
    end
  end
  form.submit
  puts "plugins installed, please restart your butler after installation done."
  puts "see #{target}/updateCenter/ for instalation progress."
end

#list(target, verbose = false) ⇒ Object

対象とするJenkinsのジョブの一覧を返す



29
30
31
32
33
34
35
36
37
# File 'lib/domo/core.rb', line 29

def list(target, verbose=false)
  (target) if auth_required?(target)
  jenkins_info = Nokogiri.XML(@agent.get_file("#{target}/api/xml"))
  jenkins_info.css("job>name").map do |name|
    result = name.text
    puts result if verbose
    result
  end
end

#toggle(target, action, job_list) ⇒ Object

ジョブ名一覧ファイルを元に対象のJenkins上にあるジョブを有効/無効化する



60
61
62
63
64
65
66
67
# File 'lib/domo/core.rb', line 60

def toggle(target, action, job_list)
  raise "target file unreadable" unless File.readable? job_list
  File.read(job_list).each_line do |name|
    encoded_name = url_encode(name.strip!)
    @agent.post("#{target}/job/#{encoded_name}/#{action}")
    puts "#{name} #{action}d."
  end
end

#toggle_all(target, action) ⇒ Object

対象のJenkins上にある全てのジョブを有効/無効化する



52
53
54
55
56
57
# File 'lib/domo/core.rb', line 52

def toggle_all(target, action)
  list(target).each do |name|
    @agent.post("#{target}/job/#{url_encode(name)}/#{action}")
    puts "#{name} #{action}d."
  end
end

#trigger(target, job_list, dryrun = true) ⇒ Object

ジョブ名一覧ファイルをもとにビルドをトリガリングする



40
41
42
43
44
45
46
47
48
49
# File 'lib/domo/core.rb', line 40

def trigger(target, job_list, dryrun=true)
  raise "target file unreadable" unless File.readable? job_list
  puts "!!dryrun mode!!" if dryrun
  (target) if auth_required?(target)
  File.read(job_list).each_line do |name|
    encoded_name = url_encode(name.strip!)
    @agent.post("#{target}/job/#{encoded_name}/build") unless dryrun
    puts "#{name} triggerd"
  end
end