Class: Bosh::Director::Jobs::UpdateStemcell

Inherits:
BaseJob show all
Includes:
DownloadHelper, ValidationHelper
Defined in:
lib/bosh/director/jobs/update_stemcell.rb

Constant Summary collapse

UPDATE_STEPS =
5

Instance Attribute Summary

Attributes inherited from BaseJob

#task_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DownloadHelper

#download_remote_file

Methods included from ValidationHelper

#invalid_type, #safe_property

Methods inherited from BaseJob

#begin_stage, #event_log, #logger, perform, #result_file, #single_step_stage, #task_cancelled?, #task_checkpoint, #track_and_log

Constructor Details

#initialize(stemcell_file, options = {}) ⇒ UpdateStemcell

Returns a new instance of UpdateStemcell.

Parameters:

  • stemcell_file (String)

    Stemcell tarball path



18
19
20
21
22
23
# File 'lib/bosh/director/jobs/update_stemcell.rb', line 18

def initialize(stemcell_file, options = {})
  @stemcell_file = stemcell_file
  @cloud = Config.cloud
  @stemcell_manager = Api::StemcellManager.new
  @remote_stemcell = options['remote'] || false
end

Class Method Details

.job_typeObject



13
14
15
# File 'lib/bosh/director/jobs/update_stemcell.rb', line 13

def self.job_type
  :update_stemcell
end

Instance Method Details

#download_remote_stemcell(downloaded_stemcell_dir) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/bosh/director/jobs/update_stemcell.rb', line 94

def download_remote_stemcell(downloaded_stemcell_dir)
  track_and_log("Downloading remote stemcell") do
    downloaded_stemcell_file = File.join(downloaded_stemcell_dir, "stemcell-#{SecureRandom.uuid}")
    download_remote_file('stemcell', @stemcell_file, downloaded_stemcell_file)
    @stemcell_file = downloaded_stemcell_file          
  end
end

#performObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# File 'lib/bosh/director/jobs/update_stemcell.rb', line 25

def perform
  logger.info("Processing update stemcell")
  event_log.begin_stage("Update stemcell", update_steps)

  if @remote_stemcell
    downloaded_stemcell_dir = Dir.mktmpdir("downloaded-stemcell")        
    download_remote_stemcell(downloaded_stemcell_dir)
  end
  
  stemcell_dir = Dir.mktmpdir("stemcell")

  track_and_log("Extracting stemcell archive") do
    result = Bosh::Exec.sh("tar -C #{stemcell_dir} -xzf #{@stemcell_file} 2>&1", :on_error => :return)
    if result.failed?
      logger.error("Extracting stemcell archive failed in dir #{stemcell_dir}, " +
                   "tar returned #{result.exit_status}, " +
                   "output: #{result.output}")
      raise StemcellInvalidArchive, "Extracting stemcell archive failed. Check task debug log for details."
    end
  end

  track_and_log("Verifying stemcell manifest") do
    stemcell_manifest_file = File.join(stemcell_dir, "stemcell.MF")
    stemcell_manifest = Psych.load_file(stemcell_manifest_file)

    @name = safe_property(stemcell_manifest, "name", :class => String)
    @version = safe_property(stemcell_manifest, "version", :class => String)
    @cloud_properties = safe_property(stemcell_manifest, "cloud_properties", :class => Hash, :optional => true)
    @sha1 = safe_property(stemcell_manifest, "sha1", :class => String)

    logger.info("Found stemcell image `#{@name}/#{@version}', " +
                "cloud properties are #{@cloud_properties.inspect}")

    logger.info("Verifying stemcell image")
    @stemcell_image = File.join(stemcell_dir, "image")
    unless File.file?(@stemcell_image)
      raise StemcellImageNotFound, "Stemcell image not found"
    end
  end

  track_and_log("Checking if this stemcell already exists") do
    if @stemcell_manager.stemcell_exists?(@name, @version)
      raise StemcellAlreadyExists,
            "Stemcell `#{@name}/#{@version}' already exists"
    end
  end

  stemcell = Models::Stemcell.new
  stemcell.name = @name
  stemcell.version = @version
  stemcell.sha1 = @sha1

  track_and_log("Uploading stemcell #{@name}/#{@version} to the cloud") do
    stemcell.cid =
      @cloud.create_stemcell(@stemcell_image, @cloud_properties)
    logger.info("Cloud created stemcell: #{stemcell.cid}")
  end

  track_and_log("Save stemcell #{@name}/#{@version} (#{stemcell.cid})") do
    stemcell.save
  end

  "/stemcells/#{stemcell.name}/#{stemcell.version}"
ensure
  FileUtils.rm_rf(stemcell_dir) if stemcell_dir
  FileUtils.rm_rf(downloaded_stemcell_dir) if downloaded_stemcell_dir
  FileUtils.rm_rf(@stemcell_file) if @stemcell_file
end