<commit_before>require 'sinatra'
require 'active_support'
require 'right_aws'
require 'sdb/active_sdb'
require 'json'
# require 'fog'
# ENV["FOG_RC"] = Will need this if we switch to Fog

class PairingMachine < Aws::ActiveSdb::Base
end

module FTL

  class Server < Sinatra::Base

    ACCESS_KEY_ID     = ENV['ACCESS_KEY_ID']     || YAML.load_file(ENV['HOME']+'/.ftl/ftl.yml')['ACCESS_KEY_ID']
    SECRET_ACCESS_KEY = ENV['SECRET_ACCESS_KEY'] || YAML.load_file(ENV['HOME']+'/.ftl/ftl.yml')['SECRET_ACCESS_KEY']

    before do
      Aws::ActiveSdb.establish_connection(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
      PairingMachine.create_domain # Raises no error if it already exists, but blows up if it doesn't
    end

    get '/machines' do
      # List the machines
      pms = PairingMachine.find(:all)
      ec2 = Aws::Ec2.new(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
      pms.each do |pm|
        i = ec2.describe_instances(pm.attributes['aws_instance_id'])[0]
        pm[:dns_name] = i[:dns_name]
        pm.save
      end
      pms.collect{|pm| pm.attributes }.to_json
    end

    post '/machines' do
      # Create new machine
      return "Need to name this machine" unless params[:name]
      return "Please provide an ami" unless params[:ami]
      pm = PairingMachine.new(params)
      pm[:instance_type] ||= 'c1.medium'
      ec2 = Aws::Ec2.new(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
      i = ec2.launch_instances(params[:ami], :user_data => "#!/", :instance_type => pm[:instance_type])[0]
      pm[:aws_instance_id] = i[:aws_instance_id]
      pm[:dns_name] = i[:dns_name]
      pm.save
      pm.attributes.to_json
    end

    delete '/machines' do
      # Destroy the machine by short name
      pms = PairingMachine.find(:all)
      pms = pms.select{|pm| pm.attributes['name'].first[params['name']] }
      return "{message: \"Provide the name of the machine(s) to be terminated\"}" if params['name'].nil? || pms.blank?
      ec2 = Aws::Ec2.new(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
      instances = pms.collect{|pm| pm.attributes['aws_instance_id'].first }
      ec2.terminate_instances(instances)
      pms.each {|pm|
        pm.delete 
      }
      "{message: \"Shutdown machines matching '#{params['name']}'\"}"
    end

  end

end
<commit_msg>rollback default instance size to small<commit_after>require 'sinatra'
require 'active_support'
require 'right_aws'
require 'sdb/active_sdb'
require 'json'
# require 'fog'
# ENV["FOG_RC"] = Will need this if we switch to Fog

class PairingMachine < Aws::ActiveSdb::Base
end

module FTL

  class Server < Sinatra::Base

    ACCESS_KEY_ID     = ENV['ACCESS_KEY_ID']     || YAML.load_file(ENV['HOME']+'/.ftl/ftl.yml')['ACCESS_KEY_ID']
    SECRET_ACCESS_KEY = ENV['SECRET_ACCESS_KEY'] || YAML.load_file(ENV['HOME']+'/.ftl/ftl.yml')['SECRET_ACCESS_KEY']

    before do
      Aws::ActiveSdb.establish_connection(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
      PairingMachine.create_domain # Raises no error if it already exists, but blows up if it doesn't
    end

    get '/machines' do
      # List the machines
      pms = PairingMachine.find(:all)
      ec2 = Aws::Ec2.new(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
      pms.each do |pm|
        i = ec2.describe_instances(pm.attributes['aws_instance_id'])[0]
        pm[:dns_name] = i[:dns_name]
        pm.save
      end
      pms.collect{|pm| pm.attributes }.to_json
    end

    post '/machines' do
      # Create new machine
      return "Need to name this machine" unless params[:name]
      return "Please provide an ami" unless params[:ami]
      pm = PairingMachine.new(params)
      pm[:instance_type] ||= 'm1.small'
      ec2 = Aws::Ec2.new(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
      i = ec2.launch_instances(params[:ami], :user_data => "#!/", :instance_type => pm[:instance_type])[0]
      pm[:aws_instance_id] = i[:aws_instance_id]
      pm[:dns_name] = i[:dns_name]
      pm.save
      pm.attributes.to_json
    end

    delete '/machines' do
      # Destroy the machine by short name
      pms = PairingMachine.find(:all)
      pms = pms.select{|pm| pm.attributes['name'].first[params['name']] }
      return "{message: \"Provide the name of the machine(s) to be terminated\"}" if params['name'].nil? || pms.blank?
      ec2 = Aws::Ec2.new(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
      instances = pms.collect{|pm| pm.attributes['aws_instance_id'].first }
      ec2.terminate_instances(instances)
      pms.each {|pm|
        pm.delete 
      }
      "{message: \"Shutdown machines matching '#{params['name']}'\"}"
    end

  end

end

{'fraction_non_alphanumeric': 0.1353682813536828, 'fraction_numerical': 0.0053085600530856005, 'mean_word_length': 3.3397312859884836, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '575034', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1594, 'n_words': 335}