<commit_before>#
# Copyright (c) 2011 RightScale Inc
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

VSCALE_DEFINITION_VERSION = 0.2

CONFIG_DRIVE_MOUNTPOINT = "/mnt/metadata" unless ::RightScale::Platform.windows?
CONFIG_DRIVE_MOUNTPOINT = "a:\\" if ::RightScale::Platform.windows?

# dependencies.
metadata_source 'metadata_sources/file_metadata_source'
metadata_writers 'metadata_writers/dictionary_metadata_writer',
                 'metadata_writers/ruby_metadata_writer',
                 'metadata_writers/shell_metadata_writer'

# set abbreviation for non-RS env var generation
abbreviation :vs

# converts CIDR to ip/netmask
#
def cidr_to_netmask(cidr_range)
  cidr = IP::CIDR.new(cidr_range)
  return cidr.ip.ip_address, cidr.long_netmask.ip_address
end



# Parses vsoup user metadata into a hash.
#
# === Parameters
# tree_climber(MetadataTreeClimber):: tree climber
# data(String):: raw data
#
# === Return
# result(Hash):: Hash-like leaf value
def create_user_metadata_leaf(tree_climber, data)
  result = tree_climber.create_branch
  ::RightScale::CloudUtilities.split_metadata(data.strip, "\n", result)
  result
end

def cloud_metadata_is_flat(clazz, path, query_result)
  false
end

# userdata defaults
default_option([:metadata_source, :user_metadata_source_file_path], File.join(CONFIG_DRIVE_MOUNTPOINT, 'user.txt'))
default_option([:user_metadata, :metadata_tree_climber, :create_leaf_override], method(:create_user_metadata_leaf))

# cloud metadata defaults
default_option([:metadata_source, :cloud_metadata_source_file_path], File.join(CONFIG_DRIVE_MOUNTPOINT, 'meta.txt'))
default_option([:cloud_metadata, :metadata_tree_climber, :create_leaf_override], method(:create_user_metadata_leaf))
# vscale cloud_metadata is flat, so paths will never have children -- always return false
default_option([:cloud_metadata, :metadata_tree_climber, :has_children_override], method(:cloud_metadata_is_flat))


# Determines if the current instance is running on vsoup.
#
# === Return
# true if running on rackspace
def is_current_cloud?
  return true
end

def write_cloud_metadata
  result = write_metadata(:cloud_metadata)
  # TODO: this is a dirty hack for Windows to avoid recompiling service DLL for
  #       vScale PoC network config features.  This functionality will be moved to
  #       system_configurator at some point and should then be removed
  configure_network if platform.windows?
  result
end

def shell_escape_if_necessary(word)
  return word if word.match(/^".*"$/) || word.match(/^\S+$/)
  word.inspect
end

def configure_network
  # load_metadata is here to be sure metadata will be loaded
  # both on Linux and Windows(Windows init_cloud_state will not call update_details)
  load_metadata

  # configure static IP (if specified in metadata)
  device = ENV['RS_STATIC_IP0_DEVICE']
  device ||= platform.windows? ? "Local Area Connection" : "eth0"
  static_ip = add_static_ip(shell_escape_if_necessary(device))
  if platform.windows?
    # setting administrator password setting (not yet supported)
  else
    # update authorized_keys file from metadata
    public_key = get_public_ssh_key_from_metadata()
    update_authorized_keys(public_key)
  end
  # add routes for nat server
  # this needs to be done after our IPs are configured
  add_static_routes_for_network
end

# Updates the given node with cloud metadata details.
#
# We also do a bunch of VM configuration here.
# There is likely a better place we can do all this.
#
# === Return
# always true
def update_details
  details = {}
  details[:public_ips] = Array.new
  details[:private_ips] = Array.new

  load_metadata
  configure_network

  if platform.windows?
    # report new network interface configuration to ohai
    if ohai = @options[:ohai_node]
      details[:public_ipv4] = ::RightScale::CloudUtilities.ip_for_windows_interface(ohai, 'Local Area Connection')
      details[:local_ipv4] = ::RightScale::CloudUtilities.ip_for_windows_interface(ohai, 'Local Area Connection 2')
    end
  else
    # report new network interface configuration to ohai
    if ohai = @options[:ohai_node]
      # Pick up all IPs detected by ohai
      n = 0
      while (ip = ::RightScale::CloudUtilities.ip_for_interface(ohai, "eth#{n}")) != nil
        type = "public_ip"
        type = "private_ip" if is_private_ipv4(ip)
        details[type.to_sym] ||= ip      # store only the first found for type
        details["#{type}s".to_sym] << ip # but append all to the list
        n += 1
      end
    end
  end

  # Override with statically assigned IP (if specified)
  static_ip = ENV['RS_STATIC_IP0_ADDR']
  if static_ip
    if is_private_ipv4(static_ip)
      details[:private_ip] ||= static_ip
      details[:private_ips] << static_ip
    else
      details[:public_ip] ||= static_ip
      details[:public_ips] << static_ip
    end
  end

  details
end
<commit_msg>acu141016 fix cloud metadata population on windows<commit_after>#
# Copyright (c) 2011 RightScale Inc
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

VSCALE_DEFINITION_VERSION = 0.2

CONFIG_DRIVE_MOUNTPOINT = "/mnt/metadata" unless ::RightScale::Platform.windows?
CONFIG_DRIVE_MOUNTPOINT = "a:\\" if ::RightScale::Platform.windows?

# dependencies.
metadata_source 'metadata_sources/file_metadata_source'
metadata_writers 'metadata_writers/dictionary_metadata_writer',
                 'metadata_writers/ruby_metadata_writer',
                 'metadata_writers/shell_metadata_writer'

# set abbreviation for non-RS env var generation
abbreviation :vs

# converts CIDR to ip/netmask
#
def cidr_to_netmask(cidr_range)
  cidr = IP::CIDR.new(cidr_range)
  return cidr.ip.ip_address, cidr.long_netmask.ip_address
end



# Parses vsoup user metadata into a hash.
#
# === Parameters
# tree_climber(MetadataTreeClimber):: tree climber
# data(String):: raw data
#
# === Return
# result(Hash):: Hash-like leaf value
def create_user_metadata_leaf(tree_climber, data)
  result = tree_climber.create_branch
  ::RightScale::CloudUtilities.split_metadata(data.strip, "\n", result)
  result
end

def cloud_metadata_is_flat(clazz, path, query_result)
  false
end

# userdata defaults
default_option([:metadata_source, :user_metadata_source_file_path], File.join(CONFIG_DRIVE_MOUNTPOINT, 'user.txt'))
default_option([:user_metadata, :metadata_tree_climber, :create_leaf_override], method(:create_user_metadata_leaf))

# cloud metadata defaults
default_option([:metadata_source, :cloud_metadata_source_file_path], File.join(CONFIG_DRIVE_MOUNTPOINT, 'meta.txt'))
default_option([:cloud_metadata, :metadata_tree_climber, :create_leaf_override], method(:create_user_metadata_leaf))
# vscale cloud_metadata is flat, so paths will never have children -- always return false
default_option([:cloud_metadata, :metadata_tree_climber, :has_children_override], method(:cloud_metadata_is_flat))


# Determines if the current instance is running on vsoup.
#
# === Return
# true if running on rackspace
def is_current_cloud?
  return true
end

def write_cloud_metadata
  result = write_metadata(:cloud_metadata)
  # TODO: this is a dirty hack for Windows to avoid recompiling service DLL for
  #       vScale PoC network config features.  This functionality will be moved to
  #       system_configurator at some point and should then be removed
  configure_network if platform.windows?
  result
end

def shell_escape_if_necessary(word)
  return word if word.match(/^".*"$/) || word.match(/^\S+$/)
  word.inspect
end

def configure_network
  # load_metadata is here to be sure metadata will be loaded
  # both on Linux and Windows(Windows init_cloud_state will not call update_details)
  load_metadata

  # configure static IP (if specified in metadata)
  device = ENV['RS_STATIC_IP0_DEVICE']
  device ||= platform.windows? ? "Local Area Connection" : "eth0"
  static_ip = add_static_ip(shell_escape_if_necessary(device))
  if platform.windows?
    # setting administrator password setting (not yet supported)
  else
    # update authorized_keys file from metadata
    public_key = get_public_ssh_key_from_metadata()
    update_authorized_keys(public_key)
  end
  # add routes for nat server
  # this needs to be done after our IPs are configured
  add_static_routes_for_network
end

# Updates the given node with cloud metadata details.
#
# We also do a bunch of VM configuration here.
# There is likely a better place we can do all this.
#
# === Return
# always true
def update_details
  details = {}
  details[:public_ips] = Array.new
  details[:private_ips] = Array.new

  load_metadata
  configure_network

  if platform.windows?
    # report new network interface configuration to ohai
    if ohai = @options[:ohai_node]
      ['Local Area Connection', 'Local Area Connection 2'].each do |device|
        ip = ::RightScale::CloudUtilities.ip_for_windows_interface(ohai, device)
        details[is_private_ipv4(ip) ? :local_ipv4 : :public_ipv4] = ip
      end
    end
  else
    # report new network interface configuration to ohai
    if ohai = @options[:ohai_node]
      # Pick up all IPs detected by ohai
      n = 0
      while (ip = ::RightScale::CloudUtilities.ip_for_interface(ohai, "eth#{n}")) != nil
        type = "public_ip"
        type = "private_ip" if is_private_ipv4(ip)
        details[type.to_sym] ||= ip      # store only the first found for type
        details["#{type}s".to_sym] << ip # but append all to the list
        n += 1
      end
    end
  end

  # Override with statically assigned IP (if specified)
  static_ip = ENV['RS_STATIC_IP0_ADDR']
  if static_ip
    if is_private_ipv4(static_ip)
      details[:private_ip] ||= static_ip
      details[:private_ips] << static_ip
    else
      details[:public_ip] ||= static_ip
      details[:public_ips] << static_ip
    end
  end

  details
end

{'fraction_non_alphanumeric': 0.08122344944774851, 'fraction_numerical': 0.0033135089209855566, 'mean_word_length': 4.581318160265528, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3487148', 'n_tokens_mistral': 3548, 'n_tokens_neox': 3473, 'n_words': 1152}