<commit_before>require 'rubygems'
require 'bundler/setup'
require 'eventmachine'
require 'rack'
require 'rubydns'
require 'thin'

require_relative 'rack/metadata'

def self.command(cmd)
  out = `#{cmd}`

  if $? == 0
    out
  else
    raise "Failed to run: #{cmd}"
  end
end

BIND     = '169.254.169.254'
IN       = Resolv::DNS::Resource::IN
UPSTREAM = RubyDNS::Resolver.new([
  [:tcp, '8.8.8.8', 53],
  [:udp, '8.8.8.8', 53]
])

DEVICE =
  if ARGV.length > 0
    ARGV.first.chomp
  else
    puts 'Usage: server.sh <NETWORK_SERVICE> (use networksetup -listallnetworkservices to obtain.)'
    exit 1
  end

OS =
  begin
    host_os = RbConfig::CONFIG['host_os']

    case host_os
    when /darwin|mac os/
      :macosx
    when /linux/
      :linux
    else
      raise "unsupported os: #{host_os.inspect}"
    end
  end

RESOLV =
  if OS == :macosx
    out = command("networksetup -getdnsservers #{DEVICE}")
    command("sudo networksetup -setdnsservers #{DEVICE} 127.0.0.1 8.8.8.8")

    out.gsub("\n", ' ')
  else
    ''
  end

def self.run
  begin
    puts "Adding #{BIND} to lo0"
    command("ifconfig lo0 alias #{BIND}")

    dns = RubyDNS::RuleBasedServer.new do
      match('instance-data', IN::A) do |tx|
        tx.respond!('169.254.169.254')
      end

      otherwise do |tx|
        tx.passthrough!(UPSTREAM)
      end
    end

    dir = Rack::Builder.new do
       root = File.join(File.expand_path(File.dirname(__FILE__)), '../www')
      run Rack::Metadata.new(root)
    end

    files = Rack::Chunked.new(Rack::ContentLength.new(dir))

    Signal.trap('INT')  { EventMachine.stop }
    Signal.trap('TERM') { EventMachine.stop }

    EventMachine.run do
      puts 'Redirecting http://instance-data to localhost'
      dns.run(:listen => [
        [:tcp, '0.0.0.0', 53],
        [:udp, '0.0.0.0', 53]
      ])

      puts "Serving metadata on http://#{BIND}/latest"
      Thin::Server.start(BIND, 80, files, :signals => false)
    end
  ensure
    puts "Removing #{BIND} from lo0"
    system("ifconfig lo0 -alias #{BIND}")

    unless RESOLV.empty?
      puts "Reverting resolv.conf to #{RESOLV}"
      system("sudo networksetup -setdnsservers #{DEVICE} #{RESOLV}")
    end
  end
end

if ENV['USER'] == 'root'
  run
else
  puts 'Error: Please re-run using sudo due to resolv.conf modifications and use of port 80.'
  exit 1
end
<commit_msg>Adding note to future self regarding MacOSX network device lookup<commit_after>require 'rubygems'
require 'bundler/setup'
require 'eventmachine'
require 'rack'
require 'rubydns'
require 'thin'

require_relative 'rack/metadata'

def self.command(cmd)
  out = `#{cmd}`

  if $? == 0
    out
  else
    raise "Failed to run: #{cmd}"
  end
end

BIND     = '169.254.169.254'
IN       = Resolv::DNS::Resource::IN
UPSTREAM = RubyDNS::Resolver.new([
  [:tcp, '8.8.8.8', 53],
  [:udp, '8.8.8.8', 53]
])

DEVICE =
  if ARGV.length > 0
    ARGV.first.chomp
  else
    puts 'Usage: server.sh <NETWORK_SERVICE> (use networksetup -listallnetworkservices to obtain.)'
    exit 1
  end

OS =
  begin
    host_os = RbConfig::CONFIG['host_os']

    case host_os
    when /darwin|mac os/
      :macosx
    when /linux/
      :linux
    else
      raise "unsupported os: #{host_os.inspect}"
    end
  end

RESOLV =
  if OS == :macosx
    out = command("networksetup -getdnsservers #{DEVICE}")
    command("sudo networksetup -setdnsservers #{DEVICE} 127.0.0.1 8.8.8.8")

    out.gsub("\n", ' ')
  else
    ''
  end

def self.run
  begin
    puts "Adding #{BIND} to lo0"
    command("ifconfig lo0 alias #{BIND}")

    dns = RubyDNS::RuleBasedServer.new do
      match('instance-data', IN::A) do |tx|
        tx.respond!('169.254.169.254')
      end

      otherwise do |tx|
        tx.passthrough!(UPSTREAM)
      end
    end

    dir = Rack::Builder.new do
      root = File.join(File.expand_path(File.dirname(__FILE__)), '../www')
      run Rack::Metadata.new(root)
    end

    files = Rack::Chunked.new(Rack::ContentLength.new(dir))

    Signal.trap('INT')  { EventMachine.stop }
    Signal.trap('TERM') { EventMachine.stop }

    EventMachine.run do
      puts 'Redirecting http://instance-data to localhost'
      dns.run(:listen => [
        [:tcp, '0.0.0.0', 53],
        [:udp, '0.0.0.0', 53]
      ])

      puts "Serving metadata on http://#{BIND}/latest"
      Thin::Server.start(BIND, 80, files, :signals => false)
    end
  ensure
    puts "Removing #{BIND} from lo0"
    system("ifconfig lo0 -alias #{BIND}")

    unless RESOLV.empty?
      puts "Reverting resolv.conf to #{RESOLV}"
      system("sudo networksetup -setdnsservers #{DEVICE} #{RESOLV}")
    end
  end
end

if ENV['USER'] == 'root'
  run
else
  puts 'Error: Please re-run using sudo due to resolv.conf modifications and use of port 80.'
  exit 1
end

{'fraction_non_alphanumeric': 0.14738610119672477, 'fraction_numerical': 0.029393239554902372, 'mean_word_length': 2.7073929961089496, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 16, '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': '5305395', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1767, 'n_words': 431}