mkit 0.6.1 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0dab845891b079ffbf0f696676c1c3b6e25e583c02a09570582571976f8ce40d
4
- data.tar.gz: 59e3174e0e09347c2e40a7f7096cfd3c9f9575a588d210c0d7dd71ccfcadf206
3
+ metadata.gz: 5ce73321371e8f675a5c28106e9287762ed5cf8b39ecfc8c97b35f62806a45c1
4
+ data.tar.gz: 21189b10e952913a03d50498201869010fef447f4b621a5f3e2f39a1ab12c94c
5
5
  SHA512:
6
- metadata.gz: a447023289277ea09120233ec72cb3b5a2de96077ee012b30fd369e21c338b487a898217d367dba7b8a86499a940944dee60ecdecbced737c00b70358e3141f4
7
- data.tar.gz: b33b3276ad887b3939d2898b2d09c0a8a807a62856b761969f50e675c2e01fcd2f48abc1a49259dea60e4d12df563b0b73d4d4852880bb0e4abf2e539714074f
6
+ metadata.gz: e38e72898702d11bf928534444a63fe1744b3af47494c4da7789ece5c01cb611b824e716cc7cc39454c738d730da5533393aefea8801da236ecce97d9b291f3e
7
+ data.tar.gz: 2e2d930ef466b48fbb75b07dbf12586cb41e0324a043eed2f334b9d4a638ba9369f6d65f85ca6c24d4573d8076dacfb26d43bc166de465212df8dbda3ee4edae
data/README.md CHANGED
@@ -7,7 +7,7 @@ It's also a frontend for `docker`, providing an easier way for your services to
7
7
  It contains an internal DNS and uses HAProxy as ingress for Pod access.
8
8
  The database is a simple sqlite3 db and the server is a Sinatra based application.
9
9
 
10
- A client is also included to access the API, e.g. `mkitc ps`.
10
+ A client is also included to access the API, e.g. `mkit ps`.
11
11
 
12
12
  The daemon is responsible for HAProxy pods routing configuration. It also provides the cluster DNS and manages the internal host interface and the docker instances.
13
13
 
@@ -102,22 +102,22 @@ mkit:
102
102
 
103
103
  ### Client configuration
104
104
 
105
- On `mkitc` first call, default configuration will be copied to `$HOME/.mkit` with `local`default profile set.
105
+ On `mkit` first call, default configuration will be copied to `$HOME/.mkit` with `local`default profile set.
106
106
 
107
- You must call `mkitc init` to initialize client configuration.
107
+ You must call `mkit init` to initialize client configuration.
108
108
 
109
109
  Client identification key (`my_id`) will be generated, printed out to console and saved to the client's configuration file.
110
110
 
111
- You may edit the local configuration file to add more servers and change active profile with `$mkitc profile set <profile_name>`, e.g. `$mkitc profile set server_2`
111
+ You may edit the local configuration file to add more servers and change active profile with `$mkit profile set <profile_name>`, e.g. `$mkit profile set server_2`
112
112
 
113
113
  ```
114
114
  # ~/.mkit/mkitc_config.yml
115
115
  mkit:
116
116
  local:
117
117
  server.uri: https://localhost:4567
118
- server_2: # you can add more servers. change the client active profile with mkitc profile command
118
+ server_2: # you can add more servers. change the client active profile with mkit profile command
119
119
  server.uri: https://192.168.29.232:4567
120
- my_id: unique_id # this id is generated running mkitc init
120
+ my_id: unique_id # this id is generated running mkit init
121
121
  ```
122
122
 
123
123
  ### Service
@@ -174,10 +174,10 @@ There's also samples for [systemd](samples/systemd) and [daemontools](samples/da
174
174
 
175
175
  A client is provided to interact with MKIt server.
176
176
 
177
- Run `mkitc help` for a list of current supported commands.
177
+ Run `mkit help` for a list of current supported commands.
178
178
 
179
179
  ```
180
- Usage: mkitc <command> [options]
180
+ Usage: mkit <command> [options]
181
181
 
182
182
  Micro k8s on Ruby - a simple tool to mimic a (very) minimalistic k8 cluster
183
183
 
@@ -197,13 +197,13 @@ version prints mkit server version
197
197
  proxy haproxy status and control
198
198
  profile mkit client configuration profile
199
199
 
200
- Run 'mkitc help <command>' for specific command information.
200
+ Run 'mkit help <command>' for specific command information.
201
201
  ```
202
202
 
203
203
  Example:
204
204
 
205
205
  ```
206
- $ mkitc ps
206
+ $ mkit ps
207
207
  +----+-------+---------------+-------------------+--------------+---------+
208
208
  | id | name | addr | ports | pods | status |
209
209
  +----+-------+---------------+-------------------+--------------+---------+
@@ -214,7 +214,7 @@ $ mkitc ps
214
214
  The service `mongo` is available on IP `10.210.198.10:27017`
215
215
  The service `nexus` is available on IP `10.210.198.11:80` and on port `443` with ssl.
216
216
 
217
- **Note:** Don't forget to call `mkitc init` to initialize client configuration and to add the `client-id`
217
+ **Note:** Don't forget to call `mkit init` to initialize client configuration and to add the `client-id`
218
218
  to the server authorized clients list.
219
219
 
220
220
  ## Development
@@ -224,6 +224,13 @@ to the server authorized clients list.
224
224
  * console
225
225
  * `rake console`
226
226
 
227
+ # Contributing
228
+ * Fork it
229
+ * Create your feature branch (git checkout -b my-new-feature)
230
+ * Commit your changes (git commit -am 'Add some feature')
231
+ * Push to the branch (git push origin my-new-feature)
232
+ * Create new Pull Request
233
+
227
234
  # Thanks
228
235
 
229
236
  For my kids. :)
data/bin/{mkitc → mkit} RENAMED
@@ -203,7 +203,7 @@ class MKItClient
203
203
 
204
204
  @configuration = cfg['mkit'][current_profile.lstrip]
205
205
  if !init_call && cfg['my_id'].nil?
206
- raise InvalidParametersException.new("Please run 'mkitc init' to initialize mkit client.", find_command('init'))
206
+ raise InvalidParametersException.new("Please run ' mkit init' to initialize mkit client.", find_command('init'))
207
207
  end
208
208
  @my_id = cfg['my_id']
209
209
  cfg
@@ -360,16 +360,16 @@ class MKItClient
360
360
  my_cmd = cause.command
361
361
  end
362
362
  if my_cmd.nil?
363
- msg += "\nUsage: mkitc <command> [options]\n\n"
363
+ msg += "\nUsage: mkit <command> [options]\n\n"
364
364
  msg += "Micro k8s on Ruby - a simple tool to mimic a (very) minimalistic k8 cluster\n\n"
365
365
  msg += "Commands:\n\n"
366
366
  dict.each do |c|
367
367
  msg += format("%-10s %s\n", c[:cmd], c[:help])
368
368
  end
369
369
  msg += "\n"
370
- msg += "Run 'mkitc help <command>' for specific command information.\n\n"
370
+ msg += "Run ' mkit help <command>' for specific command information.\n\n"
371
371
  else
372
- msg += format("\nUsage: mkitc %s %s\n\n", my_cmd[:cmd], my_cmd[:usage].nil? ? '' : my_cmd[:usage].join(' '))
372
+ msg += format("\nUsage: mkit %s %s\n\n", my_cmd[:cmd], my_cmd[:usage].nil? ? '' : my_cmd[:usage].join(' '))
373
373
  msg += format("%s\n", my_cmd[:help])
374
374
  unless my_cmd[:options].nil?
375
375
  msg += "\nOptions:\n"
@@ -16,10 +16,9 @@ class Pod < ActiveRecord::Base
16
16
  new_ip = nil
17
17
  tries = 5
18
18
  while (new_ip.nil? && tries > 0) do
19
- instance = self.properties.to_o
20
- new_ip = instance.NetworkSettings.Networks[self.service.pods_network].IPAddress
21
- sleep(1) if new_ip.nil?
22
- tries = tries - 1
19
+ new_ip = self.instance.NetworkSettings.Networks[self.service.pods_network].IPAddress
20
+ sleep(1) if new_ip.nil?
21
+ tries = tries - 1
23
22
  end
24
23
  if self.ip != new_ip
25
24
  self.ip = new_ip
@@ -40,14 +39,8 @@ class Pod < ActiveRecord::Base
40
39
  self.dns_host.save
41
40
  end
42
41
 
43
- def properties
44
- inspect_instance(self.name)
45
- end
46
-
47
42
  def set_status_from_docker
48
- instance = self.properties
49
- if self.properties
50
- instance = instance.to_o
43
+ if self.instance
51
44
  if instance.State.Running
52
45
  self.status = MKIt::Status::RUNNING
53
46
  else
@@ -65,30 +58,31 @@ class Pod < ActiveRecord::Base
65
58
  end
66
59
 
67
60
  def start
68
- if self.pod_id.nil?
61
+ if self.instance.nil?
69
62
  docker_run = parse
70
63
  MKItLogger.info("deploying docker pod, cmd [#{docker_run}]")
71
64
  create_instance(docker_run)
72
65
  else
73
- pre_check
74
-
75
- instance = self.properties.to_o
76
66
  start_instance(self.name) unless instance.State.Running
77
67
  end
78
68
  end
79
69
 
80
70
  def stop
81
- pre_check
82
- stop_instance(self.name)
71
+ stop_instance(self.name) unless self.instance.nil? || !self.instance.State.Running
83
72
  end
84
73
 
85
- def pre_check
86
- raise MKIt::PodNotFoundException.new('no pod_name found') if self.name.nil?
87
- raise MKIt::PodNotFoundException.new("no properties found for #{self.name}") if self.properties.nil?
74
+ def instance
75
+ properties = inspect_instance(self.name)
76
+ return properties.to_o unless properties.nil?
77
+ nil
88
78
  end
89
79
 
90
80
  def clean_up
91
- remove_instance(self.name) unless self.pod_id.nil?
81
+ begin
82
+ remove_instance(self.name) unless self.instance.nil?
83
+ rescue => e
84
+ MKItLogger.warn(e)
85
+ end
92
86
  MkitJob.publish(topic: :pod_destroyed, service_id: self.service.id, data: {pod_id: self.id})
93
87
  end
94
88
  end
@@ -5,16 +5,32 @@ require 'mkit/status'
5
5
  # https://docs.docker.com/engine/reference/commandline/events
6
6
  require 'mkit/app/helpers/docker_helper'
7
7
  module MKIt
8
+ class StopThread < RuntimeError; end
9
+
8
10
  class DockerListener
9
11
  include MKIt::DockerHelper
10
12
 
11
13
  def initialize
12
- @consumers = []
14
+ @queue = Queue.new
15
+ end
16
+
17
+ def enqueue(msg)
18
+ @queue << msg
19
+ end
20
+
21
+ def start
22
+ @consumer.run if register_consumer
23
+ @listener.run if register_listener
13
24
  end
14
25
 
15
- def register_consumer(consumer:)
26
+ def stop
27
+ @listener.exit if @listener
28
+ @consumer.raise StopThread.new
29
+ MKItLogger.info("docker listener stopped")
16
30
  end
17
31
 
32
+ private
33
+
18
34
  def parse_message(msg)
19
35
  action = msg['Action'].to_sym
20
36
  type = msg['Type'].to_sym
@@ -36,24 +52,26 @@ module MKIt
36
52
  pod.save
37
53
  pod.service.update_status!
38
54
  when :kill
39
- MKItLogger.debug(" #{type} #{action} <<NOOP / TODO>>")
55
+ pod.service.update_status!
40
56
  when :die
41
- MKItLogger.debug(" #{type} #{action} <<NOOP / TODO>>")
57
+ pod.service.update_status!
42
58
  when :stop
43
59
  pod.service.update_status!
44
60
  else
45
61
  MKItLogger.debug(" #{type} #{action} <<TODO>>")
46
62
  end
47
63
  else
48
- MKItLogger.warn("docker <<#{type}>> <#{action}> received: #{msg}. But I don't know anything about pod #{pod_id}")
64
+ MKItLogger.warn("docker <<#{type}>> <#{action}> received: #{msg}. But I don't know anything about pod #{pod_id}/#{pod_name}")
49
65
  end
50
66
  when :network
51
67
  pod_id = msg.Actor.Attributes.container
52
- pod = Pod.find_by(pod_id: pod_id)
68
+ inspect = inspect_instance(pod_id).to_o
69
+ pod_name = inspect.Name[1..]
70
+ pod = Pod.find_by(name: pod_name)
53
71
  unless pod.nil?
54
72
  case action
55
73
  when :connect
56
- MKItLogger.info("docker network #{action} received: #{msg}")
74
+ MKItLogger.info("docker network #{action} received: #{msg} for pod #{pod_name}")
57
75
  pod.update_ip
58
76
  pod.save
59
77
  when :disconnect
@@ -62,20 +80,41 @@ module MKIt
62
80
  MKItLogger.debug(" #{type} #{action} <<TODO>>")
63
81
  end
64
82
  else
65
- MKItLogger.warn("docker <<#{type}>> <#{action}> received: #{msg}. But I don't know anything about pod #{pod_id}")
83
+ MKItLogger.warn("docker <<#{type}>> <#{action}> received: #{msg}. But I don't know anything about pod #{pod_id}/#{pod_name}")
66
84
  end
67
85
  else
68
86
  MKItLogger.info("\t#{type} #{action} <<unknown>>")
69
87
  end
70
88
  end
71
89
 
72
- def start
73
- @thread ||= Thread.new {
90
+ def register_consumer
91
+ return false unless @consumer.nil?
92
+
93
+ @consumer = Thread.new do
94
+ running = true
95
+ while running
96
+ begin
97
+ parse_message(@queue.pop)
98
+ rescue StopThread
99
+ running = false
100
+ MKItLogger.info("docker consumer ended")
101
+ rescue => e
102
+ MKItLogger.error("error while consuming docker notification: #{e}", e.message, e.backtrace.join("\n"))
103
+ end
104
+ end
105
+ end
106
+ true
107
+ end
108
+
109
+ def register_listener
110
+ return false unless @listener.nil?
111
+
112
+ @listener = Thread.new {
74
113
  cmd = "docker events --format '{{json .}}'"
75
114
  begin
76
115
  PTY.spawn( cmd ) do |stdout, stdin, pid|
77
116
  begin
78
- stdout.each { |line| parse_message JSON.parse(line).to_o }
117
+ stdout.each { |line| enqueue JSON.parse(line).to_o }
79
118
  rescue Errno::EIO
80
119
  MKItLogger.warn("Errno:EIO error, but this probably just means " +
81
120
  "that the process has finished giving output")
@@ -85,12 +124,8 @@ module MKIt
85
124
  MKItLogger.warn("docker event listener process exited!")
86
125
  end
87
126
  }
88
- @thread.run
89
127
  MKItLogger.info("docker listener started")
90
- end
91
- def stop
92
- @thread.exit if @thread
93
- MKItLogger.info("docker listener stopped")
128
+ true
94
129
  end
95
130
  end
96
131
  end
data/lib/mkit/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module MKIt
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.3"
3
3
  end
4
4
 
data/mkit ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift File.expand_path('lib')
5
+ #
6
+ ENV['SINATRA_ENV'] ||= "development"
7
+ ENV['RACK_ENV'] ||= "development"
8
+
9
+ load 'bin/mkit'
10
+
data/mkit.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
20
20
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|TODO|db/development.sqlite)}) }
21
21
  end
22
22
  s.executables << 'mkitd'
23
- s.executables << 'mkitc'
23
+ s.executables << 'mkit'
24
24
  s.add_runtime_dependency 'async-dns', '~> 1.3', '>= 1.3.0'
25
25
  s.add_runtime_dependency 'dry-container', '~> 0.9', '>= 0.9.0'
26
26
  s.add_runtime_dependency 'net_http_unix', '~> 0.2', '>= 0.2.2'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasco Santos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-27 00:00:00.000000000 Z
11
+ date: 2024-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async-dns
@@ -316,7 +316,7 @@ email:
316
316
  - valexsantos@gmail.com
317
317
  executables:
318
318
  - mkitd
319
- - mkitc
319
+ - mkit
320
320
  extensions: []
321
321
  extra_rdoc_files: []
322
322
  files:
@@ -327,7 +327,7 @@ files:
327
327
  - LICENSE
328
328
  - README.md
329
329
  - Rakefile
330
- - bin/mkitc
330
+ - bin/mkit
331
331
  - bin/mkitd
332
332
  - config/database.yml
333
333
  - config/mkit_config.yml
@@ -386,6 +386,7 @@ files:
386
386
  - lib/mkit/workers/pod_worker.rb
387
387
  - lib/mkit/workers/service_worker.rb
388
388
  - lib/mkit/workers/worker_manager.rb
389
+ - mkit
389
390
  - mkit.gemspec
390
391
  - mkitd
391
392
  - samples/apps/kafka-cluster.yml