mkit 0.4.1 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/mkitd CHANGED
@@ -10,23 +10,21 @@ def up
10
10
  require 'sinatra/base'
11
11
  require 'mkit'
12
12
  # defaults
13
- PARAMS_CONFIG[:bind] ||= "localhost"
13
+ PARAMS_CONFIG[:bind] ||= 'localhost'
14
14
  PARAMS_CONFIG[:port] ||= 4567
15
- #
16
15
  MKIt.startup(options: PARAMS_CONFIG)
17
16
 
18
17
  use Rack::MethodOverride
19
18
  use ServicesController
20
19
  use MkitJobsController
20
+ use MkitController
21
21
 
22
22
  # sinatra::base ignores in parameters
23
23
  # set it here or via configure...
24
- Sinatra::Application.run!({port: PARAMS_CONFIG[:port], bind: PARAMS_CONFIG[:bind] })
24
+ Sinatra::Application.run!({ port: PARAMS_CONFIG[:port], bind: PARAMS_CONFIG[:bind] })
25
25
  # MKIt::Server.run
26
26
  end
27
27
 
28
-
29
- #
30
28
  if ARGV.any?
31
29
  require 'optparse'
32
30
  parser = OptionParser.new do |op|
@@ -52,4 +50,3 @@ if ARGV.any?
52
50
  end
53
51
 
54
52
  up
55
-
@@ -4,11 +4,11 @@ mkit:
4
4
  haproxy:
5
5
  config_dir: /etc/haproxy/haproxy.d
6
6
  ctrl:
7
- start: /etc/init.d/haproxy start
8
- stop: /etc/init.d/haproxy stop
9
- reload: /etc/init.d/haproxy restart
10
- restart: /etc/init.d/haproxy restart
11
- status: /etc/init.d/haproxy status
7
+ start: systemctl start haproxy
8
+ stop: systemctl stop haproxy
9
+ reload: systemctl reload haproxy
10
+ restart: systemctl restart haproxy
11
+ status: systemctl status haproxy
12
12
  bin: /usr/sbin/haproxy
13
13
  database:
14
14
  env: development
@@ -1,5 +1,6 @@
1
- class Setup < ActiveRecord::Migration[5.1]
1
+ # frozen_string_literal: true
2
2
 
3
+ class Setup < ActiveRecord::Migration[5.1]
3
4
  def up
4
5
  create_table :services do |t|
5
6
  t.string :name
@@ -16,7 +17,7 @@ class Setup < ActiveRecord::Migration[5.1]
16
17
  t.timestamp :updated_at
17
18
  end
18
19
 
19
- add_index "services", ["name"], :name => "services_name_id"
20
+ add_index 'services', ['name'], name: 'services_name_id'
20
21
 
21
22
  create_table :service_configs do |t|
22
23
  t.string :service_id
@@ -82,10 +83,10 @@ class Setup < ActiveRecord::Migration[5.1]
82
83
  end
83
84
 
84
85
  # mkit configs
85
- # e.g.
86
+ # e.g.
86
87
  # default network pool
87
88
  # rabbitmq address
88
- # haproxy
89
+ # haproxy
89
90
  # config.d path
90
91
  # reload command - pq quero usar o daemontools
91
92
  # stop
@@ -102,4 +103,3 @@ class Setup < ActiveRecord::Migration[5.1]
102
103
  drop_table :services
103
104
  end
104
105
  end
105
-
@@ -1,5 +1,6 @@
1
- class MkitJobs < ActiveRecord::Migration[5.1]
1
+ # frozen_string_literal: true
2
2
 
3
+ class MkitJobs < ActiveRecord::Migration[5.1]
3
4
  def up
4
5
  create_table :mkit_jobs do |t|
5
6
  t.string :topic
@@ -12,6 +13,4 @@ class MkitJobs < ActiveRecord::Migration[5.1]
12
13
  t.timestamp :updated_at
13
14
  end
14
15
  end
15
-
16
16
  end
17
-
data/db/schema.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is auto-generated from the current state of the database. Instead
2
4
  # of editing this file, please use the migrations feature of Active Record to
3
5
  # incrementally modify your database, and then regenerate this schema definition.
@@ -11,98 +13,97 @@
11
13
  # It's strongly recommended that you check this file into your version control system.
12
14
 
13
15
  ActiveRecord::Schema[7.0].define(version: 2) do
14
- create_table "dns_hosts", force: :cascade do |t|
15
- t.string "service_id"
16
- t.string "pod_id"
17
- t.string "name"
18
- t.string "ip"
19
- t.string "ipv6"
16
+ create_table 'dns_hosts', force: :cascade do |t|
17
+ t.string 'service_id'
18
+ t.string 'pod_id'
19
+ t.string 'name'
20
+ t.string 'ip'
21
+ t.string 'ipv6'
20
22
  end
21
23
 
22
- create_table "leases", force: :cascade do |t|
23
- t.string "pool_id"
24
- t.string "service_id"
25
- t.string "interface_name"
26
- t.string "interface_type"
27
- t.string "status"
28
- t.string "ip"
29
- t.datetime "expires_at", precision: nil
24
+ create_table 'leases', force: :cascade do |t|
25
+ t.string 'pool_id'
26
+ t.string 'service_id'
27
+ t.string 'interface_name'
28
+ t.string 'interface_type'
29
+ t.string 'status'
30
+ t.string 'ip'
31
+ t.datetime 'expires_at', precision: nil
30
32
  end
31
33
 
32
- create_table "mkit_jobs", force: :cascade do |t|
33
- t.string "topic"
34
- t.string "service_id"
35
- t.string "pod_id"
36
- t.string "status"
37
- t.string "retries"
38
- t.string "payload"
39
- t.datetime "created_at", precision: nil
40
- t.datetime "updated_at", precision: nil
34
+ create_table 'mkit_jobs', force: :cascade do |t|
35
+ t.string 'topic'
36
+ t.string 'service_id'
37
+ t.string 'pod_id'
38
+ t.string 'status'
39
+ t.string 'retries'
40
+ t.string 'payload'
41
+ t.datetime 'created_at', precision: nil
42
+ t.datetime 'updated_at', precision: nil
41
43
  end
42
44
 
43
- create_table "pods", force: :cascade do |t|
44
- t.string "service_id"
45
- t.string "pod_id"
46
- t.string "name"
47
- t.string "ip"
48
- t.string "image"
49
- t.string "status"
45
+ create_table 'pods', force: :cascade do |t|
46
+ t.string 'service_id'
47
+ t.string 'pod_id'
48
+ t.string 'name'
49
+ t.string 'ip'
50
+ t.string 'image'
51
+ t.string 'status'
50
52
  end
51
53
 
52
- create_table "pools", force: :cascade do |t|
53
- t.string "name"
54
- t.string "ip"
55
- t.string "range"
56
- t.string "netmask"
57
- t.string "status"
58
- t.boolean "preferred", default: false
54
+ create_table 'pools', force: :cascade do |t|
55
+ t.string 'name'
56
+ t.string 'ip'
57
+ t.string 'range'
58
+ t.string 'netmask'
59
+ t.string 'status'
60
+ t.boolean 'preferred', default: false
59
61
  end
60
62
 
61
- create_table "service_configs", force: :cascade do |t|
62
- t.string "service_id"
63
- t.string "key"
64
- t.string "value"
65
- t.string "ctype"
66
- t.string "version"
63
+ create_table 'service_configs', force: :cascade do |t|
64
+ t.string 'service_id'
65
+ t.string 'key'
66
+ t.string 'value'
67
+ t.string 'ctype'
68
+ t.string 'version'
67
69
  end
68
70
 
69
- create_table "service_ports", force: :cascade do |t|
70
- t.string "service_id"
71
- t.string "internal_port"
72
- t.string "external_port"
73
- t.string "mode"
74
- t.string "load_bal"
75
- t.string "version"
71
+ create_table 'service_ports', force: :cascade do |t|
72
+ t.string 'service_id'
73
+ t.string 'internal_port'
74
+ t.string 'external_port'
75
+ t.string 'mode'
76
+ t.string 'load_bal'
77
+ t.string 'version'
76
78
  end
77
79
 
78
- create_table "services", force: :cascade do |t|
79
- t.string "name"
80
- t.string "image"
81
- t.string "qdm"
82
- t.string "command"
83
- t.integer "max_replicas", default: 1
84
- t.integer "min_replicas", default: 1
85
- t.string "lease_id"
86
- t.string "pods_network", default: "mkit"
87
- t.string "version"
88
- t.string "status"
89
- t.datetime "created_at", precision: nil, default: "2023-12-28 12:41:59"
90
- t.datetime "updated_at", precision: nil
91
- t.index ["name"], name: "services_name_id"
80
+ create_table 'services', force: :cascade do |t|
81
+ t.string 'name'
82
+ t.string 'image'
83
+ t.string 'qdm'
84
+ t.string 'command'
85
+ t.integer 'max_replicas', default: 1
86
+ t.integer 'min_replicas', default: 1
87
+ t.string 'lease_id'
88
+ t.string 'pods_network', default: 'mkit'
89
+ t.string 'version'
90
+ t.string 'status'
91
+ t.datetime 'created_at', precision: nil, default: '2023-12-28 12:41:59'
92
+ t.datetime 'updated_at', precision: nil
93
+ t.index ['name'], name: 'services_name_id'
92
94
  end
93
95
 
94
- create_table "settings", force: :cascade do |t|
95
- t.string "key"
96
- t.string "value"
96
+ create_table 'settings', force: :cascade do |t|
97
+ t.string 'key'
98
+ t.string 'value'
97
99
  end
98
100
 
99
- create_table "volumes", force: :cascade do |t|
100
- t.string "service_id"
101
- t.string "name"
102
- t.string "path"
103
- t.string "ctype"
104
- t.string "status"
105
- t.string "version"
101
+ create_table 'volumes', force: :cascade do |t|
102
+ t.string 'service_id'
103
+ t.string 'name'
104
+ t.string 'path'
105
+ t.string 'ctype'
106
+ t.string 'status'
107
+ t.string 'version'
106
108
  end
107
-
108
109
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkit/version'
4
+
5
+ class MkitController < MKIt::Server
6
+ get '/mkit/version' do
7
+ "MKIt version #{MKIt::VERSION}\n"
8
+ end
9
+
10
+ put '/mkit/proxy/restart' do
11
+ MKIt::HAProxy.restart
12
+ end
13
+
14
+ put '/mkit/proxy/start' do
15
+ MKIt::HAProxy.start
16
+ end
17
+
18
+ put '/mkit/proxy/stop' do
19
+ MKIt::HAProxy.stop
20
+ end
21
+
22
+ get '/mkit/proxy/status' do
23
+ MKIt::HAProxy.status
24
+ end
25
+ end
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mkit/app/model/mkit_job'
2
4
 
3
5
  class MkitJobsController < MKIt::Server
4
-
5
6
  # curl localhost:4567/mkitjobs
6
7
  get '/mkitjobs' do
7
8
  JSON.pretty_generate(JSON.parse(MkitJob.all.to_json))
@@ -24,14 +25,12 @@ class MkitJobsController < MKIt::Server
24
25
  end
25
26
 
26
27
  post '/mkitjobs' do
27
- xx = "no file"
28
+ xx = 'no file'
28
29
  if params[:file]
29
30
  tempfile = params[:file][:tempfile]
30
- xx= YAML.load(tempfile.read)
31
+ xx = YAML.safe_load(tempfile.read)
31
32
  puts xx
32
33
  end
33
34
  JSON.pretty_generate(JSON.parse(xx.to_json))
34
35
  end
35
-
36
36
  end
37
-
@@ -1,5 +1,6 @@
1
- class PodsController < MKIt::Server
1
+ # frozen_string_literal: true
2
2
 
3
+ class PodsController < MKIt::Server
3
4
  get '/services/:service_id/pods' do
4
5
  "Not implemented\n"
5
6
  end
@@ -17,14 +18,12 @@ class PodsController < MKIt::Server
17
18
  end
18
19
 
19
20
  post '/services/:service_id/pods' do
20
- xx = "no file"
21
+ xx = 'no file'
21
22
  if params[:file]
22
23
  tempfile = params[:file][:tempfile]
23
- xx= YAML.load(tempfile.read)
24
+ xx = YAML.safe_load(tempfile.read)
24
25
  puts xx
25
26
  end
26
27
  JSON.pretty_generate(JSON.parse(xx.to_json))
27
28
  end
28
-
29
29
  end
30
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mkit/app/model/service'
2
4
  require 'mkit/app/helpers/services_helper'
3
5
 
@@ -6,11 +8,8 @@ class ServicesController < MKIt::Server
6
8
 
7
9
  # curl localhost:4567/services
8
10
  get '/services' do
9
- if params[:verbose]
10
- verbose = params[:verbose] == 'true'
11
- else
12
- verbose = false
13
- end
11
+ verbose = params[:verbose] == 'true'
12
+
14
13
  if params[:id]
15
14
  redirect "/services/#{params[:id]}"
16
15
  elsif params[:name]
@@ -24,20 +23,25 @@ class ServicesController < MKIt::Server
24
23
 
25
24
  get '/services/:id' do
26
25
  srv = find_by_id_or_name
27
- if request.env['CONTENT_TYPE'] == 'application/json'
28
- resp = srv.to_json
29
- else
30
- resp = format_response(srv)
31
- end
26
+ resp = if request.env['CONTENT_TYPE'] == 'application/json'
27
+ srv.to_json
28
+ else
29
+ format_response(srv)
30
+ end
32
31
  resp
33
32
  end
34
33
 
34
+ get '/services/:id/logs' do
35
+ srv = find_by_id_or_name
36
+ srv.log
37
+ end
38
+
35
39
  # curl -X PUT localhost:4567/services/1 -F "file=@mkit/samples/mkit.yml"
36
40
  put '/services/:id' do
37
41
  srv = find_by_id_or_name
38
42
  if params[:file]
39
43
  tempfile = params[:file][:tempfile]
40
- yaml = YAML.load(tempfile.read)
44
+ yaml = YAML.safe_load(tempfile.read)
41
45
  srv.update!(yaml.to_o)
42
46
  end
43
47
  format_response(srv)
@@ -52,10 +56,10 @@ class ServicesController < MKIt::Server
52
56
 
53
57
  # curl -X POST localhost:4567/services -F "file=@mkit/samples/mkit.yml"
54
58
  post '/services' do
55
- srv = "no file"
59
+ srv = 'no file'
56
60
  if params[:file]
57
61
  tempfile = params[:file][:tempfile]
58
- yaml = YAML.load(tempfile.read)
62
+ yaml = YAML.safe_load(tempfile.read)
59
63
  srv = Service.create(yaml.to_o)
60
64
  end
61
65
  format_response(srv)
@@ -77,4 +81,11 @@ class ServicesController < MKIt::Server
77
81
  MkitJob.publish(topic: :stop_service, service_id: srv.id)
78
82
  format_response(srv)
79
83
  end
84
+
85
+ put '/services/:id/restart' do
86
+ srv = find_by_id_or_name
87
+ MkitJob.publish(topic: :stop_service, service_id: srv.id)
88
+ MkitJob.publish(topic: :start_service, service_id: srv.id)
89
+ format_response(srv)
90
+ end
80
91
  end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mkit/cmd_runner'
2
4
 
3
5
  module MKIt
4
6
  module DockerHelper
5
-
6
7
  # from ERB template
7
8
  def create_instance(cmd)
8
9
  MKIt::CmdRunner.run(cmd)
@@ -22,7 +23,7 @@ module MKIt
22
23
 
23
24
  def inspect_instance(instance_id)
24
25
  # this one does not work on ubunto MKIt::CmdRunner.run("docker inspect #{instance_id}")
25
- x = %x{docker inspect #{instance_id}}
26
+ x = `docker inspect #{instance_id}`
26
27
  JSON.parse(x).first
27
28
  end
28
29
 
@@ -30,6 +31,13 @@ module MKIt
30
31
  MKIt::CmdRunner.run("docker exec -it #{instance_id} #{cmd}")
31
32
  end
32
33
 
34
+ #
35
+ # logs
36
+ #
37
+ def logs(instance_id)
38
+ `docker logs -n 20 #{instance_id}`
39
+ end
40
+
33
41
  #
34
42
  # network
35
43
  #
@@ -39,7 +47,7 @@ module MKIt
39
47
  end
40
48
 
41
49
  def network_exists?(network_name)
42
- x = MKIt::CmdRunner.run("docker network ls")
50
+ x = MKIt::CmdRunner.run('docker network ls')
43
51
  x.match(/\b#{network_name}\b/)
44
52
  end
45
53
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mkit/utils'
2
4
 
3
5
  module MKIt
@@ -6,9 +8,9 @@ module MKIt
6
8
  root = MKIt::Utils.root
7
9
  File.read("#{root}/lib/mkit/app/templates/#{template}.erb")
8
10
  end
9
-
11
+
10
12
  def parse_template(template, data = {})
11
- ERB.new(read_template(template)).result_with_hash(data)
13
+ ERB.new(read_template(template)).result_with_hash(data)
12
14
  end
13
15
 
14
16
  def parse_model(template)
@@ -1,54 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'text-table'
1
4
  module MKIt
2
5
  module ServicesHelper
3
- def str_template
4
- "%-5s %-18s %-15s %-25s %-10s"
5
- end
6
-
7
- def header_template
8
- ["id", "name", "addr", "ports", "status"]
9
- end
10
-
11
- def _format(template, data)
12
- template % data
13
- end
14
-
15
- def format_response(data, verbose = false)
16
- resp = []
17
- header = _format(str_template, header_template)
18
- resp << header
19
- if data.respond_to?"each"
20
- data.each { | srv |
21
- ports = srv.service_port&.each.map { |p| "#{p.mode}/#{p.external_port}"}.join(",")
22
- resp << _format(str_template, [srv.id, srv.name, srv.lease&.ip, ports, srv.status])
23
- resp+=service_pods(srv) if verbose
24
- }
6
+ def format_response(data, _verbose = false)
7
+ table = Text::Table.new
8
+ table.head = %w[id name addr ports pods status]
9
+ if data.respond_to? 'each'
10
+ data.each do |srv|
11
+ table.rows << build_table_row(srv)
12
+ end
25
13
  else
26
- ports = data.service_port&.each.map { |p| "#{p.mode}/#{p.external_port}"}.join(",")
27
- resp << _format(str_template, [data.id, data.name, data.lease&.ip, ports, data.status])
28
- resp+=service_pods(data)
14
+ table.rows << build_table_row(data)
29
15
  end
30
- resp << ""
31
- resp.join("\n")
32
- end
33
-
34
- def service_pods(srv)
35
- resp=[]
36
- resp << " pods"
37
- resp << _format(" %-5s %-15s %-15s %-15s %-10s", ["id", "pod_id", "pod_name", "pod_ip", "status"])
38
- srv.pod.each { |pod|
39
- name = pod.name.nil? ? "" : pod.name[0..11]
40
- pod_id = pod.pod_id.nil? ? "" : pod.pod_id[0..11]
41
- resp << _format(" %-5s %-15s %-15s %-15s %-10s", [pod.id, pod_id, name, pod.ip, pod.status])
42
- }
43
- resp
16
+ table.to_s
44
17
  end
45
18
 
46
19
  def find_by_id_or_name
47
20
  srv = Service.find_by_id(params[:id])
48
- srv = Service.find_by_name(params[:id]) unless srv
21
+ srv ||= Service.find_by_name(params[:id])
49
22
  error 404, "Couldn't find Service '#{params[:id]}'\n" unless srv
50
23
  srv
51
24
  end
52
25
 
26
+ def build_table_row(data)
27
+ ports = data.service_port&.each.map { |p| "#{p.mode}/#{p.external_port}" }.join(',')
28
+ pods = data.pod.each.map { |p| p.name.to_s }.join(' ')
29
+ [data.id, data.name, data.lease&.ip, ports, pods, data.status]
30
+ end
53
31
  end
54
32
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mkit/exceptions'
2
4
 
3
5
  module MKIt
@@ -131,7 +131,7 @@ class Service < ActiveRecord::Base
131
131
  end
132
132
 
133
133
  def create_pods_network
134
- create_network(self.pods_network) if !network_exists?(self.pods_network)
134
+ create_network(self.pods_network) unless network_exists?(self.pods_network)
135
135
  end
136
136
 
137
137
  def deploy_network
@@ -237,6 +237,15 @@ class Service < ActiveRecord::Base
237
237
  }
238
238
  end
239
239
 
240
+ def log
241
+ out = ""
242
+ self.pod.each { |p|
243
+ out << "<<<< %s | %s >>>>\n" % [self.name, p.name]
244
+ puts logs(p.name)
245
+ out << logs(p.name)
246
+ }
247
+ out
248
+ end
240
249
  def as_json(options = {})
241
250
  srv = super
242
251
  a=[:pod, :volume, :service_config, :service_port]
data/lib/mkit/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module MKIt
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.3"
3
3
  end
4
4