class Heroku::Kensa::Client

Attributes

options[RW]

Public Class Methods

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

Public Instance Methods

create() click to toggle source
# File lib/heroku/kensa/client.rb, line 34
def create
  app_name = @args.shift
  template = @options[:template]
  raise CommandInvalid.new("Need to supply an application name") unless app_name
  raise CommandInvalid.new("Need to supply a template") unless template
  begin
    Git.clone(app_name, template) and screen.message "Created #{app_name} from #{template} template\n"
    Dir.chdir(app_name)
    @options[:foreman] = true
    init
  rescue Exception => e
    raise CommandInvalid.new("error cloning #{Git.clone_url(template)} into #{app_name}") 
  end
end
init() click to toggle source
# File lib/heroku/kensa/client.rb, line 24
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 for foreman\n"
  end
end
pull() click to toggle source
# File lib/heroku/kensa/client.rb, line 103
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 87
def push
  user, password = ask_for_credentials
  host     = heroku_host
  data     = decoded_manifest
  resource = RestClient::Resource.new(host, user, password)
  resource['provider/addons'].post(resolve_manifest, headers)
  puts "-----> Manifest for \"#{data['id']}\" was pushed successfully"
  puts "       Continue at #{(heroku_host)}/provider/addons/#{data['id']}"
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 74
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 18
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 79
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 49
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 115
def version
  puts "Kensa #{VERSION}"
end

Private Instance Methods

ask_for_credentials() click to toggle source
# File lib/heroku/kensa/client.rb, line 187
def ask_for_credentials
  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 216
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 199
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 152
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 179
def echo_off
  system "stty -echo"
end
echo_on() click to toggle source
# File lib/heroku/kensa/client.rb, line 183
def echo_on
  system "stty echo"
end
filename() click to toggle source
# File lib/heroku/kensa/client.rb, line 128
def filename
  @options[:filename]
end
headers() click to toggle source
# File lib/heroku/kensa/client.rb, line 136
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 140
def heroku_host
  ENV['ADDONS_URL'] || 'https://addons.heroku.com'
end
manifest_exists?() click to toggle source
# File lib/heroku/kensa/client.rb, line 158
def manifest_exists?
  File.exists?(filename)
end
protect_current_manifest!() click to toggle source
# File lib/heroku/kensa/client.rb, line 120
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 144
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 162
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 175
def running_on_windows?
  RUBY_PLATFORM =~ /mswin32|mingw32/
end
screen() click to toggle source
# File lib/heroku/kensa/client.rb, line 132
def screen
  @screen ||= @options[:silent] ? NilScreen.new : Screen.new
end