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



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

def clean_up
  begin
    remove_instance(self.name) unless self.instance.nil?
  rescue => e
    MKItLogger.warn(e)
  end
  MkitJob.publish(topic: :pod_destroyed, service_id: self.service.id, data: {pod_id: self.id})
end

#instanceObject



74
75
76
77
78
# File 'lib/mkit/app/model/pod.rb', line 74

def instance
  properties = inspect_instance(self.name)
  return properties.to_o unless properties.nil?
  nil
end

#parseObject



56
57
58
# File 'lib/mkit/app/model/pod.rb', line 56

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

#set_status_from_dockerObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mkit/app/model/pod.rb', line 42

def set_status_from_docker
  if !self.instance.nil?
    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



60
61
62
63
64
65
66
67
68
# File 'lib/mkit/app/model/pod.rb', line 60

def start
  if self.instance.nil?
    docker_run = parse
    MKItLogger.info("deploying docker pod, cmd [#{docker_run}]")
    create_instance(docker_run)
  else
    start_instance(self.name) unless instance.State.Running
  end
end

#stopObject



70
71
72
# File 'lib/mkit/app/model/pod.rb', line 70

def stop
  stop_instance(self.name) unless self.instance.nil? || !self.instance.State.Running
end

#to_hObject



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

def to_h
  {
    'name' => self.name,
    'ip' => self.dns_host.nil? || self.dns_host.ip.nil? ? self.ip : self.dns_host.ip,
    'dns' => self.dns_host.nil? || self.dns_host.name.nil? ? self.ip : self.dns_host.name,
    'status' => self.status
  }
end

#update_dnsObject



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

def update_dns
  self.dns_host ||= DnsHost.new(
    service: self.service,
    name:    "#{self.service.name}.internal",
    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
# File 'lib/mkit/app/model/pod.rb', line 15

def update_ip
  new_ip = nil
  tries = 5
  while (new_ip.nil? && tries > 0) do
    new_ip = self.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