<commit_before>#!/usr/bin/env ruby

require 'rubygems'
require 'right_api_client'



# Primary Inputs
email = ENV['rs_email'] || '<EMAIL>'          # RS User Account
pass = ENV['rs_pswd'] || '<PASSWORD>'              # RS User Password
acct_id = ENV['rs_acct'] || '12345'                  # RS Account to backup scripts from


# Optional Inputs
timeout = ENV['rs_timeout'] || 60                    # Timeout in Seconds


puts ENV.has_key?('rs_email') ? 'rs_email set by ENVIRONMENT' : 'rs_email set by SCRIPT [default]'
puts ENV.has_key?('rs_pswd') ? 'rs_pswd set by ENVIRONMENT' : 'rs_pswd set by SCRIPT [default]'
puts ENV.has_key?('rs_acct') ? 'rs_acct set by ENVIRONMENT' : 'rs_acct set by SCRIPT [default]'


# Authenticate
@client = RightApi::Client.new(:email => email, :password => <PASSWORD>, :account_id => acct_id, :timeout => timeout.to_i )

puts "Authenticated!"

puts "Auditing RightScale Account: #{acct_id}"

File.open('audit.csv', 'w') do |f|
  f.puts '"deployment_name","server_type","instance_lineage","instance_name","instance_type","instance_datacenter","instance_state"'
    @client.deployments.index.each do |deployment|

      deployment_name=deployment.name

      # Query all Servers in deployment
      deployment.servers.index.each do |server|
        server_type='server'

        if defined? server.show.current_instance == "method"
          instance_lineage='current'
          instance_name=server.show.current_instance.show(:view => "extended").name.to_s
          instance_state=server.show.current_instance.show(:view => "extended").state.to_s
          instance_datacenter=server.show.current_instance.show(:view =>'extended').datacenter.show.name.to_s
          instance_type=''
          if defined? server.show.current_instance.show(:view => "extended").instance_type == "method"
            instance_type=server.show.current_instance.show(:view => "extended").instance_type.show.name.to_s
          end
          f.puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\""

        else #else current_instance does not exist.. will audit next instance
          instance_lineage='next'
          instance_name=server.show.next_instance.show.name.to_s
          instance_state=server.show.next_instance.show(:view => "extended").state.to_s
          instance_datacenter=server.show.next_instance.show(:view => "extended").cloud.show.name.to_s
          instance_type=''
          if defined? server.show.next_instance.show(:view => "extended").instance_type == "method"
            instance_type=server.show.next_instance.show(:view => "extended").instance_type.show.name.to_s
          end
          f.puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\""

        end #end if current_instances
      end #end servers.each



      # Query all ServerArrays in deployment
      deployment.server_arrays.index.each do |sa|
        server_type='array'
        if sa.show.current_instances.index.count > 0

          sa.show.current_instances.index(:view => 'extended').each do |instance|
            instance_lineage='current'
            instance_name=instance.show(:view => "extended").name.to_s
            instance_state=instance.show(:view => "extended").state.to_s
            instance_datacenter=instance.show(:view =>'extended').cloud.show.name.to_s
            instance_type=''
            if defined? instance.show(:view => "extended").instance_type == "method"
              instance_type=instance.instance_type.show.name.to_s
            end
            f.puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\""
          end
        else
          instance_lineage='next'
          instance_name=sa.next_instance.show(:view => "extended").name.to_s
          instance_state=sa.next_instance.show(:view => "extended").state.to_s
          instance_datacenter=sa.next_instance.show(:view =>'extended').cloud.show.name.to_s
          instance_type=''
          if defined? sa.next_instance.show(:view => "extended").instance_type == "method"
            instance_type=sa.next_instance.show(:view => "extended").instance_type.show.name.to_s
          end
          f.puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\""

        end #end if count==0
      end#end server_arrays.each


    end #end deployments.each
end #end File.open
<commit_msg>Added instance_href field to audit output.  Fixed issue where instance_type would sometimes not exist and cause the audit script to terminate prematurely.<commit_after>#!/usr/bin/env ruby

require 'rubygems'
require 'right_api_client'



# Primary Inputs
email = ENV['rs_email'] || '<EMAIL>'          # RS User Account
pass = ENV['rs_<PASSWORD>'] || '<PASSWORD>'              # RS User Password
acct_id = ENV['rs_acct'] || '12345'                  # RS Account to backup scripts from


# Optional Inputs
timeout = ENV['rs_timeout'] || 60                    # Timeout in Seconds


puts ENV.has_key?('rs_email') ? 'rs_email set by ENVIRONMENT' : 'rs_email set by SCRIPT [default]'
puts ENV.has_key?('rs_pswd') ? 'rs_pswd set by ENVIRONMENT' : 'rs_pswd set by SCRIPT [default]'
puts ENV.has_key?('rs_acct') ? 'rs_acct set by ENVIRONMENT' : 'rs_acct set by SCRIPT [default]'


# Authenticate
@client = RightApi::Client.new(:email => email, :password => <PASSWORD>, :account_id => acct_id, :timeout => timeout.to_i )

puts "Authenticated!"

puts "Auditing RightScale Account: #{acct_id}"

File.open('audit.csv', 'w') do |f|
  f.puts '"deployment_name","server_type","instance_lineage","instance_name","instance_type","instance_datacenter","instance_state","instance_href"'
    @client.deployments.index.each do |deployment|

      deployment_name=deployment.name
      puts "Auditing Deployment: #{deployment_name}"

      # Query all Servers in deployment
      deployment.servers.index.each do |server|
        server_type='server'

        if defined? server.show.current_instance == "method"
          instance_lineage='current'
          instance_name=server.show.current_instance.show(:view => "extended").name.to_s
          instance_state=server.show.current_instance.show(:view => "extended").state.to_s
          instance_href=server.show.current_instance.show(:view =>'extended').href.to_s
          instance_datacenter=server.show.current_instance.show(:view =>'extended').datacenter.show.name.to_s
          instance_type=''
          if defined? server.show.current_instance.show(:view => "extended").instance_type == "method"
            instance_type=server.show.current_instance.show(:view => "extended").instance_type.show.name.to_s
          end
          f.puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\",\"#{instance_href}\""
          puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\",\"#{instance_href}\""

        else #else current_instance does not exist.. will audit next instance
          instance_lineage='next'
          instance_name=server.show.next_instance.show.name.to_s
          instance_state=server.show.next_instance.show(:view => "extended").state.to_s
          instance_href=server.show.next_instance.show(:view =>'extended').href.to_s
          instance_datacenter=server.show.next_instance.show(:view => "extended").cloud.show.name.to_s
          instance_type=''
          if defined? server.show.next_instance.show(:view => "extended").instance_type.show.name == "method"
            instance_type=server.show.next_instance.show(:view => "extended").instance_type.show.name.to_s
          end
          f.puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\",\"#{instance_href}\""
          puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\",\"#{instance_href}\""


        end #end if current_instances
      end #end servers.each



      # Query all ServerArrays in deployment
      deployment.server_arrays.index.each do |sa|
        server_type='array'
        if sa.show.current_instances.index.count > 0

          sa.show.current_instances.index(:view => 'extended').each do |instance|
            instance_lineage='current'
            instance_name=instance.show(:view => "extended").name.to_s
            instance_state=instance.show(:view => "extended").state.to_s
            instance_href=instance.show(:view => "extended").href.to_s
            instance_datacenter=instance.show(:view =>'extended').cloud.show.name.to_s
            instance_type=''
            if defined? instance.show(:view => "extended").instance_type.show.name == "method"
              instance_type=instance.instance_type.show.name.to_s
            end
            f.puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\",\"#{instance_href}\""
            puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\",\"#{instance_href}\""
          end #end current_instances.each

        else
          instance_lineage='next'
          instance_name=sa.next_instance.show(:view => "extended").name.to_s
          instance_state=sa.next_instance.show(:view => "extended").state.to_s
          instance_href=sa.next_instance.show(:view => "extended").href.to_s
          instance_datacenter=sa.next_instance.show(:view =>'extended').cloud.show.name.to_s
          instance_type=''
          if defined? sa.next_instance.show(:view => "extended").instance_type.show.name == "method"
            instance_type=sa.next_instance.show(:view => "extended").instance_type.show.name.to_s
          end
          f.puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\",\"#{instance_href}\""
          puts "\"#{deployment_name}\",\"#{server_type}\",\"#{instance_lineage}\",\"#{instance_name}\",\"#{instance_type}\",\"#{instance_datacenter}\",\"#{instance_state}\",\"#{instance_href}\""

        end #end if count==0
      end#end server_arrays.each


    end #end deployments.each
end #end File.open

{'fraction_non_alphanumeric': 0.1699789357999817, 'fraction_numerical': 0.0016485026101291328, 'mean_word_length': 3.570950188363332, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 62, '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': '5438070', 'n_tokens_mistral': 3479, 'n_tokens_neox': 3448, 'n_words': 497}