def write_metadata(kind = WILDCARD)
      options = @options.dup

      kind = kind.to_sym
      if kind == WILDCARD || kind == :user_metadata
        # Both "blue-skies" cloud and "wrap instance" behave the same way, they lay down a
        # file in a predefined location (/var/spool/rightscale/user-data.txt on linux,
        # C:\ProgramData\RightScale\spool\rightscale\user-data.txt on windows. In both
        # cases this userdata has *lower* precedence than cloud data. On a start/stop
        # action where userdata is updated, we want the NEW userdata, not the old. So
        # if cloud-based values exists, than we always use those.
        api_source = RightScale::MetadataSources::RightScaleApiMetadataSource.new(options)
        cloud_userdata_raw = ""
        if api_source.source_exists?
          extra_userdata_raw = api_source.get()

          if (name == "azure")
            # Azure is a special case -- we don't want to run the cloud userdata fetcher again
            # as we can't update userdata anyways and it will currently fail as written
            extra_userdata_raw = get_updated_userdata(extra_userdata_raw)
            cloud_userdata_raw = extra_userdata_raw
          elsif (name == "rackspace")
            # Rackspace is another type of special case, for different reasons.
            # The "wait_for_instance_ready" function on rackspace will get stuck in an
            # infinite loops waiting for the userdata file to appear in the wrap instance
            # or blue-skies cases. Since we don't support start/stop on rackspace anyways
            # we can just skip the whole fetching of updated userdata to avoid this
            # infinite loop scenario, instead just always using the blue-skies/wrap
            # data that's on disk. The downside is that you better delete that data
            # before a rebundle or it won't work. See rackspace/wait_for_instance_ready.rb
            # counterpart code as well
            cloud_userdata_raw = extra_userdata_raw
          else
            cloud_userdata_raw = userdata_raw
            unless cloud_userdata_raw =~ /RS_rn_id/i
              cloud_userdata_raw = extra_userdata_raw
            end
          end
        else
          cloud_userdata_raw = userdata_raw
        end

        cloud_userdata = parse_userdata(cloud_userdata_raw)


        # Raw userdata is a special exception and gets its own writer
        raw_writer = metadata_writers(:user_metadata).find { |writer| writer.kind_of?(RightScale::MetadataWriters::RawMetadataWriter) }
        raw_writer.write(cloud_userdata_raw)
        unless cloud_userdata.empty?
          metadata_writers(:user_metadata).each { |writer| writer.write(cloud_userdata) }
        end
      end
      if kind == WILDCARD || kind == :cloud_metadata
        cloud_metadata = metadata
        unless cloud_metadata.empty?
          metadata_writers(:cloud_metadata).each { |writer| writer.write(cloud_metadata) }
        end
      end
      return ActionResult.new
    rescue Exception => e
      return ActionResult.new(:exitstatus => 1, :error => "ERROR: #{e.message}", :exception => e)
    ensure
      finish()
    end
def write_metadata(kind = WILDCARD)
      options = @options.dup

      kind = kind.to_sym
      if kind == WILDCARD || kind == :user_metadata
        # Both "blue-skies" cloud and "wrap instance" behave the same way, they lay down a
        # file in a predefined location (/var/spool/rightscale/user-data.txt on linux,
        # C:\ProgramData\RightScale\spool\rightscale\user-data.txt on windows. In both
        # cases this userdata has *lower* precedence than cloud data. On a start/stop
        # action where userdata is updated, we want the NEW userdata, not the old. So
        # if cloud-based values exists, than we always use those.
        api_source = RightScale::MetadataSources::RightScaleApiMetadataSource.new(options)
        cloud_userdata_raw = ""
        if api_source.source_exists?
          extra_userdata_raw = api_source.get()

          if (name == "azure")
            # Azure is a special case -- we don't want to run the cloud userdata fetcher again
            # as we can't update userdata anyways and it will currently fail as written
            extra_userdata_raw = get_updated_userdata(extra_userdata_raw)
            cloud_userdata_raw = extra_userdata_raw
          elsif (name == "rackspace")
            # Rackspace is another type of special case, for different reasons.
            # The "wait_for_instance_ready" function on rackspace will get stuck in an
            # infinite loops waiting for the userdata file to appear in the wrap instance
            # or blue-skies cases. Since we don't support start/stop on rackspace anyways
            # we can just skip the whole fetching of updated userdata to avoid this
            # infinite loop scenario, instead just always using the blue-skies/wrap
            # data that's on disk. The downside is that you better delete that data
            # before a rebundle or it won't work. See rackspace/wait_for_instance_ready.rb
            # counterpart code as well
            cloud_userdata_raw = extra_userdata_raw
          else
            cloud_userdata_raw = userdata_raw
            unless cloud_userdata_raw =~ /RS_rn_id/i
              cloud_userdata_raw = extra_userdata_raw
            end
          end
        else
          cloud_userdata_raw = userdata_raw
        end

        cloud_userdata = parse_userdata(cloud_userdata_raw)


        # Raw userdata is a special exception and gets its own writer
        raw_writer = metadata_writers(:user_metadata).find { |writer| writer.kind_of?(RightScale::MetadataWriters::RawMetadataWriter) }
        raw_writer.write(cloud_userdata_raw)
        unless cloud_userdata.empty?
          metadata_writers(:user_metadata).each { |writer| writer.write(cloud_userdata) }
        end
      end
      if kind == WILDCARD || kind == :cloud_metadata
        cloud_metadata = metadata
        unless cloud_metadata.empty?
          metadata_writers(:cloud_metadata).each { |writer| writer.write(cloud_metadata) }
        end
      end
      return ActionResult.new
    rescue Exception => e
      return ActionResult.new(:exitstatus => 1, :error => "ERROR: #{e.message}", :exception => e)
    ensure
      finish()
    end
Queries and writes current metadata to file.

 === Parameters
 kind(Symbol):: kind of metadata must be one of [:cloud_metadata, :user_metadata, WILDCARD]

 === Return
 result(ActionResult):: action result