Class: Bosh::Cli::JobBuilder

Inherits:
Object
  • Object
show all
Includes:
PackagingHelper
Defined in:
lib/cli/job_builder.rb

Instance Attribute Summary collapse

Attributes included from PackagingHelper

#dry_run

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PackagingHelper

#build, #checksum, #dry_run?, #file_checksum, #final?, #generate_tarball, #init_indices, #new_version?, #notes, #older_version?, #tracked_permissions, #upload_tarball, #use_dev_version, #use_final_version

Methods included from VersionCalc

#major_version, #minor_version, #version_cmp, #version_greater, #version_less, #version_same

Constructor Details

#initialize(spec, release_dir, final, blobstore, built_packages = []) ⇒ JobBuilder

Returns a new instance of JobBuilder.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cli/job_builder.rb', line 83

def initialize(spec, release_dir, final, blobstore, built_packages = [])
  spec = load_yaml_file(spec) if spec.is_a?(String) && File.file?(spec)

  @name = spec["name"]
  @version = nil
  @tarball_path = nil
  @packages = spec["packages"].to_a
  @built_packages = built_packages.to_a
  @release_dir = release_dir
  @templates_dir = File.join(job_dir, "templates")
  @tarballs_dir = File.join(release_dir, "tmp", "jobs")
  @final = final
  @blobstore = blobstore
  @artefact_type = "job"

  case spec["templates"]
  when Hash
    @templates = spec["templates"].keys
  else
    raise InvalidJob, "Incorrect templates section in `#{@name}' " +
      "job spec (Hash expected, #{spec["properties"].class} given)"
  end

  if spec.has_key?("properties")
    if spec["properties"].is_a?(Hash)
      @properties = spec["properties"]
    else
      raise InvalidJob, "Incorrect properties section in `#{@name}' " +
        "job spec (Hash expected, #{spec["properties"].class} given)"
    end
  else
    @properties = {}
  end

  if @name.blank?
    raise InvalidJob, "Job name is missing"
  end

  if @templates.nil?
    raise InvalidJob, "Please include templates section with at least 1 " +
        "(possibly dummy) file into `#{@name}' job spec"
  end

  unless @name.bosh_valid_id?
    raise InvalidJob, "`#{@name}' is not a valid BOSH identifier"
  end

  unless File.exists?(File.join(job_dir, "spec"))
    raise InvalidJob, "Cannot find spec file for '#{name}'"
  end

  if missing_packages.size > 0
    raise InvalidJob, "Some packages required by '#{name}' job " +
        "are missing: %s" % [missing_packages.join(", ")]
  end

  if missing_templates.size > 0
    raise InvalidJob, "Some template files required by '#{name}' job " +
        "are missing: %s" % [missing_templates.join(", ")]
  end

  if extra_templates.size > 0
    raise InvalidJob, "There are unused template files for job " +
        "'#{name}': %s" % [extra_templates.join(", ")]
  end

  unless monit_files.size > 0
    raise InvalidJob, "Cannot find monit file for '#{name}'"
  end

  @dev_builds_dir = File.join(@release_dir, ".dev_builds", "jobs", @name)
  @final_builds_dir = File.join(@release_dir, ".final_builds",
                                "jobs", @name)

  FileUtils.mkdir_p(job_dir)
  FileUtils.mkdir_p(@dev_builds_dir)
  FileUtils.mkdir_p(@final_builds_dir)

  init_indices
end

Instance Attribute Details

#built_packagesObject (readonly)

Returns the value of attribute built_packages.



7
8
9
# File 'lib/cli/job_builder.rb', line 7

def built_packages
  @built_packages
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/cli/job_builder.rb', line 7

def name
  @name
end

#packagesObject (readonly)

Returns the value of attribute packages.



7
8
9
# File 'lib/cli/job_builder.rb', line 7

def packages
  @packages
end

#propertiesHash (readonly)

Returns Properties defined in this job.

Returns:

  • (Hash)

    Properties defined in this job



11
12
13
# File 'lib/cli/job_builder.rb', line 11

def properties
  @properties
end

#release_dirObject (readonly)

Returns the value of attribute release_dir.



7
8
9
# File 'lib/cli/job_builder.rb', line 7

def release_dir
  @release_dir
end

#tarball_pathObject (readonly)

Returns the value of attribute tarball_path.



7
8
9
# File 'lib/cli/job_builder.rb', line 7

def tarball_path
  @tarball_path
end

#templatesObject (readonly)

Returns the value of attribute templates.



7
8
9
# File 'lib/cli/job_builder.rb', line 7

def templates
  @templates
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/cli/job_builder.rb', line 7

def version
  @version
end

Class Method Details

.discover(directory, options = {}) ⇒ Object

Parameters:

  • directory (String)

    Release directory

  • options (Hash) (defaults to: {})

    Build options



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cli/job_builder.rb', line 51

def self.discover(directory, options = {})
  builders = []

  Dir[File.join(directory, "jobs", "*")].each do |job_dir|
    next unless File.directory?(job_dir)
    job_dirname = File.basename(job_dir)

    prepare_script = File.join(job_dir, "prepare")
    if File.exists?(prepare_script)
      run_prepare_script(prepare_script)
    end

    job_spec = load_yaml_file(File.join(job_dir, "spec"))
    if job_spec["name"] != job_dirname
      raise InvalidJob,
            "Found `#{job_spec["name"]}' job in " +
            "`#{job_dirname}' directory, please fix it"
    end

    final = options[:final]
    dry_run = options[:dry_run]
    blobstore = options[:blobstore]
    package_names = options[:package_names]

    builder = new(job_spec, directory, final, blobstore, package_names)
    builder.dry_run = true if dry_run
    builders << builder
  end

  builders
end

.run_prepare_script(script_path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cli/job_builder.rb', line 13

def self.run_prepare_script(script_path)
  unless File.exists?(script_path)
    raise InvalidJob, "Prepare script at `#{script_path}' doesn't exist"
  end

  unless File.executable?(script_path)
    raise InvalidJob, "Prepare script at `#{script_path}' is not executable"
  end

  old_env = ENV

  script_dir = File.dirname(script_path)
  script_name = File.basename(script_path)

  begin
    # We need to temporarily delete some rubygems related artefacts
    # because preparation scripts shouldn't share any assumptions
    # with CLI itself
    %w{ BUNDLE_GEMFILE RUBYOPT }.each { |key| ENV.delete(key) }

    output = nil
    Dir.chdir(script_dir) do
      cmd = "./#{script_name} 2>&1"
      output = `#{cmd}`
    end

    unless $?.exitstatus == 0
      raise InvalidJob, "`#{script_path}' script failed: #{output}"
    end

    output
  ensure
    ENV.each_pair { |k, v| ENV[k] = old_env[k] }
  end
end

Instance Method Details

#all_templatesArray<String>

Returns full paths of all templates in the job (regular job templates and monit)

Returns:

  • (Array<String>)

    Returns full paths of all templates in the job (regular job templates and monit)



228
229
230
231
232
233
234
# File 'lib/cli/job_builder.rb', line 228

def all_templates
  regular_templates = @templates.map do |template|
    File.join(@templates_dir, template)
  end

  regular_templates.sort + monit_files
end

#build_dirObject



192
193
194
# File 'lib/cli/job_builder.rb', line 192

def build_dir
  @build_dir ||= Dir.mktmpdir
end

#copy_filesObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cli/job_builder.rb', line 164

def copy_files
  FileUtils.mkdir_p(File.join(build_dir, "templates"))
  copied = 0

  templates.each do |template|
    src = File.join(@templates_dir, template)
    dst = File.join(build_dir, "templates", template)
    FileUtils.mkdir_p(File.dirname(dst))

    FileUtils.cp(src, dst, :preserve => true)
    copied += 1
  end

  monit_files.each do |file|
    FileUtils.cp(file, build_dir, :preserve => true)
    copied += 1
  end

  FileUtils.cp(File.join(job_dir, "spec"), File.join(build_dir, "job.MF"),
               :preserve => true)
  copied += 1
  copied
end

#dev_builds_dirObject



200
201
202
# File 'lib/cli/job_builder.rb', line 200

def dev_builds_dir
  File.join(@release_dir, ".dev_builds", "jobs", name)
end

#final_builds_dirObject



204
205
206
# File 'lib/cli/job_builder.rb', line 204

def final_builds_dir
  File.join(@release_dir, ".final_builds", "jobs", name)
end

#fingerprintObject



208
209
210
# File 'lib/cli/job_builder.rb', line 208

def fingerprint
  @fingerprint ||= make_fingerprint
end

#job_dirObject



196
197
198
# File 'lib/cli/job_builder.rb', line 196

def job_dir
  File.join(@release_dir, "jobs", @name)
end

#monit_filesObject



212
213
214
215
216
217
218
# File 'lib/cli/job_builder.rb', line 212

def monit_files
  glob = File.join(job_dir, '*.monit')
  files = Dir.glob(glob)
  monit = File.join(job_dir, "monit")
  files << monit if File.exist?(monit)
  files
end

#prepare_filesObject



188
189
190
# File 'lib/cli/job_builder.rb', line 188

def prepare_files
  File.join(job_dir, "prepare")
end

#reloadObject



220
221
222
223
224
# File 'lib/cli/job_builder.rb', line 220

def reload
  @fingerprint = nil
  @build_dir   = nil
  self
end