Class: Service::Containers

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

Instance Method Summary collapse

Instance Method Details

#create_container!(wizard_state) ⇒ Object



17
18
19
20
21
22
23
24
# File 'app/models/service/containers.rb', line 17

def create_container!(wizard_state)
  ActiveRecord::Base.transaction do
    container = create_container_object(wizard_state)
    container.save!
    destroy_wizard_state(wizard_state)
    container
  end
end

#create_container_object(wizard_state) ⇒ Object



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
# File 'app/models/service/containers.rb', line 26

def create_container_object(wizard_state)
  container = Container.new(wizard_state.container_attributes) do |r|
    # eagerly load environment variables and exposed ports configuration
    state = DockerContainerWizardState.includes(
      :environment => [:environment_variables, :exposed_ports]).find(wizard_state.id)

    load_environment_variables(state, r)
    load_exposed_ports(state, r)
    load_dns(state, r)
  end

  Taxonomy.enabled_taxonomies.each do |taxonomy|
    container.send(:"#{taxonomy}=", wizard_state.preliminary.send(:"#{taxonomy}"))
  end

  unless container.valid?
    @errors = container.errors
    fail ActiveRecord::Rollback
  end

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

  container.name = container.in_fog.name[1..-1] unless container.name.present?

  container
end

#destroy_wizard_state(wizard_state) ⇒ Object



67
68
69
70
# File 'app/models/service/containers.rb', line 67

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

#errorsObject



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

def errors
  @errors ||= []
end

#full_messagesObject



95
96
97
# File 'app/models/service/containers.rb', line 95

def full_messages
  errors.respond_to?(:full_messages) ? errors.full_messages : errors
end

#load_dns(state, r) ⇒ Object



88
89
90
91
92
93
# File 'app/models/service/containers.rb', line 88

def load_dns(state, r)
  state.dns.each do |e|
    r.dns.build :name => e.name,
                :priority => e.priority
  end
end

#load_environment_variables(state, r) ⇒ Object



72
73
74
75
76
77
78
# File 'app/models/service/containers.rb', line 72

def load_environment_variables(state, r)
  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

#load_exposed_ports(state, r) ⇒ Object



80
81
82
83
84
85
86
# File 'app/models/service/containers.rb', line 80

def load_exposed_ports(state, r)
  state.exposed_ports.each do |e|
    r.exposed_ports.build :name => e.name,
                          :value => e.value,
                          :priority => e.priority
  end
end

#pull_image(container) ⇒ Object



53
54
55
# File 'app/models/service/containers.rb', line 53

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

#run_container(container) ⇒ Object



99
100
101
102
# File 'app/models/service/containers.rb', line 99

def run_container(container)
  docker_container = container.compute_resource.find_vm_by_uuid(container.uuid)
  error(_('Could not start container')) unless docker_container.send(:start)
end

#start_container(container) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'app/models/service/containers.rb', line 57

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
# File 'app/models/service/containers.rb', line 7

def start_container!(wizard_state)
  ActiveRecord::Base.transaction do
    container = create_container_object(wizard_state)
    container.save!
    run_container(container)
    destroy_wizard_state(wizard_state)
    container
  end
end