Class: Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/transform.rb

Constant Summary collapse

NO_VALUE =
'no'
ROWKEY =
'RowKey'
SETTING =
'setting'
VALUE =
'value'
APPID =
'AppId'
EMPTY_STR =
''
STORAGEACCOUNT =
'StorageAccount'
STORAGEACCOUNTKEY =
"#{STORAGEACCOUNT}Key"
CONNECTIONSTRING =
'connectionString'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config_filesObject

Returns the value of attribute config_files.



7
8
9
# File 'lib/transform.rb', line 7

def config_files
  @config_files
end

#envObject

Returns the value of attribute env.



3
4
5
# File 'lib/transform.rb', line 3

def env
  @env
end

#settingsObject

Returns the value of attribute settings.



6
7
8
# File 'lib/transform.rb', line 6

def settings
  @settings
end

#svcObject

Returns the value of attribute svc.



4
5
6
# File 'lib/transform.rb', line 4

def svc
  @svc
end

#tableObject

Returns the value of attribute table.



5
6
7
# File 'lib/transform.rb', line 5

def table
  @table
end

Instance Method Details

#get(key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/transform.rb', line 19

def get(key)
    begin
        result = @svc.get_entity(@table, @env, key)
    rescue
        puts $!
        puts "key : #{@key}"
        puts
    end

    result
end

#get_allObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/transform.rb', line 31

def get_all
    begin
        query = { :filter => "PartitionKey eq '#{@env}'" }
        result = @svc.query_entities(@table, query)
    rescue
        puts $!
        puts "Env : #{@env}"
        puts
    end
    
    result    
end

#get_value(key) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/transform.rb', line 44

def get_value(key)
    if (@settings.nil?)
        return
    end
    value = EMPTY_STR
    @settings.each { |i|
        if i.properties[ROWKEY] == key
            value = i.properties[SETTING]
            break
        end
    }

    value
end

#transformObject



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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/transform.rb', line 229

def transform

    # --- Get environment invoked

    @env = ENV['env'] || NO_VALUE
    if @env == NO_VALUE
        puts 'Environment name required to transform. No configuration changes will be done...'
        return false
    else
        puts "Transforming config for environment: #{@env} ..."
    end

    # --- Get Settings Account Name, Key and Table from Environment variables

     = ENV['SettingsAccount'] || ENV[STORAGEACCOUNT] || NO_VALUE
    if ( == NO_VALUE)
        puts "No settings storage account name found"
        return false
    end
    settings_access_key = ENV['SettingsAccountKey'] || ENV[STORAGEACCOUNTKEY] || NO_VALUE
    if (settings_access_key == NO_VALUE)
        puts "No settings storage account key found"
        return false
    end
    config_table = ENV['ConfigSettingsTable'] || NO_VALUE
    if (config_table == NO_VALUE)
        puts "No configuration table found"
    end

    # --- Collect config files to transform

    # find all App.config and web.config files

    @config_files = Dir.glob('**/app.config')
    @config_files.concat(Dir.glob('**/appSettings.config'))
    @config_files.concat(Dir.glob('**/web.config'))
    @config_files.concat(Dir.glob('**/RuntimeWeb/*Web.dll.config'))
    @config_files.concat(Dir.glob('**/RuntimeService/*.exe.config'))

    # --- Load Settings from storage

    # azure table storage account where settings reside

    Azure.config. = 
    Azure.config.storage_access_key = settings_access_key
    @table = config_table

    # table service

    @svc = Azure::TableService.new

    # get all settings for environment

    @settings = get_all

    # --- Start Transformations ---

    puts "updating settings #{CONNECTIONSTRING}..."
    settings_connstr = "DefaultEndpointsProtocol=https;AccountName=#{settings_account_name};AccountKey=#{settings_access_key}"
    should_update_settings_connstr = ENV['should_update_settings_connstr'] || NO_VALUE
    if should_update_settings_connstr == NO_VALUE
        puts "Flag for Setttings #{CONNECTIONSTRING} Update not set."
    else
        transform_appsettings(CONNECTIONSTRING, settings_connstr)
    end

    puts "updating unit test #{CONNECTIONSTRING}..."
    unitest_connstr = ENV["unitest#{CONNECTIONSTRING}"] || NO_VALUE
    if unitest_connstr == NO_VALUE
        puts "No unit test #{CONNECTIONSTRING} found."
    else
        transform_appsettings("unitest#{CONNECTIONSTRING}", unitest_connstr)
    end

    puts "updating #{APPID}..."
    appId = ENV[APPID] || NO_VALUE
    if appId == NO_VALUE
        puts "No #{APPID} found."
    else
        transform_appsettings(APPID, appId)
    end

    @service_name = ENV['ServiceName']
    is_service = @service_name || NO_VALUE
    if is_service != NO_VALUE
        puts "Transforming config for service: #{@service_name}" 
        
        puts 'Obtaining cloud configuration templates...'
        csdefTemplate = get_value('ServiceDefinitionTemplate')
        File.write('ServiceDefinition.csdef', csdefTemplate)
        cscfgTemplate = get_value('ServiceConfigurationTemplate')
        File.write('ServiceConfiguration.cscfg', cscfgTemplate)

        puts 'Transforming csdef...'
        transform_csdef

        puts 'Transforming cscfg...'
        transform_cscfg

        puts 'Transforming diagnostics cfg...'
        transform_diagnosticscfg

        puts 'Replacing service model settings...'
        transform_servicemodelconfig
    else
        puts 'Target to transform is not a service...'
    end
    
    puts 'Replacing app settings...'
    transform_appsettings

    puts 'Removing debug compilation attributes...'
    transform_systemwebcompilationattribs

    puts 'Transforming cache client...'
    transform_cacheclient
    
    puts 'Transforming Application Insights...'
    transform_appinsights
    
    return true
end

#transform_appinsightsObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/transform.rb', line 178

def transform_appinsights
    aiconfig = Dir.glob('**/ApplicationInsights.config')
    aiconfig.each{ |file|
        doc = Nokogiri::XML(File.read(file))

        node = doc.at_css 'InstrumentationKey'
        if (!node.nil?)
            val_from_settings = get_value('AI_InstrumentationKey')
            if (val_from_settings.to_s == EMPTY_STR)
                aikey =  ENV['AI_InstrumentationKey'] || NO_VALUE
            else
                aikey = val_from_settings
            end
            if (aikey != NO_VALUE)
                node.content = aikey
            end
        end
        
        File.write(file, doc.to_xml)
    }
end

#transform_appsettings(key = EMPTY_STR, value = EMPTY_STR) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/transform.rb', line 59

def transform_appsettings(key = EMPTY_STR, value = EMPTY_STR)
    # go to each file and replace value of matching appSettings key

    @config_files.each{|file|
        doc = Nokogiri::XML(File.read(file))
        puts "Processing file: #{file}"
        if (key.to_s != EMPTY_STR && value.to_s != EMPTY_STR)
            k = key
            v = value
            update_appsetting(k, v, doc, file)
        else
            @settings.each { |i|
                k = i.properties[ROWKEY] || NO_VALUE
                v = i.properties[SETTING] || NO_VALUE
                update_appsetting(k, v, doc, file)
            }
        end
    }
end

#transform_cacheclientObject



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/transform.rb', line 215

def transform_cacheclient
    cacheclient_id = get_value('CacheClient_Identifier')
    @config_files.each{ |file|
        doc = Nokogiri::XML(File.read(file))

        node = doc.at_css 'dataCacheClients/dataCacheClient/autoDiscover'
        if (!node.nil?)
            node['identifier'] = cacheclient_id
        end

        File.write(file, doc.to_xml)
    }
end

#transform_cscfgObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/transform.rb', line 157

def transform_cscfg
    csdef = Dir.glob('**/*.cscfg')
    csdef.each{ |file|
        doc = Nokogiri::XML(File.read(file))

        node = doc.at_css 'ServiceConfiguration'
        node['serviceName'] = @service_name if !node.nil?

        node = doc.at_css 'Role'
        node['name'] = @service_name if !node.nil?

        node = doc.at_css 'Certificates'
        node.replace(get_value('Certificates_cscfg')) if !node.nil?

        node = doc.at_css 'ConfigurationSettings'
        node.replace(get_value('ConfigurationSettings_cscfg')) if !node.nil?
        
        File.write(file, doc.to_xml)
    }
end

#transform_csdefObject



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
# File 'lib/transform.rb', line 121

def transform_csdef
    csdef = Dir.glob('**/*.csdef')
    csdef.each{ |file|
        doc = Nokogiri::XML(File.read(file))

        node = doc.at_css 'ServiceDefinition'
        node['name'] = @service_name if !node.nil?

        node = doc.at_css 'WebRole'
        node['name'] = @service_name if !node.nil?

        node = doc.at_css 'WorkerRole'
        node['name'] = @service_name if !node.nil?

        node = doc.at_css 'Certificates'
        node.replace(get_value('Certificates_csdef')) if !node.nil?
        
        node = doc.at_css 'Endpoints'
        node.replace(get_value('Endpoints')) if !node.nil?
        
        node = doc.at_css 'Bindings'
        node.replace(get_value('Bindings')) if !node.nil?
        
        node = doc.at_css 'Sites'
        node.replace(get_value('Sites')) if !node.nil?

        node = doc.at_css 'Imports'
        node.replace(get_value('Imports')) if !node.nil?
        
        node = doc.at_css 'LocalResources'
        node.replace(get_value('LocalResources')) if !node.nil?

        File.write(file, doc.to_xml)
    }
end

#transform_diagnosticscfgObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/transform.rb', line 200

def transform_diagnosticscfg
    csdef = Dir.glob('**/*.wadcfgx')
    csdef.each{ |file|
        doc = Nokogiri::XML(File.read(file))

        node = doc.at_css "PrivateConfig/#{STORAGEACCOUNT}"
        node['name'] = get_value(STORAGEACCOUNT)
        node['key'] = get_value(STORAGEACCOUNTKEY)
        node = doc.at_css STORAGEACCOUNT
        node.content = get_value(STORAGEACCOUNT)
        
        File.write(file, doc.to_xml)
    }
end

#transform_servicemodelconfigObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/transform.rb', line 90

def transform_servicemodelconfig
    @config_files.each{|file|
        doc = Nokogiri::XML(File.read(file))
        doc.xpath('//system.serviceModel').each do |node|
            #puts node

            if !node.nil?
                if file.end_with?('app.config') || file.end_with?('App.config')
                    node.replace(get_value('system.ServiceModel.Client'))
                elsif file.end_with?('Web.config')
                    node.replace(get_value('system.ServiceModel.Service'))
                end
                File.write(file, doc.to_xml)
            end
        end
    } 
end

#transform_systemwebcompilationattribsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/transform.rb', line 107

def transform_systemwebcompilationattribs
    @config_files.each{|file|
        doc = Nokogiri::XML(File.read(file))
        node = doc.at_css 'compilation'
        if !node.nil?
            #puts node

            node.xpath('//@debug').remove
            node.xpath('//@tempDirectory').remove
            #puts node

            File.write(file, doc.to_xml)
        end
    }
end

#update_appsetting(k, v, doc, file) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/transform.rb', line 78

def update_appsetting(k, v, doc, file)
    if (k != NO_VALUE && v != NO_VALUE)
        node = doc.at_css "appSettings/add[@key='#{k}']"
        if !node.nil?
            #puts "Old value: #{node[VALUE]}"

            node[VALUE] = v
            #puts "New value: #{v}"

            File.write(file, doc.to_xml)
        end
    end
end