Class: Transform

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

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



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/transform.rb', line 9

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

    result
end

#get_allObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/transform.rb', line 21

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



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/transform.rb', line 34

def get_value(key)
    value = ''
    @settings.each { |i|
        if i.properties['RowKey'] == key
            value = i.properties['setting']
            break
        end
    }

    value
end

#transformObject



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

def transform

    # environment invoked
    @env = ENV['env'] || 'noenv'
    if @env == 'noenv'
        puts 'Environment name required to transform. No configuration changes will be done...'
        return false
    end

    is_service = ENV['ServiceName'] || 'nosvc'
    puts 'Target to transform is not a service...' if is_service == 'nosvc'

    puts "Transforming config for environment: #{@env} ..."

    # azure table storage account where settings reside
    Azure.config. = ENV['StorageAccount']
    Azure.config.storage_access_key = ENV['StorageAccountKey']
    @table = ENV['ConfigSettingsTable']

    # table service
    @svc = Azure::TableService.new

    # get all settings for environment
    @settings = get_all

    # find all App.config and web.config files
    @config_files = Dir.glob("**/app.config")
    @config_files.concat(Dir.glob("**/web.config"))

    if is_service

        puts "Obtaining cloud configuration templates..."
        csdefTemplate = get_value('ServiceDefinitionTemplate')
        File.write('ServiceDefinition.csdef', csdefTemplate)
        cscfgTemplate = get_value('ServiceConfigurationTemplate')
        File.write('ServiceConfiguration.cscfg', cscfgTemplate)

        @config_files.concat(Dir.glob("**/RuntimeWeb/*Web.dll.config"))
        @config_files.concat(Dir.glob("**/RuntimeService/*.exe.config"))

        puts "Transforming csdef..."
        transform_csdef

        puts "Transforming cscfg..."
        transform_cscfg

        puts "Transforming diagnostics cfg..."
        transform_diagnosticscfg

        puts "Replacing service model settings..."
        transform_servicemodelconfig
        
    end

    puts "Replacing app settings..."
    transform_appsettings

    puts "Removing debug compilation attributes..."
    transform_systemwebcompilationattribs

    return true
end

#transform_appsettingsObject



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

def transform_appsettings
    # 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}"
        @settings.each { |i|
            key = i.properties['RowKey']
            node = doc.at_css "appSettings/add[@key='#{key}']"
            if !node.nil?
                oldVal = node['value']
                #puts "Old value: #{oldVal}"
                node['value'] = i.properties['setting']
                newVal = node['value']
                #puts "New value: #{newVal}"
            end
        }
        File.write(file, doc.to_xml)
    }
end

#transform_cscfgObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/transform.rb', line 131

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

        node = doc.at_css "ServiceConfiguration"
        node['serviceName'] = ENV['ServiceName'] if !node.nil?

        node = doc.at_css "Role"
        node['name'] = ENV['ServiceName'] 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



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

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

        node = doc.at_css "ServiceDefinition"
        node['name'] = ENV['ServiceName'] if !node.nil?

        node = doc.at_css "WebRole"
        node['name'] = ENV['ServiceName'] if !node.nil?

        node = doc.at_css "WorkerRole"
        node['name'] = ENV['ServiceName'] 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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/transform.rb', line 152

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/transform.rb', line 66

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

#transform_systemwebcompilationattribsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/transform.rb', line 81

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