knife-windows 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in knife-windows.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -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 [yyyy] [name of copyright owner]
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.
data/README.rdoc ADDED
@@ -0,0 +1,109 @@
1
+ = Knife Windows Plugin
2
+
3
+ = DESCRIPTION:
4
+
5
+ This plugins adds additional functionality to the Chef Knife CLI tool for configuring/interacting with nodes running Microsoft Windows. The subcommands should function on any system running Ruby 1.9.1+ but nodes being configured via these subcommands require Windows Remote Management (WinRM) 1.0+. WinRM allows you to call native objects in Windows. This includes, but is not limited to, running batch scripts, powershell scripts and fetching WMI variables. For more information on WinRM, please visit {Microsoft's WinRM site}[http://msdn.microsoft.com/en-us/library/aa384426(v=VS.85).aspx]. You will want to familiarize yourself with (certain key aspects) of WinRM because you will be {writing scripts/running commands} with this tool to get you from (specific point A) to (specific point B).
6
+
7
+ WinRM is built into Windows 7 and Windows Server 2008+. It can also be easily installed in older version of Windows, including:
8
+
9
+ * Windows XP
10
+ * Windows Server 2003
11
+ * Windows Vista
12
+
13
+ More information can be found on {Microsoft Support article 968930}[http://support.microsoft.com/?kbid=968930].
14
+
15
+ = SUBCOMMANDS:
16
+
17
+ This plugin provides the following Knife subcommands. Specific command options can be found by invoking the subcommand with a --+help+ flag
18
+
19
+ == knife winrm
20
+
21
+ The +winrm+ subcommand allows you to invoke commands in parallel on a subset of the nodes in your infrastructure. The +winrm+ subcommand uses the same syntax as the {search subcommand}[http://wiki.opscode.com/display/chef/Search]; you could could find the uptime of all your web servers using the command:
22
+
23
+ % knife winrm "role:web" "net stats srv" -x Administrator -P 'super_secret_password'
24
+
25
+ Or force a chef run:
26
+
27
+ % knife winrm 'ec2-50-xx-xx-124.compute-1.amazonaws.com' 'chef-client -c c:/chef/client.rb' -m -x Administrator -P 'super_secret_password'
28
+ ec2-50-xx-xx-124.compute-1.amazonaws.com [Fri, 04 Mar 2011 22:00:49 +0000] INFO: Starting Chef Run (Version 0.9.12)
29
+ ec2-50-xx-xx-124.compute-1.amazonaws.com [Fri, 04 Mar 2011 22:00:50 +0000] WARN: Node ip-0A502FFB has an empty run list.
30
+ ec2-50-xx-xx-124.compute-1.amazonaws.com [Fri, 04 Mar 2011 22:00:53 +0000] INFO: Chef Run complete in 4.383966 seconds
31
+ ec2-50-xx-xx-124.compute-1.amazonaws.com [Fri, 04 Mar 2011 22:00:53 +0000] INFO: cleaning the checksum cache
32
+ ec2-50-xx-xx-124.compute-1.amazonaws.com [Fri, 04 Mar 2011 22:00:53 +0000] INFO: Running report handlers
33
+ ec2-50-xx-xx-124.compute-1.amazonaws.com [Fri, 04 Mar 2011 22:00:53 +0000] INFO: Report handlers complete
34
+
35
+ This subcommand operates in a very similar manner as {knife ssh}[http://wiki.opscode.com/display/chef/Knife#Knife-SSHSubcommand]...just leveraging the WinRM protocol for communication.
36
+
37
+ == knife winrm bootstrap
38
+
39
+ Performs a Chef Bootstrap (via the WinRM protocol) on the target node. The goal of the bootstrap is to get Chef installed on the target system so it can run Chef Client with a Chef Server. The main assumption is a baseline OS installation exists. It is primarily intended for Chef Client systems that talk to a Chef server.
40
+
41
+ In the future this subcommand will be used internally by some cloud computing server create commands like the current *knife bootstrap* subcommand is. This subcommand operates in a very similar manner as {knife bootstrap}[http://wiki.opscode.com/display/chef/Knife+Bootstrap]...just leveraging the WinRM protocol for communication. An initial run_list for the node can also be passed to the subcommand. Example usage:
42
+
43
+ knife winrm bootstrap ec2-50-xx-xx-124.compute-1.amazonaws.com -r 'role[webserver]','role[production]' -x Administrator -P 'super_secret_password'
44
+
45
+ = BOOTSTRAP TEMPLATES:
46
+
47
+ This gem provides the following bootstrap templates:
48
+
49
+ == windows-shell
50
+
51
+ This bootstrap template does the following:
52
+
53
+ * Installs Ruby 1.8.7 via the {Ruby Installer for Windows}[http://rubyinstaller.org/] which also includes the latest version of RubyGems
54
+ * Installs required Windows-related gems from RubyGems.org
55
+ * Installs latest Chef version from RubyGems.org including {Ohai}[https://rubygems.org/gems/ohai] and {Chef}[https://rubygems.org/gems/chef].
56
+ * Writes the validation.pem per the local knife configuration.
57
+ * Writes a default config file for Chef (C:\chef\client.rb) using values from the +knife.rb+.
58
+ * Creates a JSON attributes file containing the specified run list and run Chef.
59
+
60
+ This is the default bootstrap template used by the +winrm+ +bootstrap+ subcommand.
61
+
62
+ = REQUIREMENTS/SETUP:
63
+
64
+ == Chef Version
65
+
66
+ {CHEF-1248}[http://tickets.opscode.com/browse/CHEF-1248] added the ability to load Knife subcommands from alternate locations including Ruby gems (such as this one). This feature will be built into the 0.10 release of Chef. Until then this feature can be added to Chef 0.9.x releases by using a monkey patched +knife+ bin file:
67
+
68
+ cd YOUR_CHEF_REPO
69
+ curl https://gist.github.com/raw/f33e15bac2820dd572d4/cb6ab81023440235868e83b96fa0ae527d37e2d7/knife >> knife
70
+ chmod 744 knife
71
+
72
+ More details about Knife plugins can be {found on the Chef wiki}[http://wiki.opscode.com/display/chef/Knife+Plugins].
73
+
74
+ == Nodes
75
+
76
+ *NOTE*: Before any WinRM related knife subcommands will function correctly a node's WinRM installation must be configured correctly. The below settings should be added to your base server image (AMI) or passed in using some sort of user-data mechanism provided by your cloud provider.
77
+
78
+ A server running WinRM must also be configured properly to allow outside connections and the entire network path from the knife workstation to the server. The easiest way to accomplish this is to use {WinRM's quick configuration option}[http://msdn.microsoft.com/en-us/library/aa384372(v=vs.85).aspx#quick_default_configuration]:
79
+
80
+ C:\Users\Administrator> winrm quickconfig -q
81
+
82
+ The Chef and Ohai gem installations (that occur during bootstrap) take more memory than the default 150MB WinRM allocates per shell. Bump it up to 300MB with the following setting:
83
+
84
+ C:\Users\Administrator> winrm set winrm/config/winrs @{MaxMemoryPerShellMB="300"}
85
+
86
+ WinRM supports both the HTTP and HTTPS transports and the following authentication schemes: Kerberos, Digest, Certificate and Basic. The details of these authentication transports are outside of the scope of this README but details can be found on the {WinRM configuration guide}[http://msdn.microsoft.com/en-us/library/aa384372(v=vs.85).aspx]. Currently, this plugin support Kerberos and Basic authentication schemes.
87
+
88
+ For development and testing purposes, unencrypted traffic with Basic authentication can make things easier:
89
+
90
+ C:\Users\Administrator> winrm set winrm/config/service @{AllowUnencrypted="true"}
91
+ C:\Users\Administrator> winrm set winrm/config/service/auth @{Basic="true"}
92
+
93
+ = LICENSE:
94
+
95
+ Author:: Seth Chisamore (<schisamo@opscode.com>)
96
+ Copyright:: Copyright (c) 2011 Opscode, Inc.
97
+ License:: Apache License, Version 2.0
98
+
99
+ Licensed under the Apache License, Version 2.0 (the "License");
100
+ you may not use this file except in compliance with the License.
101
+ You may obtain a copy of the License at
102
+
103
+ http://www.apache.org/licenses/LICENSE-2.0
104
+
105
+ Unless required by applicable law or agreed to in writing, software
106
+ distributed under the License is distributed on an "AS IS" BASIS,
107
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
108
+ See the License for the specific language governing permissions and
109
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "knife-windows/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "knife-windows"
7
+ s.version = Knife::Windows::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.has_rdoc = true
10
+ s.extra_rdoc_files = ["README.rdoc", "LICENSE" ]
11
+ s.authors = ["Seth Chisamore"]
12
+ s.email = ["schisamo@opscode.com"]
13
+ s.homepage = "https://github.com/opscode/knife-windows"
14
+ s.summary = %q{Plugin that adds functionality to Chef's Knife CLI for configuring/interacting with nodes running Microsoft Windows}
15
+ s.description = s.summary
16
+
17
+ s.required_ruby_version = '>= 1.9.1'
18
+ s.add_dependency "chef", ">= 0.9.12"
19
+ s.add_dependency "em-winrm", ">= 0.0.2"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,91 @@
1
+ @rem
2
+ @rem Author:: Seth Chisamore (<schisamo@opscode.com>)
3
+ @rem Copyright:: Copyright (c) 2011 Opscode, Inc.
4
+ @rem License:: Apache License, Version 2.0
5
+ @rem
6
+ @rem Licensed under the Apache License, Version 2.0 (the "License");
7
+ @rem you may not use this file except in compliance with the License.
8
+ @rem You may obtain a copy of the License at
9
+ @rem
10
+ @rem http://www.apache.org/licenses/LICENSE-2.0
11
+ @rem
12
+ @rem Unless required by applicable law or agreed to in writing, software
13
+ @rem distributed under the License is distributed on an "AS IS" BASIS,
14
+ @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ @rem See the License for the specific language governing permissions and
16
+ @rem limitations under the License.
17
+ @rem
18
+
19
+ mkdir C:\chef
20
+
21
+ > C:\chef\wget.vbs (
22
+ echo.strFileUrl = WScript.Arguments.Item^(0^)
23
+ echo.strHDLocation = WScript.Arguments.Item^(1^)
24
+ echo.Set objXMLHTTP = CreateObject^("MSXML2.XMLHTTP"^)
25
+ echo.objXMLHTTP.open "GET", strFileURL, false
26
+ echo.objXMLHTTP.send^(^)
27
+ echo.If objXMLHTTP.Status = 200 Then
28
+ echo.Set objADOStream = CreateObject^("ADODB.Stream"^)
29
+ echo.objADOStream.Open
30
+ echo.objADOStream.Type = 1
31
+ echo.objADOStream.Write objXMLHTTP.ResponseBody
32
+ echo.objADOStream.Position = 0
33
+ echo.Set objFSO = Createobject^("Scripting.FileSystemObject"^)
34
+ echo.If objFSO.Fileexists^(strHDLocation^) Then objFSO.DeleteFile strHDLocation
35
+ echo.Set objFSO = Nothing
36
+ echo.objADOStream.SaveToFile strHDLocation
37
+ echo.objADOStream.Close
38
+ echo.Set objADOStream = Nothing
39
+ echo.End if
40
+ echo.Set objXMLHTTP = Nothing
41
+ )
42
+
43
+ cscript /nologo C:\chef\wget.vbs http://files.rubyforge.vm.bytemark.co.uk/rubyinstaller/rubyinstaller-1.8.7-p334.exe %TEMP%\rubyinstaller.exe
44
+ %TEMP%\rubyinstaller.exe /verysilent /dir="C:\ruby" /tasks="assocfiles,modpath"
45
+ cmd.exe /C C:\ruby\bin\gem install win32-open3 ruby-wmi windows-api windows-pr --no-rdoc --no-ri --verbose
46
+
47
+ @rem Install Chef gems separately to prevent 'failed to allocate memory' errors
48
+ cmd.exe /C C:\ruby\bin\gem install ohai --no-rdoc --no-ri --verbose <%= '--prerelease' if @config[:prerelease] %>
49
+ cmd.exe /C C:\ruby\bin\gem install chef --no-rdoc --no-ri --verbose <%= '--prerelease' if @config[:prerelease] %>
50
+
51
+ > C:\chef\validation.pem (
52
+ <% IO.read(Chef::Config[:validation_key]).each_line do |line| -%>
53
+ <%= "echo.#{line.strip}" %>
54
+ <% end -%>
55
+ echo.
56
+ )
57
+
58
+ > C:\chef\client.rb (
59
+ echo.require 'win32ole'
60
+ echo.WIN32OLE.codepage = WIN32OLE::CP_UTF8
61
+ echo.log_level :info
62
+ echo.log_location STDOUT
63
+ echo.chef_server_url "<%= Chef::Config[:chef_server_url] %>"
64
+ echo.validation_client_name "<%= Chef::Config[:validation_client_name] %>"
65
+ echo.client_key "c:/chef/client.pem"
66
+ echo.validation_key "c:/chef/validation.pem"
67
+ echo.checksum_path "c:/chef/checksums"
68
+ echo.file_cache_path "c:/chef/cache"
69
+ echo.file_backup_path "c:/chef/backup"
70
+ echo.cache_options ^( {:path, "c:/chef/cache/checksums", :skip_expires, true} ^)
71
+ <% if Chef::Config[:http_proxy] != nil %>
72
+ echo.http_proxy "<%= Chef::Config[:http_proxy] %>"
73
+ <% end %>
74
+ <% if Chef::Config[:https_proxy] != nil %>
75
+ echo.https_proxy "<%= Chef::Config[:https_proxy] %>"
76
+ <% end %>
77
+ <% if Chef::Config[:no_proxy] != nil %>
78
+ echo.no_proxy "<%= Chef::Config[:no_proxy] %>"
79
+ <% end %>
80
+ <% if @config[:chef_node_name] == nil %>
81
+ echo.# Using default node name
82
+ <% else %>
83
+ echo.node_name "<%= @config[:chef_node_name] %>"
84
+ <% end %>
85
+ )
86
+
87
+ > C:\chef\first-boot.json (
88
+ echo.<%= { "run_list" => @run_list }.to_json %>
89
+ )
90
+
91
+ c:/ruby/bin/ruby c:/ruby/bin/chef-client -c c:/chef/client.rb -j c:/chef/first-boot.json -l <%= Chef::Config[:log_level] %>
@@ -0,0 +1,265 @@
1
+ #
2
+ # Author:: Seth Chisamore (<schisamo@opscode.com>)
3
+ # Copyright:: Copyright (c) 2011 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/knife'
20
+ require 'chef/data_bag_item'
21
+
22
+ begin
23
+ gem "em-winrm"
24
+ rescue LoadError
25
+ end
26
+
27
+ class Chef
28
+ class Knife
29
+ class Winrm < Knife
30
+
31
+ attr_writer :password
32
+
33
+ banner "knife winrm QUERY COMMAND (options)"
34
+
35
+ option :attribute,
36
+ :short => "-a ATTR",
37
+ :long => "--attribute ATTR",
38
+ :description => "The attribute to use for opening the connection - default is fqdn",
39
+ :default => "fqdn"
40
+
41
+ option :manual,
42
+ :short => "-m",
43
+ :long => "--manual-list",
44
+ :boolean => true,
45
+ :description => "QUERY is a space separated list of servers",
46
+ :default => false
47
+
48
+ option :winrm_user,
49
+ :short => "-x USERNAME",
50
+ :long => "--winrm-user USERNAME",
51
+ :description => "The WinRM username"
52
+
53
+ option :winrm_password,
54
+ :short => "-P PASSWORD",
55
+ :long => "--winrm-password PASSWORD",
56
+ :description => "The WinRM password"
57
+
58
+ option :winrm_port,
59
+ :short => "-p PORT",
60
+ :long => "--winrm-port PORT",
61
+ :description => "The WinRM port",
62
+ :default => "80",
63
+ :proc => Proc.new { |key| Chef::Config[:knife][:winrm_port] = key }
64
+
65
+ option :winrm_transport,
66
+ :short => "-t TRANSPORT",
67
+ :long => "--winrm-transport TRANSPORT",
68
+ :description => "The WinRM transport type: ssl, or plaintext",
69
+ :default => 'plaintext',
70
+ :proc => Proc.new { |transport| Chef::Config[:knife][:winrm_transport] = transport }
71
+
72
+ option :kerberos_keytab_file,
73
+ :short => "-i KEYTAB_FILE",
74
+ :long => "--keytab-file KEYTAB_FILE",
75
+ :description => "The Kerberos keytab file used for authentication",
76
+ :proc => Proc.new { |keytab| Chef::Config[:knife][:kerberos_keytab_file] = keytab }
77
+
78
+ option :kerberos_realm,
79
+ :short => "-R KERBEROS_REALM",
80
+ :long => "--kerberos-realm KERBEROS_REALM",
81
+ :description => "The Kerberos realm used for authentication",
82
+ :proc => Proc.new { |realm| Chef::Config[:knife][:kerberos_realm] = realm }
83
+
84
+ option :kerberos_service,
85
+ :short => "-S KERBEROS_SERVICE",
86
+ :long => "--kerberos-service KERBEROS_SERVICE",
87
+ :description => "The Kerberos service used for authentication",
88
+ :proc => Proc.new { |service| Chef::Config[:knife][:kerberos_service] = service }
89
+
90
+ option :keytab_file,
91
+ :short => "-i KEYTAB_FILE",
92
+ :long => "--keytab-file KEYTAB_FILE",
93
+ :description => "The Kerberos keytab file used for authentication",
94
+ :proc => Proc.new { |keytab| Chef::Config[:knife][:keytab_file] = keytab }
95
+
96
+ option :ca_trust_file,
97
+ :short => "-f CA_TRUST_FILE",
98
+ :long => "--ca-trust-file CA_TRUST_FILE",
99
+ :description => "The Certificate Authority (CA) trust file used for SSL transport",
100
+ :proc => Proc.new { |trust| Chef::Config[:knife][:ca_trust_file] = trust }
101
+
102
+ def session
103
+ session_opts = {}
104
+ session_opts[:logger] = Chef::Log.logger if Chef::Log.level == :debug
105
+ @session ||= EventMachine::WinRM::Session.new(session_opts)
106
+ end
107
+
108
+ def h
109
+ @highline ||= HighLine.new
110
+ end
111
+
112
+ def configure_session
113
+ list = case config[:manual]
114
+ when true
115
+ @name_args[0].split(" ")
116
+ when false
117
+ r = Array.new
118
+ q = Chef::Search::Query.new
119
+ @action_nodes = q.search(:node, @name_args[0])[0]
120
+ @action_nodes.each do |item|
121
+ i = format_for_display(item)[config[:attribute]]
122
+ r.push(i) unless i.nil?
123
+ end
124
+ r
125
+ end
126
+ (Chef::Log.fatal("No nodes returned from search!"); exit 10) if list.length == 0
127
+ session_from_list(list)
128
+ end
129
+
130
+ def session_from_list(list)
131
+ list.each do |item|
132
+ Chef::Log.debug("Adding #{item}")
133
+
134
+ session_opts = {}
135
+ session_opts[:user] = config[:winrm_user] if config[:winrm_user]
136
+ session_opts[:password] = config[:winrm_password] if config[:winrm_password]
137
+ session_opts[:port] = config[:winrm_port]
138
+ session_opts[:keytab] = config[:kerberos_keytab_file] if config[:kerberos_keytab_file]
139
+ session_opts[:realm] = config[:kerberos_realm] if config[:kerberos_realm]
140
+ session_opts[:service] = config[:kerberos_service] if config[:kerberos_service]
141
+ session_opts[:ca_trust_path] = config[:ca_trust_file] if config[:ca_trust_file]
142
+
143
+ if config.keys.any? {|k| k.to_s =~ /kerberos/ }
144
+ session_opts[:transport] = :kerberos
145
+ else
146
+ session_opts[:transport] = config[:winrm_transport].to_sym
147
+ end
148
+
149
+ session.use(item, session_opts)
150
+
151
+ @longest = item.length if item.length > @longest
152
+ end
153
+ session
154
+ end
155
+
156
+ def print_data(host, data, color = :cyan)
157
+ if data =~ /\n/
158
+ data.split(/\n/).each { |d| print_data(host, d, color) }
159
+ else
160
+ padding = @longest - host.length
161
+ print h.color(host, color)
162
+ padding.downto(0) { print " " }
163
+ puts data.chomp
164
+ end
165
+ end
166
+
167
+ def winrm_command(command, subsession=nil)
168
+ subsession ||= session
169
+
170
+ subsession.on_output do |host, data|
171
+ print_data(host, data)
172
+ end
173
+ subsession.on_error do |host, err|
174
+ print_data(host, err, :red)
175
+ end
176
+ subsession.on_finish do |host|
177
+ host = host == :done ? 'All Servers' : host
178
+ Chef::Log.debug("command complete on #{host}")
179
+ end
180
+ subsession.relay_command(command)
181
+ end
182
+
183
+ def get_password
184
+ @password ||= h.ask("Enter your password: ") { |q| q.echo = false }
185
+ end
186
+
187
+ # Present the prompt and read a single line from the console. It also
188
+ # detects ^D and returns "exit" in that case. Adds the input to the
189
+ # history, unless the input is empty. Loops repeatedly until a non-empty
190
+ # line is input.
191
+ def read_line
192
+ loop do
193
+ command = reader.readline("#{h.color('knife-winrm>', :bold)} ", true)
194
+
195
+ if command.nil?
196
+ command = "exit"
197
+ puts(command)
198
+ else
199
+ command.strip!
200
+ end
201
+
202
+ unless command.empty?
203
+ return command
204
+ end
205
+ end
206
+ end
207
+
208
+ def reader
209
+ Readline
210
+ end
211
+
212
+ def interactive
213
+ puts "Connected to #{h.list(session.servers_for.collect { |s| h.color(s.host, :cyan) }, :inline, " and ")}"
214
+ puts
215
+ puts "To run a command on a list of servers, do:"
216
+ puts " on SERVER1 SERVER2 SERVER3; COMMAND"
217
+ puts " Example: on latte foamy; echo foobar"
218
+ puts
219
+ puts "To exit interactive mode, use 'quit!'"
220
+ puts
221
+ while 1
222
+ command = read_line
223
+ case command
224
+ when 'quit!'
225
+ puts 'Bye!'
226
+ break
227
+ when /^on (.+?); (.+)$/
228
+ raw_list = $1.split(" ")
229
+ server_list = Array.new
230
+ session.servers.each do |session_server|
231
+ server_list << session_server if raw_list.include?(session_server.host)
232
+ end
233
+ command = $2
234
+ winrm_command(command, session.on(*server_list))
235
+ else
236
+ winrm_command(command)
237
+ end
238
+ end
239
+ end
240
+
241
+ def run
242
+ @longest = 0
243
+ load_late_dependencies
244
+
245
+ configure_session
246
+
247
+ case @name_args[1]
248
+ when "interactive"
249
+ interactive
250
+ else
251
+ winrm_command(@name_args[1..-1].join(" "))
252
+ end
253
+ end
254
+
255
+ def load_late_dependencies
256
+ require 'readline'
257
+ %w[em-winrm highline].each do |dep|
258
+ load_late_dependency dep
259
+ end
260
+ end
261
+
262
+ end
263
+ end
264
+ end
265
+
@@ -0,0 +1,195 @@
1
+ #
2
+ # Author:: Seth Chisamore (<schisamo@opscode.com>)
3
+ # Copyright:: Copyright (c) 2011 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/knife/bootstrap'
20
+
21
+ class Chef
22
+ class Knife
23
+ class WinrmBootstrap < Chef::Knife::Bootstrap
24
+
25
+ banner "knife winrm bootstrap FQDN [RUN LIST...] (options)"
26
+
27
+ option :winrm_user,
28
+ :short => "-x USERNAME",
29
+ :long => "--winrm-user USERNAME",
30
+ :description => "The WinRM username"
31
+
32
+ option :winrm_password,
33
+ :short => "-P PASSWORD",
34
+ :long => "--winrm-password PASSWORD",
35
+ :description => "The WinRM password"
36
+
37
+ option :winrm_port,
38
+ :short => "-p PORT",
39
+ :long => "--winrm-port PORT",
40
+ :description => "The WinRM port",
41
+ :default => "80",
42
+ :proc => Proc.new { |key| Chef::Config[:knife][:winrm_port] = key }
43
+
44
+ option :winrm_transport,
45
+ :short => "-t TRANSPORT",
46
+ :long => "--winrm-transport TRANSPORT",
47
+ :description => "The WinRM transport type: ssl, or plaintext",
48
+ :default => 'plaintext',
49
+ :proc => Proc.new { |transport| Chef::Config[:knife][:winrm_transport] = transport }
50
+
51
+ option :keytab_file,
52
+ :short => "-i KEYTAB_FILE",
53
+ :long => "--keytab-file KEYTAB_FILE",
54
+ :description => "The Kerberos keytab file used for authentication",
55
+ :proc => Proc.new { |keytab| Chef::Config[:knife][:keytab_file] = keytab }
56
+
57
+ option :kerberos_realm,
58
+ :short => "-R KERBEROS_REALM",
59
+ :long => "--kerberos-realm KERBEROS_REALM",
60
+ :description => "The Kerberos realm used for authentication",
61
+ :proc => Proc.new { |realm| Chef::Config[:knife][:kerberos_realm] = realm }
62
+
63
+ option :kerberos_service,
64
+ :short => "-S KERBEROS_SERVICE",
65
+ :long => "--kerberos-service KERBEROS_SERVICE",
66
+ :description => "The Kerberos service used for authentication",
67
+ :proc => Proc.new { |service| Chef::Config[:knife][:kerberos_service] = service }
68
+
69
+ option :ca_trust_file,
70
+ :short => "-f CA_TRUST_FILE",
71
+ :long => "--ca-trust-file CA_TRUST_FILE",
72
+ :description => "The Certificate Authority (CA) trust file used for SSL transport",
73
+ :proc => Proc.new { |trust| Chef::Config[:knife][:ca_trust_file] = trust }
74
+
75
+ option :chef_node_name,
76
+ :short => "-N NAME",
77
+ :long => "--node-name NAME",
78
+ :description => "The Chef node name for your new node"
79
+
80
+ option :prerelease,
81
+ :long => "--prerelease",
82
+ :description => "Install the pre-release chef gems"
83
+
84
+ option :distro,
85
+ :short => "-d DISTRO",
86
+ :long => "--distro DISTRO",
87
+ :description => "Bootstrap a distro using a template",
88
+ :default => "windows-shell"
89
+
90
+ option :template_file,
91
+ :long => "--template-file TEMPLATE",
92
+ :description => "Full path to location of template to use",
93
+ :default => false
94
+
95
+ option :run_list,
96
+ :short => "-r RUN_LIST",
97
+ :long => "--run-list RUN_LIST",
98
+ :description => "Comma separated list of roles/recipes to apply",
99
+ :proc => lambda { |o| o.split(",") },
100
+ :default => []
101
+
102
+ def load_template(template=nil)
103
+ # Are we bootstrapping using an already shipped template?
104
+ if config[:template_file]
105
+ bootstrap_files = config[:template_file]
106
+ else
107
+ bootstrap_files = []
108
+ bootstrap_files << File.join(File.dirname(__FILE__), 'bootstrap', "#{config[:distro]}.erb")
109
+ bootstrap_files << File.join(Dir.pwd, ".chef", "bootstrap", "#{config[:distro]}.erb")
110
+ bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{config[:distro]}.erb")
111
+ bootstrap_files << Gem.find_files(File.join("chef","knife","bootstrap","#{config[:distro]}.erb"))
112
+ end
113
+
114
+ template = Array(bootstrap_files).find do |bootstrap_template|
115
+ Chef::Log.debug("Looking for bootstrap template in #{File.dirname(bootstrap_template)}")
116
+ File.exists?(bootstrap_template)
117
+ end
118
+
119
+ unless template
120
+ Chef::Log.info("Can not find bootstrap definition for #{config[:distro]}")
121
+ raise Errno::ENOENT
122
+ end
123
+
124
+ Chef::Log.debug("Found bootstrap template in #{File.dirname(template)}")
125
+
126
+ IO.read(template).chomp
127
+ end
128
+
129
+ def run
130
+ require 'highline'
131
+
132
+ validate_name_args!
133
+
134
+ $stdout.sync = true
135
+
136
+ Chef::Log.info("Bootstrapping Chef on #{h.color(config[:server_name], :bold)}")
137
+
138
+ knife_winrm.load_late_dependencies
139
+
140
+ # create a bootstrap.bat file on the node
141
+ # we have to run the remote commands in 2047 char chunks
142
+ create_bootstrap_bat_command do |command_chunk, chunk_num|
143
+ knife_winrm("echo \"Rendering bootstrap.bat chunk #{chunk_num}\" && #{command_chunk}").run
144
+ end
145
+
146
+ # execute the bootstrap.bat file
147
+ knife_winrm(bootstrap_command).run
148
+ end
149
+
150
+ def knife_winrm(command = '')
151
+ winrm = Chef::Knife::Winrm.new
152
+ winrm.name_args = [ server_name, command ]
153
+ winrm.config[:winrm_user] = config[:winrm_user]
154
+ winrm.config[:winrm_password] = config[:winrm_password]
155
+ winrm.config[:winrm_transport] = config[:winrm_transport]
156
+ winrm.config[:kerberos_keytab_file] = config[:kerberos_keytab_file] if config[:kerberos_keytab_file]
157
+ winrm.config[:kerberos_realm] = config[:kerberos_realm] if config[:kerberos_realm]
158
+ winrm.config[:kerberos_service] = config[:kerberos_service] if config[:kerberos_service]
159
+ winrm.config[:ca_trust_file] = config[:ca_trust_file] if config[:ca_trust_file]
160
+ winrm.config[:manual] = true
161
+ winrm
162
+ end
163
+
164
+ def bootstrap_command
165
+ @bootstrap_command ||= "cmd /C #{bootstrap_bat_file}"
166
+ end
167
+
168
+ def create_bootstrap_bat_command(&block)
169
+ bootstrap_bat = []
170
+ chunk_num = 0
171
+ render_template(load_template(config[:bootstrap_template])).each_line do |line|
172
+ # escape WIN BATCH special chars
173
+ line.gsub!(/[(<|>)^]/).each{|m| "^#{m}"}
174
+ # windows commands are limited to 2047 characters
175
+ if((bootstrap_bat + [line]).join(" && ").size > 2047 )
176
+ yield bootstrap_bat.join(" && "), chunk_num += 1
177
+ bootstrap_bat = []
178
+ end
179
+ bootstrap_bat << ">> #{bootstrap_bat_file} (echo.#{line.chomp.strip})"
180
+ end
181
+ yield bootstrap_bat.join(" && "), chunk_num += 1
182
+ end
183
+
184
+ def bootstrap_bat_file
185
+ "%TEMP%\\bootstrap.bat"
186
+ end
187
+
188
+ def load_late_dependencies
189
+ super
190
+ require 'chef/knife/winrm'
191
+ end
192
+ end
193
+ end
194
+ end
195
+
@@ -0,0 +1,6 @@
1
+ module Knife
2
+ module Windows
3
+ VERSION = "0.0.1"
4
+ MAJOR, MINOR, TINY = VERSION.split('.')
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-windows
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Seth Chisamore
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-04 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: chef
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.9.12
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: em-winrm
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.0.2
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description: Plugin that adds functionality to Chef's Knife CLI for configuring/interacting with nodes running Microsoft Windows
39
+ email:
40
+ - schisamo@opscode.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files:
46
+ - README.rdoc
47
+ - LICENSE
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.rdoc
53
+ - Rakefile
54
+ - knife-windows.gemspec
55
+ - lib/chef/knife/bootstrap/windows-shell.erb
56
+ - lib/chef/knife/winrm.rb
57
+ - lib/chef/knife/winrm_bootstrap.rb
58
+ - lib/knife-windows/version.rb
59
+ has_rdoc: true
60
+ homepage: https://github.com/opscode/knife-windows
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 1.9.1
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.5.2
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Plugin that adds functionality to Chef's Knife CLI for configuring/interacting with nodes running Microsoft Windows
87
+ test_files: []
88
+