class CC::CLI::Prepare

Constants

ARGUMENT_LIST
FetchError
HELP
InternalHostError
PRIVATE_ADDRESS_SUBNETS
SHORT_HELP

Public Instance Methods

run() click to toggle source
# File lib/cc/cli/prepare.rb, line 32
def run
  ::CC::Resolv.with_fixed_dns { fetch_all }
rescue FetchError, InternalHostError => ex
  fatal(ex.message)
end

Private Instance Methods

allow_internal_ips?() click to toggle source
# File lib/cc/cli/prepare.rb, line 40
def allow_internal_ips?
  @args.include?("--allow-internal-ips")
end
config() click to toggle source
# File lib/cc/cli/prepare.rb, line 48
def config
  @config ||= CC::Config.load
end
ensure_external!(url) click to toggle source
# File lib/cc/cli/prepare.rb, line 78
def ensure_external!(url)
  uri = URI.parse(url)

  if internal?(uri.host)
    raise InternalHostError, "Won't fetch #{url.inspect}: it maps to an internal address"
  end
end
fetch(url, target_path) click to toggle source
# File lib/cc/cli/prepare.rb, line 58
def fetch(url, target_path)
  ensure_external!(url) unless allow_internal_ips?

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  resp = http.get(uri)
  if resp.code == "200"
    write_file(target_path, resp.body)
    say("Wrote #{url} to #{target_path}")
  else
    raise FetchError, "Failed fetching #{url}: code=#{resp.code}"
  end
end
fetch_all() click to toggle source
# File lib/cc/cli/prepare.rb, line 52
def fetch_all
  fetches.each do |entry|
    fetch(entry.url, entry.path)
  end
end
fetches() click to toggle source
# File lib/cc/cli/prepare.rb, line 44
def fetches
  @fetches ||= config.prepare.fetch
end
internal?(host) click to toggle source

rubocop:disable Style/CaseEquality

# File lib/cc/cli/prepare.rb, line 87
def internal?(host)
  address = ::Resolv.getaddress(host)

  PRIVATE_ADDRESS_SUBNETS.any? do |subnet|
    subnet === IPAddr.new(address.to_s)
  end
rescue ::Resolv::ResolvError
  true # localhost
end
write_file(target_path, content) click to toggle source
# File lib/cc/cli/prepare.rb, line 73
def write_file(target_path, content)
  FileUtils.mkdir_p(Pathname.new(target_path).parent.to_s)
  File.write(target_path, content)
end