class Heroku::Kensa::Client

Attributes

options[RW]

Public Class Methods

new(args, options = {}) click to toggle source
# File lib/heroku/kensa/client.rb, line 12
def initialize(args, options = {})
  @args    = args
  @options = OptParser.parse(args).merge(options)
end

Public Instance Methods

init() click to toggle source
# File lib/heroku/kensa/client.rb, line 25
def init
  manifest = Manifest.new(@options)
  protect_current_manifest!
  manifest.write
  screen.message "Initialized new addon manifest in #{filename}\n"
  if @options[:foreman]
    screen.message "Initialized new .env file\n"
  end
end
pull() click to toggle source
# File lib/heroku/kensa/client.rb, line 93
def pull
  addon = @args.first || abort('usage: kensa pull <add-on name>')
  protect_current_manifest!

  user, password = ask_for_credentials
  host     = heroku_host
  resource = RestClient::Resource.new(host, user, password)
  manifest = resource["provider/addons/#{addon}"].get(headers)
  File.open(filename, 'w') { |f| f.puts manifest }
  puts "-----> Manifest for \"#{addon}\" received successfully"
end
push() click to toggle source
# File lib/heroku/kensa/client.rb, line 73
def push
  user, password = ask_for_credentials
  host     = heroku_host
  data     = decoded_manifest
  resource = RestClient::Resource.new(host, user, password)
  manifest = resource['provider/addons'].post(resolve_manifest, headers)

  puts "-----> Manifest for \"#{data['id']}\" was pushed successfully"
  puts "       Continue at #{(heroku_host)}/provider/addons/#{data['id']}"

  # Update local manifest with response from addons.heroku.com
  File.open(filename, 'w') { |f| f.puts manifest } unless manifest.strip.empty?
rescue RestClient::UnprocessableEntity, RestClient::BadRequest => e
  abort("FAILED: #{e.response}")
rescue RestClient::Unauthorized
  abort("Authentication failure")
rescue RestClient::Forbidden
  abort("Not authorized to push this manifest. Please make sure you have permissions to push it")
end
run() click to toggle source
# File lib/heroku/kensa/client.rb, line 60
def run
  abort "! missing command to run; see usage" if @args.empty?
  run_check AllCheck, :args => @args
end
run!() click to toggle source
# File lib/heroku/kensa/client.rb, line 19
def run!
  command = @args.shift || @options[:command]
  raise CommandInvalid unless command && respond_to?(command)
  send(command)
end
sso() click to toggle source
# File lib/heroku/kensa/client.rb, line 65
def sso
  id = @args.shift || abort("! no id specified; see usage")
  data = decoded_manifest
  sso = Sso.new(data.merge(@options).merge(:id => id)).start
  puts sso.message
  Launchy.open sso.sso_url
end
test() click to toggle source
# File lib/heroku/kensa/client.rb, line 35
def test
  case check = @args.shift
    when "manifest"
      run_check ManifestCheck
    when "provision"
      run_check ManifestCheck, ProvisionCheck
    when "deprovision"
      id = @args.shift || abort("! no id specified; see usage")
      run_check ManifestCheck, DeprovisionCheck, :id => id
    when "planchange"
      id   = @args.shift || abort("! no id specified; see usage")
      plan = @args.shift || abort("! no plan specified; see usage")
      run_check ManifestCheck, PlanChangeCheck, :id => id, :plan => plan
    when "sso"
      id = @args.shift || abort("! no id specified; see usage")
      run_check ManifestCheck, SsoCheck, :id => id
    when "all"
      run_check AllCheck
    when nil
      run_check AllCheck
    else
      abort "! Unknown test '#{check}'; see usage"
  end
end
version() click to toggle source
# File lib/heroku/kensa/client.rb, line 105
def version
  puts "Kensa #{VERSION}"
end

Private Instance Methods

ask_for_credentials() click to toggle source
# File lib/heroku/kensa/client.rb, line 177
def ask_for_credentials
  netrc_creds = Netrc.read['api.heroku.com']
  if netrc_creds
    print "Found credentials for #{netrc_creds.login}, proceed? (y/N) "
    if gets.chomp.downcase == "y"
      return [ netrc_creds.login, netrc_creds.password]
    end
  end

  puts "Enter your Heroku Provider credentials."

  print "Email: "
  user = gets.strip

  print "Password: "
  password = running_on_windows? ? ask_for_password_on_windows : ask_for_password

  [ user, password ]
end
ask_for_password() click to toggle source
# File lib/heroku/kensa/client.rb, line 214
def ask_for_password
  echo_off
  password = gets.strip
  puts
  echo_on
  return password
end
ask_for_password_on_windows() click to toggle source
# File lib/heroku/kensa/client.rb, line 197
def ask_for_password_on_windows
  require "Win32API"
  char = nil
  password = ''

  while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do
    break if char == 10 || char == 13 # received carriage return or newline
    if char == 127 || char == 8 # backspace and delete
      password.slice!(-1, 1)
    else
      password << char.chr
    end
  end
  puts
  return password
end
decoded_manifest() click to toggle source
# File lib/heroku/kensa/client.rb, line 142
def decoded_manifest
  OkJson.decode(resolve_manifest)
rescue OkJson::Error => e
  raise CommandInvalid, "#{filename} includes invalid JSON"
end
echo_off() click to toggle source
# File lib/heroku/kensa/client.rb, line 169
def echo_off
  system "stty -echo"
end
echo_on() click to toggle source
# File lib/heroku/kensa/client.rb, line 173
def echo_on
  system "stty echo"
end
filename() click to toggle source
# File lib/heroku/kensa/client.rb, line 118
def filename
  @options[:filename]
end
headers() click to toggle source
# File lib/heroku/kensa/client.rb, line 126
def headers
  { :accept => :json, "X-Kensa-Version" => "1", "User-Agent" => "kensa/#{VERSION}" }
end
heroku_host() click to toggle source
# File lib/heroku/kensa/client.rb, line 130
def heroku_host
  ENV['ADDONS_URL'] || 'https://addons.heroku.com'
end
manifest_exists?() click to toggle source
# File lib/heroku/kensa/client.rb, line 148
def manifest_exists?
  File.exists?(filename)
end
protect_current_manifest!() click to toggle source
# File lib/heroku/kensa/client.rb, line 110
def protect_current_manifest!
  if manifest_exists?
    print "Manifest already exists. Replace it? (y/n) "
    abort unless gets.strip.downcase == 'y'
    puts
  end
end
resolve_manifest() click to toggle source
# File lib/heroku/kensa/client.rb, line 134
def resolve_manifest
  if manifest_exists?
    File.read(filename)
  else
    abort("fatal: #{filename} not found")
  end
end
run_check(*args) click to toggle source
# File lib/heroku/kensa/client.rb, line 152
def run_check(*args)
  options = {}
  options = args.pop if args.last.is_a?(Hash)

  args.each do |klass|
    data   = decoded_manifest
    check  = klass.new(data.merge(@options.merge(options)), screen)
    result = check.call
    screen.finish
    exit 1 if !result && !(@options[:test])
  end
end
running_on_windows?() click to toggle source
# File lib/heroku/kensa/client.rb, line 165
def running_on_windows?
  RUBY_PLATFORM =~ /mswin32|mingw32/
end
screen() click to toggle source
# File lib/heroku/kensa/client.rb, line 122
def screen
  @screen ||= @options[:silent] ? NilScreen.new : Screen.new
end