Class: SpecInfra::Command::Windows

Inherits:
Object
  • Object
show all
Defined in:
lib/specinfra/command/windows.rb

Defined Under Namespace

Classes: NotSupportedError

Constant Summary collapse

REGISTRY_KEY_TYPES =
{
  :type_string       => 'String',
  :type_binary       => 'Binary',
  :type_dword        => 'DWord',
  :type_qword        => 'QWord',
  :type_multistring  => 'MultiString',
  :type_expandstring => 'ExpandString'
}

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



14
15
16
17
# File 'lib/specinfra/command/windows.rb', line 14

def method_missing method, *args
  raise NotSupportedError.new "#{method} currently not supported in Windows os" if method.to_s =~ /^check_.+/
  super(method, *args)
end

Instance Method Details

#check_access_by_user(file, user, access) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/specinfra/command/windows.rb', line 69

def check_access_by_user(file, user, access)
  case access
  when 'r'
    check_readable(file, user)
  when 'w'
    check_writable(file, user)
  when 'x'
    check_executable(file, user)
  end
end

#check_belonging_group(user, group) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/specinfra/command/windows.rb', line 173

def check_belonging_group(user, group)
  user_id, user_domain =  user
  group_id, group_domain =  group
  Backend::PowerShell::Command.new do
    using 'find_user.ps1'
    using 'find_group.ps1'
    using 'find_usergroup.ps1'
    exec "(FindUserGroup -userName '#{user_id}'#{user_domain.nil? ? "" : " -userDomain '#{user_domain}'"} -groupName '#{group_id}'#{group_domain.nil? ? "" : " -groupDomain '#{group_domain}'"}) -ne $null"
  end
end

#check_directory(dir) ⇒ Object



47
48
49
50
51
52
# File 'lib/specinfra/command/windows.rb', line 47

def check_directory(dir)
  cmd = item_has_attribute dir, 'Directory'
  Backend::PowerShell::Command.new do
    exec cmd
  end
end

#check_enabled(service, level = nil) ⇒ Object



123
124
125
126
127
128
# File 'lib/specinfra/command/windows.rb', line 123

def check_enabled(service, level=nil)
  Backend::PowerShell::Command.new do
    using 'find_service.ps1'
    exec "(FindService -name '#{service}').StartMode -eq 'Auto'"
  end
end

#check_executable(file, by_whom) ⇒ Object



94
95
96
97
98
99
# File 'lib/specinfra/command/windows.rb', line 94

def check_executable(file, by_whom)
  Backend::PowerShell::Command.new do
    using 'check_file_access_rules.ps1'
    exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'ReadAndExecute', 'ExecuteFile')"
  end
end

#check_file(file) ⇒ Object



19
20
21
22
23
24
# File 'lib/specinfra/command/windows.rb', line 19

def check_file(file)
  cmd = item_has_attribute file, 'Archive'
  Backend::PowerShell::Command.new do
    exec cmd
  end
end

#check_file_contain(file, pattern) ⇒ Object



54
55
56
57
58
# File 'lib/specinfra/command/windows.rb', line 54

def check_file_contain(file, pattern)
  Backend::PowerShell::Command.new do
    exec "[Io.File]::ReadAllText('#{file}') -match '#{convert_regexp(pattern)}'"
  end
end

#check_file_contain_within(file, pattern, from = nil, to = nil) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/specinfra/command/windows.rb', line 60

def check_file_contain_within file, pattern, from=nil, to=nil
  from ||= '^'
  to ||= '$'
  Backend::PowerShell::Command.new do
    using 'crop_text.ps1'
    exec %Q[(CropText -text ([Io.File]::ReadAllText('#{file}')) -fromPattern '#{convert_regexp(from)}' -toPattern '#{convert_regexp(to)}') -match '#{pattern}']
  end
end

#check_file_hidden(file) ⇒ Object



26
27
28
29
30
31
# File 'lib/specinfra/command/windows.rb', line 26

def check_file_hidden(file)
  cmd = item_has_attribute file, 'Hidden'
  Backend::PowerShell::Command.new do
    exec cmd
  end
end

#check_file_readonly(file) ⇒ Object



33
34
35
36
37
38
# File 'lib/specinfra/command/windows.rb', line 33

def check_file_readonly(file)
  cmd = item_has_attribute file, 'ReadOnly'
  Backend::PowerShell::Command.new do
    exec cmd
  end
end

#check_file_system(file) ⇒ Object



40
41
42
43
44
45
# File 'lib/specinfra/command/windows.rb', line 40

def check_file_system(file)
  cmd = item_has_attribute file, 'System'
  Backend::PowerShell::Command.new do
    exec cmd
  end
end

#check_file_version(name, version) ⇒ Object



234
235
236
237
# File 'lib/specinfra/command/windows.rb', line 234

def check_file_version(name,version)
   cmd = "((Get-Command '#{name}').FileVersionInfo.ProductVersion -eq '#{version}') -or ((Get-Command '#{name}').FileVersionInfo.FileVersion -eq '#{version}')"
   Backend::PowerShell::Command.new { exec cmd }
end

#check_group(group) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/specinfra/command/windows.rb', line 165

def check_group(group)
  group_id, domain =  group
  Backend::PowerShell::Command.new do
    using 'find_group.ps1'
    exec "(FindGroup -groupName '#{group_id}'#{domain.nil? ? "" : " -domain '#{domain}'"}) -ne $null"
  end
end

#check_iis_app_pool(name) ⇒ Object



274
275
276
277
278
279
# File 'lib/specinfra/command/windows.rb', line 274

def check_iis_app_pool(name)
    Backend::PowerShell::Command.new do
      using 'find_iis_component.ps1'
      exec "@(FindIISAppPool -name '#{name}').count -gt 0"
    end
end

#check_iis_app_pool_dotnet(name, dotnet) ⇒ Object



281
282
283
284
285
286
# File 'lib/specinfra/command/windows.rb', line 281

def check_iis_app_pool_dotnet(name, dotnet)
    Backend::PowerShell::Command.new do
      using 'find_iis_component.ps1'
      exec "(FindIISAppPool -name '#{name}').managedRuntimeVersion -match 'v#{dotnet}'"
    end
end

#check_iis_website_app_pool(name, app_pool) ⇒ Object



260
261
262
263
264
265
# File 'lib/specinfra/command/windows.rb', line 260

def check_iis_website_app_pool(name, app_pool)
Backend::PowerShell::Command.new do
      using 'find_iis_component.ps1'
      exec "(FindIISWebsite -name '#{name}').applicationPool -match '#{app_pool}'"
    end
end

#check_iis_website_enabled(name) ⇒ Object



239
240
241
242
243
244
# File 'lib/specinfra/command/windows.rb', line 239

def check_iis_website_enabled(name)
   Backend::PowerShell::Command.new do
     using 'find_iis_component.ps1'
     exec "(FindIISWebsite -name '#{name}').serverAutoStart -eq $true"
   end
end

#check_iis_website_installed(name) ⇒ Object



246
247
248
249
250
251
# File 'lib/specinfra/command/windows.rb', line 246

def check_iis_website_installed(name)
   Backend::PowerShell::Command.new do
     using 'find_iis_component.ps1'
     exec "@(FindIISWebsite -name '#{name}').count -gt 0"
   end
end

#check_iis_website_path(name, path) ⇒ Object



267
268
269
270
271
272
# File 'lib/specinfra/command/windows.rb', line 267

def check_iis_website_path(name, path)
    Backend::PowerShell::Command.new do
      using 'find_iis_component.ps1'
      exec "[System.Environment]::ExpandEnvironmentVariables( ( FindIISWebsite -name '#{name}' ).physicalPath ).replace('\\', '/' ) -eq ('#{path}'.trimEnd('/').replace('\\', '/'))"
    end
end

#check_iis_website_running(name) ⇒ Object



253
254
255
256
257
258
# File 'lib/specinfra/command/windows.rb', line 253

def check_iis_website_running(name)
   Backend::PowerShell::Command.new do
     using 'find_iis_component.ps1'
     exec "(FindIISWebsite -name '#{name}').state -eq 'Started'"
   end
end

#check_installed(package, version = nil) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/specinfra/command/windows.rb', line 101

def check_installed(package, version=nil)
  version_selection = version.nil? ? "" : "-appVersion '#{version}'"
  Backend::PowerShell::Command.new do
    using 'find_installed_application.ps1'
    exec "(FindInstalledApplication -appName '#{package}' #{version_selection}) -eq $true"
  end
end

#check_listening(port) ⇒ Object



143
144
145
146
147
148
# File 'lib/specinfra/command/windows.rb', line 143

def check_listening(port)
  Backend::PowerShell::Command.new do
    using 'is_port_listening.ps1'
    exec "IsPortListening -portNumber #{port}"
  end
end

#check_listening_with_protocol(port, protocol) ⇒ Object



150
151
152
153
154
155
# File 'lib/specinfra/command/windows.rb', line 150

def check_listening_with_protocol(port, protocol)
  Backend::PowerShell::Command.new do
    using 'is_port_listening.ps1'
    exec "IsPortListening -portNumber #{port} -protocol '#{protocol}'"
  end
end

#check_process(process) ⇒ Object



137
138
139
140
141
# File 'lib/specinfra/command/windows.rb', line 137

def check_process(process)
  Backend::PowerShell::Command.new do
    exec "(Get-Process '#{process}') -ne $null"
  end
end

#check_reachable(host, port, proto, timeout) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/specinfra/command/windows.rb', line 207

def check_reachable(host, port, proto, timeout)
    if port.nil?
       Backend::PowerShell::Command.new do
        exec "(New-Object System.Net.NetworkInformation.Ping).send('#{host}').Status -eq 'Success'"
       end
    else
      Backend::PowerShell::Command.new do
       using 'is_remote_port_listening.ps1'
       exec"(IsRemotePortListening -hostname #{host} -port #{port} -timeout #{timeout} -proto #{proto}) -eq $true"
      end
    end
end

#check_readable(file, by_whom) ⇒ Object



80
81
82
83
84
85
# File 'lib/specinfra/command/windows.rb', line 80

def check_readable(file, by_whom)
  Backend::PowerShell::Command.new do
    using 'check_file_access_rules.ps1'
    exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'ReadAndExecute', 'Read', 'ListDirectory')"
  end
end

#check_registry_key(key_name, key_property = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/specinfra/command/windows.rb', line 184

def check_registry_key(key_name, key_property = {})
  if key_property.empty?
    cmd = "(Get-Item 'Registry::#{key_name}') -ne $null"
  else
    if key_property.key? :value
      value = convert_key_property_value key_property
      cmd = "(Compare-Object (Get-Item 'Registry::#{key_name}').GetValue('#{key_property[:name]}') #{value}) -eq $null"
    else
      cmd = "(Get-Item 'Registry::#{key_name}').GetValueKind('#{key_property[:name]}') -eq '#{REGISTRY_KEY_TYPES[key_property[:type]]}'"
    end
  end
  Backend::PowerShell::Command.new { exec cmd }
end

#check_resolvable(name, type) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/specinfra/command/windows.rb', line 198

def check_resolvable(name, type)
  if type == "hosts"
      cmd = "@(Select-String -path (Join-Path -Path $($env:windir) -ChildPath 'system32/drivers/etc/hosts') -pattern '#{name}\\b').count -gt 0"
  else
      cmd = "@([System.Net.Dns]::GetHostAddresses('#{name}')).count -gt 0"
  end
  Backend::PowerShell::Command.new { exec cmd }
end

#check_running(service) ⇒ Object



130
131
132
133
134
135
# File 'lib/specinfra/command/windows.rb', line 130

def check_running(service)
  Backend::PowerShell::Command.new do
    using 'find_service.ps1'
    exec "(FindService -name '#{service}').State -eq 'Running'"
  end
end

#check_service_installed(service) ⇒ Object



109
110
111
112
113
114
# File 'lib/specinfra/command/windows.rb', line 109

def check_service_installed(service)
  Backend::PowerShell::Command.new do
    using 'find_service.ps1'
    exec "@(FindService -name '#{service}').count -gt 0"
  end
end

#check_service_start_mode(service, mode) ⇒ Object



116
117
118
119
120
121
# File 'lib/specinfra/command/windows.rb', line 116

def check_service_start_mode(service, mode)
  Backend::PowerShell::Command.new do
    using 'find_service.ps1'
    exec  "'#{mode}' -match (FindService -name '#{service}').StartMode -and (FindService -name '#{service}') -ne $null"
  end
end

#check_user(user) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/specinfra/command/windows.rb', line 157

def check_user(user)
  user_id, domain =  user
  Backend::PowerShell::Command.new do
    using 'find_user.ps1'
    exec "(FindUser -userName '#{user_id}'#{domain.nil? ? "" : " -domain '#{domain}'"}) -ne $null"
  end
end

#check_windows_feature_enabled(name, provider) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/specinfra/command/windows.rb', line 220

def check_windows_feature_enabled(name,provider)

 if provider.nil?
   cmd =  "@(ListWindowsFeatures -feature #{name}).count -gt 0"
 else
   cmd =  "@(ListWindowsFeatures -feature #{name} -provider #{provider}).count -gt 0"
 end

 Backend::PowerShell::Command.new do
   using 'list_windows_features.ps1'
   exec cmd
 end
end

#check_writable(file, by_whom) ⇒ Object



87
88
89
90
91
92
# File 'lib/specinfra/command/windows.rb', line 87

def check_writable(file, by_whom)
  Backend::PowerShell::Command.new do
    using 'check_file_access_rules.ps1'
    exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'Write')"
  end
end