Skip to content

Commit

Permalink
Fix bug in parsing timezone for symlink, add headers to output
Browse files Browse the repository at this point in the history
  • Loading branch information
sugr4 committed Sep 26, 2016
1 parent f8c5f9b commit bbef5f9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
31 changes: 28 additions & 3 deletions Providers/Modules/Plugins/Common/plugin/oms_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Common
require 'time'
require 'zlib'
require 'digest'
require 'date'
require 'securerandom'

require_relative 'omslog'
require_relative 'oms_configuration'
Expand Down Expand Up @@ -475,10 +477,14 @@ class Common
@@tzRightFolder = 'right/'

class << self
# get the unified timezone id by absolute file path of the timezone file
# file path: the absolute path of the file
def get_unified_timezoneid(filepath)
# remove the baseFolder path
tzID = filepath[@@tzBaseFolder.length..-1] if filepath.start_with?(@@tzBaseFolder)

return 'Unknown' if tzID.nil?

# if the rest starts with 'right/', remove it to unify the format
tzID = tzID[@@tzRightFolder.length..-1] if tzID.start_with?(@@tzRightFolder)

Expand All @@ -493,7 +499,7 @@ def get_current_timezone
begin
# if /etc/localtime is a symlink, check the link file's path
if File.symlink?(@@tzLocalTimePath)
symlinkpath = File.readlink(@@tzLocalTimePath)
symlinkpath = File.absolute_path(File.readlink(@@tzLocalTimePath), File.dirname(@@tzLocalTimePath))
tzID = get_unified_timezoneid(symlinkpath)

# look for the entry in the timezone mapping
Expand Down Expand Up @@ -672,6 +678,23 @@ def create_ods_http(ods_uri, proxy={})
def create_ods_request(path, record, compress, extra_headers=nil, serializer=method(:parse_json_record_encoding))
headers = extra_headers.nil? ? {} : extra_headers

azure_resource_id = OMS::Configuration.azure_resource_id
if !azure_resource_id.to_s.empty?
headers[OMS::CaseSensitiveString.new("x-ms-AzureResourceId")] = azure_resource_id
end

omscloud_id = OMS::Configuration.omscloud_id
if !omscloud_id.to_s.empty?
headers[OMS::CaseSensitiveString.new("x-ms-OMSCloudId")] = omscloud_id
end

uuid = OMS::Configuration.uuid
if !uuid.to_s.empty?
headers[OMS::CaseSensitiveString.new("x-ms-UUID")] = uuid
end

headers[OMS::CaseSensitiveString.new("X-Request-ID")] = SecureRandom.uuid

headers["Content-Type"] = "application/json"
if compress == true
headers["Content-Encoding"] = "deflate"
Expand Down Expand Up @@ -793,7 +816,8 @@ def start_request(req, secure_http, ignore404 = false)

if res.code != "200"
# Retry all failure error codes...
res_summary = "(class=#{res.class.name}; code=#{res.code}; message=#{res.message}; body=#{res.body};)"
res_summary = "(request-id=#{req["X-Request-ID"]}; class=#{res.class.name}; code=#{res.code}; message=#{res.message}; body=#{res.body};)"
Log.error_once("HTTP Error: #{res_summary}")
raise RetryRequestException, "HTTP error: #{res_summary}"
end

Expand Down Expand Up @@ -830,7 +854,8 @@ def get_ip(hostname)
def get_ip_from_socket(hostname)
begin
addrinfos = Socket::getaddrinfo(hostname, "echo", Socket::AF_UNSPEC)
rescue => e
rescue => error
Log.error_once("Unable to resolve the IP of '#{hostname}': #{error}")
return nil
end

Expand Down
29 changes: 28 additions & 1 deletion Providers/Modules/Plugins/Common/plugin/oms_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class Configuration
@@ODSEndpoint = nil
@@GetBlobODSEndpoint = nil
@@NotifyBlobODSEndpoint = nil

@@OmsCloudId = nil
@@AzureResourceId = nil
@@UUID = nil

class << self

# test the onboard file existence
Expand Down Expand Up @@ -106,6 +109,18 @@ def load_configuration(conf_path, cert_path, key_path)
return false
end

File.open(conf_path).each_line do |line|
if line =~ /AZURE_RESOURCE_ID/
@@AzureResourceId = line.sub("AZURE_RESOURCE_ID=","").strip
end
if line =~ /OMSCLOUD_ID/
@@OmsCloudId = line.sub("OMSCLOUD_ID=","").strip
end
if line =~ /UUID/
@@UUID = line.sub("UUID=","").strip
end
end

begin
raw = File.read cert_path
@@Cert = OpenSSL::X509::Certificate.new raw
Expand Down Expand Up @@ -144,6 +159,18 @@ def notify_blob_ods_endpoint
@@NotifyBlobODSEndpoint
end # getter notify_blob_ods_endpoint

def azure_resource_id
@@AzureResourceId
end

def omscloud_id
@@OmsCloudId
end

def uuid
@@UUID
end #getter for VM uuid

end # Class methods

end # class Common
Expand Down
31 changes: 27 additions & 4 deletions Providers/Modules/Plugins/Common/plugin/out_oms_blob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,45 @@ def create_blob_get_request(uri)
# file_path: string. file path
# returns:
# HTTPRequest. blob PUT request
def create_blob_put_request(uri, msg, file_path = nil)
def create_blob_put_request(uri, msg, request_id, file_path = nil)
headers = {}

headers[OMS::CaseSensitiveString.new("x-ms-meta-TimeZoneid")] = OMS::Common.get_current_timezone
headers[OMS::CaseSensitiveString.new("x-ms-meta-ComputerName")] = OMS::Common.get_hostname
if !file_path.nil?
headers[OMS::CaseSensitiveString.new("x-ms-meta-FilePath")] = file_path
end

azure_resource_id = OMS::Configuration.azure_resource_id
if !azure_resource_id.to_s.empty?
headers[OMS::CaseSensitiveString.new("x-ms-AzureResourceId")] = azure_resource_id
end

omscloud_id = OMS::Configuration.omscloud_id
if !omscloud_id.to_s.empty?
headers[OMS::CaseSensitiveString.new("x-ms-OMSCloudId")] = omscloud_id
end

uuid = OMS::Configuration.uuid
if !uuid.to_s.empty?
headers[OMS::CaseSensitiveString.new("x-ms-UUID")] = uuid
end

headers[OMS::CaseSensitiveString.new("X-Request-ID")] = request_id

headers["Content-Type"] = "application/octet-stream"
headers["Content-Length"] = msg.bytesize.to_s

req = Net::HTTP::Put.new(uri.request_uri, headers)
req.body = msg
return req
rescue OMS::RetryRequestException => e
Log.error_once("HTTP error for Request-ID: #{request_id} Error: #{e}")
raise e.message, "Request-ID: #{request_id}"
end # create_blob_put_request

# get the blob SAS URI from ODS
# parameters:
# parameters
# container_type: string. ContainerType of the data
# data_type: string. DataTypeId of the data
# custom_data_type: string. CustomDataType of the CustomLog
Expand Down Expand Up @@ -181,9 +202,10 @@ def get_committed_blocks(uri)
# string. block id
def upload_block(uri, msg)
base64_blockid = Base64.encode64(SecureRandom.uuid)
request_id = SecureRandom.uuid
append_uri = URI.parse("#{uri.to_s}&comp=block&blockid=#{base64_blockid}")

put_block_req = create_blob_put_request(append_uri, msg, nil)
put_block_req = create_blob_put_request(append_uri, msg, request_id, nil)
http = OMS::Common.create_secure_http(append_uri, @proxy_config)
OMS::Common.start_request(put_block_req, http)

Expand All @@ -205,7 +227,8 @@ def commit_blocks(uri, blocks_committed, blocks_uncommitted, file_path)
commit_msg = doc.to_s

blocklist_uri = URI.parse("#{uri.to_s}&comp=blocklist")
put_blocklist_req = create_blob_put_request(blocklist_uri, commit_msg, file_path)
request_id = SecureRandom.uuid
put_blocklist_req = create_blob_put_request(blocklist_uri, commit_msg, request_id, file_path)
http = OMS::Common.create_secure_http(blocklist_uri, @proxy_config)
OMS::Common.start_request(put_blocklist_req, http)
end # commit_blocks
Expand Down

0 comments on commit bbef5f9

Please sign in to comment.