ranch-hand 0.6.0 → 0.8.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: 6008b690f312b7f5966b8f846774db3cf7732ae65ed7bb5f5aa739c99e6e75dc
4
- data.tar.gz: 7c5f3229830e658d2ebfa002562df901791802920459d3f3e88764dd825b4820
3
+ metadata.gz: e00a609606b6a4c9fab740093f660f8de3b4a655cbe8d3ab23a381d6cb26be97
4
+ data.tar.gz: fe56f11021d0a8ed6e68231bf7096cbc06b053ee02341a7ce53d369f63d92db1
5
5
  SHA512:
6
- metadata.gz: ad85870f429f70ca11025e5e00e1fd442b935f1683b651c73235dede9b433e16acb3e50c33f389468949717afa2781854c15c926e9e16477db6659fceb7bcc8f
7
- data.tar.gz: b37cd3ad67bf7f34dfb52942b8cc81c643c8654daeaee66263757e2a80a2ce49a6eda5fa6c45ac8841e5d646a8973f2bd792b78dcf0effd1e7f989b52b2001a2
6
+ metadata.gz: 876144c9dc6339c290eb167e6d5f9a3eb29f73b1188e210b9e362eea27b60abcb6c5f6fb881d65b98751b865b2efde82103b4b510ce0f180fec1be1a326d7cb8
7
+ data.tar.gz: c7ef6622566ab70f3f8765b6d4efb86c8c0380ecd80dcfd049ae1c3b5d4c5ac4a20bec5a5855bf27280fb0ad281b2b87a298e6eda3687e632c0d9206c8b2ab92
data/Changelog.md ADDED
@@ -0,0 +1,21 @@
1
+ ### 0.8.0
2
+ - Add ability to specify a command prefix that will be added to all commands run via `ranch-hand exec` by default (peregrinator/ranch-hand/issues/8)
3
+ - Add ability to edit the project config file via new command `ranch-hand update_config` (peregrinator/ranch-hand/issues/9)
4
+ - Add ohmyzsh aliases for new commands
5
+ ### 0.7.1
6
+ Add Oh My Zsh plugin to provide shortcuts, see files/ranch-hand.plugin.zsh
7
+ Add new `ranch-hand ohmyzsh` command to install plugins
8
+
9
+ ### 0.7.0
10
+ #### Features
11
+ Add support for command arguments. Commands that use arguments no longer need to be wrapped in a string in order to be passed properly. This provides a better interface for editor plugins that automatically append arguments to the end of a custom command.
12
+ - https://github.com/peregrinator/ranch-hand/commit/a70d44042507d407c0088a2a8ffa4d4514a800af
13
+ - ex: before: `ranch-hand exec -c 'rspec spec/foo.rb:21'`, now: `ranch-hand exec -c rspec spec/foo.rb:21`
14
+
15
+ #### Bugfixes:
16
+ - d76855: correctly handle scenario in which the namespace queried has no pods
17
+
18
+ ### 0.6.0
19
+
20
+ Updates for Ruby 3.0 compat
21
+ - https://rubyreferences.github.io/rubychanges/3.0.html#keyword-arguments-are-now-fully-separated-from-positional-arguments
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  Provides a simple interface on top the Rancher CLI and the Kubectl commands to make running commands in pods easier.
6
6
  This is particularily useful when using Kubernetes in a development environment.
7
7
 
8
- ![ranch-hand demo](https://github.com/peregrinator/ranch-hand/raw/master/doc/ranch-hand-demo.gif "Ranch-hand Demo")
8
+ ![ranch-hand demo](https://github.com/peregrinator/ranch-hand/raw/main/doc/ranch-hand-demo.gif "Ranch-hand Demo")
9
9
 
10
10
  ## Installation
11
11
 
data/bin/ranch-hand CHANGED
@@ -14,7 +14,7 @@ module RanchHandCLI
14
14
  version RanchHand::VERSION
15
15
 
16
16
  subcommand_option_handling :normal
17
- arguments :strict
17
+ arguments :loose
18
18
 
19
19
  desc 'Sets up ranch hand'
20
20
  command :setup do |c|
@@ -23,13 +23,27 @@ module RanchHandCLI
23
23
  end
24
24
  end
25
25
 
26
- desc 'Initializes ranch hand for project'
26
+ desc 'Installs ranch hand Oh My Zsh shortcuts'
27
+ command :ohmyzsh do |c|
28
+ c.action do |global_options, options, args|
29
+ RanchHand::Base.install_ohmyzsh
30
+ end
31
+ end
32
+
33
+ desc 'Initializes ranch hand for current project'
27
34
  command :init do |c|
28
35
  c.action do |global_options, command_options, args|
29
36
  RanchHand::Base.init
30
37
  end
31
38
  end
32
39
 
40
+ desc 'Updates ranch hand config for current project'
41
+ command :update_config do |c|
42
+ c.action do |global_options, command_options, args|
43
+ RanchHand::Base.update_config
44
+ end
45
+ end
46
+
33
47
  desc 'Execs into a running container'
34
48
  command :exec do |c|
35
49
  config = RanchHand::Config.load
@@ -37,14 +51,20 @@ module RanchHandCLI
37
51
  c.switch [:g, :group], negatable: true, default_value: config[:group] || false, desc: "Group pods returned so that there is only one from each deployment, --no-group can be used to override project config as needed."
38
52
  c.switch [:rm, :remove], negatable: false, desc: "Used to indicated that you want to remove a previously saved command."
39
53
  c.switch [:r, :repeat], negatable: false, desc: "Repeat the last command ran (against the same pod)."
40
-
54
+ c.switch [:sp, :skip_prefix], negatable: false, default_value: false, desc: "Do not add configured command prefix."
55
+
41
56
  c.flag [:c, :command], desc: "Command to run once (not permanently stored)"
42
57
  c.flag [:f, :filter], desc: "Filter pods returned to those that contain the string provided (negative matches supported). Examples: '-f nginx', '-f -apache'"
43
58
  c.flag [:n, :namespace], required: true, default_value: config[:namespace], desc: "Namespace against which to retreive pods and run command"
44
59
  c.flag [:p, :pod], default_value: config[: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"
45
60
 
61
+ default_command_options = {
62
+ command_prefix: config[:command_prefix],
63
+ prefer_ui: config[:prefer_ui]
64
+ }
65
+
46
66
  c.action do |global_options, command_options, args|
47
- RanchHand::KubeCtl.new.exec(**command_options)
67
+ RanchHand::KubeCtl.new.exec(args: args, cmd_options: default_command_options.merge(command_options))
48
68
  end
49
69
  end
50
70
 
@@ -0,0 +1,6 @@
1
+ alias rh='ranch-hand' # main binary alias
2
+ alias rhs='ranch-hand setup' # setup ranch hand with Rancher CLI credentials
3
+ alias rhi='ranch-hand init' # initialize default ranch hand settings in a project
4
+ alias rhu='ranch-hand update_config' # update default ranch hand settings for a project
5
+ alias rhe='ranch-hand exec' # run a command in a container
6
+ alias rhes='ranch-hand exec --skip-prefix' # run a command in a container but skip default command_prefix
@@ -12,5 +12,33 @@ module RanchHand
12
12
  RanchHand::Config.create
13
13
  RanchHand::Logger.info("initialization complete")
14
14
  end
15
+
16
+ def self.install_ohmyzsh
17
+ RanchHand::Logger.info("installing ranch-hand Oh My Zsh shortcuts")
18
+
19
+ unless ENV['ZSH_CUSTOM'] || ENV['ZSH']
20
+ RanchHand::Logger.warn("Could not find $ZSH_CUSTOM or $ZSH in your environment, can not install shortcuts")
21
+ return
22
+ end
23
+
24
+ zsh_custom_dir = ENV['ZSH_CUSTOM'] || File.join(ENV['ZSH'], 'custom')
25
+
26
+ zsh_plugin_dir = File.join(zsh_custom_dir, 'plugins', 'ranch-hand')
27
+ FileUtils.mkdir_p(zsh_plugin_dir)
28
+
29
+ current_path = File.expand_path(File.dirname(__FILE__))
30
+ gem_plugin_path = File.join(current_path, '..', '..', 'files', 'ranch-hand.plugin.zsh')
31
+
32
+ FileUtils.cp(gem_plugin_path, zsh_plugin_dir)
33
+
34
+ RanchHand::Logger.info("installation complete")
35
+ RanchHand::Logger.info("Don't forget to add 'ranch-hand' to your plugins in ~/.zshrc -- e.g. plugins=(ranch-hand) -- and then 'source ~/.zshrc'")
36
+ end
37
+
38
+ def self.update_config
39
+ RanchHand::Logger.info("updating ranch-hand config for project")
40
+ RanchHand::Config.update
41
+ RanchHand::Logger.info("update complete")
42
+ end
15
43
  end
16
- end
44
+ end
@@ -10,4 +10,4 @@ module RanchHand
10
10
  TTY::Prompt.new
11
11
  end
12
12
  end
13
- end
13
+ end
@@ -15,24 +15,51 @@ module RanchHand
15
15
  end
16
16
  end
17
17
 
18
+ def self.update
19
+ save(generate_config(load))
20
+ end
21
+
18
22
  private
19
23
 
20
- def self.generate_config
24
+ def self.generate_config(current={})
25
+ unless current.empty?
26
+ puts "Current config:"
27
+ pp current
28
+ puts "A blank entry to the following prompts will keep the current config value."
29
+ end
30
+
21
31
  namespace = prompt.ask('Namespace:')
22
32
  group = prompt.ask('Use group command by default? (Y/n):')
23
- group = %w(n N).include?(group) ? false : true
24
33
  pod = prompt.ask('Pod name:')
34
+ prefix = prompt.ask('Command prefix (optional):')
25
35
 
26
- {
36
+ # set default value if there is no current or entered value
37
+ if group.nil? && current[:group].nil?
38
+ group = %w(n N).include?(group) ? false : true
39
+ end
40
+
41
+ # if command prefix is provided set prefer_ui to false
42
+ # prefer_ui skips the #exec command line ui if set to true
43
+ # this supports a base case where the prefix command is the
44
+ # only command the user wants to run (eg. simple connection to the pod)
45
+ if current[:prefer_ui]
46
+ prefer_ui = nil
47
+ elsif prefix || current[:command_prefix]
48
+ prefer_ui = false
49
+ else
50
+ prefer_ui = true
51
+ end
52
+
53
+ current.merge({
54
+ command_prefix: prefix,
27
55
  group: group,
28
56
  namespace: namespace,
29
- pod: pod
30
- }
57
+ pod: pod,
58
+ prefer_ui: prefer_ui
59
+ }.compact)
31
60
  end
32
61
 
33
62
  def self.save(config)
34
- # File.new(project_config_path, 'w+', 0640)
35
-
36
63
  File.open(project_config_path, 'w', 0640) do |f|
37
64
  f.write(config.to_yaml)
38
65
  end
@@ -1,4 +1,4 @@
1
1
  module RanchHand
2
2
  RANCH_HAND_HOME = File.join(ENV['HOME'], '.ranch-hand')
3
3
  STORE_FILE = File.join(RANCH_HAND_HOME, 'store.yml')
4
- end
4
+ end
@@ -2,32 +2,39 @@ module RanchHand
2
2
  class KubeCtl
3
3
  include RanchHand::Commands
4
4
 
5
- def exec(**options)
6
- options ||= {}
7
- # options = project_config.merge(options)
5
+ def exec(args: [], cmd_options: {})
6
+ args = Array(args)
8
7
 
9
- namespace = options.delete(:namespace)
10
-
11
- if options[:remove]
8
+ namespace = cmd_options.delete(:namespace)
9
+
10
+ if cmd_options[:remove]
12
11
  remove_command(namespace)
13
- elsif options[:repeat]
12
+ elsif cmd_options[:repeat]
14
13
  repeat_command(namespace)
15
- elsif options[:command]
16
- pod = select_pod(namespace, options)
17
- run_command(namespace, pod, options[:command])
14
+ elsif cmd_options[:command] || (!cmd_options[:prefer_ui] && cmd_options[:command_prefix])
15
+ pod = select_pod(namespace, cmd_options)
16
+ run_command(namespace, pod, generate_command(cmd_options), args)
18
17
  else
19
- choose_command(namespace, options)
18
+ choose_command(namespace, cmd_options)
20
19
  end
21
20
  end
22
21
 
23
- def run_command(namespace, pod, cmd)
24
- system("rancher kubectl -n #{namespace} exec -it #{pod} -- #{cmd}")
22
+ def generate_command(cmd_options)
23
+ if cmd_options[:skip_prefix]
24
+ command = cmd_options[:command]
25
+ else
26
+ command = "#{cmd_options[:command_prefix]} #{cmd_options[:command]}".strip
27
+ end
28
+ end
29
+
30
+ def run_command(namespace, pod, cmd, args=[])
31
+ system("rancher kubectl -n #{namespace} exec -it #{pod} -- #{cmd} #{args.join(' ')}".strip)
25
32
  end
26
33
 
27
34
  def choose_command(namespace, options={})
28
35
  pod = select_pod(namespace, options)
29
36
  type, cmd = select_command(namespace, pod)
30
-
37
+
31
38
  run_command(namespace, pod, cmd)
32
39
  end
33
40
 
@@ -60,9 +67,9 @@ module RanchHand
60
67
 
61
68
  if options[:filter]
62
69
  if options[:filter].start_with?('-')
63
- pods = pods.reject{|p| p.match?(/#{options[:filter][1..-1]}/)}
70
+ pods = pods.reject{|p| p.match?(/#{options[:filter][1..-1]}/)}
64
71
  else
65
- pods = pods.select{|p| p.match?(/#{options[:filter]}/)}
72
+ pods = pods.select{|p| p.match?(/#{options[:filter]}/)}
66
73
  end
67
74
  end
68
75
  prompt.error("No pods matching filter: '#{options[:filter]}'") and exit if pods.empty?
@@ -176,16 +183,13 @@ module RanchHand
176
183
 
177
184
  def pods(namespace)
178
185
  pods_cmd = "rancher kubectl -n #{namespace} get po"
179
- command(printer: :null).run(pods_cmd).out.split("\n")[1..-1].map{|l| l.split(/\s+/)[0]}
186
+ pods = command(printer: :null).run(pods_cmd).out.split("\n")[1..-1]&.map{|l| l.split(/\s+/)[0]}
187
+
188
+ pods || []
180
189
  end
181
190
 
182
191
  def storage
183
192
  @storage ||= RanchHand::Storage.new
184
193
  end
185
-
186
- # def project_config
187
- # project_config_file = File.join(Dir.pwd, ".ranch-hand")
188
- # YAML.load_file(project_config_file) || {}
189
- # end
190
194
  end
191
- end
195
+ end
@@ -8,4 +8,4 @@ module RanchHand
8
8
  @@logger ||= ::Logger.new(STDOUT)
9
9
  end
10
10
  end
11
- end
11
+ end
@@ -21,4 +21,4 @@ module RanchHand
21
21
  end
22
22
  end
23
23
  end
24
- end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module RanchHand
2
- VERSION = "0.6.0"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/ranch_hand.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # stdlib
2
2
  require 'fileutils'
3
3
  require 'logger'
4
+ require 'pp'
4
5
  require 'yaml'
5
6
 
6
7
  # gems
@@ -15,4 +16,4 @@ require 'ranch_hand/constants.rb'
15
16
  require 'ranch_hand/kube_ctl.rb'
16
17
  require 'ranch_hand/logger.rb'
17
18
  require 'ranch_hand/storage.rb'
18
- require 'ranch_hand/version.rb'
19
+ require 'ranch_hand/version.rb'
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.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peregrinator
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-31 00:00:00.000000000 Z
11
+ date: 2022-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -120,6 +120,7 @@ files:
120
120
  - ".rspec"
121
121
  - ".travis.yml"
122
122
  - CODE_OF_CONDUCT.md
123
+ - Changelog.md
123
124
  - Gemfile
124
125
  - LICENSE.txt
125
126
  - README.md
@@ -128,6 +129,7 @@ files:
128
129
  - bin/ranch-hand
129
130
  - bin/setup
130
131
  - doc/ranch-hand-demo.gif
132
+ - files/ranch-hand.plugin.zsh
131
133
  - lib/ranch_hand.rb
132
134
  - lib/ranch_hand/base.rb
133
135
  - lib/ranch_hand/commands.rb
@@ -157,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
159
  - !ruby/object:Gem::Version
158
160
  version: '0'
159
161
  requirements: []
160
- rubygems_version: 3.2.3
162
+ rubygems_version: 3.2.22
161
163
  signing_key:
162
164
  specification_version: 4
163
165
  summary: Provides an interface between the Rancher CLI and the Kubectl commands