Class: Service

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

Class Method Summary collapse

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

Class Method Details

.create(yaml) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mkit/app/model/service.rb', line 33

def self.create(yaml)
  config = yaml["service"]
  raise MKIt::ServiceAlreadyExists.new unless Service.find_by_name(config.name).nil?

  ActiveRecord::Base.transaction do
    srv = Service.new(
      name: config.name,
      version: 1,
      image: config.image,
      command: config.command,
      status: MKIt::Status::CREATING
    )

    # reserve pool ip
    srv.lease = Pool.find_by_name(MKIt::Utils.me).reserve_for(srv)

    srv.dns_host = DnsHost.new(
      service: srv,
      name: srv.name,
      ip: srv.lease.ip
    )

    # create service network
    srv.deploy_network

    # configure
    srv.configure(config)
    #
    srv.status = MKIt::Status::CREATED
    srv.save!
    data = { service_id: srv.id, version: srv.version }
    # create pod
    (1..srv.min_replicas).each { |i|
      MkitJob.publish(topic: :create_pod_saga, service_id: srv.id, data: data)
    }
    srv
  end
end

Instance Method Details

#add_service_config(key, value) ⇒ Object



149
150
151
152
153
# File 'lib/mkit/app/model/service.rb', line 149

def add_service_config(key, value)
  v = ServiceConfig.create(service: self, key: key, value: value)
  self.service_config << v
  v
end

#add_volume(volume_config) ⇒ Object



143
144
145
146
147
# File 'lib/mkit/app/model/service.rb', line 143

def add_volume(volume_config)
  v = Volume.create(self, volume_config)
  self.volume << v
  v
end

#as_json(options = {}) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
# File 'lib/mkit/app/model/service.rb', line 302

def as_json(options = {})
  srv = super
  a=[:pod, :volume, :service_config, :service_port]
  a.each { | k | 
    srv[k] ||= []
    self.send(k).each { |v| 
      srv[k] << v.as_json
    }
  }
  srv
end

#clean_upObject



219
220
221
222
223
# File 'lib/mkit/app/model/service.rb', line 219

def clean_up
  my_addr = self.lease.ip.split('.')[3]
  filename = "#{'%04i' % my_addr.to_i}_#{self.name}.cfg"
  MkitJob.publish(topic: :destroy_proxy_config, data: {filename: filename})
end

#configure(config) ⇒ Object



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
# File 'lib/mkit/app/model/service.rb', line 72

def configure(config)
  self.image = config.image if config.image != self.image
  self.command = config.command if config.command != self.command

  unless config.resources.nil?
    self.max_replicas = config.resources.max_replicas unless config.resources.max_replicas.nil? || config.resources.max_replicas < 1
    self.min_replicas = config.resources.min_replicas unless config.resources.min_replicas.nil? || config.resources.min_replicas < 1
  else
    self.min_replicas = 1
    self.max_replicas = 1
  end
  self.max_replicas = self.min_replicas if self.min_replicas > self.max_replicas

  # docker network
  if config.network.nil? || config.network.empty?
    self.pods_network="mkit"
  else
    self.pods_network=config.network
  end
  self.create_pods_network

  # haproxy ports
  self.service_port = []
  config.ports&.each do |p|
    port = ServicePort.create(service: self, config: p)
    self.service_port << port
  end

  # volumes
  self.volume = []
  config.volumes&.each { |volume|
    self.add_volume(volume)
  }
  # environment
  self.service_config=[]
  config.environment&.each_pair { |key,value|
    self.add_service_config(key, value)
  }
  self.volume.each { | volume |
    volume.deploy
  }
end

#create_pods_networkObject



133
134
135
# File 'lib/mkit/app/model/service.rb', line 133

def create_pods_network
  create_network(self.pods_network) unless network_exists?(self.pods_network)
end

#current_configsObject



155
156
157
# File 'lib/mkit/app/model/service.rb', line 155

def current_configs
  self.service_config&.select{ |x| x.ctype == MKIt::CType::ENVIRONMENT.to_s && x.version == self.version}
end

#current_portsObject



159
160
161
# File 'lib/mkit/app/model/service.rb', line 159

def current_ports
  self.service_port&.select{ |x| x.version == self.version}
end

#deploy_networkObject



137
138
139
140
141
# File 'lib/mkit/app/model/service.rb', line 137

def deploy_network
  # create service interface...
  self.lease.confirm
  self.lease.up
end

#find_pod_by_id_or_name(pod_id) ⇒ Object



225
226
227
228
229
# File 'lib/mkit/app/model/service.rb', line 225

def find_pod_by_id_or_name(pod_id)
  pod = self.pod.find_by(id: pod_id)
  pod = self.pod.find_by(name: pod_id) unless pod
  pod
end

#logObject



245
246
247
248
249
250
251
252
# File 'lib/mkit/app/model/service.rb', line 245

def log
  out = ""
  self.pod.each { |p|
    out << "<<<< %s | %s >>>>\n" % [self.name, p.name]
    out << logs(p.name)
  }
  out
end

#my_dnsObject



163
164
165
# File 'lib/mkit/app/model/service.rb', line 163

def my_dns
  MKIt::Interface.ip
end

#parseObject



215
216
217
# File 'lib/mkit/app/model/service.rb', line 215

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

#proxy_configObject



206
207
208
209
210
211
212
213
# File 'lib/mkit/app/model/service.rb', line 206

def proxy_config
  # config
  haproxy = parse
  my_addr = self.lease.ip.split('.')[3]
  filename = "#{'%04i' % my_addr.to_i}_#{self.name}.cfg"
  MKItLogger.debug("haproxy config file: #{filename}")
  {filename: filename, data: haproxy}
end

#startObject

ctrl



233
234
235
236
237
# File 'lib/mkit/app/model/service.rb', line 233

def start
  self.pod.each { |p|
    MkitJob.publish(topic: :start_pod, service_id: self.id, pod_id: p.id)
  }
end

#stopObject



239
240
241
242
243
# File 'lib/mkit/app/model/service.rb', line 239

def stop
  self.pod.each { |p|
    MkitJob.publish(topic: :stop_pod, service_id: self.id, pod_id: p.id)
  }
end

#to_h(options = {}) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/mkit/app/model/service.rb', line 253

def to_h(options = {})
  details = options[:details] || false
  yaml = {}
  yaml['service'] = {}
  srv = yaml['service']
  srv['name'] = self.name
  srv['image'] = self.image
  srv['command'] = self.command
  srv['network'] = self.pods_network
  if details
    srv['status'] = self.status
    srv['version'] = self.version
    srv['ip'] = self.lease.ip
    srv['dns'] = self.dns_host.name
    srv['pods'] = []
    self.pod.each { |p|
      srv['pods'] << p.to_h
    }
  end
  srv['ports'] = []
  self.service_port.each { |p|
    "#{p.internal_port}:#{p.external_port}:#{p.mode}:#{p.load_bal}".tap { |x|
      if p.ssl == 'true'
        x << ':ssl'
        if !p.crt.nil? && p.crt != MKIt::Utils.proxy_cert
          x << ":#{p.crt}"
        end
      end
      srv['ports'] << x
    }
  }
  srv['resources'] = {}
  srv['resources']['min_replicas'] = self.min_replicas
  srv['resources']['max_replicas'] = self.max_replicas
  srv['volumes'] = []
  self.volume.each { |v|
    if v.ctype ==  MKIt::CType::DOCKER_STORAGE.to_s
      srv['volumes'] << "docker://#{v.name}:#{v.path}"
    elsif v.ctype ==  MKIt::CType::LOCAL_STORAGE.to_s
      srv['volumes'] << "#{v.name}:#{v.path}"
    end
  }
  srv['environment'] = {}
  self.service_config.each { |c|
    srv['environment'][c.key] = "#{c.value}"
  }
  yaml
end

#update!(yaml) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mkit/app/model/service.rb', line 115

def update!(yaml)
  ActiveRecord::Base.transaction do
    config = yaml["service"]
    raise MKIt::ServiceNameMismatch.new unless config.name == self.name
    self.version+=1
    self.configure(config)

    # start new pod, destroy old pod...
    self.pod.each { |pod| MkitJob.publish(topic: :destroy_pod, pod_id: pod.id, data: {}) }
    # create pod
    data = { service_id: self.id, version: self.version }
    (1..self.min_replicas).each { |i|
      MkitJob.publish(topic: :create_pod_saga, service_id: self.id, data: data)
    }
    self.save
  end
end

#update_proxyObject

ha proxy configs & template



202
203
204
# File 'lib/mkit/app/model/service.rb', line 202

def update_proxy
  MkitJob.publish(topic: :update_proxy_config, application_id: self.id, data: proxy_config)
end

#update_status!Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/mkit/app/model/service.rb', line 167

def update_status!
  combined_status = nil
  self.pod.each { |pod|
    child_status = pod.set_status_from_docker
    if combined_status
      case combined_status
      when MKIt::Status::RUNNING
        case child_status
        when MKIt::Status::STOPPED || MKIt::Status::PENDING
          combined_status = MKIt::Status::DEGRATED
        end
      when MKIt::Status::STOPPED
        case child_status
        when MKIt::Status::RUNNING || MKIt::Status::PENDING
          combined_status = MKIt::Status::DEGRATED
        end
      when MKIt::Status::PENDING
        case child_status
        when MKIt::Status::RUNNING || MKIt::Status::STOPPED
          combined_status = MKIt::Status::DEGRATED
        end
      end
    else
      combined_status = child_status
    end
  }
  combined_status = MKIt::Status::CREATING unless combined_status
  self.status = combined_status
  self.save
  self.status
end