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.



47
48
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
80
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
# File 'lib/cli/job_builder.rb', line 47

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"]
  @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 (should resolve to a hash)"
  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)

  at_exit { FileUtils.rm_rf(build_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

#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

.run_prepare_script(script_path) ⇒ Object



10
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 10

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) }

    Dir.chdir(script_dir) do
      cmd = "./#{script_name} 2>&1"
      say("Running #{cmd}...")
      script_output = `#{cmd}`
      script_output.split("\n").each do |line|
        say("> #{line}")
      end
    end

    unless $?.exitstatus == 0
      raise InvalidJob, "`#{script_path}' script failed"
    end
  ensure
    ENV.each_pair { |k, v| ENV[k] = old_env[k] }
  end
end

Instance Method Details

#build_dirObject



145
146
147
# File 'lib/cli/job_builder.rb', line 145

def build_dir
  @build_dir ||= Dir.mktmpdir
end

#copy_filesObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cli/job_builder.rb', line 117

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



153
154
155
# File 'lib/cli/job_builder.rb', line 153

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

#final_builds_dirObject



157
158
159
# File 'lib/cli/job_builder.rb', line 157

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

#fingerprintObject



161
162
163
# File 'lib/cli/job_builder.rb', line 161

def fingerprint
  @fingerprint ||= make_fingerprint
end

#job_dirObject



149
150
151
# File 'lib/cli/job_builder.rb', line 149

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

#monit_filesObject



165
166
167
168
169
170
171
# File 'lib/cli/job_builder.rb', line 165

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



141
142
143
# File 'lib/cli/job_builder.rb', line 141

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

#reloadObject



173
174
175
176
177
# File 'lib/cli/job_builder.rb', line 173

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