Class: Pod

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
MKIt::DockerHelper, MKIt::ERBHelper
Defined in:
lib/mkit/app/model/pod.rb

Instance Method Summary collapse

Methods included from MKIt::DockerHelper

#attach_network, #create_instance, #create_network, #create_volume, #delete_volume, #dettach_network, #execute_local, #inspect_instance, #inspect_volume, #logs, #network_exists?, #remove_instance, #remove_network, #start_instance, #stop_instance

Methods included from MKIt::ERBHelper

#parse_model, #parse_template, #read_template

Instance Method Details

#clean_upObject



90
91
92
93
# File 'lib/mkit/app/model/pod.rb', line 90

def clean_up
  remove_instance(self.name) unless self.pod_id.nil?
  MkitJob.publish(topic: :pod_destroyed, service_id: self.service.id, data: {pod_id: self.id})
end

#parseObject



63
64
65
# File 'lib/mkit/app/model/pod.rb', line 63

def parse
  parse_model(MKIt::Templates::DOCKER_RUN).result(binding)
end

#pre_checkObject



85
86
87
88
# File 'lib/mkit/app/model/pod.rb', line 85

def pre_check
  raise MKIt::PodNotFoundException.new('no pod_name found') if self.name.nil?
  raise MKIt::PodNotFoundException.new("no properties found for #{self.name}") if self.properties.nil?
end

#propertiesObject



43
44
45
# File 'lib/mkit/app/model/pod.rb', line 43

def properties
  inspect_instance(self.name)
end

#set_status_from_dockerObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mkit/app/model/pod.rb', line 47

def set_status_from_docker
  instance = self.properties
  if self.properties
    instance = instance.to_o
    if instance.State.Running
      self.status = MKIt::Status::RUNNING
    else
      self.status = MKIt::Status::STOPPED
    end
  else
    self.status = MKIt::Status::PENDING
  end
  self.save
  self.status
end

#startObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mkit/app/model/pod.rb', line 67

def start
  if self.pod_id.nil?
    docker_run = parse
    MKItLogger.info("deploying docker pod, cmd [#{docker_run}]")
    create_instance(docker_run)
  else
    pre_check

    instance = self.properties.to_o
    start_instance(self.name) unless instance.State.Running
  end
end

#stopObject



80
81
82
83
# File 'lib/mkit/app/model/pod.rb', line 80

def stop
  pre_check
  stop_instance(self.name)
end

#update_dnsObject



33
34
35
36
37
38
39
40
41
# File 'lib/mkit/app/model/pod.rb', line 33

def update_dns
  self.dns_host ||= DnsHost.new(
    service: self.service,
    name:    "#{name}.#{self.service.name}",
    ip:      self.ip
  )
  self.dns_host.ip = self.ip
  self.dns_host.save
end

#update_ipObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mkit/app/model/pod.rb', line 15

def update_ip
  new_ip = nil
  tries = 5
  while (new_ip.nil? && tries > 0) do
      instance = self.properties.to_o
      new_ip = instance.NetworkSettings.Networks[self.service.pods_network].IPAddress
      sleep(1) if new_ip.nil?
      tries = tries - 1
  end
  if self.ip != new_ip
    self.ip = new_ip
    self.update_dns
    MkitJob.publish(topic: :pod_ip_updated, service_id: self.service.id, pod_id: self.id)
  end
  MKItLogger.info("docker ip updated [#{self.ip}]")
  self.ip
end