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, #tracked_permissions, #upload_tarball, #use_dev_version, #use_final_version

Constructor Details

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

Returns a new instance of JobBuilder.



81
82
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
# File 'lib/cli/job_builder.rb', line 81

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.



5
6
7
# File 'lib/cli/job_builder.rb', line 5

def built_packages
  @built_packages
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/cli/job_builder.rb', line 5

def name
  @name
end

#packagesObject (readonly)

Returns the value of attribute packages.



5
6
7
# File 'lib/cli/job_builder.rb', line 5

def packages
  @packages
end

#propertiesHash (readonly)

Returns Properties defined in this job.

Returns:

  • (Hash)

    Properties defined in this job



9
10
11
# File 'lib/cli/job_builder.rb', line 9

def properties
  @properties
end

#release_dirObject (readonly)

Returns the value of attribute release_dir.



5
6
7
# File 'lib/cli/job_builder.rb', line 5

def release_dir
  @release_dir
end

#tarball_pathObject (readonly)

Returns the value of attribute tarball_path.



5
6
7
# File 'lib/cli/job_builder.rb', line 5

def tarball_path
  @tarball_path
end

#templatesObject (readonly)

Returns the value of attribute templates.



5
6
7
# File 'lib/cli/job_builder.rb', line 5

def templates
  @templates
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/cli/job_builder.rb', line 5

def version
  @version
end

Class Method Details

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

Parameters:

  • directory (String)

    Release directory

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

    Build options



49
50
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
# File 'lib/cli/job_builder.rb', line 49

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



11
12
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
# File 'lib/cli/job_builder.rb', line 11

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)



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

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

  regular_templates.sort + monit_files
end

#build_dirObject



190
191
192
# File 'lib/cli/job_builder.rb', line 190

def build_dir
  @build_dir ||= Dir.mktmpdir
end

#copy_filesObject



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

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



198
199
200
# File 'lib/cli/job_builder.rb', line 198

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

#final_builds_dirObject



202
203
204
# File 'lib/cli/job_builder.rb', line 202

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

#fingerprintObject



206
207
208
# File 'lib/cli/job_builder.rb', line 206

def fingerprint
  @fingerprint ||= make_fingerprint
end

#job_dirObject



194
195
196
# File 'lib/cli/job_builder.rb', line 194

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

#monit_filesObject



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

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



186
187
188
# File 'lib/cli/job_builder.rb', line 186

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

#reloadObject



218
219
220
221
222
# File 'lib/cli/job_builder.rb', line 218

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