knife-windows 0.8.6.rc.0 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,99 +1,99 @@
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/encrypted_data_bag_item'
21
- require 'kconv'
22
-
23
- class Chef
24
- class Knife
25
- module WinrmBase
26
-
27
- # :nodoc:
28
- # Would prefer to do this in a rational way, but can't be done b/c of
29
- # Mixlib::CLI's design :(
30
- def self.included(includer)
31
- includer.class_eval do
32
-
33
- deps do
34
- require 'readline'
35
- require 'chef/json_compat'
36
- end
37
-
38
- option :winrm_user,
39
- :short => "-x USERNAME",
40
- :long => "--winrm-user USERNAME",
41
- :description => "The WinRM username",
42
- :default => "Administrator",
43
- :proc => Proc.new { |key| Chef::Config[:knife][:winrm_user] = key }
44
-
45
- option :winrm_password,
46
- :short => "-P PASSWORD",
47
- :long => "--winrm-password PASSWORD",
48
- :description => "The WinRM password",
49
- :proc => Proc.new { |key| Chef::Config[:knife][:winrm_password] = key }
50
-
51
- option :winrm_port,
52
- :short => "-p PORT",
53
- :long => "--winrm-port PORT",
54
- :description => "The WinRM port, by default this is 5985",
55
- :default => "5985",
56
- :proc => Proc.new { |key| Chef::Config[:knife][:winrm_port] = key }
57
-
58
- option :identity_file,
59
- :short => "-i IDENTITY_FILE",
60
- :long => "--identity-file IDENTITY_FILE",
61
- :description => "The SSH identity file used for authentication"
62
-
63
- option :winrm_transport,
64
- :short => "-t TRANSPORT",
65
- :long => "--winrm-transport TRANSPORT",
66
- :description => "The WinRM transport type. valid choices are [ssl, plaintext]",
67
- :default => 'plaintext',
68
- :proc => Proc.new { |transport| Chef::Config[:knife][:winrm_transport] = transport }
69
-
70
- option :kerberos_keytab_file,
71
- :short => "-i KEYTAB_FILE",
72
- :long => "--keytab-file KEYTAB_FILE",
73
- :description => "The Kerberos keytab file used for authentication",
74
- :proc => Proc.new { |keytab| Chef::Config[:knife][:kerberos_keytab_file] = keytab }
75
-
76
- option :kerberos_realm,
77
- :short => "-R KERBEROS_REALM",
78
- :long => "--kerberos-realm KERBEROS_REALM",
79
- :description => "The Kerberos realm used for authentication",
80
- :proc => Proc.new { |realm| Chef::Config[:knife][:kerberos_realm] = realm }
81
-
82
- option :kerberos_service,
83
- :short => "-S KERBEROS_SERVICE",
84
- :long => "--kerberos-service KERBEROS_SERVICE",
85
- :description => "The Kerberos service used for authentication",
86
- :proc => Proc.new { |service| Chef::Config[:knife][:kerberos_service] = service }
87
-
88
- option :ca_trust_file,
89
- :short => "-f CA_TRUST_FILE",
90
- :long => "--ca-trust-file CA_TRUST_FILE",
91
- :description => "The Certificate Authority (CA) trust file used for SSL transport",
92
- :proc => Proc.new { |trust| Chef::Config[:knife][:ca_trust_file] = trust }
93
-
94
- end
95
- end
96
-
97
- end
98
- end
99
- end
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/encrypted_data_bag_item'
21
+ require 'kconv'
22
+
23
+ class Chef
24
+ class Knife
25
+ module WinrmBase
26
+
27
+ # :nodoc:
28
+ # Would prefer to do this in a rational way, but can't be done b/c of
29
+ # Mixlib::CLI's design :(
30
+ def self.included(includer)
31
+ includer.class_eval do
32
+
33
+ deps do
34
+ require 'readline'
35
+ require 'chef/json_compat'
36
+ end
37
+
38
+ option :winrm_user,
39
+ :short => "-x USERNAME",
40
+ :long => "--winrm-user USERNAME",
41
+ :description => "The WinRM username",
42
+ :default => "Administrator",
43
+ :proc => Proc.new { |key| Chef::Config[:knife][:winrm_user] = key }
44
+
45
+ option :winrm_password,
46
+ :short => "-P PASSWORD",
47
+ :long => "--winrm-password PASSWORD",
48
+ :description => "The WinRM password",
49
+ :proc => Proc.new { |key| Chef::Config[:knife][:winrm_password] = key }
50
+
51
+ option :winrm_port,
52
+ :short => "-p PORT",
53
+ :long => "--winrm-port PORT",
54
+ :description => "The WinRM port, by default this is 5985",
55
+ :default => "5985",
56
+ :proc => Proc.new { |key| Chef::Config[:knife][:winrm_port] = key }
57
+
58
+ option :identity_file,
59
+ :short => "-i IDENTITY_FILE",
60
+ :long => "--identity-file IDENTITY_FILE",
61
+ :description => "The SSH identity file used for authentication"
62
+
63
+ option :winrm_transport,
64
+ :short => "-t TRANSPORT",
65
+ :long => "--winrm-transport TRANSPORT",
66
+ :description => "The WinRM transport type. valid choices are [ssl, plaintext]",
67
+ :default => 'plaintext',
68
+ :proc => Proc.new { |transport| Chef::Config[:knife][:winrm_transport] = transport }
69
+
70
+ option :kerberos_keytab_file,
71
+ :short => "-i KEYTAB_FILE",
72
+ :long => "--keytab-file KEYTAB_FILE",
73
+ :description => "The Kerberos keytab file used for authentication",
74
+ :proc => Proc.new { |keytab| Chef::Config[:knife][:kerberos_keytab_file] = keytab }
75
+
76
+ option :kerberos_realm,
77
+ :short => "-R KERBEROS_REALM",
78
+ :long => "--kerberos-realm KERBEROS_REALM",
79
+ :description => "The Kerberos realm used for authentication",
80
+ :proc => Proc.new { |realm| Chef::Config[:knife][:kerberos_realm] = realm }
81
+
82
+ option :kerberos_service,
83
+ :short => "-S KERBEROS_SERVICE",
84
+ :long => "--kerberos-service KERBEROS_SERVICE",
85
+ :description => "The Kerberos service used for authentication",
86
+ :proc => Proc.new { |service| Chef::Config[:knife][:kerberos_service] = service }
87
+
88
+ option :ca_trust_file,
89
+ :short => "-f CA_TRUST_FILE",
90
+ :long => "--ca-trust-file CA_TRUST_FILE",
91
+ :description => "The Certificate Authority (CA) trust file used for SSL transport",
92
+ :proc => Proc.new { |trust| Chef::Config[:knife][:ca_trust_file] = trust }
93
+
94
+ end
95
+ end
96
+
97
+ end
98
+ end
99
+ end
@@ -1,157 +1,157 @@
1
- #
2
- # Author:: Bryan McLellan <btm@loftninjas.org>
3
- # Copyright:: Copyright (c) 2014 Chef Software, 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
- # Sourced from Chef::Util::PathHelper.
20
- # Should be removed when Chef 11 catches up or we stop supporting Chef 11
21
-
22
- module Knife
23
- module Windows
24
- class PathHelper
25
- # Maximum characters in a standard Windows path (260 including drive letter and NUL)
26
- WIN_MAX_PATH = 259
27
-
28
- def self.dirname(path)
29
- if Chef::Platform.windows?
30
- # Find the first slash, not counting trailing slashes
31
- end_slash = path.size
32
- while true
33
- slash = path.rindex(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]/, end_slash - 1)
34
- if !slash
35
- return end_slash == path.size ? '.' : path_separator
36
- elsif slash == end_slash - 1
37
- end_slash = slash
38
- else
39
- return path[0..slash-1]
40
- end
41
- end
42
- else
43
- ::File.dirname(path)
44
- end
45
- end
46
-
47
- BACKSLASH = '\\'.freeze
48
-
49
- def self.path_separator
50
- if Chef::Platform.windows?
51
- File::ALT_SEPARATOR || BACKSLASH
52
- else
53
- File::SEPARATOR
54
- end
55
- end
56
-
57
- def self.join(*args)
58
- args.flatten.inject do |joined_path, component|
59
- # Joined path ends with /
60
- joined_path = joined_path.sub(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]+$/, '')
61
- component = component.sub(/^[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]+/, '')
62
- joined_path += "#{path_separator}#{component}"
63
- end
64
- end
65
-
66
- def self.validate_path(path)
67
- if Chef::Platform.windows?
68
- unless printable?(path)
69
- msg = "Path '#{path}' contains non-printable characters. Check that backslashes are escaped with another backslash (e.g. C:\\\\Windows) in double-quoted strings."
70
- Chef::Log.error(msg)
71
- raise Chef::Exceptions::ValidationFailed, msg
72
- end
73
-
74
- if windows_max_length_exceeded?(path)
75
- Chef::Log.debug("Path '#{path}' is longer than #{WIN_MAX_PATH}, prefixing with'\\\\?\\'")
76
- path.insert(0, "\\\\?\\")
77
- end
78
- end
79
-
80
- path
81
- end
82
-
83
- def self.windows_max_length_exceeded?(path)
84
- # Check to see if paths without the \\?\ prefix are over the maximum allowed length for the Windows API
85
- # http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
86
- unless path =~ /^\\\\?\\/
87
- if path.length > WIN_MAX_PATH
88
- return true
89
- end
90
- end
91
-
92
- false
93
- end
94
-
95
- def self.printable?(string)
96
- # returns true if string is free of non-printable characters (escape sequences)
97
- # this returns false for whitespace escape sequences as well, e.g. \n\t
98
- if string =~ /[^[:print:]]/
99
- false
100
- else
101
- true
102
- end
103
- end
104
-
105
- # Produces a comparable path.
106
- def self.canonical_path(path, add_prefix=true)
107
- # Rather than find an equivalent for File.absolute_path on 1.8.7, just bail out
108
- raise NotImplementedError, "This feature is not supported on Ruby versions < 1.9" if RUBY_VERSION.to_f < 1.9
109
-
110
- # First remove extra separators and resolve any relative paths
111
- abs_path = File.absolute_path(path)
112
-
113
- if Chef::Platform.windows?
114
- # Add the \\?\ API prefix on Windows unless add_prefix is false
115
- # Downcase on Windows where paths are still case-insensitive
116
- abs_path.gsub!(::File::SEPARATOR, path_separator)
117
- if add_prefix && abs_path !~ /^\\\\?\\/
118
- abs_path.insert(0, "\\\\?\\")
119
- end
120
-
121
- abs_path.downcase!
122
- end
123
-
124
- abs_path
125
- end
126
-
127
- def self.cleanpath(path)
128
- path = Pathname.new(path).cleanpath.to_s
129
- # ensure all forward slashes are backslashes
130
- if Chef::Platform.windows?
131
- path = path.gsub(File::SEPARATOR, path_separator)
132
- end
133
- path
134
- end
135
-
136
- def self.paths_eql?(path1, path2)
137
- canonical_path(path1) == canonical_path(path2)
138
- end
139
-
140
- # Paths which may contain glob-reserved characters need
141
- # to be escaped before globbing can be done.
142
- # http://stackoverflow.com/questions/14127343
143
- def self.escape_glob(*parts)
144
- path = cleanpath(join(*parts))
145
- path.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\"+x }
146
- end
147
-
148
- def self.relative_path_from(from, to)
149
- pathname = Pathname.new(Chef::Util::PathHelper.cleanpath(to)).relative_path_from(Pathname.new(Chef::Util::PathHelper.cleanpath(from)))
150
- end
151
- end
152
- end
153
- end
154
-
155
- # Break a require loop when require chef/util/path_helper
156
- require 'chef/platform'
157
- require 'chef/exceptions'
1
+ #
2
+ # Author:: Bryan McLellan <btm@loftninjas.org>
3
+ # Copyright:: Copyright (c) 2014 Chef Software, 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
+ # Sourced from Chef::Util::PathHelper.
20
+ # Should be removed when Chef 11 catches up or we stop supporting Chef 11
21
+
22
+ module Knife
23
+ module Windows
24
+ class PathHelper
25
+ # Maximum characters in a standard Windows path (260 including drive letter and NUL)
26
+ WIN_MAX_PATH = 259
27
+
28
+ def self.dirname(path)
29
+ if Chef::Platform.windows?
30
+ # Find the first slash, not counting trailing slashes
31
+ end_slash = path.size
32
+ while true
33
+ slash = path.rindex(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]/, end_slash - 1)
34
+ if !slash
35
+ return end_slash == path.size ? '.' : path_separator
36
+ elsif slash == end_slash - 1
37
+ end_slash = slash
38
+ else
39
+ return path[0..slash-1]
40
+ end
41
+ end
42
+ else
43
+ ::File.dirname(path)
44
+ end
45
+ end
46
+
47
+ BACKSLASH = '\\'.freeze
48
+
49
+ def self.path_separator
50
+ if Chef::Platform.windows?
51
+ File::ALT_SEPARATOR || BACKSLASH
52
+ else
53
+ File::SEPARATOR
54
+ end
55
+ end
56
+
57
+ def self.join(*args)
58
+ args.flatten.inject do |joined_path, component|
59
+ # Joined path ends with /
60
+ joined_path = joined_path.sub(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]+$/, '')
61
+ component = component.sub(/^[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]+/, '')
62
+ joined_path += "#{path_separator}#{component}"
63
+ end
64
+ end
65
+
66
+ def self.validate_path(path)
67
+ if Chef::Platform.windows?
68
+ unless printable?(path)
69
+ msg = "Path '#{path}' contains non-printable characters. Check that backslashes are escaped with another backslash (e.g. C:\\\\Windows) in double-quoted strings."
70
+ Chef::Log.error(msg)
71
+ raise Chef::Exceptions::ValidationFailed, msg
72
+ end
73
+
74
+ if windows_max_length_exceeded?(path)
75
+ Chef::Log.debug("Path '#{path}' is longer than #{WIN_MAX_PATH}, prefixing with'\\\\?\\'")
76
+ path.insert(0, "\\\\?\\")
77
+ end
78
+ end
79
+
80
+ path
81
+ end
82
+
83
+ def self.windows_max_length_exceeded?(path)
84
+ # Check to see if paths without the \\?\ prefix are over the maximum allowed length for the Windows API
85
+ # http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
86
+ unless path =~ /^\\\\?\\/
87
+ if path.length > WIN_MAX_PATH
88
+ return true
89
+ end
90
+ end
91
+
92
+ false
93
+ end
94
+
95
+ def self.printable?(string)
96
+ # returns true if string is free of non-printable characters (escape sequences)
97
+ # this returns false for whitespace escape sequences as well, e.g. \n\t
98
+ if string =~ /[^[:print:]]/
99
+ false
100
+ else
101
+ true
102
+ end
103
+ end
104
+
105
+ # Produces a comparable path.
106
+ def self.canonical_path(path, add_prefix=true)
107
+ # Rather than find an equivalent for File.absolute_path on 1.8.7, just bail out
108
+ raise NotImplementedError, "This feature is not supported on Ruby versions < 1.9" if RUBY_VERSION.to_f < 1.9
109
+
110
+ # First remove extra separators and resolve any relative paths
111
+ abs_path = File.absolute_path(path)
112
+
113
+ if Chef::Platform.windows?
114
+ # Add the \\?\ API prefix on Windows unless add_prefix is false
115
+ # Downcase on Windows where paths are still case-insensitive
116
+ abs_path.gsub!(::File::SEPARATOR, path_separator)
117
+ if add_prefix && abs_path !~ /^\\\\?\\/
118
+ abs_path.insert(0, "\\\\?\\")
119
+ end
120
+
121
+ abs_path.downcase!
122
+ end
123
+
124
+ abs_path
125
+ end
126
+
127
+ def self.cleanpath(path)
128
+ path = Pathname.new(path).cleanpath.to_s
129
+ # ensure all forward slashes are backslashes
130
+ if Chef::Platform.windows?
131
+ path = path.gsub(File::SEPARATOR, path_separator)
132
+ end
133
+ path
134
+ end
135
+
136
+ def self.paths_eql?(path1, path2)
137
+ canonical_path(path1) == canonical_path(path2)
138
+ end
139
+
140
+ # Paths which may contain glob-reserved characters need
141
+ # to be escaped before globbing can be done.
142
+ # http://stackoverflow.com/questions/14127343
143
+ def self.escape_glob(*parts)
144
+ path = cleanpath(join(*parts))
145
+ path.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\"+x }
146
+ end
147
+
148
+ def self.relative_path_from(from, to)
149
+ pathname = Pathname.new(Chef::Util::PathHelper.cleanpath(to)).relative_path_from(Pathname.new(Chef::Util::PathHelper.cleanpath(from)))
150
+ end
151
+ end
152
+ end
153
+ end
154
+
155
+ # Break a require loop when require chef/util/path_helper
156
+ require 'chef/platform'
157
+ require 'chef/exceptions'