ranch-hand 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6810dd022dd993929014b497c21309de90d66a78dfc606216a5e36a621256c8b
4
- data.tar.gz: 704ae19d0977277481eb7bd563d82d271dbab936faa0c273e053433eb6d77dc2
3
+ metadata.gz: 28e355a00dd066ac8d0b0270f59a2a97c25bd28e69066a289aec029db9661fa9
4
+ data.tar.gz: 56a686da7e0be9a823b75fd7d349d75b7d7cd8c497106d153fe3018f6921ce87
5
5
  SHA512:
6
- metadata.gz: 31a7c159877709b9c5486dd7dae9a7cf3e9c6df9be3e3b6a58fe17ac5d1c56f42e24aad18218c216b7baa6ae6e3d82e04eeda9db185ea0725219b0761471d15d
7
- data.tar.gz: 7fe741b1e01dc48e7c5d62fcf2f2c17e639899ac8daf4ed47b348bcd7c2393a138b778198443790f41b9222ec4bb8569ca3924d7cdc308b4eea4bd568863d159
6
+ metadata.gz: d7639b1029f2cfb0a415794dd22ca8af87f38a20be8efdb757373a0218ae2fbdbf50f13365985afdd55a0e44fdcce72e6cf40c260d33823f1dd8dafe0ece392b
7
+ data.tar.gz: be9d54eed465ab6b3bb9b09f845ddb1d514f701076cf9d1f4b601b07fadfad36e2672c74d3b816380ab311ec4d619825ef4b966ebd657d7f4293f92900bd24f8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ranch-hand (0.2.0)
4
+ ranch-hand (0.3.0)
5
5
  gli (~> 2.18)
6
6
  pry (~> 0.12.2)
7
7
  tty-command (~> 0.8.2)
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # RanchHand
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ranch_hand`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Provides an interface between the Rancher CLI and the Kubectl commands to make running commands in pods easier.
4
+ This is particularily useful when using Kubernetes in a development environment.
6
5
 
7
6
  ## Installation
8
7
 
@@ -22,7 +21,7 @@ Or install it yourself as:
22
21
 
23
22
  ## Usage
24
23
 
25
- TODO: Write usage instructions here
24
+ RanchHand makes use of the [GLI](https://github.com/davetron5000/gli) (Git Like Interface) gem to create a CLI with features similar to the Git CLI. Run `ranch-hand help` or `ranch-hand command help` for usage information.
26
25
 
27
26
  ## Development
28
27
 
@@ -40,4 +39,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
39
 
41
40
  ## Code of Conduct
42
41
 
43
- Everyone interacting in the Ranch::Hand project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/peregrinator/ranch-hand/blob/master/CODE_OF_CONDUCT.md).
42
+ Everyone interacting in the RanchHand project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/peregrinator/ranch-hand/blob/master/CODE_OF_CONDUCT.md).
data/bin/ranch-hand CHANGED
@@ -2,6 +2,7 @@
2
2
  module RanchHandCLI
3
3
  require 'gli'
4
4
  require 'ranch_hand'
5
+ require 'pry'
5
6
 
6
7
  include GLI::App
7
8
  # our cli is wrapped in a module (to not pollute the global namespace with GLI methods),
@@ -23,11 +24,18 @@ module RanchHandCLI
23
24
  end
24
25
 
25
26
  desc 'Execs into a running container'
26
- arg_name 'namespace'
27
27
  command :exec do |c|
28
- c.switch [:rm, :remove], negatable: false
29
- c.action do |global_options, options, args|
30
- RanchHand::KubeCtl.new.exec(args[0], options)
28
+ c.switch [:g, :group], negatable: false, desc: "Group pods returned so that there is only one from each deployment."
29
+ c.switch [:rm, :remove], negatable: false, desc: "Used to indicated that you want to remove a previously saved command."
30
+ c.switch [:r, :repeat], negatable: false, desc: "Repeat the last command ran (against the same pod)."
31
+
32
+ c.flag [:c, :command], desc: "Command to run once (not permanently stored)"
33
+ c.flag [:f, :filter], desc: "Filter pods returned to those that contain the string provided (negative matches supported). Examples: '-f nginx', '-f -apache'"
34
+ c.flag [:n, :namespace], required: true, desc: "Namespace against which to retreive pods and run command"
35
+ c.flag [:p, :pod], desc: "Run command in a specific pod. If used with -g only the pod name can be specified and the command will be run against the first in the group"
36
+
37
+ c.action do |global_options, command_options, args|
38
+ RanchHand::KubeCtl.new.exec(command_options)
31
39
  end
32
40
  end
33
41
 
@@ -2,19 +2,30 @@ module RanchHand
2
2
  class KubeCtl
3
3
  include RanchHand::Commands
4
4
 
5
- def exec(namespace, options={})
6
- if options[:rm]
5
+ def exec(options={})
6
+ namespace = options.delete(:namespace)
7
+
8
+ if options[:remove]
7
9
  remove_command(namespace)
10
+ elsif options[:repeat]
11
+ repeat_command(namespace)
12
+ elsif options[:command]
13
+ pod = select_pod(namespace, options)
14
+ run_command(namespace, pod, options[:command])
8
15
  else
9
- run_command(namespace)
16
+ choose_command(namespace, options)
10
17
  end
11
18
  end
12
19
 
13
- def run_command(namespace)
14
- pod = select_pod(namespace)
20
+ def run_command(namespace, pod, cmd)
21
+ system("rancher kubectl -n #{namespace} exec -it #{pod} -- #{cmd}")
22
+ end
23
+
24
+ def choose_command(namespace, options={})
25
+ pod = select_pod(namespace, options)
15
26
  type, cmd = select_command(namespace, pod)
16
27
 
17
- system("rancher kubectl -n #{namespace} exec -it #{pod} -- #{cmd}")
28
+ run_command(namespace, pod, cmd)
18
29
  end
19
30
 
20
31
  def remove_command(namespace)
@@ -33,13 +44,46 @@ module RanchHand
33
44
  storage.set(storage_key, storage.get(storage_key) - Array(cmd))
34
45
  end
35
46
 
36
- def select_pod(namespace)
37
- pods = pods(namespace)
38
- pod = prompt.enum_select("Which pod?", pods, per_page: 10,
39
- default: pods.index(
40
- storage.get("exec:#{namespace}:latest:pod")
41
- ).to_i + 1
47
+ def repeat_command(namespace)
48
+ run_command(
49
+ namespace,
50
+ storage.get("exec:#{namespace}:latest:pod"),
51
+ storage.get("exec:#{namespace}:latest:cmd")
42
52
  )
53
+ end
54
+
55
+ def select_pod(namespace, options={})
56
+ pods = pods(namespace)
57
+
58
+ if options[:filter]
59
+ if options[:filter].start_with?('-')
60
+ pods = pods.reject{|p| p.match?(/#{options[:filter][1..-1]}/)}
61
+ else
62
+ pods = pods.select{|p| p.match?(/#{options[:filter]}/)}
63
+ end
64
+ end
65
+ prompt.error("No pods matching filter: '#{options[:filter]}'") and exit if pods.empty?
66
+
67
+ if options[:group]
68
+ pods = pods.group_by{|p| pod_name(p)}.map{|name, pods| pods.first}
69
+ end
70
+
71
+ if options[:pod]
72
+ if options[:group]
73
+ pod = pods.select{|p| p.match?(/#{options[:pod]}/)}.first
74
+ else
75
+ pod = pods.select{|p| options[:pod] == p}.first
76
+ end
77
+
78
+ prompt.error("No pods match: '#{options[:pod]}'. Did you mean to use the group (-g) switch?") and exit unless pod
79
+ else
80
+ pod = prompt.enum_select("Which pod?", pods, per_page: 10,
81
+ default: pods.index(
82
+ storage.get("exec:#{namespace}:latest:pod")
83
+ ).to_i + 1
84
+ )
85
+ end
86
+
43
87
  storage.set("exec:#{namespace}:latest:pod", pod)
44
88
  pod
45
89
  end
@@ -63,6 +107,8 @@ module RanchHand
63
107
  unless options[:remove]
64
108
  if cmd == "Add command"
65
109
  type, cmd = add_command(namespace, pod)
110
+ elsif cmd == "Run once"
111
+ type, cmd = nil, run_once(namespace, pod)
66
112
  end
67
113
 
68
114
  # save cmd as latest
@@ -90,6 +136,10 @@ module RanchHand
90
136
  [type, cmd]
91
137
  end
92
138
 
139
+ def run_once(namespace, pod)
140
+ prompt.ask('Command:')
141
+ end
142
+
93
143
  private
94
144
 
95
145
  def all_commands(namespace, pod, options)
@@ -101,7 +151,7 @@ module RanchHand
101
151
  if options[:remove]
102
152
  {base: []}
103
153
  else
104
- {base: ["Add command"]}
154
+ {base: ["Add command", "Run once"]}
105
155
  end
106
156
  end
107
157
 
@@ -1,3 +1,3 @@
1
1
  module RanchHand
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ranch-hand
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peregrinator
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-28 00:00:00.000000000 Z
11
+ date: 2019-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler