kuberun 0.2.0 → 0.3.0

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: ac46e803d30eaa6d62fb73cf5ba6e861c469aea28693f41a4fbd6c25f57e87e9
4
- data.tar.gz: ef5142c4449bc1cc1d868ef45f38f530a529ae55e8c88612e6775f1da0f35a4b
3
+ metadata.gz: a1c64caef5ffc4e1578751e44ca133f0f608d8f3a24ca93d0a864434e1cd0add
4
+ data.tar.gz: f404f4e23c941d0aa992f1d10ba4e6a3dd38fa1285210bb71f4acdff0bd4e484
5
5
  SHA512:
6
- metadata.gz: b7b210af108dec39b08b7a5cd89098e32c427d997639459ac0439215154aa1d72816b3c5b9de323e4e2ac41bd60b46a9635672774be4896c24655b975c48c7a7
7
- data.tar.gz: 1b3905add1b114061b20bd9d6125290f4ef55a668be4183b0bc53e0192646ecbfdec1a8fd25565e0cb7a4fe32c017a7eb691467b97eeaa17f88a6532fbb7a16c
6
+ metadata.gz: 9abde70e3c0a339c4d5c3ad94c9b242c64f88a3b73c220736da21d025c6fd0a5ee287a8248dc5bcd4a68a33f47e55ba22fb85d815371c8ffdc267ea947243286
7
+ data.tar.gz: 434e29139e6da16f13202735c7fd647e2828dfb82bceebf12228f780588ea36479044fd37d43b1daa5418de3c82b13c3f153fefa18907abbc3486c2f29c14015
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kuberun (0.1.5)
4
+ kuberun (0.3.0)
5
5
  pastel (~> 0.8.0)
6
6
  thor (~> 1.2.1)
7
7
  tty-command (~> 0.10.1)
@@ -94,4 +94,4 @@ DEPENDENCIES
94
94
  rubocop (~> 0.58.0)
95
95
 
96
96
  BUNDLED WITH
97
- 2.3.20
97
+ 2.3.19
data/exe/kuberun CHANGED
@@ -3,8 +3,9 @@
3
3
 
4
4
  lib_path = File.expand_path('../lib', __dir__)
5
5
  $LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
6
- require 'kuberun/cli'
7
6
  require 'kuberun'
7
+ require 'kuberun/cli'
8
+
8
9
 
9
10
  Signal.trap('INT') do
10
11
  warn("\n#{caller.join("\n")}: interrupted")
data/lib/kuberun/cli.rb CHANGED
@@ -9,19 +9,7 @@ module Kuberun
9
9
  # @api public
10
10
  class CLI < Thor
11
11
  DEFAULT_OPTIONS_FOR_KUBECTL_OPTIONS = { type: :string, default: '', desc: 'See kubectl options' }.freeze
12
- BASE_KUBECTL_OPTIONS = {
13
- 'certificate-authority': {},
14
- 'client-certificate': {},
15
- 'client-key': {},
16
- 'cluster': {},
17
- 'context': {},
18
- 'insecure-skip-tls-verify': {},
19
- 'kubeconfig': {},
20
- 'namespace': { aliases: :'-n' },
21
- 'token': {},
22
- 'v': { type: :numeric, default: 0, desc: 'Log level passed to kubectl' }
23
- }.freeze
24
- BASE_KUBECTL_OPTIONS.each do |option_name, hash|
12
+ ::Kubectl::OPTIONS.each do |option_name, hash|
25
13
  class_option option_name, DEFAULT_OPTIONS_FOR_KUBECTL_OPTIONS.merge(hash)
26
14
  end
27
15
  class_option :debug, type: :boolean, default: false, desc: 'Debug logging'
@@ -14,23 +14,29 @@ module Kuberun
14
14
  end
15
15
 
16
16
  def execute(input: $stdin, output: $stdout)
17
- output.puts(Kuberun::Pastel.yellow('Checking access to needed commands...'))
18
- Kuberun::Kubectl.auth_check('create', resource: 'pods')
19
- Kuberun::Kubectl.auth_check('exec', resource: 'pods')
20
- output.puts(Kuberun::Pastel.green('You have all permissions needed.'))
17
+ if @options['perform-auth-check']
18
+ output.puts(Kuberun::Pastel.yellow('Checking access to needed commands...'))
19
+ Kuberun::Kubectl.auth_check('create', resource: 'pods')
20
+ Kuberun::Kubectl.auth_check('exec', resource: 'pods')
21
+ output.puts(Kuberun::Pastel.green('You have all permissions needed.'))
22
+ end
21
23
 
22
24
  output.puts(Kuberun::Pastel.yellow('Searching for existing pods'))
23
25
  existing_pods = Kuberun::Kubectl.get(resource: 'pods', options: "-l kuberun-provisioned=true,kuberun-source=#{@deployment_name}")
24
26
  if existing_pods['items'].size > 0
25
- select = existing_pods['items'].map { |item| item.dig('metadata', 'name') }
26
- select << NEW_POD
27
+ if @options['use-first-pod']
28
+ @generated_pod_name = existing_pods['items'].first.dig('metadata', 'name')
29
+ else
30
+ select = existing_pods['items'].map { |item| item.dig('metadata', 'name') }
31
+ select << NEW_POD
27
32
 
28
- selection = prompt.select('I found some already running pods. Do you want to use one?', select)
33
+ selection = prompt.select('I found some already running pods. Do you want to use one?', select)
29
34
 
30
- if selection == NEW_POD
31
- create_pod_from_deployment(output)
32
- else
33
- @generated_pod_name = selection
35
+ if selection == NEW_POD
36
+ create_pod_from_deployment(output)
37
+ else
38
+ @generated_pod_name = selection
39
+ end
34
40
  end
35
41
  else
36
42
  create_pod_from_deployment(output)
@@ -38,7 +44,7 @@ module Kuberun
38
44
 
39
45
  execute_command(input, output)
40
46
 
41
- unless prompt.yes?(Kuberun::Pastel.yellow('Should I delete pod?'))
47
+ if @options['cleanup-pod'] || prompt.yes?(Kuberun::Pastel.yellow('Should I delete pod?'))
42
48
  Kuberun::Kubectl.delete(resource: 'pod', resource_name: generated_pod_name)
43
49
  Kuberun::Pastel.green("Pod #{generated_pod_name} has been deleted!")
44
50
  end
@@ -122,7 +128,7 @@ module Kuberun
122
128
  def execute_command(_input, output)
123
129
  output.puts(Kuberun::Pastel.green('Executing command'))
124
130
 
125
- Kuberun::Kubectl.exec(pod: generated_pod_name, command: '-it -- /bin/bash')
131
+ Kuberun::Kubectl.exec(generated_pod_name, @options)
126
132
 
127
133
  output.puts(Kuberun::Pastel.green('Kubectl exec exited'))
128
134
  end
@@ -14,11 +14,16 @@ class Kubectl
14
14
  @kubectl = kubectl
15
15
  end
16
16
 
17
- def exec(pod:, command:)
18
- old_state = `stty -g`
17
+ def exec(pod, options)
18
+ command = "#{kubectl_base_command('exec', resource: pod)} -it -- #{options['cluster-command']}"
19
19
 
20
- PTY.spawn("#{kubectl_base_command('exec', resource: pod)} #{command}") do |out, inp, pid|
21
- pty_process(out, inp, pid, old_state)
20
+ if options['pty']
21
+ old_state = `stty -g`
22
+ PTY.spawn(command) do |out, inp, pid|
23
+ pty_process(out, inp, pid, old_state)
24
+ end
25
+ else
26
+ system(command)
22
27
  end
23
28
  end
24
29
 
@@ -8,7 +8,27 @@ require 'kuberun/kubectl/exec'
8
8
  # Handles running kubectl
9
9
  class Kubectl
10
10
  CAT_MULTILINE = 'EOFCONFIG'
11
- KUBECTL_OPTIONS = Kuberun::CLI::BASE_KUBECTL_OPTIONS.keys.map(&:to_s)
11
+
12
+ KUBECTL_OPTIONS = {
13
+ 'certificate-authority': {},
14
+ 'client-certificate': {},
15
+ 'client-key': {},
16
+ 'cluster': {},
17
+ 'context': {},
18
+ 'insecure-skip-tls-verify': {},
19
+ 'kubeconfig': {},
20
+ 'namespace': { aliases: :'-n' },
21
+ 'token': {},
22
+ 'v': { type: :numeric, default: 0, desc: 'Log level passed to kubectl' },
23
+ }.freeze
24
+ SCRIPT_OPTIONS = {
25
+ 'cluster-command': { type: 'string', default: '/bin/bash', desc: 'Command to run on cluster' },
26
+ 'use-first-pod': { type: 'boolean', default: false, desc: 'Should first existing pod be used automatically?' },
27
+ 'cleanup-pod': { type: 'boolean', default: false, desc: 'Should pod be removed after finishing?' },
28
+ 'perform-auth-check': { type: 'boolean', default: true, desc: 'Should auth check be performed?' },
29
+ 'pty': { type: 'boolean', default: true, desc: 'Should PTY be used?' },
30
+ }.freeze
31
+ OPTIONS = KUBECTL_OPTIONS.merge(SCRIPT_OPTIONS).freeze
12
32
 
13
33
  def load_options(options)
14
34
  self.options = options
@@ -30,8 +50,8 @@ class Kubectl
30
50
  cmd.run(kubectl_base_input_command('create', configuration: configuration, options: options))
31
51
  end
32
52
 
33
- def exec(pod:, command:)
34
- Kubectl::Exec.new(self).exec(pod: pod, command: command)
53
+ def exec(pod, options = {})
54
+ Kubectl::Exec.new(self).exec(pod, options)
35
55
  end
36
56
 
37
57
  def delete(resource:, resource_name:)
@@ -63,7 +83,7 @@ class Kubectl
63
83
 
64
84
  def parsed_options(options)
65
85
  options.each_with_object([]) do |(option_name, option_value), arr|
66
- next if !KUBECTL_OPTIONS.include?(option_name) || option_value == ''
86
+ next if !KUBECTL_OPTIONS.keys.map(&:to_s).include?(option_name) || option_value == ''
67
87
 
68
88
  arr << "--#{option_name}=#{option_value}"
69
89
  end.join(' ')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kuberun
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kruczjak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-23 00:00:00.000000000 Z
11
+ date: 2022-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel