Skip to content

Commit

Permalink
Fetching and updating Azure Resource ID in agent
Browse files Browse the repository at this point in the history
The load_configuration method called by all the output plugins create
a thread at first that will keep fetching and updating Azure Res ID.
  • Loading branch information
shpimpal committed Sep 25, 2018
1 parent d44c8ea commit 8a1b944
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion source/code/plugins/oms_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Configuration
@@NotifyBlobODSEndpoint = nil
@@OmsCloudId = nil
@@AzureResourceId = nil
@@AzureIMDSEndpoint = "http://169.254.169.254/metadata/instance?api-version=2017-12-01"
@@AzureResIDThreadLock = Mutex.new
@@ProxyConfig = nil
@@ProxyConfigFilePath = "/etc/opt/microsoft/omsagent/proxy.conf"
@@UUID = nil

class << self
Expand Down Expand Up @@ -75,11 +79,61 @@ def parse_proxy_config(proxy_conf_str)
Hash[ matches.names.map{ |name| name.to_sym}.zip( matches.captures ) ]
end

def get_azure_resid_from_imds()
begin
uri = URI.parse(@@AzureIMDSEndpoint)
http_get_req = Net::HTTP::Get.new(uri, initheader = {'Metadata' => 'true'})

if @@ProxyConfig
http_req = Net::HTTP.new(uri.host, uri.port, @@ProxyConfig[:addr], @@ProxyConfig[:port], @@ProxyConfig[:user], @@ProxyConfig[:pass])
else
http_req = Net::HTTP.new(uri.host, uri.port)
end

http_req.open_timeout = 5
http_req.read_timeout = 5

res = http_req.start() do |http|
http.request(http_get_req)
end

imds_instance_json = JSON.parse(res.body)
if !imds_instance_json.has_key?("compute")
return nil #classic vm
end

azure_resource_id = '/subscriptions/' + imds_instance_json['compute']['subscriptionId'] + '/resourceGroups/' + imds_instance_json['compute']['resourceGroupName'] + '/providers/Microsoft.Compute/'

if (imds_instance_json['compute']['vmScaleSetName'].empty?)
azure_resource_id = azure_resource_id + 'virtualMachines/' + imds_instance_json['compute']['name']
else
azure_resource_id = azure_resource_id + 'virtualMachineScaleSets/' + imds_instance_json['compute']['vmScaleSetName'] + '/virtualMachines/' + imds_instance_json['compute']['name']
end

return azure_resource_id

rescue => e
# this may be a container instance or a non-Azure VM
OMS::Log.warn_once("Could not fetch Azure Resource ID from IMDS, Reason: #{e}")
return nil
end
end

def update_azure_resource_id()
loop do
sleep 60
azure_resource_id = get_azure_resid_from_imds()
@@AzureResourceId = azure_resource_id unless @@AzureResourceId == azure_resource_id
end
end

# load the configuration from the configuration file, cert, and key path
def load_configuration(conf_path, cert_path, key_path)
return true if @@ConfigurationLoaded
return false if !test_onboard_file(conf_path) or !test_onboard_file(cert_path) or !test_onboard_file(key_path)

@@ProxyConfig = get_proxy_config(@@ProxyConfigFilePath)

endpoint_lines = IO.readlines(conf_path).select{ |line| line.start_with?("OMS_ENDPOINT")}
if endpoint_lines.size == 0
OMS::Log.error_once("Could not find OMS_ENDPOINT setting in #{conf_path}")
Expand Down Expand Up @@ -135,7 +189,22 @@ def load_configuration(conf_path, cert_path, key_path)

File.open(conf_path).each_line do |line|
if line =~ /AZURE_RESOURCE_ID/
@@AzureResourceId = line.sub("AZURE_RESOURCE_ID=","").strip
azure_resource_id = get_azure_resid_from_imds()

retries = 0
until !azure_resource_id.nil? do
azure_resource_id = get_azure_resid_from_imds()
retries += 1
break if retries > 2
sleep 2
end

if azure_resource_id.nil?
@@AzureResourceId = line.sub("AZURE_RESOURCE_ID=","").strip
else
@@AzureResourceId = azure_resource_id
Thread.new(&method(:update_azure_resource_id)) if @@AzureResIDThreadLock.try_lock
end
end
if line =~ /OMSCLOUD_ID/
@@OmsCloudId = line.sub("OMSCLOUD_ID=","").strip
Expand Down

0 comments on commit 8a1b944

Please sign in to comment.