mkit 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/publish.yml +22 -0
- data/.rubocop.yml +10 -0
- data/Gemfile +12 -10
- data/Gemfile.lock +3 -0
- data/README.md +33 -21
- data/Rakefile +17 -15
- data/bin/mkitc +258 -84
- data/bin/mkitd +3 -6
- data/config/mkit_config.yml +5 -5
- data/db/migrate/001_setup.rb +5 -5
- data/db/migrate/002_mkit_jobs.rb +2 -3
- data/db/schema.rb +76 -75
- data/lib/mkit/app/controllers/mkit_controller.rb +25 -0
- data/lib/mkit/app/controllers/mkitjobs_controller.rb +4 -5
- data/lib/mkit/app/controllers/pods_controller.rb +4 -5
- data/lib/mkit/app/controllers/services_controller.rb +24 -13
- data/lib/mkit/app/helpers/docker_helper.rb +11 -3
- data/lib/mkit/app/helpers/erb_helper.rb +4 -2
- data/lib/mkit/app/helpers/services_helper.rb +18 -40
- data/lib/mkit/app/mkit_server.rb +2 -0
- data/lib/mkit/app/model/service.rb +10 -1
- data/lib/mkit/version.rb +1 -1
- data/lib/mkit.rb +46 -47
- data/mkit.gemspec +33 -35
- data/samples/apps/kafka-cluster.yml +21 -0
- data/samples/apps/kafka-magic.yml +16 -0
- data/samples/apps/kafka-zookeeper.yml +15 -0
- data/samples/apps/minio.yml +16 -0
- data/samples/apps/mongo.yml +9 -0
- data/samples/apps/nexus.yml +13 -0
- data/samples/apps/redis-sentinel.yml +11 -0
- data/samples/apps/redis.yml +9 -0
- metadata +64 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb997b1bc61151dde6209eeaefdf4f669df9255b7e69735eb0c5c56e346222f1
|
4
|
+
data.tar.gz: 3819a29d9335a2d4831051d133d9e0056b93d54803701a27ed596a750f907d7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c95a967e4a016554700b9e844de87481fd8f4b1f71e2cb4cdae16509104b177485f6cdcc3aa114f3d4ab8680e8d39fb05c125fca4dd81edbb089800d08ec275e
|
7
|
+
data.tar.gz: f62ac16133370c7bc41f901fe148b708c046f4598331848daefd4bb108419f7b2fc510a9176bff771fbff3afa4e38d5ce000d70d8cad1feeb41b8f339956d802
|
@@ -0,0 +1,22 @@
|
|
1
|
+
name: Publish Gem
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
tags:
|
6
|
+
- v[0-9]*.[0-9]*.[0-9]*
|
7
|
+
|
8
|
+
jobs:
|
9
|
+
build:
|
10
|
+
runs-on: ubuntu-latest
|
11
|
+
steps:
|
12
|
+
- uses: actions/checkout@v3
|
13
|
+
- name: Publish to RubyGems
|
14
|
+
run: |
|
15
|
+
mkdir -p $HOME/.gem
|
16
|
+
touch $HOME/.gem/credentials
|
17
|
+
chmod 0600 $HOME/.gem/credentials
|
18
|
+
printf -- "---\n:rubygems_api_key: ${RUBYGEMS_API_KEY}\n" > $HOME/.gem/credentials
|
19
|
+
gem build *.gemspec
|
20
|
+
gem push *.gem
|
21
|
+
env:
|
22
|
+
RUBYGEMS_API_KEY: "${{secrets.RUBYGEMS_API_KEY}}"
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# The behavior of RuboCop can be controlled via the .rubocop.yml
|
2
|
+
# configuration file. It makes it possible to enable/disable
|
3
|
+
# certain cops (checks) and to alter their behavior if they accept
|
4
|
+
# any parameters. The file can be placed either in your home
|
5
|
+
# directory or in some project directory.
|
6
|
+
#
|
7
|
+
# RuboCop will start looking for the configuration file in the directory
|
8
|
+
# where the inspected file is and continue its way up to the root directory.
|
9
|
+
#
|
10
|
+
# See https://docs.rubocop.org/rubocop/configuration
|
data/Gemfile
CHANGED
@@ -1,21 +1,23 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# ruby '3.0.2'
|
4
6
|
|
5
|
-
gem 'net-ping'
|
6
7
|
gem 'dry-container'
|
8
|
+
gem 'net-ping'
|
9
|
+
gem 'rake'
|
7
10
|
gem 'sqlite3'
|
8
11
|
gem 'standalone_migrations'
|
12
|
+
gem 'text-table'
|
9
13
|
|
10
|
-
gem '
|
11
|
-
gem 'rack', '>= 2.0.6'
|
14
|
+
gem 'async-dns'
|
12
15
|
gem 'pry'
|
16
|
+
gem 'rack', '>= 2.0.6'
|
13
17
|
gem 'rubydns'
|
14
|
-
gem '
|
18
|
+
gem 'sinatra-activerecord'
|
15
19
|
# rest http server
|
20
|
+
gem 'net_http_unix' # socket client
|
16
21
|
gem 'sinatra'
|
17
22
|
gem 'thin' # socket server
|
18
|
-
gem '
|
19
|
-
#gem 'puma'
|
20
|
-
|
21
|
-
|
23
|
+
# gem 'puma'
|
data/Gemfile.lock
CHANGED
@@ -104,6 +104,7 @@ GEM
|
|
104
104
|
nokogiri (~> 1.14)
|
105
105
|
railties (>= 6.0.0, < 7.1.0)
|
106
106
|
rake (>= 10.0)
|
107
|
+
text-table (1.2.4)
|
107
108
|
thin (1.8.2)
|
108
109
|
daemons (~> 1.0, >= 1.0.9)
|
109
110
|
eventmachine (~> 1.0, >= 1.0.4)
|
@@ -126,11 +127,13 @@ DEPENDENCIES
|
|
126
127
|
net_http_unix
|
127
128
|
pry
|
128
129
|
rack (>= 2.0.6)
|
130
|
+
rake
|
129
131
|
rubydns
|
130
132
|
sinatra
|
131
133
|
sinatra-activerecord
|
132
134
|
sqlite3
|
133
135
|
standalone_migrations
|
136
|
+
text-table
|
134
137
|
thin
|
135
138
|
|
136
139
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -53,35 +53,47 @@ There's also samples on the samples dir, for daemontools and systemd.
|
|
53
53
|
|
54
54
|
### Accessing the API
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
56
|
+
A client is provided to interact with mkit server.
|
57
|
+
|
58
|
+
Run `mkitc help` to list current supported commands.
|
59
|
+
|
60
|
+
```
|
61
|
+
Usage: mkitc <command> [options]
|
62
|
+
|
63
|
+
Micro k8s on Ruby - a simple tool to mimic a (very) minimalistic k8 cluster
|
64
|
+
|
65
|
+
Commands:
|
66
|
+
|
67
|
+
ps show services status (alias for status)
|
68
|
+
status show services status
|
69
|
+
logs prints service logs
|
70
|
+
start start service
|
71
|
+
stop stop service
|
72
|
+
restart restart service
|
73
|
+
create create new service
|
74
|
+
update update service
|
75
|
+
rm remove service
|
76
|
+
version prints mkit server version
|
77
|
+
proxy haproxy status and control
|
78
|
+
|
79
|
+
Run 'mkitc help <command>' for specific command information.
|
80
|
+
```
|
69
81
|
|
70
82
|
Example:
|
71
83
|
|
72
84
|
```
|
73
85
|
$ mkitc ps postgres
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
86
|
+
+----+----------+---------------+----------+--------------+---------+
|
87
|
+
| id | name | addr | ports | pods | status |
|
88
|
+
+----+----------+---------------+----------+--------------+---------+
|
89
|
+
| 2 | postgres | 10.210.198.10 | tcp/4532 | 49b5e4c8f247 | RUNNING |
|
90
|
+
+----+----------+---------------+----------+--------------+---------+
|
79
91
|
```
|
80
92
|
The service `postgres` is available on IP `10.210.198.10:5432`
|
81
93
|
|
82
94
|
## Configuration
|
83
95
|
|
84
|
-
On startup, configuration files on `config` directory will be copied to `/etc/mkit`. HAProxy config
|
96
|
+
On startup, configuration files on `config` directory will be copied to `/etc/mkit`. HAProxy config directory and control commands are defined on `mkit_config.yml`
|
85
97
|
|
86
98
|
You must configure `haproxy` to use config directory. e.g. on Ubuntu
|
87
99
|
|
@@ -115,8 +127,8 @@ service:
|
|
115
127
|
- 5672:5672:tcp:round_robin
|
116
128
|
- 80:15672:http:round_robin
|
117
129
|
resources:
|
118
|
-
|
119
|
-
|
130
|
+
min_replicas: 1 # default value
|
131
|
+
max_replicas: 1 # default value
|
120
132
|
volumes:
|
121
133
|
- docker://mkit_rabbitmq_data:/var/lib/rabbitmq # a docker volume - it will be created if it does not exists
|
122
134
|
- /var/log/rabbitmq/logs:/var/log/rabbitmq # a local volume
|
data/Rakefile
CHANGED
@@ -1,42 +1,45 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ENV['SINATRA_ENV'] ||= 'development'
|
2
4
|
|
3
5
|
require 'rubygems'
|
4
|
-
require 'sinatra/activerecord/rake'
|
5
6
|
require 'standalone_migrations'
|
6
7
|
require 'rubygems/package_task'
|
7
8
|
require 'rubygems/specification'
|
8
9
|
require 'rake/testtask'
|
9
10
|
require 'pry'
|
10
11
|
require 'fileutils'
|
12
|
+
require 'bundler'
|
11
13
|
require 'bundler/setup'
|
12
14
|
require 'dry-container'
|
13
15
|
require 'sinatra/activerecord'
|
16
|
+
require 'sinatra/activerecord/rake'
|
14
17
|
require 'rubydns'
|
15
18
|
require_relative 'lib/mkit/version.rb'
|
16
19
|
require_relative 'lib/mkit/utils'
|
17
20
|
require_relative 'lib/mkit'
|
18
21
|
|
19
22
|
$LOAD_PATH.unshift File.expand_path('lib')
|
20
|
-
rails_env=ENV[
|
23
|
+
rails_env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
21
24
|
# db migrations, use database config
|
22
|
-
ENV[
|
25
|
+
ENV['DATABASE_URL'] = MKIt::Utils.db_config_to_uri(rails_env)
|
23
26
|
|
24
27
|
desc 'Builds the gem'
|
25
28
|
task :package do
|
26
|
-
sh %
|
29
|
+
sh %(gem build "mkit.gemspec")
|
27
30
|
end
|
28
31
|
|
29
|
-
task :
|
30
|
-
sh %
|
32
|
+
task install: [:package] do
|
33
|
+
sh %(gem install mkit-#{MKIt::VERSION}.gem)
|
31
34
|
end
|
32
35
|
|
33
36
|
desc 'Copy rb to packaging dir'
|
34
|
-
task :
|
35
|
-
FileUtils.cp_r('app', 'target/build', {:
|
36
|
-
FileUtils.cp_r('config', 'target/build', {:
|
37
|
-
FileUtils.cp_r('bin', 'target/build', {:
|
38
|
-
FileUtils.cp_r('lib', 'target/build', {:
|
39
|
-
FileUtils.cp_r('config.ru', 'target/build', {:
|
37
|
+
task build: [:init] do
|
38
|
+
FileUtils.cp_r('app', 'target/build', { remove_destination: true })
|
39
|
+
FileUtils.cp_r('config', 'target/build', { remove_destination: true })
|
40
|
+
FileUtils.cp_r('bin', 'target/build', { remove_destination: true })
|
41
|
+
FileUtils.cp_r('lib', 'target/build', { remove_destination: true })
|
42
|
+
FileUtils.cp_r('config.ru', 'target/build', { remove_destination: true })
|
40
43
|
end
|
41
44
|
|
42
45
|
desc 'Create build dirs'
|
@@ -45,10 +48,9 @@ task :init do
|
|
45
48
|
FileUtils.mkdir_p('target/package')
|
46
49
|
end
|
47
50
|
|
48
|
-
desc
|
51
|
+
desc 'Rake Console'
|
49
52
|
task :console do
|
50
53
|
Pry.start
|
51
54
|
end
|
52
55
|
|
53
56
|
StandaloneMigrations::Tasks.load_tasks
|
54
|
-
|
data/bin/mkitc
CHANGED
@@ -8,104 +8,279 @@ require 'net/http'
|
|
8
8
|
require 'json'
|
9
9
|
require 'net_http_unix'
|
10
10
|
require 'securerandom'
|
11
|
+
require 'erb'
|
11
12
|
|
12
|
-
class
|
13
|
+
class InvalidParametersException < RuntimeError
|
14
|
+
attr_reader :command
|
15
|
+
def initialize(cause, command = nil)
|
16
|
+
super(cause)
|
17
|
+
@command = command
|
18
|
+
end
|
19
|
+
end
|
13
20
|
|
21
|
+
class MKItClient
|
14
22
|
def initialize
|
15
|
-
@client = NetX::HTTPUnix.new(
|
23
|
+
@client = NetX::HTTPUnix.new('localhost', 4567)
|
16
24
|
end
|
17
25
|
|
18
|
-
def
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
26
|
+
def dict
|
27
|
+
global_args = [
|
28
|
+
{ short: '-v', long: '--verbose', help: 'verbose', mandatory: false, value: nil }
|
29
|
+
]
|
30
|
+
command_dict = [
|
31
|
+
{
|
32
|
+
cmd: 'ps',
|
33
|
+
args: [
|
34
|
+
{ name: 'id', mandatory: false, uri: '/<%=id%>' }
|
35
|
+
],
|
36
|
+
help: 'show services status (alias for status)',
|
37
|
+
usage: ['[service_id_or_name]'],
|
38
|
+
request: { verb: :get, uri: '/services' }
|
39
|
+
},
|
40
|
+
{
|
41
|
+
cmd: 'status',
|
42
|
+
args: [
|
43
|
+
{ name: 'id', mandatory: false, uri: '/<%=id%>' }
|
44
|
+
],
|
45
|
+
help: 'show services status',
|
46
|
+
usage: ['[service_id_or_name]'],
|
47
|
+
request: { verb: :get, uri: '/services' }
|
48
|
+
},
|
49
|
+
{
|
50
|
+
cmd: 'logs',
|
51
|
+
args: [
|
52
|
+
{ name: 'id', mandatory: true }
|
53
|
+
],
|
54
|
+
help: 'prints service logs',
|
55
|
+
usage: ['<service_id_or_name>'],
|
56
|
+
request: { verb: :get, uri: '/services/<%=id%>/logs' }
|
57
|
+
},
|
58
|
+
{
|
59
|
+
cmd: 'start',
|
60
|
+
args: [
|
61
|
+
{ name: 'id', mandatory: true }
|
62
|
+
],
|
63
|
+
help: 'start service',
|
64
|
+
usage: ['<service_id_or_name>'],
|
65
|
+
request: { verb: :put, uri: '/services/<%=id%>/start' }
|
66
|
+
},
|
67
|
+
{
|
68
|
+
cmd: 'stop',
|
69
|
+
args: [
|
70
|
+
{ name: 'id', mandatory: true }
|
71
|
+
],
|
72
|
+
help: 'stop service',
|
73
|
+
usage: ['<service_id_or_name>'],
|
74
|
+
request: { verb: :put, uri: '/services/<%=id%>/stop' }
|
75
|
+
},
|
76
|
+
{
|
77
|
+
cmd: 'restart',
|
78
|
+
args: [
|
79
|
+
{ name: 'id', mandatory: true }
|
80
|
+
],
|
81
|
+
help: 'restart service',
|
82
|
+
usage: ['<service_id_or_name>'],
|
83
|
+
request: { verb: :put, uri: '/services/<%=id%>/restart' }
|
84
|
+
},
|
85
|
+
{
|
86
|
+
cmd: 'create',
|
87
|
+
args: [
|
88
|
+
{ name: 'file', mandatory: true }
|
89
|
+
],
|
90
|
+
help: 'create new service',
|
91
|
+
usage: ['<service.yaml>'],
|
92
|
+
request: { verb: :post, uri: '/services' }
|
93
|
+
},
|
94
|
+
{
|
95
|
+
cmd: 'update',
|
96
|
+
args: [
|
97
|
+
{ name: 'file', mandatory: true }
|
98
|
+
],
|
99
|
+
help: 'update service',
|
100
|
+
usage: ['<service.yaml>'],
|
101
|
+
request: { verb: :put, uri: '/services/<%=id%>' }
|
102
|
+
},
|
103
|
+
{
|
104
|
+
cmd: 'rm',
|
105
|
+
args: [
|
106
|
+
{ name: 'id', mandatory: true }
|
107
|
+
],
|
108
|
+
help: 'remove service',
|
109
|
+
usage: ['<service_id_or_name>'],
|
110
|
+
request: { verb: :delete, uri: '/services/<%=id%>' }
|
111
|
+
},
|
112
|
+
{
|
113
|
+
cmd: 'version',
|
114
|
+
help: 'prints mkit server version',
|
115
|
+
request: { verb: :get, uri: '/mkit/version' }
|
116
|
+
},
|
117
|
+
{
|
118
|
+
cmd: 'proxy',
|
119
|
+
options: [
|
120
|
+
{
|
121
|
+
cmd: 'start',
|
122
|
+
request: { verb: :put, uri: '/mkit/proxy/start' },
|
123
|
+
help: 'start proxy service'
|
124
|
+
},
|
125
|
+
{
|
126
|
+
cmd: 'stop',
|
127
|
+
request: { verb: :put, uri: '/mkit/proxy/stop' },
|
128
|
+
help: 'stop proxy service'
|
129
|
+
},
|
130
|
+
{
|
131
|
+
cmd: 'restart',
|
132
|
+
request: { verb: :put, uri: '/mkit/proxy/restart' },
|
133
|
+
help: 'restarts proxy service'
|
134
|
+
},
|
135
|
+
{
|
136
|
+
cmd: 'status',
|
137
|
+
request: { verb: :get, uri: '/mkit/proxy/status' },
|
138
|
+
help: 'proxy service status'
|
139
|
+
}
|
140
|
+
],
|
141
|
+
help: 'haproxy status and control',
|
142
|
+
usage: ['<start|stop|restart|status>']
|
143
|
+
}
|
144
|
+
]
|
145
|
+
command_dict
|
146
|
+
end
|
147
|
+
|
148
|
+
def help(cause: nil, cmd: nil)
|
149
|
+
msg = ''
|
150
|
+
if cause.nil?
|
151
|
+
my_cmd = cmd
|
152
|
+
else
|
153
|
+
msg += "MKItc: #{cause.message}\n"
|
154
|
+
my_cmd = cause.command
|
155
|
+
end
|
156
|
+
if my_cmd.nil?
|
157
|
+
msg += "\nUsage: mkitc <command> [options]\n\n"
|
158
|
+
msg += "Micro k8s on Ruby - a simple tool to mimic a (very) minimalistic k8 cluster\n\n"
|
159
|
+
msg += "Commands:\n\n"
|
160
|
+
dict.each do |c|
|
161
|
+
msg += format("%-10s %s\n", c[:cmd], c[:help])
|
50
162
|
end
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
163
|
+
msg += "\n"
|
164
|
+
msg += "Run 'mkitc help <command>' for specific command information.\n\n"
|
165
|
+
else
|
166
|
+
msg += format("\nUsage: mkitc %s %s\n\n", my_cmd[:cmd], my_cmd[:usage].nil? ? '' : my_cmd[:usage].join(' '))
|
167
|
+
msg += format("%s\n", my_cmd[:help])
|
168
|
+
unless my_cmd[:options].nil?
|
169
|
+
msg += "\nOptions:\n"
|
170
|
+
my_cmd[:options].each do |c|
|
171
|
+
msg += format("%-10s %s\n", c[:cmd], c[:help])
|
172
|
+
end
|
59
173
|
end
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
174
|
+
msg += "\n"
|
175
|
+
end
|
176
|
+
puts msg
|
177
|
+
exit 1
|
178
|
+
end
|
179
|
+
|
180
|
+
def create(request, request_hash = nil)
|
181
|
+
unless File.file?(request_hash[:file])
|
182
|
+
raise InvalidParametersException.new('File not found.', c = dict.select { |k| k[:cmd] == 'create' }.first)
|
183
|
+
end
|
184
|
+
|
185
|
+
yaml = YAML.load_file(request_hash[:file])
|
186
|
+
if yaml['service'].nil?
|
187
|
+
raise InvalidParametersException.new('Invalid configuration file', c = dict.select { |k| k[:cmd] == 'create' }.first)
|
188
|
+
else
|
189
|
+
request(request, request_hash)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def update(request, request_hash = nil)
|
194
|
+
unless File.file?(request_hash[:file])
|
195
|
+
raise InvalidParametersException.new('File not found.', c = dict.select { |k| k[:cmd] == 'update' }.first)
|
196
|
+
end
|
197
|
+
|
198
|
+
yaml = YAML.load_file(request_hash[:file])
|
199
|
+
if yaml['service'].nil?
|
200
|
+
raise InvalidParametersException.new('Invalid configuration file', c = dict.select { |k| k[:cmd] == 'update' }.first)
|
201
|
+
else
|
202
|
+
id = yaml['service']['name']
|
203
|
+
request_hash[:id] = id
|
204
|
+
request(request, request_hash)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
def parse_args(args)
|
209
|
+
cmd = args[0]
|
210
|
+
c = nil
|
211
|
+
# short circuit for help
|
212
|
+
if cmd == 'help' || args.empty?
|
213
|
+
if args.size > 1
|
214
|
+
c = dict.select { |k| k[:cmd] == args[1] }.first
|
215
|
+
raise InvalidParametersException, "'#{args[1]}' is not a valid help topic." if c.nil?
|
68
216
|
end
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
217
|
+
return help(cmd: c)
|
218
|
+
else
|
219
|
+
c = dict.select { |k| k[:cmd] == cmd }.first
|
220
|
+
end
|
221
|
+
raise InvalidParametersException, 'Command not found' if c.nil?
|
222
|
+
|
223
|
+
myargs = args.dup
|
224
|
+
myargs.delete(cmd)
|
225
|
+
|
226
|
+
max_args_size = c[:args].nil? ? 0 : c[:args].size
|
227
|
+
max_options_size = c[:options].nil? ? 0 : 1
|
228
|
+
max_args_size += max_options_size
|
229
|
+
|
230
|
+
min_args_size = c[:args].nil? ? 0 : c[:args].select { |a| a[:mandatory] == true }.size
|
231
|
+
min_options_size = c[:options].nil? ? 0 : 1
|
232
|
+
min_args_size += min_options_size
|
233
|
+
|
234
|
+
if myargs.size > max_args_size || myargs.size < min_args_size
|
235
|
+
raise InvalidParametersException.new('Invalid parameters found.', c)
|
236
|
+
end
|
237
|
+
|
238
|
+
request_hash = {}
|
239
|
+
request = c[:request]
|
240
|
+
unless myargs.empty?
|
241
|
+
unless c[:args].nil?
|
242
|
+
idx = 0
|
243
|
+
c[:args].each do |a|
|
244
|
+
request_hash[a[:name].to_sym] = myargs[idx]
|
245
|
+
request[:uri] = request[:uri] + a[:uri] unless a[:uri].nil?
|
246
|
+
idx += 1
|
247
|
+
end
|
77
248
|
end
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
if yaml["service"].nil?
|
85
|
-
raise 'invalid configuration file'
|
86
|
-
else
|
87
|
-
id = yaml["service"]["name"]
|
88
|
-
request = { verb: :put, uri: "/services/#{id}", file: file }
|
249
|
+
# options
|
250
|
+
unless c[:options].nil?
|
251
|
+
option = nil
|
252
|
+
myargs.each do |s|
|
253
|
+
option = c[:options].select { |o| o[:cmd] == s }.first
|
254
|
+
raise InvalidParametersException.new('Invalid parameters found.', c) if option.nil? || option.empty?
|
89
255
|
end
|
90
|
-
|
91
|
-
|
256
|
+
raise InvalidParametersException.new('Invalid parameters found.', c) if option.nil? || option.empty?
|
257
|
+
|
258
|
+
request = option[:request]
|
92
259
|
end
|
260
|
+
end
|
261
|
+
raise InvalidParametersException, "Can't find request." if request.nil?
|
262
|
+
|
263
|
+
if respond_to? c[:cmd]
|
264
|
+
send(c[:cmd], request, request_hash)
|
93
265
|
else
|
94
|
-
|
266
|
+
request(request, request_hash)
|
95
267
|
end
|
96
|
-
request
|
97
268
|
end
|
98
269
|
|
99
270
|
def doIt(args)
|
100
|
-
|
101
|
-
puts
|
271
|
+
result = parse_args(args)
|
272
|
+
puts result
|
273
|
+
rescue InvalidParametersException => e
|
274
|
+
help(cause: e)
|
102
275
|
end
|
103
276
|
|
104
|
-
def request(request)
|
277
|
+
def request(request, request_args = nil)
|
105
278
|
req = nil
|
106
|
-
uri = request[:uri]
|
279
|
+
uri = ERB.new(request[:uri]).result_with_hash(request_args)
|
280
|
+
request[:file] = request_args[:file]
|
281
|
+
|
107
282
|
unless request[:params].nil? || request[:params].empty?
|
108
|
-
uri = uri + '?' + request[:params].map{|k,v| "#{k}=#{v}"}.join('&')
|
283
|
+
uri = uri + '?' + request[:params].map { |k, v| "#{k}=#{v}" }.join('&')
|
109
284
|
end
|
110
285
|
case request[:verb]
|
111
286
|
when :post
|
@@ -113,14 +288,14 @@ class MKItClient
|
|
113
288
|
unless request[:file].nil?
|
114
289
|
(body, boundary) = attach(request[:file])
|
115
290
|
req.body = body
|
116
|
-
req[
|
291
|
+
req['Content-Type'] = "multipart/form-data, boundary=#{boundary}"
|
117
292
|
end
|
118
293
|
when :put
|
119
294
|
req = Net::HTTP::Put.new(uri)
|
120
295
|
unless request[:file].nil?
|
121
296
|
(body, boundary) = attach(request[:file])
|
122
297
|
req.body = body
|
123
|
-
req[
|
298
|
+
req['Content-Type'] = "multipart/form-data, boundary=#{boundary}"
|
124
299
|
end
|
125
300
|
when :patch
|
126
301
|
req = Net::HTTP::Patch.new(uri)
|
@@ -129,11 +304,11 @@ class MKItClient
|
|
129
304
|
when :delete
|
130
305
|
req = Net::HTTP::Delete.new(uri)
|
131
306
|
end
|
132
|
-
@client.request(req)
|
307
|
+
@client.request(req).body
|
133
308
|
end
|
134
309
|
|
135
310
|
def attach(file)
|
136
|
-
boundary=SecureRandom.alphanumeric
|
311
|
+
boundary = SecureRandom.alphanumeric
|
137
312
|
body = []
|
138
313
|
body << "--#{boundary}\r\n"
|
139
314
|
body << "Content-Disposition: form-data; name=file; filename='#{File.basename(file)}'\r\n"
|
@@ -141,7 +316,7 @@ class MKItClient
|
|
141
316
|
body << "\r\n"
|
142
317
|
body << File.read(file)
|
143
318
|
body << "\r\n--#{boundary}--\r\n"
|
144
|
-
[
|
319
|
+
[body.join, boundary]
|
145
320
|
end
|
146
321
|
end
|
147
322
|
|
@@ -155,4 +330,3 @@ client.doIt(ARGV.dup)
|
|
155
330
|
# parse args
|
156
331
|
# host, socket, config file
|
157
332
|
# end
|
158
|
-
|