Class: Jenkins::Peace::WarFile

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins/peace/war_file.rb

Constant Summary collapse

JENKINS_VERSION_REGEX =
/jenkins-core-(1\.\d{3}).jar/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, opts = {}) ⇒ WarFile

Returns a new instance of WarFile.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jenkins/peace/war_file.rb', line 25

def initialize(version, opts = {})
  @version      = version
  @lib_path     = opts.fetch(:lib_path, '')
  @base_path    = opts.fetch(:base_path, '')
  @base_url     = opts.fetch(:base_url, '')
  @server_path  = opts.fetch(:server_path, '')
  @logger       = opts.fetch(:logger, $stdout)

  @file_name    = 'jenkins.war'
  @base_dir     = File.join(base_path, version)
  @lib_dir      = File.join(lib_path, version)
  @klass_path   = File.join(lib_dir, '**/*.jar')
  @plugins_path = File.join(lib_dir, 'WEB-INF', 'plugins')
  @location     = File.join(base_dir, file_name)
  @url          = File.join(base_url, version, file_name)
end

Instance Attribute Details

#base_dirObject (readonly)

Returns the value of attribute base_dir.



17
18
19
# File 'lib/jenkins/peace/war_file.rb', line 17

def base_dir
  @base_dir
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



11
12
13
# File 'lib/jenkins/peace/war_file.rb', line 11

def base_path
  @base_path
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



12
13
14
# File 'lib/jenkins/peace/war_file.rb', line 12

def base_url
  @base_url
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



16
17
18
# File 'lib/jenkins/peace/war_file.rb', line 16

def file_name
  @file_name
end

#klass_pathObject (readonly)

Returns the value of attribute klass_path.



19
20
21
# File 'lib/jenkins/peace/war_file.rb', line 19

def klass_path
  @klass_path
end

#lib_dirObject (readonly)

Returns the value of attribute lib_dir.



18
19
20
# File 'lib/jenkins/peace/war_file.rb', line 18

def lib_dir
  @lib_dir
end

#lib_pathObject (readonly)

Returns the value of attribute lib_path.



10
11
12
# File 'lib/jenkins/peace/war_file.rb', line 10

def lib_path
  @lib_path
end

#locationObject (readonly)

Returns the value of attribute location.



21
22
23
# File 'lib/jenkins/peace/war_file.rb', line 21

def location
  @location
end

#loggerObject (readonly)

Returns the value of attribute logger.



14
15
16
# File 'lib/jenkins/peace/war_file.rb', line 14

def logger
  @logger
end

#plugins_pathObject (readonly)

Returns the value of attribute plugins_path.



20
21
22
# File 'lib/jenkins/peace/war_file.rb', line 20

def plugins_path
  @plugins_path
end

#server_pathObject (readonly)

Returns the value of attribute server_path.



13
14
15
# File 'lib/jenkins/peace/war_file.rb', line 13

def server_path
  @server_path
end

#urlObject (readonly)

Returns the value of attribute url.



22
23
24
# File 'lib/jenkins/peace/war_file.rb', line 22

def url
  @url
end

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/jenkins/peace/war_file.rb', line 9

def version
  @version
end

Instance Method Details

#build_command_line(options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/jenkins/peace/war_file.rb', line 112

def build_command_line(options = {})
  home    = options.fetch(:home, server_path)
  port    = options.fetch(:port, 3001).to_i
  control = options.fetch(:control, 3002).to_i
  daemon  = options.fetch(:daemon, false)
  logfile = options.fetch(logfile, nil)

  java_tmp = File.join(home, 'javatmp')
  FileUtils.mkdir_p java_tmp

  ENV['HUDSON_HOME'] = home
  cmd = ['java', "-Djava.io.tmpdir=#{java_tmp}", '-jar', location]
  cmd << '--daemon' if daemon
  cmd << "--httpPort=#{port}"
  cmd << "--controlPort=#{control}"
  cmd << "--logfile=#{File.expand_path(logfile)}" if logfile
  cmd
end

#classpathObject



55
56
57
# File 'lib/jenkins/peace/war_file.rb', line 55

def classpath
  File.join(lib_dir, 'WEB-INF', 'lib', "jenkins-core-#{real_version}.jar")
end

#download!Object



75
76
77
78
# File 'lib/jenkins/peace/war_file.rb', line 75

def download!
  FileUtils.mkdir_p base_dir
  fetch_content(url, location)
end

#exists?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/jenkins/peace/war_file.rb', line 60

def exists?
  File.exists?(location)
end

#install!Object



81
82
83
84
# File 'lib/jenkins/peace/war_file.rb', line 81

def install!
  download!
  unpack!
end

#installed?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/jenkins/peace/war_file.rb', line 70

def installed?
  exists? && unpacked?
end

#latest_version?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/jenkins/peace/war_file.rb', line 43

def latest_version?
  version == 'latest'
end

#real_versionObject



48
49
50
51
52
# File 'lib/jenkins/peace/war_file.rb', line 48

def real_version
  return version unless latest_version?
  klass = find_core_librairy
  klass.nil? ? nil : klass.match(JENKINS_VERSION_REGEX)[1]
end

#remove!Object



87
88
89
90
# File 'lib/jenkins/peace/war_file.rb', line 87

def remove!
  FileUtils.rm_rf base_dir
  FileUtils.rm_rf lib_dir
end

#start!(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jenkins/peace/war_file.rb', line 99

def start!(options = {})
  control = options.fetch(:control, 3002).to_i
  kill    = options.fetch(:kill, false)

  if kill
    TCPSocket.open('localhost', control) { |sock| sock.write('0') }
  else
    command = build_command_line(options)
    exec(*command)
  end
end

#unpack!Object



93
94
95
96
# File 'lib/jenkins/peace/war_file.rb', line 93

def unpack!
  FileUtils.mkdir_p(lib_dir)
  execute_command("cd #{lib_dir} && jar xvf #{location}")
end

#unpacked?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/jenkins/peace/war_file.rb', line 65

def unpacked?
  File.exists?(lib_dir) && File.exists?(classpath)
end