Class: Transform

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

Constant Summary collapse

NO_VALUE =
'no'
ROWKEY =
'RowKey'
SETTING =
'setting'
VALUE =
'value'
APPCLIENTID =
'AppClientId'
APPIDURI =
'AppIdUri'
NEWRELIC_APPNAME =
'NewRelic.AppName'
SERVICENAME =
'ServiceName'
OLDAPPID =
'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

#debug_modeObject

Returns the value of attribute debug_mode.



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

def debug_mode
  @debug_mode
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



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/transform.rb', line 24

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

  result
end

#get_allObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/transform.rb', line 36

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

  result
end

#get_value(key) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/transform.rb', line 49

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

#remove_paths_from_list(paths_to_exclude, source_list) ⇒ Object



384
385
386
387
388
389
390
# File 'lib/transform.rb', line 384

def remove_paths_from_list paths_to_exclude, source_list
  paths_to_exclude.each { |path|
    source_list.delete_if { |file|
      file.include? path
    }
  }
end

#transform(paths_to_exclude = []) ⇒ Object

paths_to_exclude is array of partial or full paths of projects where transform needs skipped



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/transform.rb', line 393

def transform paths_to_exclude = []

  @debug_mode = ENV['transform_debug_mode']

  # --- 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')

  # remove projects which need not be transformed
  remove_paths_from_list paths_to_exclude, @config_files

  # --- 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=#{};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 #{APPCLIENTID}..."
  appClientId = ENV[APPCLIENTID] || NO_VALUE
  if appClientId == NO_VALUE
    puts "No #{APPCLIENTID} found."
    # old version notify check
    oldAppId = ENV[OLDAPPID]
    if oldAppId != NO_VALUE
      puts "You appear to be using the old AppId environment variable, AppClientId is expected. Proceeding with old AppId update."
      transform_appsettings OLDAPPID, oldAppId
    end
  else
    transform_appsettings APPCLIENTID, appClientId
  end

  puts "updating #{APPIDURI}..."
  appIdUri = ENV[APPIDURI] || NO_VALUE
  if appIdUri == NO_VALUE
    puts "No #{APPIDURI} found."
  else
    transform_appsettings APPIDURI, appIdUri
  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...'
    xml_node_processing = nil
    use_riverbed = ENV['UseRiverbed'] || NO_VALUE
    if use_riverbed == "true"
      puts 'Using riverbed...'
      xml_node_processing = method(:transform_riverbed)
    end
    use_newrelic = ENV['UseNewrelic'] || NO_VALUE
    if use_newrelic == "true"
      puts 'Using new relic...'
      transform_appsettings NEWRELIC_APPNAME, @service_name
      xml_node_processing = method(:transform_newrelic)
    end
    use_appdynamics = ENV['UseAppdynamics'] || NO_VALUE
    if use_appdynamics == "true"
      puts 'Using app dynamics...'
      xml_node_processing = method(:transform_appdynamics)
    end

    if !((use_riverbed == "true" and use_newrelic != "true" and use_appdynamics != "true") or
      (use_riverbed != "true" and use_newrelic == "true" and use_appdynamics != "true") or
      (use_riverbed != "true" and use_newrelic != "true" and use_appdynamics == "true") or
      (use_riverbed != "true" and use_newrelic != "true" and use_appdynamics != "true"))
      puts 'Only one APM tool can be used at one time, aborting.'
      return false
    end


    transform_csdef xml_node_processing

    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 paths_to_exclude

  puts 'Transforming log4net config...'
  transform_log4net

  return true
end

#transform_appdynamics(doc) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/transform.rb', line 249

def transform_appdynamics doc
  @service_name = ENV[SERVICENAME]
  is_service = @service_name || NO_VALUE
  if is_service != NO_VALUE
    node = doc.at_css 'WebRole'
    transform_appdynamics_node(doc, node) if !node.nil?
    node = doc.at_css 'WorkerRole'
    transform_appdynamics_node(doc, node) if !node.nil?
  else
    puts 'Not a service, avoiding transforming the csdef.'
  end
end

#transform_appdynamics_node(doc, xml_node) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/transform.rb', line 262

def transform_appdynamics_node doc, xml_node
  task_node = Nokogiri::XML::Node.new 'Task', doc

  task_node['commandLine'] = 'AppDynamics\startup.cmd'
  task_node['executionContext'] = 'elevated'
  task_node['taskType'] = 'foreground'

  existing_start_up_node = xml_node.at_css('Startup')
  if existing_start_up_node.nil?
    start_up_node = Nokogiri::XML::Node.new 'Startup', doc
    xml_node.add_child(start_up_node)
    start_up_node.add_child(task_node)
  else
    existing_start_up_node.add_child(task_node)
  end
end

#transform_appinsights(paths_to_exclude) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/transform.rb', line 303

def transform_appinsights paths_to_exclude
  aiconfig = Dir.glob '**/ApplicationInsights.config'

  remove_paths_from_list paths_to_exclude, aiconfig

  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



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

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
      status = update_appsetting k, v, doc, file
      if status == :updated
        if @debug_mode
          puts "Updated key #{k} with #{v}"
        else
          puts "Updated key: #{k}"
        end
      end
    else
      if !@settings.nil?
        @settings.each { |i|
          k = i.properties[ROWKEY] || NO_VALUE
          v = i.properties[SETTING] || NO_VALUE
          status = update_appsetting k, v, doc, file
          if status == :updated
            if @debug_mode
              puts "Updated key #{k} with #{v}"
            else
              puts "Updated key: #{k}"
            end
          end
        }
      end
    end
  }
end

#transform_cacheclientObject



343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/transform.rb', line 343

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



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/transform.rb', line 279

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_csdef(custom_xml_process = nil) ⇒ Object



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

def transform_csdef custom_xml_process=nil
  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?

    custom_xml_process.call(doc) if !custom_xml_process.nil? and !doc.nil?

    File.write file, doc.to_xml

  }
end

#transform_diagnosticscfgObject



328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/transform.rb', line 328

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_log4netObject

log level and azure table appender parameters are transformed



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/transform.rb', line 358

def transform_log4net
  config_files = Dir.glob '**/log4net.config'
  config_files.each{ |file|
    doc = Nokogiri::XML(File.read file)

    # azure table name is service name
    node = doc.at_css 'log4net/appender[@name=AzureTableAppender]/param[@name=TableName]'
    node['value'] = @service_name if !node.nil?

    node = doc.at_css 'log4net/appender[@name=AzureTableAppender]/param[@name=ConnectionString]'
    log_connstr = ENV["log#{CONNECTIONSTRING}"] || NO_VALUE
    if log_connstr == NO_VALUE
      puts "No logging #{CONNECTIONSTRING} found."
    else
      node['value'] = log_connstr if !node.nil?
    end

    # log level
    node = doc.at_css 'log4net/root/level'
    loglevel = ENV['loglevel'] || NO_VALUE
    node['value'] = ENV['loglevel'] if (!node.nil? && loglevel != NO_VALUE)

    File.write file, doc.to_xml
  }
end

#transform_newrelic(doc) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/transform.rb', line 219

def transform_newrelic doc
  @service_name = ENV[SERVICENAME]
  is_service = @service_name || NO_VALUE
  if is_service != NO_VALUE
    node = doc.at_css 'WebRole'
    transform_newrelic_node(doc, node, @service_name) if !node.nil?
    node = doc.at_css 'WorkerRole'
    transform_newrelic_node(doc, node, @service_name) if !node.nil?
  else
    puts 'Not a service, avoiding transforming the csdef.'
  end
end

#transform_newrelic_node(doc, xml_node, service_name) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/transform.rb', line 232

def transform_newrelic_node doc, xml_node, service_name
  task_node = Nokogiri::XML::Node.new 'Task', doc

  task_node['commandLine'] = 'newrelic_env.cmd'
  task_node['executionContext'] = 'elevated'
  task_node['taskType'] = 'simple'

  existing_start_up_node = xml_node.at_css('Startup')
  if existing_start_up_node.nil?
    start_up_node = Nokogiri::XML::Node.new 'Startup', doc
    xml_node.add_child(start_up_node)
    start_up_node.add_child(task_node)
  else
    existing_start_up_node.add_child(task_node)
  end
end

#transform_riverbed(doc) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/transform.rb', line 189

def transform_riverbed doc
  @service_name = ENV[SERVICENAME]
  is_service = @service_name || NO_VALUE
  if is_service != NO_VALUE
    node = doc.at_css 'WebRole'
    transform_riverbed_node(doc, node) if !node.nil?
    node = doc.at_css 'WorkerRole'
    transform_riverbed_node(doc, node) if !node.nil?
  else
    puts 'Not a service, avoiding transforming the csdef.'
  end
end

#transform_riverbed_node(doc, xml_node) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/transform.rb', line 202

def transform_riverbed_node doc, xml_node
  task_node = Nokogiri::XML::Node.new 'Task', doc

  task_node['commandLine'] = 'AppInternals\startup.cmd'
  task_node['executionContext'] = 'elevated'
  task_node['taskType'] = 'foreground'

  existing_start_up_node = xml_node.at_css('Startup')
  if existing_start_up_node.nil?
    start_up_node = Nokogiri::XML::Node.new 'Startup', doc
    xml_node.add_child(start_up_node)
    start_up_node.add_child(task_node)
  else
    existing_start_up_node.add_child(task_node)
  end
end

#transform_servicemodelconfigObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/transform.rb', line 117

def transform_servicemodelconfig
  @config_files.each{|file|
    doc = Nokogiri::XML(File.read file)
    doc.xpath('//system.serviceModel').each do |node|
      if !node.nil?
        if file.end_with?('app.config') || file.end_with?('App.config')
          val = get_value 'system.ServiceModel.Client'
          node.replace val if !val.nil?
        elsif file.end_with?('Web.config')
          val = get_value 'system.ServiceModel.Service'
          node.replace val if !val.nil?
        end
        File.write file, doc.to_xml
      end
    end
  }
end

#transform_systemwebcompilationattribsObject



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/transform.rb', line 135

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/transform.rb', line 99

def update_appsetting k, v, doc, file
  status = :noargs
  if (k != NO_VALUE && v != NO_VALUE)
    node = doc.at_css "appSettings/add[@key='#{k}']"
    if !node.nil?
      puts "Old value: #{node[VALUE]}" if @debug_mode
      node[VALUE] = v
      puts "New value: #{v}" if @debug_mode
      File.write file, doc.to_xml
      status = :updated
    else
      status = :notfound
    end
  end

  status
end