knife-org-utils 0.1.0 → 1.0.1

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
  SHA1:
3
- metadata.gz: af93ea12eb77961bc616479637517b54ba380e8b
4
- data.tar.gz: d46ed3be6bf9dd7834a38879cc0bf02acb6fa44b
3
+ metadata.gz: 19d0abf791e3377010066677e0b004cd07a2723a
4
+ data.tar.gz: dd002443f0a027407a77d2610d8fc5cd4193b184
5
5
  SHA512:
6
- metadata.gz: cf180b95ce94d019dcc9978d18c24141a3d2e6676da0c1a8dc702816fa6e24edb8980bf9595b3c17a0e0fbd6106521f6be93255a68838d05c5c0b52a5d45f549
7
- data.tar.gz: 78d9e0139ebae2f513254101bdac08f133b5da596f52fb0c15405c1f2cbea90c7975f7355f34dc40dc1981fd18cd37e0aaa8f76d2d09e46ae9e5a233cd229a77
6
+ metadata.gz: 3070cf9eb65d75ef3136d76ae5855695dc77a9e90dc54a118621bf3823c2f80dafbadf745e72cce8aa11e562c448518487dd6b70637a65ae1ccfcb0c9dbb3358
7
+ data.tar.gz: 832f6b4d9e2d133fc58e7b3cef415eb02e12ae3ee9704cfddc2d8a766b701cc2ec555ad27812b78b1c585382aa548ff2bf5b03548a33d3a99814915ca0b782a9
data/README.md CHANGED
@@ -2,10 +2,16 @@
2
2
  [![Gem Version](https://badge.fury.io/rb/knife-org-utils.svg)](http://badge.fury.io/rb/knife-org-utils) [![Build Status](https://travis-ci.org/secret-sauce/knife-org-utils.svg?branch=master)](https://travis-ci.org/secret-sauce/knife-org-utils) [![Dependency Status](https://gemnasium.com/secret-sauce/knife-org-utils.svg)](https://gemnasium.com/secret-sauce/knife-org-utils)
3
3
 
4
4
 
5
- ### Description:
5
+ ## :no_entry: Warning :no_entry:
6
+
7
+ Run `knife switch init` first.
8
+
9
+ Pre 1.0.0 version used git to manage the `~/.chef` folder. Version 1.0.x and above will not use git to manage the `~/.chef` directory. Please backup your `~/.chef` directory before adding new configurations.
10
+
11
+ ## Description:
6
12
  This is an EXPERIMENTAL knife plugin that allows you :
7
13
 
8
- - to switch your .chef config files and keys to point to one of your orgs based on command line options
14
+ - to switch config files and keys in `~/.chef` quickly from command line.
9
15
  - to display information from the `knife.rb` config file in knife's configuration file search path.
10
16
 
11
17
  ## Installation
@@ -15,22 +21,22 @@ git repository and run the following command from inside the cloned repo:
15
21
 
16
22
  rake install
17
23
 
18
-
19
24
  ## Requirements
20
- * create a `~/.chef` directory on your workstation and make it a git repo
21
- * create branches, each branch containing the appropriate config file and pem files pointing to a specific org
25
+ :warning: backup your current `~/.chef` directory :warning:
22
26
 
27
+ ## Available Subcommands and what they do for you
23
28
 
24
- ## Subcommands
29
+ ### `knife switch init`
30
+ Initializes your `.chef` directory.
25
31
 
26
32
  ### `knife switch add $CHEF_RERO_DIR`
27
- Imports `.chef` files from `$CHEF_RERO_DIR/.chef` into a new git branch in `~/.chef` folder. The name of the branch will be based on the `chef_server_url` in the `knife.rb` file. Starter Kit is a valid chef-repo directory.
33
+ Imports `.chef` files from `$CHEF_RERO_DIR/.chef` into `~/.chef` folder. The name of the imported CONFIG will be based on the `chef_server_url` in the `knife.rb` file. Starter Kit is a valid chef-repo directory.
28
34
 
29
- ### `knife switch BRANCH`
30
- checkout to this git branch containing your chef credentials, provided it exists.
35
+ ### `knife switch CONFIG`
36
+ switches the configuration in `~/.chef` to the named CONFIG
31
37
 
32
38
  ### `knife switch list`
33
- list of available branches in `~/.chef` folder.
39
+ list of available CONFIGS in `~/.chef` folder.
34
40
 
35
41
  ### `knife info [options]`
36
42
  prints the current chef server referenced by your `~/.chef/knife.rb`.
@@ -1,26 +1,86 @@
1
1
  require 'chef/knife'
2
- require 'git'
3
2
 
4
3
  module KnifeOrgUtils
5
4
  class Switch < Chef::Knife
6
5
 
7
- banner 'knife switch BRANCH'
6
+ banner 'knife switch CONFIG'
7
+
8
+ def root
9
+ @root ||= File.expand_path( '~/.chef' )
10
+ end
11
+
12
+ def config_name
13
+ @config_name ||= @name_args[0]
14
+ end
15
+
16
+ def server
17
+ parse_config_name unless @server
18
+ @server
19
+ end
20
+
21
+ def org
22
+ parse_config_name unless @org
23
+ @org
24
+ end
25
+
26
+ def parse_config_name
27
+ %r{^(?<server>[a-z_]+)/(?<org>[a-z_]+)$} =~ config_name
28
+ @server = server
29
+ @org = org
30
+ end
31
+
32
+ def source_knife_config
33
+ @source_knife_config ||= File.join( root, config_name, 'knife.rb' )
34
+ end
35
+
36
+ def dest_knife_config
37
+ ::File.join( root, 'knife.rb' )
38
+ end
39
+
40
+ def config_methods
41
+ %w(
42
+ log_level
43
+ log_location
44
+ node_name
45
+ client_key
46
+ validation_client_name
47
+ validation_key
48
+ chef_server_url
49
+ )
50
+ end
8
51
 
9
52
  def run
10
53
  unless @name_args.length == 1
11
- ui.fatal 'You must specify an BRANCH name'
54
+ ui.fatal 'You must specify an CONFIG name.'
12
55
  show_usage
13
56
  exit 1
14
57
  end
15
58
 
16
- @git = ::Git.open( '~/.chef' )
59
+ unless ::File.exists? source_knife_config
60
+ ui.fatal "#{source_knife_config} not found for #{config_name} config."
61
+ show_usage
62
+ exit 1
63
+ end
64
+
65
+ File.open( dest_knife_config, 'w' ) do | output |
66
+
67
+ server_dir = ::File.join( root, server )
68
+ org_dir = ::File.join( server_dir, org )
69
+
70
+ output.write( "# Generated by knife-org-utils for config #{config_name}\n" )
71
+ output.write( "server_dir = '#{server_dir}'\n" )
72
+ output.write( "org_dir = '#{org_dir}'\n" )
17
73
 
18
- begin
19
- @git.checkout( "#{@name_args[0]}" )
20
- ui.msg "Switched to branch #{@name_args[0]}"
21
- rescue Git::GitExecuteError => e
22
- ui.error e.message
74
+ File.foreach( source_knife_config ) do | line |
75
+ %r{^(?<method>[a-zA-Z0-9_]+)\s+(?<param>.*)$} =~ line
76
+ if config_methods.include? method
77
+ dest_dir = ( method == 'client_key' ) ? 'server_dir' : 'org_dir'
78
+ param.gsub!( '#{current_dir}', "\#\{#{dest_dir}\}" )
79
+ output.write "#{method} #{param}\n"
80
+ end
81
+ end
23
82
  end
83
+ ui.msg "Switched to #{config_name} knife config."
24
84
  end
25
85
  end
26
86
  end
@@ -1,5 +1,6 @@
1
1
  require 'chef/knife'
2
- require 'git'
2
+ require 'fileutils'
3
+ require "digest"
3
4
 
4
5
  module KnifeOrgUtils
5
6
  class SwitchAdd < Chef::Knife
@@ -10,93 +11,114 @@ module KnifeOrgUtils
10
11
  @kit_path ||= ::File.join( ::File.expand_path( @name_args[0] ), '.chef' )
11
12
  end
12
13
 
13
- def dot_chef_path
14
- @dot_chef_path ||= File.expand_path( '~/.chef' )
14
+ def root
15
+ @root ||= File.expand_path( '~/.chef' )
15
16
  end
16
17
 
17
- def git
18
- git = ::Git.open( dot_chef_path )
18
+ def server
19
+ parse_config unless @server
20
+ @server
19
21
  end
20
22
 
21
- def run
22
- unless @name_args.length == 1
23
- ui.fatal 'You must specify the path to started kit CHEF-RERO-DIR'
24
- show_usage
25
- exit 1
26
- end
23
+ def org
24
+ parse_config unless @org
25
+ @org
26
+ end
27
27
 
28
- kit_files = get_kit_files
29
- branch_name = get_branch_name
28
+ def user_name
29
+ parse_config unless @user_name
30
+ @user_name
31
+ end
30
32
 
31
- unless branch_exists? branch_name
32
- checkout( branch_name )
33
- clean_files
34
- copy_files( kit_files )
35
- commit( branch_name )
36
- else
37
- ui.info "Branch for #{branch_name} already exists"
38
- end
33
+ def validation_client_name
34
+ parse_config unless @validation_client_name
35
+ @validation_client_name
36
+ end
37
+
38
+ def user_pem
39
+ "#{user_name}.pem"
39
40
  end
40
41
 
41
42
  def get_kit_files
42
43
  unless ::File.exists?( kit_path ) && ::File.directory?( kit_path )
43
- ui.fatal "Valid starter kit is not found at #{@name_args[0]}"
44
+ ui.fatal "Valid starter kit is not found at #{@name_args[0]}."
44
45
  exit 1
45
46
  end
46
47
  ::Dir.glob( "#{@kit_path}/**" ).select{ | f | ::File.file? f }
47
48
  end
48
49
 
49
- def get_branch_name
50
+ def parse_config
50
51
  knife_path = ::File.join( kit_path, 'knife.rb' )
51
52
  knife_data = ::File.read( knife_path )
52
53
 
53
- %r(^chef_server_url\s+"https{0,1}://(?<host>[a-zA-Z0-9-]+).*/organizations/(?<org>[a-zA-Z0-9_]+)"$) =~ knife_data
54
+ %r{
55
+ ^chef_server_url\s+"https{0,1}://(?<server>[a-zA-Z0-9-]+).*/organizations/(?<org>[a-zA-Z0-9_]+)"$
56
+ }x =~ knife_data
57
+
58
+ %r{
59
+ ^node_name\s+"(?<user_name>.*)"$
60
+ }x =~ knife_data
54
61
 
55
- if host.nil? || org.nil?
56
- ui.fatal "Invalid knife.rb at #{knife_path}"
62
+ %r{
63
+ ^validation_client_name\s+"(?<validation_client_name>.*)"$
64
+ }x =~ knife_data
65
+
66
+ if server.nil? || org.nil? || user_name.nil?
67
+ ui.fatal "Invalid knife.rb at #{knife_path}."
57
68
  exit 1
58
69
  end
59
70
 
60
- "#{host}/#{org}"
71
+ @server = server
72
+ @org = org
73
+ @user_name = user_name
74
+ @validation_client_name = validation_client_name
61
75
  end
62
76
 
63
- def branch_exists?( branch_name )
64
- git.branches.local.find do | branch |
65
- branch.name == branch_name
66
- end
77
+ def get_config_name
78
+ ::File.join( server, org )
67
79
  end
68
80
 
69
- def checkout( branch_name )
70
- begin
71
- git.branch( branch_name ).checkout
72
- rescue Git::GitExecuteError => e
73
- ui.fatal e.message
74
- exit 1
75
- end
81
+ def get_dest_path( config_name )
82
+ ::File.join( root, config_name )
76
83
  end
77
84
 
78
85
  def copy_files( files )
79
- files.each do | file |
80
- filename = ::File.basename file
81
- dest = ::File.join( dot_chef_path, filename )
82
- ::FileUtils.copy( file, dest )
86
+ server_dir = ::File.join( root, server )
87
+ org_dir = ::File.join( server_dir, org )
88
+
89
+ files.each do | source |
90
+ filename = ::File.basename source
91
+
92
+ dest_dir = ( user_pem == filename ) ? server_dir : org_dir
93
+ dest = ::File.join( dest_dir, filename )
94
+
95
+ if ::File.exist?( dest )
96
+ source_sha = Digest::SHA2.file( source ).hexdigest
97
+ dest_sha = Digest::SHA2.file( dest ).hexdigest
98
+ ui.warn "File #{dest} already exists with different content. Skipped." unless source_sha.eql?( dest_sha )
99
+ else
100
+ ::FileUtils.copy( source, dest )
101
+ end
83
102
  end
84
103
  end
85
104
 
86
- def clean_files
87
- files = ::Dir.glob( "#{dot_chef_path}/**" ).select{ | f | ::File.file? f }
88
- files.each do |file|
89
- ::File.unlink( file )
105
+ def run
106
+ unless @name_args.length == 1
107
+ ui.fatal 'You must specify the path to started kit CHEF-RERO-DIR.'
108
+ show_usage
109
+ exit 1
90
110
  end
91
- end
92
111
 
93
- def commit( branch_name )
94
- begin
95
- git.add( :all=>true )
96
- git.commit( "Adding #{branch_name} from #{kit_path}" )
97
- rescue Git::GitExecuteError => e
98
- ui.fatal e.message
99
- exit 1
112
+ kit_files = get_kit_files
113
+ config_name = get_config_name
114
+ dest_path = get_dest_path config_name
115
+
116
+ if ::File.directory? dest_path
117
+ ui.info "Configuration for #{dest_path} already exists."
118
+ else
119
+ ::FileUtils.mkpath dest_path
120
+ copy_files( kit_files )
121
+ ui.msg "Added #{config_name} to #{root}."
100
122
  end
101
123
  end
102
124
  end
@@ -0,0 +1,21 @@
1
+ require 'chef/knife'
2
+
3
+ module KnifeOrgUtils
4
+ class SwitchInit < Chef::Knife
5
+
6
+ banner 'knife switch init'
7
+
8
+ def dot_chef_path
9
+ @dot_chef_path ||= ::File.expand_path( '~/.chef' )
10
+ end
11
+
12
+ def run
13
+ if ::File.directory? dot_chef_path
14
+ ui.error "Directory #{dot_chef_path} exists. Please backup and remove this directory before initializing."
15
+ exit 1
16
+ else
17
+ ::Dir.mkdir dot_chef_path
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,14 +1,40 @@
1
1
  require 'chef/knife'
2
- require 'git'
3
2
 
4
3
  module KnifeOrgUtils
5
4
  class SwitchList < Chef::Knife
6
5
 
7
6
  banner 'knife switch list'
8
7
 
8
+ def root
9
+ @root ||= File.expand_path( '~/.chef' )
10
+ end
11
+
12
+ def current_config
13
+ parse_knife_rb unless @current_config
14
+ @current_config
15
+ end
16
+
17
+ def parse_knife_rb
18
+ knife_rb = ::File.join( root, 'knife.rb' )
19
+ knife_data = ::File.read( knife_rb )
20
+ %r{
21
+ ^chef_server_url\s+"https{0,1}://(?<server>[a-zA-Z0-9-]+).*/organizations/(?<org>[a-zA-Z0-9_]+)"$
22
+ }x =~ knife_data
23
+
24
+ @current_config = "#{server}/#{org}"
25
+ end
26
+
9
27
  def run
10
- @git = ::Git.open( '~/.chef' )
11
- ui.msg @git.branches.local
28
+ Dir.glob("#{root}/*/*/").each do | dir |
29
+ config_name = dir.gsub(%r{^#{root}/}, '').chomp('/')
30
+
31
+ current = if ( current_config == config_name )
32
+ "\xF0\x9F\x8D\xB4 "
33
+ else
34
+ " "
35
+ end
36
+ ui.msg "#{current}#{config_name}"
37
+ end
12
38
  end
13
39
  end
14
40
  end
@@ -1,3 +1,3 @@
1
1
  module KnifeOrgUtils
2
- VERSION = '0.1.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-org-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Venkat Venkataraju
@@ -9,93 +9,71 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-10-20 00:00:00.000000000 Z
12
+ date: 2014-12-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ">="
19
- - !ruby/object:Gem::Version
20
- version: '0'
21
- type: :runtime
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
18
+ - - "~>"
26
19
  - !ruby/object:Gem::Version
27
- version: '0'
28
- - !ruby/object:Gem::Dependency
29
- name: git
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
20
+ version: '11.16'
32
21
  - - ">="
33
22
  - !ruby/object:Gem::Version
34
- version: '0'
23
+ version: 11.16.4
35
24
  type: :runtime
36
25
  prerelease: false
37
26
  version_requirements: !ruby/object:Gem::Requirement
38
27
  requirements:
39
- - - ">="
28
+ - - "~>"
40
29
  - !ruby/object:Gem::Version
41
- version: '0'
42
- - !ruby/object:Gem::Dependency
43
- name: json
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
30
+ version: '11.16'
46
31
  - - ">="
47
32
  - !ruby/object:Gem::Version
48
- version: '0'
49
- type: :development
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: '0'
33
+ version: 11.16.4
56
34
  - !ruby/object:Gem::Dependency
57
35
  name: bundler
58
36
  requirement: !ruby/object:Gem::Requirement
59
37
  requirements:
60
- - - ">="
38
+ - - "~>"
61
39
  - !ruby/object:Gem::Version
62
- version: '0'
40
+ version: '1'
63
41
  type: :development
64
42
  prerelease: false
65
43
  version_requirements: !ruby/object:Gem::Requirement
66
44
  requirements:
67
- - - ">="
45
+ - - "~>"
68
46
  - !ruby/object:Gem::Version
69
- version: '0'
47
+ version: '1'
70
48
  - !ruby/object:Gem::Dependency
71
49
  name: rake
72
50
  requirement: !ruby/object:Gem::Requirement
73
51
  requirements:
74
- - - ">="
52
+ - - "~>"
75
53
  - !ruby/object:Gem::Version
76
- version: '0'
54
+ version: '10'
77
55
  type: :development
78
56
  prerelease: false
79
57
  version_requirements: !ruby/object:Gem::Requirement
80
58
  requirements:
81
- - - ">="
59
+ - - "~>"
82
60
  - !ruby/object:Gem::Version
83
- version: '0'
61
+ version: '10'
84
62
  - !ruby/object:Gem::Dependency
85
63
  name: rspec
86
64
  requirement: !ruby/object:Gem::Requirement
87
65
  requirements:
88
- - - ">="
66
+ - - "~>"
89
67
  - !ruby/object:Gem::Version
90
- version: '0'
68
+ version: '3'
91
69
  type: :development
92
70
  prerelease: false
93
71
  version_requirements: !ruby/object:Gem::Requirement
94
72
  requirements:
95
- - - ">="
73
+ - - "~>"
96
74
  - !ruby/object:Gem::Version
97
- version: '0'
98
- description: Manages your .chef org, and provides info about your chef config
75
+ version: '3'
76
+ description: Manages multiple chef server/org configuration files in the .chef folder.
99
77
  email:
100
78
  - ven@yahoo-inc.com
101
79
  - shruthiv@yahoo-inc.com
@@ -107,21 +85,26 @@ files:
107
85
  - lib/chef/knife/info.rb
108
86
  - lib/chef/knife/switch.rb
109
87
  - lib/chef/knife/switch_add.rb
88
+ - lib/chef/knife/switch_init.rb
110
89
  - lib/chef/knife/switch_list.rb
111
90
  - lib/knife-org-utils/version.rb
112
91
  homepage: https://github.com/secret-sauce/knife-org-utils.git
113
92
  licenses:
114
93
  - MIT
115
94
  metadata: {}
116
- post_install_message:
95
+ post_install_message: " \e[1;33;40m!!! Warning !!!\e[0m\n \e[0;36;40mPlease ignore
96
+ this message if you do not use 'knife switch'\e[0m\n Pre 1.0.0 version used git
97
+ to manage the ~/.chef folder. Version 1.0.x and\n above will not use git to manage
98
+ the ~/.chef directory. Please backup your\n ~/.chef directory before adding new
99
+ configurations.\n More info: https://github.com/secret-sauce/knife-org-utils/blob/master/README\n\n"
117
100
  rdoc_options: []
118
101
  require_paths:
119
102
  - lib
120
103
  required_ruby_version: !ruby/object:Gem::Requirement
121
104
  requirements:
122
- - - ">="
105
+ - - "~>"
123
106
  - !ruby/object:Gem::Version
124
- version: '2.0'
107
+ version: '2'
125
108
  required_rubygems_version: !ruby/object:Gem::Requirement
126
109
  requirements:
127
110
  - - ">="