Class: Service
Class Method Summary
collapse
Instance Method Summary
collapse
#attach_network, #create_instance, #create_network, #create_volume, #delete_volume, #dettach_network, #execute_local, #inspect_instance, #inspect_network, #inspect_volume, #remove_instance, #remove_network, #start_instance, #stop_instance
#parse_model, #parse_template, #read_template
Class Method Details
.create(yaml) ⇒ Object
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
71
72
73
74
75
76
77
78
79
|
# File 'lib/mkit/app/model/service.rb', line 37
def self.create(yaml)
config = yaml["service"]
raise MKIt::ServiceAlreadyExists.new unless Service.find_by_name(config.name).nil?
srv = Service.new(
name: config.name,
version: 1,
image: config.image,
command: config.command,
status: MKIt::Status::CREATING
)
if config.network.nil? || config.network.empty?
srv.pods_network="mkit"
else
srv.pods_network=config.network
end
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
)
srv.deploy_network
srv.configure(config)
srv.status = MKIt::Status::CREATED
srv.save
data = { service_id: srv.id, version: srv.version }
(1..srv.min_replicas).each { |i|
MkitJob.publish(topic: :create_pod_saga, service_id: srv.id, data: data)
}
srv
end
|
Instance Method Details
#add_service_config(key, value) ⇒ Object
151
152
153
154
155
|
# File 'lib/mkit/app/model/service.rb', line 151
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
145
146
147
148
149
|
# File 'lib/mkit/app/model/service.rb', line 145
def add_volume(volume_config)
v = Volume.create(self, volume_config)
self.volume << v
v
end
|
#as_json(options = {}) ⇒ Object
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/mkit/app/model/service.rb', line 255
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_up ⇒ Object
234
235
236
237
238
|
# File 'lib/mkit/app/model/service.rb', line 234
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
|
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
114
|
# File 'lib/mkit/app/model/service.rb', line 81
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
self.service_port = []
config.ports&.each do |p|
port = ServicePort.create(service: self, config: p)
self.service_port << port
end
self.volume = []
config.volumes&.each { |volume|
self.add_volume(volume)
}
self.service_config=[]
config.environment&.each_pair { |key,value|
self.add_service_config(key, value)
}
self.volume.each { | volume |
volume.deploy
}
end
|
#create_pods_network ⇒ Object
132
133
134
135
|
# File 'lib/mkit/app/model/service.rb', line 132
def create_pods_network
netw = inspect_network(self.pods_network)
create_network(self.pods_network) if netw.nil?
end
|
#current_configs ⇒ Object
157
158
159
|
# File 'lib/mkit/app/model/service.rb', line 157
def current_configs
self.service_config&.select{ |x| x.ctype == MKIt::CType::ENVIRONMENT.to_s && x.version == self.version}
end
|
#current_ports ⇒ Object
161
162
163
|
# File 'lib/mkit/app/model/service.rb', line 161
def current_ports
self.service_port&.select{ |x| x.version == self.version}
end
|
#deploy_network ⇒ Object
137
138
139
140
141
142
143
|
# File 'lib/mkit/app/model/service.rb', line 137
def deploy_network
self.lease.confirm
self.lease.up
self.create_pods_network
end
|
#my_dns ⇒ Object
165
166
167
|
# File 'lib/mkit/app/model/service.rb', line 165
def my_dns
MKIt::Interface.ip
end
|
#parse ⇒ Object
230
231
232
|
# File 'lib/mkit/app/model/service.rb', line 230
def parse
parse_model(MKIt::Templates::HAPROXY).result(binding)
end
|
#ports_by_external(external_port) ⇒ Object
208
209
210
|
# File 'lib/mkit/app/model/service.rb', line 208
def ports_by_external(external_port)
self.service_port.where('external_port = ?', external_port)
end
|
#ports_mode_by_external(external_port) ⇒ Object
212
213
214
215
|
# File 'lib/mkit/app/model/service.rb', line 212
def ports_mode_by_external(external_port)
ports = self.service_port.where('external_port = ?', external_port).first
ports.mode if ports
end
|
#proxy_config ⇒ Object
221
222
223
224
225
226
227
228
|
# File 'lib/mkit/app/model/service.rb', line 221
def proxy_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
|
#public_ports ⇒ Object
ha proxy configs & template
204
205
206
|
# File 'lib/mkit/app/model/service.rb', line 204
def public_ports
self.service_port.each.map{|p| p.external_port}.uniq
end
|
#start ⇒ Object
243
244
245
246
247
|
# File 'lib/mkit/app/model/service.rb', line 243
def start
self.pod.each { |p|
MkitJob.publish(topic: :start_pod, service_id: self.id, pod_id: p.id)
}
end
|
#stop ⇒ Object
249
250
251
252
253
|
# File 'lib/mkit/app/model/service.rb', line 249
def stop
self.pod.each { |p|
MkitJob.publish(topic: :stop_pod, service_id: self.id, pod_id: p.id)
}
end
|
#update!(yaml) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/mkit/app/model/service.rb', line 116
def update!(yaml)
config = yaml["service"]
raise MKIt::ServiceNameMismatch.new unless config.name == self.name
self.version+=1
self.configure(config)
self.pod.each { |pod| MkitJob.publish(topic: :destroy_pod, pod_id: pod.id, data: {}) }
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
|
#update_proxy ⇒ Object
217
218
219
|
# File 'lib/mkit/app/model/service.rb', line 217
def update_proxy
MkitJob.publish(topic: :update_proxy_config, application_id: self.id, data: proxy_config)
end
|
#update_status! ⇒ Object
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
198
199
|
# File 'lib/mkit/app/model/service.rb', line 169
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
|