kontena-plugin-shell 0.1.3 → 0.2.0
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 +5 -5
- data/lib/kontena/plugin/shell/commands/kontena.rb +1 -1
- data/lib/kontena/plugin/shell/prompt_loader.rb +27 -0
- data/lib/kontena/plugin/shell/session.rb +74 -5
- data/lib/kontena/plugin/shell/stacks_common_ext.rb +21 -0
- data/lib/kontena/plugin/shell/version.rb +1 -1
- data/spec/kontena/plugin/shell/session_spec.rb +1 -0
- metadata +5 -4
- data/lib/kontena/plugin/shell/callbacks/stack_file.rb +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 09358261da75699f6afc793984e370be07c06c5cdda3ceaf24d2a7bd93804007
|
4
|
+
data.tar.gz: ea48df5a69dcb362faa3d9cb47a2949d4aaea1c191ccc908ebd0bb57e9c803b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0344b8bd9795dbd7288f17ed58618f393f3d7d00dc7e03036a7a266e223906063bd872173fa4bbdbf09c6f695790a849f7d916a3eecd2bee70244cd75cc8f7a
|
7
|
+
data.tar.gz: b35ea14e3b3d9d69022f0cee239bd467b8aadb06247ada4edc8dd335dbc54dd87d06662f5c6c3e0b45722b376392044d5ed87e9bd8669c207c1d909a4cd95bd8
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'kontena/cli/stacks/yaml/stack_file_loader'
|
2
|
+
|
3
|
+
module Kontena
|
4
|
+
module Plugin
|
5
|
+
module Shell
|
6
|
+
class PromptLoader < Kontena::Cli::Stacks::YAML::StackFileLoader
|
7
|
+
def self.match?(source, parent = nil)
|
8
|
+
source.end_with?('kontena.yml') && !Kontena::Cli::Stacks::YAML::FileLoader.match?(source, parent)
|
9
|
+
end
|
10
|
+
|
11
|
+
def read_content
|
12
|
+
content = Kontena.prompt.multiline("Enter or paste a stack YAML").join
|
13
|
+
raise "Invalid stack YAML" unless YAML.safe_load(content).kind_of?(Hash)
|
14
|
+
content
|
15
|
+
end
|
16
|
+
|
17
|
+
def origin
|
18
|
+
"prompt"
|
19
|
+
end
|
20
|
+
|
21
|
+
def registry
|
22
|
+
"prompt"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -27,6 +27,25 @@ module Kontena::Plugin
|
|
27
27
|
tokens = buf.split(/\s(?=(?:[^"]|"[^"]*")*$)/).map(&:strip)
|
28
28
|
runner = Shell.command(tokens.first) || Shell.command(context.first) || Kontena::Plugin::Shell::KontenaCommand
|
29
29
|
command = runner.new(context, tokens, self)
|
30
|
+
if fork_supported? && forkable_command?(command)
|
31
|
+
execute_with_fork(command)
|
32
|
+
else
|
33
|
+
execute_with_thread(command)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def forkable_command?(command)
|
38
|
+
return false if !command.is_a?(Kontena::Plugin::Shell::KontenaCommand)
|
39
|
+
return false if command.subcommand_class.has_subcommands?
|
40
|
+
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
44
|
+
def fork_supported?
|
45
|
+
Process.respond_to?(:fork)
|
46
|
+
end
|
47
|
+
|
48
|
+
def execute_with_thread(command)
|
30
49
|
old_trap = trap('INT', Proc.new { Thread.main[:command_thread] && Thread.main[:command_thread].kill })
|
31
50
|
Thread.main[:command_thread] = Thread.new do
|
32
51
|
command.run
|
@@ -35,6 +54,33 @@ module Kontena::Plugin
|
|
35
54
|
trap('INT', old_trap)
|
36
55
|
end
|
37
56
|
|
57
|
+
def execute_with_fork(command)
|
58
|
+
start = Time.now
|
59
|
+
pid = fork do
|
60
|
+
Process.setproctitle("kosh-runner")
|
61
|
+
command.run
|
62
|
+
end
|
63
|
+
trap('INT') {
|
64
|
+
begin
|
65
|
+
Process.kill('TERM', pid)
|
66
|
+
rescue Errno::ESRCH
|
67
|
+
raise SignalException, 'SIGINT'
|
68
|
+
end
|
69
|
+
}
|
70
|
+
Process.waitpid(pid)
|
71
|
+
if config_file_modified_since?(start)
|
72
|
+
puts ""
|
73
|
+
puts pastel.yellow("Config file has been modified, reloading configuration")
|
74
|
+
puts ""
|
75
|
+
config.reset_instance
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def config_file_modified_since?(time)
|
80
|
+
return false unless config.config_file_available?
|
81
|
+
return true if File.mtime(config.config_filename) >= time
|
82
|
+
end
|
83
|
+
|
38
84
|
def run
|
39
85
|
puts File.read(__FILE__)[/__END__$(.*)/m, 1]
|
40
86
|
puts "Kontena Shell v#{Kontena::Plugin::Shell::VERSION} (c) 2017 Kontena"
|
@@ -47,7 +93,17 @@ module Kontena::Plugin
|
|
47
93
|
end
|
48
94
|
|
49
95
|
# Hook stack command kontena.yml content prompting
|
50
|
-
|
96
|
+
if Gem::Version.new(Kontena::Cli::VERSION) >= Gem::Version.new('1.4.0')
|
97
|
+
require 'kontena/plugin/shell/prompt_loader'
|
98
|
+
else
|
99
|
+
require 'kontena/cli/stacks/validate_command'
|
100
|
+
require 'kontena/cli/stacks/install_command'
|
101
|
+
require 'kontena/cli/stacks/upgrade_command'
|
102
|
+
require 'kontena/plugin/shell/stacks_common_ext'
|
103
|
+
Kontena::Cli::Stacks::ValidateCommand.send(:include, Kontena::Plugin::Shell::StacksCommonExt)
|
104
|
+
Kontena::Cli::Stacks::InstallCommand.send(:include, Kontena::Plugin::Shell::StacksCommonExt)
|
105
|
+
Kontena::Cli::Stacks::UpgradeCommand.send(:include, Kontena::Plugin::Shell::StacksCommonExt)
|
106
|
+
end
|
51
107
|
|
52
108
|
Readline.completion_proc = Proc.new do |word|
|
53
109
|
line = Readline.line_buffer
|
@@ -92,7 +148,20 @@ module Kontena::Plugin
|
|
92
148
|
end
|
93
149
|
|
94
150
|
def prompt
|
95
|
-
|
151
|
+
if master_name && master_name.include?('/'.freeze)
|
152
|
+
org, name = master_name.split('/')
|
153
|
+
"#{pastel.bright_cyan(org)} / #{pastel.cyan(name)} #{pastel.yellow(context)} #{caret} "
|
154
|
+
elsif master_name && grid_name
|
155
|
+
"#{pastel.bright_cyan(master_name)} / #{pastel.cyan(grid_name)} #{pastel.yellow(context)} #{caret} "
|
156
|
+
elsif master_name
|
157
|
+
"#{pastel.bright_cyan(master_name)} / #{pastel.red('<no grid>')} #{pastel.yellow(context)} #{caret} "
|
158
|
+
else
|
159
|
+
if org = ENV['KONTENA_ORGANIZATION']
|
160
|
+
"#{pastel.bright_cyan(org)} #{pastel.yellow(context)} #{caret} "
|
161
|
+
else
|
162
|
+
"#{pastel.yellow(context)} #{caret} "
|
163
|
+
end
|
164
|
+
end
|
96
165
|
end
|
97
166
|
|
98
167
|
def caret
|
@@ -100,17 +169,17 @@ module Kontena::Plugin
|
|
100
169
|
end
|
101
170
|
|
102
171
|
def master_name
|
103
|
-
config.current_master
|
172
|
+
config.current_master.name if config.current_master
|
104
173
|
end
|
105
174
|
|
106
175
|
def grid_name
|
107
|
-
config.current_grid
|
176
|
+
config.current_grid
|
108
177
|
end
|
109
178
|
end
|
110
179
|
end
|
111
180
|
end
|
112
181
|
__END__
|
113
|
-
|
182
|
+
_ _
|
114
183
|
| | _____ _ __ | |_ ___ _ __ __ _
|
115
184
|
| |/ / _ \| '_ \| __/ _ \ '_ \ / _` |
|
116
185
|
| < (_) | | | | || __/ | | | (_| |
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module Kontena
|
4
|
+
module Plugin
|
5
|
+
module Shell
|
6
|
+
module StacksCommonExt
|
7
|
+
def reader_from_yaml(*args)
|
8
|
+
if args.first == 'kontena.yml' && !File.exist?('kontena.yml')
|
9
|
+
tempfile = Tempfile.new('kontena.yml')
|
10
|
+
tempfile.write(Kontena.prompt.multiline("Enter or paste a stack YAML").join)
|
11
|
+
tempfile.close
|
12
|
+
args[0] = tempfile.path
|
13
|
+
end
|
14
|
+
reader = super(*args)
|
15
|
+
File.unlink(args[0]) if File.exist?(args[0])
|
16
|
+
reader
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -8,6 +8,7 @@ describe Kontena::Plugin::Shell::Session do
|
|
8
8
|
let(:subject) { described_class.new(context) }
|
9
9
|
|
10
10
|
it 'runs commands' do
|
11
|
+
allow(subject).to receive(:fork_supported?).and_return(false)
|
11
12
|
expect(Readline).to receive(:readline).and_return('exit')
|
12
13
|
expect{subject.run}.to output(/Bye/).to_stdout.and raise_error(SystemExit)
|
13
14
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kontena-plugin-shell
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kontena, Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kontena-cli
|
@@ -79,7 +79,6 @@ files:
|
|
79
79
|
- README.md
|
80
80
|
- bin/kosh
|
81
81
|
- lib/kontena/plugin/shell.rb
|
82
|
-
- lib/kontena/plugin/shell/callbacks/stack_file.rb
|
83
82
|
- lib/kontena/plugin/shell/command.rb
|
84
83
|
- lib/kontena/plugin/shell/commands/batch.rb
|
85
84
|
- lib/kontena/plugin/shell/commands/batch_do.rb
|
@@ -92,8 +91,10 @@ files:
|
|
92
91
|
- lib/kontena/plugin/shell/commands/kontena.rb
|
93
92
|
- lib/kontena/plugin/shell/completer.rb
|
94
93
|
- lib/kontena/plugin/shell/context.rb
|
94
|
+
- lib/kontena/plugin/shell/prompt_loader.rb
|
95
95
|
- lib/kontena/plugin/shell/session.rb
|
96
96
|
- lib/kontena/plugin/shell/shell_command.rb
|
97
|
+
- lib/kontena/plugin/shell/stacks_common_ext.rb
|
97
98
|
- lib/kontena/plugin/shell/version.rb
|
98
99
|
- lib/kontena_cli_plugin.rb
|
99
100
|
- spec/helpers/cli_helper.rb
|
@@ -129,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
130
|
version: '0'
|
130
131
|
requirements: []
|
131
132
|
rubyforge_project:
|
132
|
-
rubygems_version: 2.
|
133
|
+
rubygems_version: 2.7.2
|
133
134
|
signing_key:
|
134
135
|
specification_version: 4
|
135
136
|
summary: Kontena interactive shell plugin
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'tempfile'
|
2
|
-
module Kontena
|
3
|
-
module Plugin
|
4
|
-
module Shell
|
5
|
-
module Callbacks
|
6
|
-
class StackFile < Kontena::Callback
|
7
|
-
|
8
|
-
matches_commands 'stacks install', 'stacks validate', 'stacks upgrade'
|
9
|
-
|
10
|
-
def before
|
11
|
-
Kontena::Cli::Stacks::YAML::Reader.class_eval do
|
12
|
-
def self.new(*args)
|
13
|
-
if args.first == 'kontena.yml' && !File.exist?('kontena.yml')
|
14
|
-
@tempfile = Tempfile.new('kontena.yml')
|
15
|
-
@tempfile.write(Kontena.prompt.multiline("Enter or paste a stack YAML").join)
|
16
|
-
@tempfile.close
|
17
|
-
args[0] = @tempfile.path
|
18
|
-
end
|
19
|
-
super *args
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def after
|
25
|
-
@tempfile.unlink if @tempfile
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|