kuberun 0.1.0 → 0.1.1

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: da5d548aede020fab4769a2a45582b3f414ce86245b3d50dc970703ca6ab4f24
4
- data.tar.gz: 5f772f24c91a560370e6acd080da010a2bfc4c2212d5373378f2d1b9fc94dfde
3
+ metadata.gz: fca4d9708c1d5a9a9afa70d9983940d31f8dfebb6900c5f570c4add622dac72f
4
+ data.tar.gz: 836812210b8df17491153f82c7d3feba0ac2b6067fe2bea92bb8a8af83380f6a
5
5
  SHA512:
6
- metadata.gz: a22538c40dbf5188836b1814f3a214e3ce3eb6e83ba75e999f35e91d4cc036e5892c2629bb92dfc8a2839e87fb44948a333a440db0f5cbefab4d9ab8ff1eaae5
7
- data.tar.gz: 41cc9a3d8ad0f05208cfc841f7ddb07130e6e30d2c525945489c6246b3e79d7bbcf8d0bf6c6a613ec4a947405144bea7cc9ce9bf00fb6bcf4b4c8e20184511f9
6
+ metadata.gz: f77eda69cc9423990517a5810ec17a6fa7465e12a31d40a049c06a6c196273b3673df6ca87c715c5dae3dc5187ce8608e55084f319a0036ed149b2b95330780d
7
+ data.tar.gz: 2e553ff1da4812949e6ab878b5925a36aa34436784f4208194dec0279692e09ac333c8f1b4d0b726d80dbf3355d2692ed27f48e6c1b5af5a8a7e932ad1d4e21a
data/.travis.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
+ - 2.4.0
4
5
  - 2.5.1
5
6
  before_install: gem install bundler -N
data/Gemfile.lock CHANGED
@@ -26,14 +26,11 @@ GEM
26
26
  remote: https://rubygems.org/
27
27
  specs:
28
28
  ast (2.4.0)
29
- byebug (10.0.2)
30
- coderay (1.1.2)
31
29
  diff-lcs (1.3)
32
30
  equatable (0.5.0)
33
31
  hitimes (1.3.0)
34
32
  jaro_winkler (1.5.1)
35
33
  kramdown (1.16.2)
36
- method_source (0.9.0)
37
34
  necromancer (0.4.0)
38
35
  parallel (1.12.1)
39
36
  parser (2.5.1.2)
@@ -42,12 +39,6 @@ GEM
42
39
  equatable (~> 0.5.0)
43
40
  tty-color (~> 0.4.0)
44
41
  powerpack (0.1.2)
45
- pry (0.11.3)
46
- coderay (~> 1.1.0)
47
- method_source (~> 0.9.0)
48
- pry-byebug (3.6.0)
49
- byebug (~> 10.0)
50
- pry (~> 0.10)
51
42
  rainbow (3.0.0)
52
43
  rake (10.5.0)
53
44
  rouge (3.1.1)
@@ -139,8 +130,6 @@ PLATFORMS
139
130
  DEPENDENCIES
140
131
  bundler (~> 1.16)
141
132
  kuberun!
142
- pry
143
- pry-byebug
144
133
  rake (~> 10.0)
145
134
  rspec (~> 3.0)
146
135
  rubocop (~> 0.58.0)
data/README.md CHANGED
@@ -6,13 +6,17 @@ This CLI tool aims to create completely separate pod for those commands without
6
6
 
7
7
  Uses `kubectl` inside.
8
8
 
9
+ ## Status
10
+
11
+ This tool is in early alpha stage.
12
+
9
13
  ## Installation
10
14
 
11
15
  Dependencies:
12
16
  * Ruby >= 2.4
13
17
  * kubectl in `$PATH`
14
18
 
15
- Add this line to your application's Gemfile:
19
+ Add this line to your application's Gemfile (preferably in `development` group):
16
20
 
17
21
  ```ruby
18
22
  gem 'kuberun'
@@ -28,7 +32,9 @@ Or install it yourself as:
28
32
 
29
33
  ## Usage
30
34
 
31
- TODO: Write usage instructions here
35
+ ```
36
+ kuberun help
37
+ ```
32
38
 
33
39
  ## Development
34
40
 
data/lib/kuberun/cli.rb CHANGED
@@ -8,7 +8,7 @@ module Kuberun
8
8
  #
9
9
  # @api public
10
10
  class CLI < Thor
11
- DEFAULT_OPTIONS_FOR_KUBECTL_OPTIONS = { type: :string, default: '', desc: 'See kubectl options' }
11
+ DEFAULT_OPTIONS_FOR_KUBECTL_OPTIONS = { type: :string, default: '', desc: 'See kubectl options' }.freeze
12
12
  BASE_KUBECTL_OPTIONS = {
13
13
  'certificate-authority': {},
14
14
  'client-certificate': {},
@@ -19,11 +19,12 @@ module Kuberun
19
19
  'kubeconfig': {},
20
20
  'namespace': { aliases: :'-n' },
21
21
  'token': {},
22
- 'v': { type: :numeric, default: 0, desc: 'Log level, also passed to kubectl' },
23
- }
22
+ 'v': { type: :numeric, default: 0, desc: 'Log level passed to kubectl' }
23
+ }.freeze
24
24
  BASE_KUBECTL_OPTIONS.each do |option_name, hash|
25
25
  class_option option_name, DEFAULT_OPTIONS_FOR_KUBECTL_OPTIONS.merge(hash)
26
26
  end
27
+ class_option :debug, type: :boolean, default: false, desc: 'Debug logging'
27
28
 
28
29
  # Error raised by this runner
29
30
  Error = Class.new(StandardError)
@@ -38,8 +38,9 @@ module Kuberun
38
38
 
39
39
  execute_command(input, output)
40
40
 
41
- unless prompt.no?('Should I delete pod?')
41
+ unless prompt.no?(Kuberun::Pastel.yellow('Should I delete pod?'))
42
42
  Kuberun::Kubectl.delete(resource: 'pod', resource_name: generated_pod_name)
43
+ Kuberun::Pastel.green("Pod #{generated_pod_name} has been deleted!")
43
44
  end
44
45
 
45
46
  output.puts(Kuberun::Pastel.green('Done!'))
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ class Kubectl
6
+ ##
7
+ # Handles running kubectl exec
8
+ class Exec
9
+ extend Forwardable
10
+
11
+ def_delegators :@kubectl, :kubectl_base_command
12
+
13
+ def initialize(kubectl)
14
+ @kubectl = kubectl
15
+ end
16
+
17
+ def exec(pod:, command:)
18
+ old_state = `stty -g`
19
+
20
+ PTY.spawn("#{kubectl_base_command('exec', resource: pod)} #{command}") do |out, inp, pid|
21
+ pty_process(out, inp, pid, old_state)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :stdin_thread, :stdout_thread
28
+
29
+ def prepare_stdin_thread(inp)
30
+ @stdin_thread = Thread.new do
31
+ until inp.closed?
32
+ input = $stdin.getch
33
+ inp.write(input)
34
+ inp.flush
35
+ end
36
+ end
37
+ end
38
+
39
+ def prepare_stdout_thread(out)
40
+ @stdout_thread = Thread.new do
41
+ until out.eof?
42
+ $stdout.print(out.readchar)
43
+ $stdout.flush
44
+ end
45
+ rescue Errno::EIO, EOFError
46
+ nil
47
+ end
48
+ end
49
+
50
+ def pty_process(out, inp, pid, old_state)
51
+ prepare_stdin_thread(inp)
52
+ prepare_stdout_thread(out)
53
+
54
+ stdin_thread.run
55
+
56
+ wait_for_process(pid)
57
+
58
+ stdout_thread.join
59
+ stdin_thread.kill
60
+
61
+ sleep 0.1
62
+ ensure
63
+ cleanup_pty(old_state)
64
+ end
65
+
66
+ def wait_for_process(pid)
67
+ Process.waitpid(pid)
68
+ rescue StandardError
69
+ nil # "rescue nil" is there in case process already ended.
70
+ end
71
+
72
+ def cleanup_pty(old_stty_state)
73
+ stdout_thread&.kill
74
+ stdin_thread&.kill
75
+ $stdout.puts
76
+ $stdout.flush
77
+
78
+ system("stty #{old_stty_state}")
79
+ end
80
+ end
81
+ end
@@ -2,12 +2,16 @@
2
2
 
3
3
  require 'open3'
4
4
  require 'pty'
5
+ require 'kuberun/kubectl/exec'
5
6
 
7
+ ##
8
+ # Handles running kubectl
6
9
  class Kubectl
7
10
  CAT_MULTILINE = 'EOFCONFIG'
8
11
  KUBECTL_OPTIONS = Kuberun::CLI::BASE_KUBECTL_OPTIONS.keys.map(&:to_s)
9
12
 
10
13
  def load_options(options)
14
+ self.options = options
11
15
  self.kubectl_options = parsed_options(options)
12
16
  end
13
17
 
@@ -27,63 +31,28 @@ class Kubectl
27
31
  end
28
32
 
29
33
  def exec(pod:, command:)
30
- old_state = `stty -g`
31
-
32
- PTY.spawn("#{kubectl_base_command('exec', resource: pod)} #{command}") do |o, i, pid|
33
- t_in = Thread.new do
34
- until i.closed? do
35
- input = $stdin.getch
36
- i.write(input)
37
- i.flush
38
- end
39
- end
40
-
41
- t_out = Thread.new do
42
- begin
43
- until o.eof? do
44
- $stdout.print(o.readchar)
45
- $stdout.flush
46
- end
47
- rescue Errno::EIO, EOFError
48
- nil
49
- end
50
- end
51
-
52
- t_in.run
53
-
54
- Process::waitpid(pid) rescue nil
55
- # "rescue nil" is there in case process already ended.
56
-
57
- t_out.join
58
- t_in.kill
59
- sleep 0.1
60
- ensure
61
- t_out&.kill
62
- t_in&.kill
63
- $stdout.puts
64
- $stdout.flush
65
- system "stty #{ old_state }"
66
- end
34
+ Kubectl::Exec.new(self).exec(pod: pod, command: command)
67
35
  end
68
36
 
69
37
  def delete(resource:, resource_name:)
70
38
  cmd.run(kubectl_base_command('delete', resource: resource, resource_name: resource_name))
71
39
  end
72
40
 
41
+ def kubectl_base_command(verb, resource:, resource_name: nil, options: nil)
42
+ base = "#{kubectl} #{options} #{verb} #{resource}"
43
+ base = "#{base}/#{resource_name}" if resource_name
44
+ base
45
+ end
46
+
73
47
  private
74
48
 
75
- attr_accessor :kubectl_options
49
+ attr_accessor :kubectl_options, :options
76
50
 
77
51
  def cmd(tty_options = {})
52
+ tty_options[:printer] = :progress unless options['debug']
78
53
  TTY::Command.new(tty_options)
79
54
  end
80
55
 
81
- def kubectl_base_command(verb, resource:, resource_name: nil, options: nil)
82
- base = "#{kubectl} #{options} #{verb} #{resource}"
83
- base = "#{base}/#{resource_name}" if resource_name
84
- base
85
- end
86
-
87
56
  def kubectl_base_input_command(verb, configuration:, options:)
88
57
  "cat << '#{CAT_MULTILINE}' | #{kubectl} #{options} #{verb} -f - 2>&1\n#{configuration.to_json}\n#{CAT_MULTILINE}"
89
58
  end
@@ -101,6 +70,6 @@ class Kubectl
101
70
  end
102
71
 
103
72
  def parsed_json
104
- JSON.load(yield)
73
+ JSON.parse(yield)
105
74
  end
106
75
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kuberun
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kuberun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kruczjak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-10 00:00:00.000000000 Z
11
+ date: 2018-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel
@@ -363,6 +363,7 @@ files:
363
363
  - lib/kuberun/commands/.gitkeep
364
364
  - lib/kuberun/commands/run_pod.rb
365
365
  - lib/kuberun/kubectl.rb
366
+ - lib/kuberun/kubectl/exec.rb
366
367
  - lib/kuberun/templates/.gitkeep
367
368
  - lib/kuberun/templates/run_pod/.gitkeep
368
369
  - lib/kuberun/version.rb