/
features.rb
281 lines (241 loc) · 10.5 KB
/
features.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
SQL_2012 ||= 'SQL_2012'
SQL_2014 ||= 'SQL_2014'
SQL_2016 ||= 'SQL_2016'
ALL_SQL_VERSIONS = [SQL_2012, SQL_2014, SQL_2016]
module PuppetX
module Sqlserver
# https://msdn.microsoft.com/en-us/library/ms143786.aspx basic feature docs
class Features
private
SQL_CONFIGURATION ||= {
SQL_2012 => {
:major_version => 11,
:wmi_path => 'ComputerManagement11',
:registry_path => '110',
},
SQL_2014 => {
:major_version => 12,
:wmi_path => 'ComputerManagement12',
:registry_path => '120',
},
SQL_2016 => {
:major_version => 13,
:wmi_path => 'ComputerManagement13',
:registry_path => '130',
}
}
SQL_REG_ROOT ||= 'Software\Microsoft\Microsoft SQL Server'
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v=vs.85).aspx
KEY_WOW64_64KEY ||= 0x100
KEY_READ ||= 0x20019
def self.connect(version)
require 'win32ole'
ver = SQL_CONFIGURATION[version][:wmi_path]
context = WIN32OLE.new('WbemScripting.SWbemNamedValueSet')
context.Add("__ProviderArchitecture", 64)
locator = WIN32OLE.new('WbemScripting.SWbemLocator')
# WMI Path must use backslashes. Ruby 2.3 can cause crashes with forward slashes
locator.ConnectServer(nil, "root\\Microsoft\\SqlServer\\#{ver}", nil, nil, nil, nil, nil, context)
end
def self.get_parent_path(key_path)
# should be the same as SQL_REG_ROOT
key_path.slice(0, key_path.rindex('\\'))
end
def self.get_reg_key_val(win32_reg_key, val_name, reg_type)
win32_reg_key[val_name, reg_type]
rescue
nil
end
def self.get_sql_reg_val_features(key_name, reg_val_feat_hash)
require 'win32/registry'
vals = []
begin
hklm = Win32::Registry::HKEY_LOCAL_MACHINE
vals = hklm.open(key_name, KEY_READ | KEY_WOW64_64KEY) do |key|
reg_val_feat_hash
.select { |val_name, _| get_reg_key_val(key, val_name, Win32::Registry::REG_DWORD) == 1 }
.map { |_, feat_name| feat_name }
end
rescue Win32::Registry::Error # subkey doesn't exist
end
vals
end
def self.get_sql_reg_key_features(key_name, reg_key_feat_hash, instance_name)
require 'win32/registry'
installed = reg_key_feat_hash.select do |subkey, feat_name|
begin
hklm = Win32::Registry::HKEY_LOCAL_MACHINE
hklm.open("#{key_name}\\#{subkey}", KEY_READ | KEY_WOW64_64KEY) do |feat_key|
get_reg_key_val(feat_key, instance_name, Win32::Registry::REG_SZ)
end
rescue Win32::Registry::Error # subkey doesn't exist
end
end
installed.values
end
def self.get_wmi_property_values(wmi, query, prop_name = 'PropertyStrValue')
vals = []
wmi.ExecQuery(query).each do |v|
vals.push(v.Properties_(prop_name).Value)
end
vals
end
def self.get_instance_names_by_ver(version)
query = 'SELECT InstanceName FROM ServerSettings'
get_wmi_property_values(connect(version), query, 'InstanceName')
rescue WIN32OLERuntimeError => e # version doesn't exist
# WBEM_E_INVALID_NAMESPACE from wbemcli.h
return [] if e.message =~ /8004100e/im
raise
end
def self.get_sql_property_values(version, instance_name, property_name)
query = <<-END
SELECT * FROM SqlServiceAdvancedProperty
WHERE PropertyName='#{property_name}'
AND SqlServiceType=1 AND ServiceName LIKE '%#{instance_name}'
END
# WMI LIKE query to substring match since ServiceName will be of the format
# MSSQLSERVER (first install) or MSSQL$MSSQLSERVER (second install)
get_wmi_property_values(connect(version), query)
end
def self.get_wmi_instance_info(version, instance_name)
{
'name' => instance_name,
'version_friendly' => version,
'version' => get_sql_property_values(version, instance_name, 'VERSION').first,
# typically Software\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER
'reg_root' => get_sql_property_values(version, instance_name, 'REGROOT').first,
}
end
def self.get_instance_features(reg_root, instance_name)
instance_features = {
# also reg Replication/IsInstalled set to 1
'SQL_Replication_Core_Inst' => 'Replication', # SQL Server Replication
# also WMI: SqlService WHERE SQLServiceType = 1 # MSSQLSERVER
'SQL_Engine_Core_Inst' => 'SQLEngine', # Database Engine Services
'SQL_FullText_Adv' => 'FullText', # Full-Text and Semantic Extractions for Search
'SQL_DQ_Full' => 'DQ', # Data Quality Services
'sql_inst_mr' => 'ADVANCEDANALYTICS', # R Services (In-Database)
'SQL_Polybase_Core_Inst' => 'POLYBASE', # PolyBase Query Service for External Data
}
feat_root = "#{reg_root}\\ConfigurationState"
features = get_sql_reg_val_features(feat_root, instance_features)
# https://msdn.microsoft.com/en-us/library/ms179591.aspx
# WMI equivalents require trickier name parsing
parent_subkey_features = {
# also WMI: SqlService WHERE SQLServiceType = 5 # MSSQLServerOLAPService
'OLAP' => 'AS', # Analysis Services,
# also WMI: SqlService WHERE SQLServiceType = 6 # ReportServer
'RS' => 'RS' # Reporting Services - Native
}
# instance features found in non-parented reg keys
feat_root = "#{get_parent_path(reg_root)}\\Instance Names"
parent_features = get_sql_reg_key_features(feat_root, parent_subkey_features, instance_name)
features + parent_features
end
def self.get_shared_features(version)
shared_features = {
'Connectivity_Full' => 'Conn', # Client Tools Connectivity
'SDK_Full' => 'SDK', # Client Tools SDK
'MDSCoreFeature' => 'MDS', # Master Data Services
'Tools_Legacy_Full' => 'BC', # Client Tools Backwards Compatibility
'SQL_SSMS_Full' => 'ADV_SSMS', # Management Tools - Complete (Does not exist in SQL 2016)
'SQL_SSMS_Adv' => 'SSMS', # Management Tools - Basic (Does not exist in SQL 2016)
'SQL_DQ_CLIENT_Full' => 'DQC', # Data Quality Client
'SQL_BOL_Components' => 'BOL', # Documentation Components
'SQL_DReplay_Controller' => 'DREPLAY_CTLR', # Distributed Replay Controller
'SQL_DReplay_Client' => 'DREPLAY_CLT', # Distributed Replay Client
'sql_shared_mr' => 'SQL_SHARED_MR', # R Server (Standalone)
# also WMI: SqlService WHERE SQLServiceType = 4 # MsDtsServer
'SQL_DTS_Full' => 'IS', # Integration Services
# currently ignoring Reporting Services Shared
# currently ignoring R Server Standalone
}
reg_ver = SQL_CONFIGURATION[version][:registry_path]
reg_root = "#{SQL_REG_ROOT}\\#{reg_ver}\\ConfigurationState"
get_sql_reg_val_features(reg_root, shared_features)
end
public
# return a hash of version => instance info
#
# {
# "SQL_2012" => {},
# "SQL_2014" => {
# "MSSQLSERVER" => {
# "name" => "MSSQLSERVER",
# "version_friendly" => "SQL_2014",
# "version" => "12.0.2000.8",
# "reg_root" => "Software\\Microsoft\\Microsoft SQL Server\\MSSQL12.MSSQLSERVER",
# "features" => [
# "Replication",
# "SQLEngine",
# "FullText",
# "DQ",
# "AS",
# "RS"
# ]
# }
# }
# }
def self.get_instances
version_instance_map = ALL_SQL_VERSIONS
.map do |version|
major_version = SQL_CONFIGURATION[version][:major_version]
instances = get_instance_names_by_ver(version)
.map { |name| [ name, get_instance_info(version, name) ] }
# Instance names are unique on a single host, but not for a particular SQL Server version therefore
# it's possible to request information for a valid instance_name but not for version. In this case
# we just reject any instances that have no information
instances.reject! { |value| value[1].nil? }
# Unfortunately later SQL versions can return previous version SQL instances. We can weed these out
# by inspecting the major version of the instance_version
instances.reject! do |value|
return true if value[1]['version'].nil?
ver = Gem::Version.new(value[1]['version'])
# Segment 0 is the major version number of the SQL Instance
ver.segments[0] != major_version
end
[ version, Hash[instances] ]
end
Hash[version_instance_map]
end
# return a hash of version => shared features array
#
# {
# "SQL_2012" => ["Conn", "SDK", "MDS", "BC", "SSMS", "ADV_SSMS", "IS"],
# "SQL_2014" => []
# }
def self.get_features
features = {}
ALL_SQL_VERSIONS.each { |version| features[version] = get_shared_features(version) }
features
end
# returns a hash containing instance details
#
# {
# "name" => "MSSQLSERVER2",
# "version_friendly" => "SQL_2014",
# "version" => "12.0.2000.8",
# "reg_root" => "Software\\Microsoft\\Microsoft SQL Server\\MSSQL12.MSSQLSERVER2",
# "features" =>[
# "Replication",
# "SQLEngine",
# "FullText",
# "DQ",
# "AS",
# "RS"
# ]
# }
def self.get_instance_info(version, instance_name)
return nil if version.nil?
sql_instance = get_wmi_instance_info(version, instance_name)
# Instance names are unique on a single host, but not for a particular SQL Server version therefore
# it's possible to request information for a valid instance_name but not for version. In this case
# we just return nil.
return nil if sql_instance['reg_root'].nil?
feats = get_instance_features(sql_instance['reg_root'], sql_instance['name'])
sql_instance.merge({'features' => feats})
end
end
end
end