Class: Bosh::Director::DeploymentPlan::Steps::PackageCompileStep

Inherits:
Object
  • Object
show all
Includes:
LockHelper
Defined in:
lib/bosh/director/deployment_plan/steps/package_compile_step.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LockHelper

#with_compile_lock, #with_deployment_lock, #with_release_lock, #with_release_locks, #with_stemcell_lock

Constructor Details

#initialize(deployment_plan, cloud, logger, event_log, director_job) ⇒ PackageCompileStep

Returns a new instance of PackageCompileStep.

Parameters:



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
# File 'lib/bosh/director/deployment_plan/steps/package_compile_step.rb', line 13

def initialize(deployment_plan, cloud, logger, event_log, director_job)
  @deployment_plan = deployment_plan

  @cloud = cloud
  @event_log = event_log
  @logger = logger
  @director_job = director_job

  @tasks_mutex = Mutex.new
  @network_mutex = Mutex.new
  @counter_mutex = Mutex.new

  compilation_config = @deployment_plan.compilation

  @network = compilation_config.network
  @compilation_resources = compilation_config.cloud_properties
  @compilation_env = compilation_config.env

  @vm_reuser = VmReuser.new

  @compile_task_generator = CompileTaskGenerator.new(@logger, @event_log)

  @compile_tasks = {}
  @ready_tasks = []
  @compilations_performed = 0
end

Instance Attribute Details

#compilations_performedObject (readonly)

Returns the value of attribute compilations_performed.



10
11
12
# File 'lib/bosh/director/deployment_plan/steps/package_compile_step.rb', line 10

def compilations_performed
  @compilations_performed
end

Instance Method Details

#compile_package(task) ⇒ Object



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/bosh/director/deployment_plan/steps/package_compile_step.rb', line 69

def compile_package(task)
  package = task.package
  stemcell = task.stemcell

  with_compile_lock(package.id, stemcell.id) do
    # Check if the package was compiled in a parallel deployment
    compiled_package = task.find_compiled_package(@logger, @event_log)
    if compiled_package.nil?
      build = Models::CompiledPackage.generate_build_number(package, stemcell)
      task_result = nil

      prepare_vm(stemcell) do |vm_data|
        .update(vm_data.vm, :compiling => package.name)
        agent_task =
          vm_data.agent.compile_package(package.blobstore_id,
                                        package.sha1, package.name,
                                        "#{package.version}.#{build}",
                                        task.dependency_spec)
        task_result = agent_task['result']
      end

      compiled_package = Models::CompiledPackage.create do |p|
        p.package = package
        p.stemcell = stemcell
        p.sha1 = task_result['sha1']
        p.build = build
        p.blobstore_id = task_result['blobstore_id']
        p.dependency_key = task.dependency_key
      end

      if Config.use_compiled_package_cache?
        if BlobUtil.exists_in_global_cache?(package, task.cache_key)
          @logger.info('Already exists in global package cache, skipping upload')
        else
          @logger.info('Uploading to global package cache')
          BlobUtil.save_to_global_cache(compiled_package, task.cache_key)
        end
      else
        @logger.info('Global blobstore not configured, skipping upload')
      end

      @counter_mutex.synchronize { @compilations_performed += 1 }
    end

    task.use_compiled_package(compiled_package)
  end
end

#compile_tasks_countObject



59
60
61
# File 'lib/bosh/director/deployment_plan/steps/package_compile_step.rb', line 59

def compile_tasks_count
  @compile_tasks.size
end

#performObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bosh/director/deployment_plan/steps/package_compile_step.rb', line 40

def perform
  @logger.info('Generating a list of compile tasks')
  prepare_tasks

  @compile_tasks.each_value do |task|
    if task.ready_to_compile?
      @logger.info("Package `#{task.package.desc}' is ready to be compiled for stemcell `#{task.stemcell.desc}'")
      @ready_tasks << task
    end
  end

  if @ready_tasks.empty?
    @logger.info('All packages are already compiled')
  else
    compile_packages
    director_job_checkpoint
  end
end

#prepare_vm(stemcell) {|VmData| ... } ⇒ Object

This method will create a VM for each stemcell in the stemcells array passed in. The VMs are yielded and their destruction is ensured.

Parameters:

  • stemcell (Models::Stemcell)

    The stemcells that need to have compilation VMs created.

Yields:

  • (VmData)

    Yields a VmData object that contains all the data for the VM that should be used for compilation. This may be a reused VM or a freshly created VM.



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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/bosh/director/deployment_plan/steps/package_compile_step.rb', line 124

def prepare_vm(stemcell)
  # If we're reusing VMs, try to just return an already-created VM.
  if @deployment_plan.compilation.reuse_compilation_vms
    vm_data = @vm_reuser.get_vm(stemcell)
    if vm_data
      @logger.info("Reusing compilation VM `#{vm_data.vm.cid}' for stemcell `#{stemcell.desc}'")
      begin
        yield vm_data
      ensure
        vm_data.release
      end
      return
    end
    # This shouldn't happen. If it does there's a bug.
    if @vm_reuser.get_num_vms(stemcell) >=
      @deployment_plan.compilation.workers
      raise PackageCompilationNotEnoughWorkersForReuse,
            'There should never be more VMs for a stemcell than the number of workers in reuse_compilation_vms mode'
    end
  end

  @logger.info("Creating compilation VM for stemcell `#{stemcell.desc}'")

  reservation = reserve_network

  network_settings = {
    @network.name => @network.network_settings(reservation)
  }

  vm = VmCreator.create(@deployment_plan.model, stemcell,
                        @compilation_resources, network_settings,
                        nil, @compilation_env)
  vm_data = @vm_reuser.add_vm(reservation, vm, stemcell, network_settings)

  @logger.info("Configuring compilation VM: #{vm.cid}")

  begin
    agent = AgentClient.with_defaults(vm.agent_id)
    agent.wait_until_ready
    agent.update_settings(Bosh::Director::Config.trusted_certs)
    vm.update(:trusted_certs_sha1 => Digest::SHA1.hexdigest(Bosh::Director::Config.trusted_certs))

    configure_vm(vm, agent, network_settings)
    vm_data.agent = agent
    yield vm_data
  rescue RpcTimeout => e
    # if we time out waiting for the agent, we should clean up the the VM
    # as it will leave us in an unrecoverable state otherwise
    @vm_reuser.remove_vm(vm_data)
    tear_down_vm(vm_data)
    raise e
  ensure
    vm_data.release
    unless @deployment_plan.compilation.reuse_compilation_vms
      tear_down_vm(vm_data)
    end
  end
end

#ready_tasks_countObject



63
64
65
# File 'lib/bosh/director/deployment_plan/steps/package_compile_step.rb', line 63

def ready_tasks_count
  @tasks_mutex.synchronize { @ready_tasks.size }
end