class ElasticDot::Command::Db

Public Class Methods

console(opts) click to toggle source
# File lib/elasticdot/command/db.rb, line 37
def self.console(opts)
  unless which 'mysql'
    puts 'MySQL client is not installed.'
    puts 'Please install it to proceed.'
    exit 1
  end

  find_db! opts

  info = api.get("/databases/#{@db}")

  uri = URI.parse info['uri']

  puts 'Attaching... '
  system "mysql -f -u#{info['user']} -p#{info['pass']} -h#{uri.host} -P#{uri.port} #{info['name']}"
end
create(opts) click to toggle source
# File lib/elasticdot/command/db.rb, line 83
def self.create(opts)
  find_plan! opts

  params = {plan: @plan}

  if conf = opts[:conf]
    unless File.exists? conf
      puts "#{conf}: no such file or directory"
      exit 1
    end

    f = File.read conf

    params.merge!(conf: f)
  end

  info = api.post "/databases", params

  spinner "Database #{info['identifier']} is provisioning..." do
    until info['status'] == 'active'
      sleep 3
      info = api.get("/databases/#{info['identifier']}")
    end
  end
end
destroy(opts) click to toggle source
# File lib/elasticdot/command/db.rb, line 109
def self.destroy(opts)
  find_db! opts

  print "Destroying database #{@db}... "

  info = api.delete("/databases/#{@db}")

  puts 'done'
end
dump(opts) click to toggle source
# File lib/elasticdot/command/db.rb, line 21
def self.dump(opts)
  unless which 'mysqldump'
    puts 'MySQL client is not installed.'
    puts 'Please install it to proceed.'
    exit 1
  end

  find_db! opts

  info = api.get("/databases/#{@db}")

  uri = URI.parse info['uri']

  system "mysqldump --opt -c -u#{info['user']} -p#{info['pass']} -h#{uri.host} -P#{uri.port} #{info['name']}"
end
import(args, opts) click to toggle source
# File lib/elasticdot/command/db.rb, line 54
def self.import(args, opts)
  unless which 'mysql'
    puts 'MySQL client is not installed.'
    puts 'Please install it to proceed.'
    exit 1
  end

  find_db! opts

  dump = args.shift
  unless dump
    puts 'Please specify a dump file.'
    exit 1
  end

  unless File.exists? dump
    puts "#{dump}: no such file or directory"
    exit 1
  end

  info = api.get("/databases/#{@db}")

  uri = URI.parse info['uri']

  spinner 'Importing...' do
    system "mysql -f -u#{info['user']} -p#{info['pass']} -h#{uri.host} -P#{uri.port} #{info['name']} < #{dump}"
  end
end
info(opts) click to toggle source
# File lib/elasticdot/command/db.rb, line 119
def self.info(opts)
  find_db! opts

  db  = api.get("/databases/#{@db}")

  puts "=== Database #{db['identifier']}"
  puts "Plan:           \t#{db['plan']['name']}"
  puts "Nodes:          \t#{db['plan']['nodes']}" unless db['plan']['shared']
  puts "Version:        \t#{db['version']}"
  puts "Status:         \t#{db['status']}"
  puts "Name:           \t#{db['name']}"
  puts "User:           \t#{db['user']}"
  puts "Password:       \t#{db['pass']}"
  puts "URI:            \t#{db['uri']}"
  puts "Tables:         \t#{db['tables']}"
  puts "Disk Space Used:\t#{db['space_used']}"
  puts "AVG CPU Load:   \t#{db['cpu_load']}"
  puts "Created at:     \t#{db['created_at']}"
end
list(opts) click to toggle source
# File lib/elasticdot/command/db.rb, line 139
def self.list(opts)
  puts "=== database list"

  list = api.get("/databases")

  list.each {|db| puts db['identifier'] }
end
promote(opts) click to toggle source
# File lib/elasticdot/command/db.rb, line 4
def self.promote(opts)
  find_app! opts
  find_db!  opts

  spinner "Promoting database..." do
    info = api.post "/databases/#{@db}/promote", app: @app
  end

  spinner "Restarting dots..." do
    loop do
      sleep 3
      info = api.get "/domains/#{@app}"
      break if info['status'] == 'active'
    end
  end
end

Private Class Methods

find_db!(opts) click to toggle source
# File lib/elasticdot/command/db.rb, line 148
def self.find_db!(opts)
  @db  = opts[:db]
  return true if @db

  app   = opts[:app]
  app ||= extract_app_in_dir

  unless app
    puts 'No db specified.'
    puts 'Specify at least option --app or --database.'
    exit 1
  end

  info = api.get "/domains/#{app}"

  unless info['db']
    puts 'This app has no database associated.'
    puts 'Specify --database option.'
    exit 1
  end

  @db = info['db']['identifier']

  true
end
find_plan!(opts) click to toggle source
# File lib/elasticdot/command/db.rb, line 174
def self.find_plan!(opts)
  @plan = opts[:plan]
  return true if @plan

  puts 'No plan specified.'
  puts 'Specify which plan to use with -p, --plan PLAN.'
  exit 1
end