Class: Service::Containers

Inherits:
Object
  • Object
show all
Defined in:
app/models/service/containers.rb

Instance Method Summary collapse

Instance Method Details

#destroy_wizard_state(wizard_state) ⇒ Object



45
46
47
48
# File 'app/models/service/containers.rb', line 45

def destroy_wizard_state(wizard_state)
  wizard_state.destroy
  DockerContainerWizardState.destroy_all(["updated_at < ?", (Time.now - 24.hours)])
end

#errorsObject



3
4
5
# File 'app/models/service/containers.rb', line 3

def errors
  @errors ||= []
end

#pull_image(container) ⇒ Object



31
32
33
# File 'app/models/service/containers.rb', line 31

def pull_image(container)
  container.compute_resource.create_image(:fromImage => container.repository_pull_url)
end

#start_container(container) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'app/models/service/containers.rb', line 35

def start_container(container)
  started = container.compute_resource.create_container(container.parametrize)
  if started
    container.uuid = started.id
  else
    errors << container.compute_resource.errors[:base]
  end
  started
end

#start_container!(wizard_state) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/service/containers.rb', line 7

def start_container!(wizard_state)
  ActiveRecord::Base.transaction do
    container = Container.new(wizard_state.container_attributes) do |r|
      # eagerly load environment variables
      state = DockerContainerWizardState.includes(:environment => [:environment_variables])
              .find(wizard_state.id)
      state.environment_variables.each do |environment_variable|
        r.environment_variables.build :name     => environment_variable.name,
                                      :value    => environment_variable.value,
                                      :priority => environment_variable.priority
      end
    end
    Taxonomy.enabled_taxonomies.each do |taxonomy|
      container.send(:"#{taxonomy}=", wizard_state.preliminary.send(:"#{taxonomy}"))
    end

    fail ActiveRecord::Rollback unless pull_image(container) && start_container(container)

    container.save!
    destroy_wizard_state(wizard_state)
    container
  end
end