knife-whisk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 14bd4c3ad621d14ee341d773ff75ad7afa5dacf1
4
+ data.tar.gz: b028449ba2113dc75b28262ebfec758692db58f7
5
+ SHA512:
6
+ metadata.gz: 19dc6286ebf16222c7d5682f9b795492f29d3a677c987fa562bb66a00d98482e92ff31ca27e5c1b554a06e3ffac85d5b3709dfc11f297dfc8f1078693327c623
7
+ data.tar.gz: 3ad95946d5165a3926a8a0bdf0510e54005035142b68d2ef80a3d771a16331a3a848ff8273735d9a4394e9141d8af204bcc26350814ed8a73b399507a8e6cfc4
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .bundle
19
+
@@ -0,0 +1,8 @@
1
+ ## Contributing
2
+ 1. Fork it
3
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
4
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
5
+ 4. Push to the branch (`git push origin my-new-feature`)
6
+ 5. Create new Pull Request
7
+
8
+ Pester us if we don't get your Pull Requests merged in a timely fashion. :)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in knife-whisk.gemspec
4
+ gemspec
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2013 Banno, LLC.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,153 @@
1
+ # knife-whisk
2
+
3
+ A utility for quickly whipping up new servers in a team environment
4
+
5
+ ![whisk](https://github.com/Banno/knife-whisk/raw/master/whisk.jpg)
6
+ ## Overview
7
+ Chef is a great tool for configuring servers once they're up, but it doesn't address configuration management for initializing cloud servers in the first place. This tool lets your team define named servers and configuration mixins to help your team always create new servers uniformly. Currently, it will generate `knife ec2 server create` commands based upon the configuration defined in your `whisk.yml` file. You're free to provide overrides and additional flags when generating a new command to spit out a slight alteration of an existing server's config.
8
+
9
+ All together, this tool provides us with a "frictionless" experience for understanding how servers are created in our organization and quick access to recreate them correctly during a crisis.
10
+
11
+ ## Usage
12
+ Assume you've got knife-whisk installed and configured correctly and it's pointing to a whisk.yml that looks like this:
13
+
14
+ ```
15
+ mixins:
16
+ defaults:
17
+ image: ami-950680fc # ubuntu instance store
18
+ subnet: subnet-12345678 # private subnet
19
+ region: us-east-1
20
+ user-data: user_data/before_bootstrap.sh
21
+ flavor: m1.small
22
+ ssh-user: ubuntu
23
+ template: chef_full
24
+ security-groups: default
25
+ ebs_instance:
26
+ image: ami-e50e888c
27
+ public_subnet:
28
+ subnet: subnet-87654321
29
+
30
+ security-groups:
31
+ default: sg-12345678
32
+ java_app_server: sg-34567890
33
+
34
+ servers:
35
+ app_server:
36
+ mixins:
37
+ - defaults
38
+ config:
39
+ run-list:
40
+ - "recipe[application-wrapper]"
41
+ security-groups:
42
+ - default
43
+ - java_app_server
44
+ environment: prod
45
+ ```
46
+
47
+ `knife whisk generate` will work like this:
48
+
49
+ ```
50
+ $ knife whisk generate app_server
51
+ knife ec2 server create --image ami-950680fc --subnet subnet-12345678 --region us-east-1 --user-data user_data/before_bootstrap.sh --flavor m1.small --ssh-user ubuntu --template chef_full --run-list recipe[application-wrapper] --environment prod --security-groups-ids sg-12345678,sg-3456789
52
+ ```
53
+
54
+ Let's mix in the public_subnet mixin:
55
+
56
+ ```
57
+ $ knife whisk generate app_server --mixins public_subnet
58
+
59
+ knife ec2 server create --image ami-950680fc --subnet subnet-87654321 --region us-east-1 --user-data user_data/before_bootstrap.sh --flavor m1.small --ssh-user ubuntu --template chef_full --run-list recipe[application-wrapper] --environment prod --security-groups-ids sg-12345678,sg-34567890
60
+ ```
61
+
62
+ See how the subnet flag was updated? Now let's try something with overrides.
63
+
64
+ ```
65
+ $ knife whisk generate app_server --mixins public_subnet --overrides "--environment dev --flavor m2.4xlarge"
66
+
67
+ knife ec2 server create --image ami-950680fc --subnet subnet-87654321 --region us-east-1 --user-data user_data/before_bootstrap.sh --flavor m2.4xlarge --ssh-user ubuntu --template chef_full --run-list recipe[application-wrapper] --environment dev --security-groups-ids sg-12345678,sg-34567890
68
+ ```
69
+
70
+ This is where knife-whisk shines. You don't have to be aware of all the ins and outs of AWS or whatever cloud provider you use, you just ask for the defaults or whatever other mixins you need on the server you want to fire up.
71
+
72
+ Most importantly, as a team you define what your production servers look like in your whisk.yml file and you'll never bring up a new node with a missing security group because you forgot some ancillary service was on the node that needed it.
73
+
74
+ ## Installation
75
+
76
+ Add this line to your application's Gemfile:
77
+
78
+ ```
79
+ gem 'knife-whisk'
80
+ ```
81
+
82
+ And then execute:
83
+
84
+ ```
85
+ $ bundle install
86
+ ```
87
+
88
+ Or install it yourself as:
89
+
90
+ ```
91
+ $ gem install knife-whisk
92
+ ```
93
+
94
+ ##Configuration
95
+
96
+ A `whisk.yml` file must exist for knife-whisk to work. There is an example in the example directory. The path to the file can be set in your knife config, via the `--whisk-config` or `-C` flag, or will look for it by default in your .chef directory.
97
+
98
+ Best way to do it is to just put your whisk.yml in your chef directory under config/ and put something like this in your knife.rb.
99
+
100
+ ```
101
+ knife[:whisk_config_file] = "#{ENV['CHEF_REPO_DIR']}/config/whisk.yml"
102
+ ```
103
+
104
+ ##Subcommands
105
+
106
+ This plugin provides the following Knife subcommands. Specific command options can be found by invoking the subcommand with a `--help` flag.
107
+
108
+ ### knife whisk mixin list
109
+ Lists the available mixins
110
+ ###knife whisk mixin show
111
+ Shows the yaml output of the details of the mixin
112
+ ###knife whisk server list
113
+ Lists the available servers templates
114
+ ###knife whisk server show
115
+ Shows the yaml output of the details of the server
116
+ ###knife whisk generate
117
+ Outputs a knife command with the provided server template, mixins, or overrides. When using the `--overrides` flag you must provide long form flag names, I.E `--node-name` not `-N`.
118
+
119
+ ####Examples:
120
+
121
+ ```
122
+ knife whisk generate --mixins defaults,public_subnet --overrides "--node-name server1 --image ami-123456"
123
+ ```
124
+
125
+ ```
126
+ knife whisk generate application_server
127
+ ```
128
+
129
+ ```
130
+ knife whisk generate application_server --mixins public_subnet
131
+ ```
132
+
133
+ ```
134
+ knife whisk generate application_server --overrides "--environment=dev --node-name dev-application-server"
135
+ ```
136
+
137
+ ## Tab Completion
138
+
139
+ ### ZSH
140
+ This repository also includes a zsh folder, with a replacement for oh-my-zsh's knife plugin. Copy that to your ~/.oh-my-zsh/plugins/knife folder, and enabled the plugin by adding "knife" to your plugins=() in your .zshrc. You will likely need to reload your shell.
141
+
142
+ ## Todo
143
+ * Support for non ec2 setups
144
+ * whisk add mixin
145
+ * whisk add server
146
+ * bash tab completion
147
+
148
+ ##Authors
149
+ - Nic Grayson (<nic.grayson@banno.com>)
150
+ - Kevin Nuckolls (<kevin.nuckolls@banno.com>)
151
+ - Danny Lockard (<danny.lockard@banno.com>)
152
+
153
+ If you want to contribute, thanks! Please see [contribution guidelines](https://github.com/Banno/knife-whisk/blob/master/CONTRIBUTING.markdown).
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,53 @@
1
+ mixins:
2
+ defaults:
3
+ image: ami-950680fc # ubuntu instance store
4
+ subnet: subnet-12345678 # private subnet
5
+ region: us-east-1
6
+ user-data: user_data/before_bootstrap.sh
7
+ flavor: m1.small
8
+ ssh-user: ubuntu
9
+ template: chef_full
10
+ security-groups: default
11
+ ebs:
12
+ image: ami-e50e888c
13
+ public_subnet:
14
+ subnet: subnet-87654321
15
+
16
+ security-groups:
17
+ default: sg-12345678
18
+ load_balancer: sg-23456789
19
+ java_app_server: sg-34567890
20
+ database_server: sg-45678901
21
+
22
+ servers:
23
+ prod_nginx_load_balancer:
24
+ mixins:
25
+ - defaults
26
+ - public_subnet
27
+ config:
28
+ run-list:
29
+ - "recipe[nginx]"
30
+ - "role[load_balancer]"
31
+ security-groups:
32
+ - default
33
+ - load_balancer
34
+ environment: stag
35
+ app_server:
36
+ mixins:
37
+ - defaults
38
+ - public_subnet
39
+ config:
40
+ run-list:
41
+ - "recipe[essentials]"
42
+ security-groups:
43
+ - default
44
+ - java_app_server
45
+ environment: prod
46
+ database_server:
47
+ mixins:
48
+ - defaults
49
+ - ebs
50
+ config:
51
+ run-list:
52
+ - "recipe[postgres]"
53
+ environment: prod
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'knife-whisk/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "knife-whisk"
8
+ gem.version = Knife::Whisk::VERSION
9
+ gem.authors = ["Nic Grayson"]
10
+ gem.email = ["nic.grayson@banno.com"]
11
+ gem.summary = %q{Extends knife to display knife ec2 server create commands}
12
+ gem.description = %q{A utility for quickly whipping up new servers in a team environment}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.add_development_dependency 'chef'
19
+ gem.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,190 @@
1
+ require 'chef/knife'
2
+ require 'yaml'
3
+
4
+ class Chef
5
+ class Knife
6
+ module WhiskBase
7
+ def self.included(includer)
8
+ includer.class_eval do
9
+
10
+ option :whisk_config_file,
11
+ :short => '-C PATH',
12
+ :long => '--whisk-config PATH',
13
+ :description => "Specify path to your whisk.yml file",
14
+ :proc => Proc.new { |path| Chef::Config[:knife][:whisk_config_file] = path }
15
+
16
+ option :mixins,
17
+ :short => '-M MIXINS',
18
+ :long => '--mixins MIXINS',
19
+ :description => "Overrides server mixins, takes comma seperated list of mixins",
20
+ :proc => Proc.new { |input| input.split(",") }
21
+
22
+ option :overrides,
23
+ :short => '-O STRING',
24
+ :long => '--overrides STRING',
25
+ :description => "Override flags, takes string containing flags and values",
26
+ :proc => Proc.new { |string|
27
+ # checks if the flags have -- in front, if not error, if so strip them before returning
28
+ if Hash[*string.split].keys.select { |n| n[0..1] != "--" }.size == 0
29
+ Hash[Hash[*string.split].map {|key, val|[key.sub(/^--/, ''), val]}]
30
+ else
31
+ false
32
+ end
33
+ }
34
+ end
35
+ end
36
+
37
+ def exit_with_message(message)
38
+ ui.fatal message
39
+ Kernel.exit(1)
40
+ end
41
+
42
+ def get_config
43
+ if Chef::Config[:knife][:whisk_config_file].nil?
44
+ if File.exists?(::Chef::Knife::chef_config_dir+"/whisk.yml")
45
+ YAML.load_file(::Chef::Knife::chef_config_dir+"/whisk.yml")
46
+ else
47
+ return false
48
+ end
49
+ else
50
+ YAML.load_file(Chef::Config[:knife][:whisk_config_file])
51
+ end
52
+ end
53
+
54
+ def get_security_groups(groups)
55
+ groups.split(',').map! { |name| name.replace(get_config["security-groups"][name]) }.join(',')
56
+ end
57
+
58
+ def security_group_exists?(group)
59
+ ! get_config["security-groups"][group].nil?
60
+ end
61
+
62
+ def mixin_exists?(mixin)
63
+ ! get_config["mixins"][mixin].nil?
64
+ end
65
+
66
+ def server_exists?(server)
67
+ ! get_config["servers"][server].nil?
68
+ end
69
+
70
+ def add_quotes(val)
71
+ "\"#{val}\""
72
+ end
73
+
74
+ end
75
+ end
76
+
77
+ class WhiskServerList < Chef::Knife
78
+ include Knife::WhiskBase
79
+ banner "knife whisk server list"
80
+
81
+ def run
82
+ exit_with_message("Required whisk.yml does not exist") unless get_config
83
+ get_config["servers"].each do |server|
84
+ puts server.first
85
+ end
86
+ end
87
+ end
88
+
89
+ class WhiskServerShow < Chef::Knife
90
+ include Knife::WhiskBase
91
+ banner "knife whisk server show SERVER"
92
+
93
+ def run
94
+ exit_with_message("Required whisk.yml does not exist") unless get_config
95
+ exit_with_message("You must specify a server name") unless name_args.size == 1
96
+
97
+ if server_exists?(name_args.first)
98
+ puts name_args.first
99
+ puts get_config["servers"]["#{name_args.first}"].to_yaml
100
+ else
101
+ exit_with_message("#{name_args.first} server template does not exist")
102
+ end
103
+ end
104
+ end
105
+
106
+ class WhiskMixinList < Chef::Knife
107
+ include Knife::WhiskBase
108
+ banner "knife whisk mixin list"
109
+
110
+ def run
111
+ exit_with_message("Required whisk.yml does not exist") unless get_config
112
+ get_config["mixins"].each do |mixin|
113
+ puts mixin.first
114
+ end
115
+ end
116
+ end
117
+
118
+ class WhiskMixinShow < Chef::Knife
119
+ include Knife::WhiskBase
120
+ banner "knife whisk mixin show MIXIN"
121
+
122
+ def run
123
+ exit_with_message("Required whisk.yml does not exist") unless get_config
124
+ exit_with_message("You must specify a mixin") unless name_args.size == 1
125
+
126
+ if mixin_exists?(name_args.first)
127
+ puts name_args.first
128
+ puts get_config["mixins"]["#{name_args.first}"].to_yaml
129
+ else
130
+ exit_with_message("#{name_args.first} mixin does not exist")
131
+ end
132
+ end
133
+ end
134
+
135
+ class WhiskGenerate < Chef::Knife
136
+ include Knife::WhiskBase
137
+ banner "knife whisk generate [SERVER]"
138
+
139
+ def run
140
+ exit_with_message("Required whisk.yml does not exist") unless get_config
141
+
142
+ full_hash = get_config
143
+
144
+ if name_args.size == 0
145
+ server_config = full_hash["mixins"]["defaults"]
146
+ server_mixins = ["defaults"]
147
+ elsif server_exists?(name_args.first)
148
+ server_mixins = full_hash["servers"][name_args.first]["mixins"]
149
+ server_config = full_hash["servers"][name_args.first]["config"]
150
+ else
151
+ exit_with_message("#{name_args.first} server template does not exist")
152
+ end
153
+
154
+ unless @config[:mixins].nil?
155
+ server_mixins = server_mixins + @config[:mixins]
156
+ end
157
+
158
+ #checks to make sure all mixins exist
159
+ server_mixins.each { |mixin| exit_with_message("#{mixin} mixin does not exist") unless mixin_exists?(mixin) }
160
+
161
+ #merges together all mixin config values with server config values
162
+ output_hash = server_mixins.inject(Hash.new) {|output, mixin| output.merge(full_hash["mixins"][mixin])}.merge(server_config)
163
+
164
+ #convert config values that are arrays to comma seperates strings
165
+ output_hash.each do |mixin, value|
166
+ if value.kind_of?(Array)
167
+ output_hash[mixin] = value.join(",")
168
+ end
169
+ end
170
+
171
+ unless @config[:overrides].nil?
172
+ exit_with_message("Please use long form flags only in overrides") unless @config[:overrides].kind_of?(Hash)
173
+ output_hash = output_hash.merge(@config[:overrides])
174
+ end
175
+
176
+ #convert security-group names to ids if needed and make sure they exist in the lookup hash
177
+ unless output_hash["security-groups"].nil?
178
+ exit_with_message("security-groups not defined in whisk.yml") unless get_config["security-groups"]
179
+ output_hash["security-groups"].split(',').each { |group| exit_with_message("#{group} security group does not exist in whisk.yml") unless security_group_exists?(group)}
180
+ output_hash["security-group-ids"] = get_security_groups(output_hash["security-groups"])
181
+ output_hash.delete("security-groups")
182
+ end
183
+
184
+ # run-list needs quotes for knife ec2 to accept the arg
185
+ output_hash["run-list"] = add_quotes(output_hash["run-list"]) unless output_hash["run-list"].nil?
186
+
187
+ printf "knife ec2 server create %s\n", output_hash.map { |key, value| ["--"+key, value] }.join(" ")
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module Whisk
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
Binary file
@@ -0,0 +1,197 @@
1
+ #compdef knife
2
+
3
+ # These flags should be available everywhere according to man knife
4
+ knife_general_flags=( --help --server-url --key --config --editor --format --log_level --logfile --no-editor --user --print-after --version --yes )
5
+
6
+ # knife has a very special syntax, some example calls are:
7
+ # knife status
8
+ # knife cookbook list
9
+ # knife role show ROLENAME
10
+ # knife data bag show DATABAGNAME
11
+ # knife role show ROLENAME --attribute ATTRIBUTENAME
12
+ # knife cookbook show COOKBOOKNAME COOKBOOKVERSION recipes
13
+
14
+ # The -Q switch in compadd allow for completions of things like "data bag" without having to go through two rounds of completion and avoids zsh inserting a \ for escaping spaces
15
+ _knife() {
16
+ local curcontext="$curcontext" state line
17
+ typeset -A opt_args
18
+ cloudproviders=(bluebox ec2 rackspace slicehost terremark)
19
+ _arguments \
20
+ '1: :->knifecmd'\
21
+ '2: :->knifesubcmd'\
22
+ '3: :->knifesubcmd2' \
23
+ '4: :->knifesubcmd3' \
24
+ '5: :->knifesubcmd4' \
25
+ '6: :->knifesubcmd5'
26
+
27
+ case $state in
28
+ knifecmd)
29
+ compadd -Q "$@" bootstrap client configure cookbook "cookbook site" "data bag" exec environment index node recipe role search ssh status windows $cloudproviders whisk
30
+ ;;
31
+ knifesubcmd)
32
+ case $words[2] in
33
+ (bluebox|ec2|rackspace|slicehost|terremark)
34
+ compadd "$@" server images
35
+ ;;
36
+ client)
37
+ compadd -Q "$@" "bulk delete" list create show delete edit reregister
38
+ ;;
39
+ configure)
40
+ compadd "$@" client
41
+ ;;
42
+ cookbook)
43
+ compadd -Q "$@" test list create download delete "metadata from" show "bulk delete" metadata upload
44
+ ;;
45
+ environment)
46
+ compadd -Q "$@" list create delete edit show "from file"
47
+ ;;
48
+ node)
49
+ compadd -Q "$@" "from file" create show edit delete list run_list "bulk delete"
50
+ ;;
51
+ recipe)
52
+ compadd "$@" list
53
+ ;;
54
+ role)
55
+ compadd -Q "$@" "bulk delete" create delete edit "from file" list show
56
+ ;;
57
+ windows)
58
+ compadd "$@" bootstrap
59
+ ;;
60
+ whisk)
61
+ compadd "$@" mixin server generate
62
+ ;;
63
+ *)
64
+ _arguments '2:Subsubcommands:($(_knife_options1))'
65
+ esac
66
+ ;;
67
+ knifesubcmd2)
68
+ case $words[3] in
69
+ server)
70
+ compadd "$@" list create delete
71
+ ;;
72
+ images)
73
+ compadd "$@" list
74
+ ;;
75
+ site)
76
+ compadd "$@" vendor show share search download list unshare
77
+ ;;
78
+ (show|delete|edit)
79
+ _arguments '3:Subsubcommands:($(_chef_$words[2]s_remote))'
80
+ ;;
81
+ (upload|test)
82
+ _arguments '3:Subsubcommands:($(_chef_$words[2]s_local) --all)'
83
+ ;;
84
+ list)
85
+ compadd -a "$@" knife_general_flags
86
+ ;;
87
+ bag)
88
+ compadd -Q "$@" show edit list "from file" create delete
89
+ ;;
90
+ mixin)
91
+ compadd "$@" list show
92
+ ;;
93
+ server)
94
+ compadd "$@" list show
95
+ ;;
96
+ generate)
97
+ compadd "$@" `knife whisk server list`
98
+ ;;
99
+ *)
100
+ _arguments '3:Subsubcommands:($(_knife_options2))'
101
+ esac
102
+ ;;
103
+ knifesubcmd3)
104
+ case $words[3] in
105
+ show)
106
+ case $words[2] in
107
+ cookbook)
108
+ versioncomp=1
109
+ _arguments '4:Cookbookversions:($(_cookbook_versions) latest)'
110
+ ;;
111
+ (node|client|role)
112
+ compadd "$@" --attribute
113
+ esac
114
+ esac
115
+ case $words[4] in
116
+ (show|edit)
117
+ _arguments '4:Subsubsubcommands:($(_chef_$words[2]_$words[3]s_remote))'
118
+ ;;
119
+ file)
120
+ _arguments '*:file or directory:_files -g "*.(rb|json)"'
121
+ ;;
122
+ list)
123
+ compadd -a "$@" knife_general_flags
124
+ ;;
125
+ *)
126
+ _arguments '*:Subsubcommands:($(_knife_options3))'
127
+ esac
128
+ ;;
129
+ knifesubcmd4)
130
+ if (( versioncomp > 0 )); then
131
+ compadd "$@" attributes definitions files libraries providers recipes resources templates
132
+ else
133
+ _arguments '*:Subsubcommands:($(_knife_options2))'
134
+ fi
135
+ ;;
136
+ knifesubcmd5)
137
+ _arguments '*:Subsubcommands:($(_knife_options3))'
138
+ esac
139
+ }
140
+
141
+ # Helper functions to provide the argument completion for several depths of commands
142
+ _knife_options1() {
143
+ ( for line in $( knife $words[2] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
144
+ }
145
+
146
+ _knife_options2() {
147
+ ( for line in $( knife $words[2] $words[3] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
148
+ }
149
+
150
+ _knife_options3() {
151
+ ( for line in $( knife $words[2] $words[3] $words[4] --help | grep -v "^knife" ); do echo $line | grep "\-\-"; done )
152
+ }
153
+
154
+ # The chef_x_remote functions use knife to get a list of objects of type x on the server
155
+ _chef_roles_remote() {
156
+ (knife role list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
157
+ }
158
+
159
+ _chef_clients_remote() {
160
+ (knife client list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
161
+ }
162
+
163
+ _chef_nodes_remote() {
164
+ (knife node list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
165
+ }
166
+
167
+ _chef_cookbooks_remote() {
168
+ (knife cookbook list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
169
+ }
170
+
171
+ _chef_sitecookbooks_remote() {
172
+ (knife cookbook site list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
173
+ }
174
+
175
+ _chef_data_bags_remote() {
176
+ (knife data bag list --format json | grep \" | awk '{print $1}' | awk -F"," '{print $1}' | awk -F"\"" '{print $2}')
177
+ }
178
+
179
+ _chef_environments_remote() {
180
+ (knife environment list | awk '{print $1}')
181
+ }
182
+
183
+ # The chef_x_local functions use the knife config to find the paths of relevant objects x to be uploaded to the server
184
+ _chef_cookbooks_local() {
185
+ local knife_rb="$HOME/.chef/knife.rb"
186
+ if [ -f ./.chef/knife.rb ]; then
187
+ knife_rb="./.chef/knife.rb"
188
+ fi
189
+ (for i in $( grep cookbook_path $knife_rb | awk 'BEGIN {FS = "[" }; {print $2}' | sed 's/\,//g' | sed "s/'//g" | sed 's/\(.*\)]/\1/' ); do ls $i; done)
190
+ }
191
+
192
+ # This function extracts the available cookbook versions on the chef server
193
+ _cookbook_versions() {
194
+ (knife cookbook show $words[4] | grep -v $words[4] | grep -v -E '\]|\[|\{|\}' | sed 's/ //g' | sed 's/"//g')
195
+ }
196
+
197
+ _knife "$@"
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-whisk
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nic Grayson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chef
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A utility for quickly whipping up new servers in a team environment
28
+ email:
29
+ - nic.grayson@banno.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - CONTRIBUTING.markdown
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.markdown
39
+ - Rakefile
40
+ - examples/whisk.yml
41
+ - knife-whisk.gemspec
42
+ - lib/chef/knife/knife-whisk.rb
43
+ - lib/knife-whisk/version.rb
44
+ - whisk.jpg
45
+ - zsh/_knife
46
+ homepage: ''
47
+ licenses: []
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.0.0
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Extends knife to display knife ec2 server create commands
69
+ test_files: []