Class: Kscript::KkJenkinsManageUtils

Inherits:
Base
  • Object
show all
Defined in:
lib/kscript/plugins/kk_jenkins_manage_utils.rb

Instance Attribute Summary

Attributes inherited from Base

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#human_output?, inherited, #with_error_handling

Constructor Details

#initialize(*args, **opts) ⇒ KkJenkinsManageUtils

Returns a new instance of KkJenkinsManageUtils.



12
13
14
15
16
17
18
19
20
21
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 12

def initialize(*args, **opts)
  super
  jenkins_url, user, password = args
  @jenkins_url = jenkins_url
  @user = user
  @password = password
  @auth_header = "Basic #{Base64.strict_encode64("#{@user}:#{@password}")}"
  @output = $stdout
  @output.sync = true
end

Class Method Details

.argumentsObject



107
108
109
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 107

def self.arguments
  '[subcommand] [options]'
end

.authorObject



119
120
121
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 119

def self.author
  'kk'
end

.descriptionObject



123
124
125
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 123

def self.description
  'Jenkins job export/import automation.'
end

.groupObject



115
116
117
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 115

def self.group
  'ci'
end

.usageObject



111
112
113
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 111

def self.usage
  "kscript jenkins_manage list --host=jenkins.local\nkscript jenkins_manage trigger --job=build"
end

Instance Method Details

#export_all_jobsObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 29

def export_all_jobs
  FileUtils.mkdir_p('jobs')

  job_names = get_all_job_names
  job_names.each do |job_name|
    config_xml = export_job(job_name)
    if config_xml
      File.write("jobs/#{job_name}.xml", config_xml)
      logger.kinfo("Exported job: #{job_name}")
    end
  end
end

#export_job(job_name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 78

def export_job(job_name)
  url = "#{@jenkins_url}/job/#{job_name}/config.xml"
  response = HTTPX.get(url, headers: { 'Authorization' => @auth_header })
  response = response.first if response.is_a?(Array)
  return response.body.to_s if response.status == 200

  logger.kerror("Error exporting job #{job_name}: #{response.status}")
  nil
rescue StandardError => e
  logger.kerror("Exception exporting job #{job_name}: #{e.message}")
  nil
end

#get_all_job_namesObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 62

def get_all_job_names
  url = "#{@jenkins_url}/api/json?tree=jobs[name]"
  response = HTTPX.get(url, headers: { 'Authorization' => @auth_header })
  response = response.first if response.is_a?(Array)
  if response.status == 200
    jobs = JSON.parse(response.body.to_s)['jobs']
    jobs.map { |job| job['name'] }
  else
    logger.kerror("Error fetching job list: #{response.status}")
    []
  end
rescue StandardError => e
  logger.kerror("Exception fetching job list: #{e.message}")
  []
end

#import_all_jobs_from_filesObject



52
53
54
55
56
57
58
59
60
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 52

def import_all_jobs_from_files
  job_files = Dir.glob('jobs/*.xml')
  job_files.each do |file_path|
    job_name = File.basename(file_path, '.xml')
    logger.kinfo("Importing or updating job: #{job_name}")
    config_xml = File.read(file_path)
    import_or_update_job(job_name, config_xml)
  end
end

#import_job_from_file(job_name) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 42

def import_job_from_file(job_name)
  file_path = "jobs/#{job_name}.xml"
  if File.exist?(file_path)
    config_xml = File.read(file_path)
    import_or_update_job(job_name, config_xml)
  else
    logger.kerror("Job file #{file_path} does not exist!")
  end
end

#import_or_update_job(job_name, config_xml) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 91

def import_or_update_job(job_name, config_xml)
  url = "#{@jenkins_url}/job/#{job_name}/config.xml"
  logger.kinfo(url)
  begin
    logger.kinfo("Creating new job #{job_name}")
    create_new_job(job_name, config_xml)
  rescue StandardError
    logger.kinfo("Updating existing job #{job_name}")
    response = HTTPX.put(url, body: config_xml, headers: {
                           'Authorization' => @auth_header,
                           'Content-Type' => 'application/xml'
                         })
    response.first if response.is_a?(Array)
  end
end

#runObject



23
24
25
26
27
# File 'lib/kscript/plugins/kk_jenkins_manage_utils.rb', line 23

def run
  with_error_handling do
    logger.kinfo('Jenkins job manager executed.')
  end
end