class DokkuInstaller::Cli

Public Instance Methods

config() click to toggle source
# File lib/dokku_installer/config.rb, line 5
def config
  run_command "config #{app_name}"
end
config_get(key) click to toggle source
# File lib/dokku_installer/config.rb, line 10
def config_get(key)
  run_command "config:get #{app_name} #{key}"
end
config_set(*args) click to toggle source
# File lib/dokku_installer/config.rb, line 15
def config_set(*args)
  # FIXME: Requires root to send config values with spaces
  user = "dokku"

  args = args.map{|arg|
    key_value = arg.split("=")
    if key_value.length == 2
      if key_value[1].index(" ")
        user = "root"
        return_value  = "#{key_value[0]}="
        return_value += '\"'
        return_value += key_value[1].gsub(/"|'/, "")
        return_value += '\"'
        return_value
      else
        "#{key_value[0]}=#{key_value[1].gsub(/"|'/, "")}"
      end
    else
      arg
    end
  }

  command  = "ssh #{user}@#{domain} "
  command += user == "root" ? "dokku " : ""
  command += "config:set #{app_name} #{args.join(' ')}"

  puts "Running #{command}..."
  exec(command)
end
config_unset(*args) click to toggle source
# File lib/dokku_installer/config.rb, line 46
def config_unset(*args)
  run_command "config:unset #{app_name} #{args.join(' ')}"
end
domains() click to toggle source
# File lib/dokku_installer/domains.rb, line 5
def domains
  run_command "domains #{app_name}"
end
domains_set(*args) click to toggle source
# File lib/dokku_installer/domains.rb, line 10
def domains_set(*args)
  run_command "domains:set #{app_name} #{args.join(' ')}"
end
postgres_backups() click to toggle source
# File lib/dokku_installer/postgres.rb, line 5
def postgres_backups
  command = "ssh -t dokku@#{domain} postgres:backups #{app_name}"
  puts "Running #{command}..."
  backups = `#{command}`
  backups.split("\n").select{|backup| backup != "" }.reverse.each_with_index do |line, index|
    number = "#{index + 1}"
    if number.length < 2
      number = " #{number}"
    end
    puts "[#{number}] #{line}"
  end
end
postgres_backups_create() click to toggle source
# File lib/dokku_installer/postgres.rb, line 19
def postgres_backups_create
  command = "ssh -t dokku@#{domain} postgres:backups:create #{app_name}"
  puts "Running #{command}..."
  result = `#{command}`

  if result.include?("Backup created")
    puts "Database backup created successfully."
  else
    puts "Database backup could not be created."
  end
end
postgres_backups_disable() click to toggle source
# File lib/dokku_installer/postgres.rb, line 32
def postgres_backups_disable
  run_command "postgres:backups:disable #{app_name}"
end
postgres_backups_download(number = nil) click to toggle source
# File lib/dokku_installer/postgres.rb, line 37
def postgres_backups_download(number = nil)
  number ||= 1

  if backup = backup_filename(number)
    command = "postgres:backups:download #{app_name} #{backup} > #{backup}"
    puts "Saving to local file: #{backup}"
    run_command(command)
  else
    puts "Invalid backup number"
  end
end
postgres_backups_enable() click to toggle source
# File lib/dokku_installer/postgres.rb, line 50
def postgres_backups_enable
  command = "ssh -t root@#{domain} \"dokku postgres:backups:enable #{app_name} && service cron restart\""
  puts "Running #{command}..."
  exec(command)
end
postgres_backups_restore(number = nil) click to toggle source
# File lib/dokku_installer/postgres.rb, line 57
def postgres_backups_restore(number = nil)
  if number.nil?
    puts "You must specify a numbered backup."
    exit
  end

  if backup = backup_filename(number)
    command = "postgres:backups:restore #{app_name} #{backup}"
    run_command(command)
  else
    puts "Invalid backup number"
  end
end
postgres_backups_restore_local(number = nil) click to toggle source
# File lib/dokku_installer/postgres.rb, line 72
def postgres_backups_restore_local(number = nil)
  # Download the backup file
  number ||= 1

  if backup = backup_filename(number)
    command = "ssh -t dokku@#{domain} postgres:backups:download #{app_name} #{backup} > #{backup}"
    puts "Saving to local file: #{backup}"
    `#{command}`

    if psql_options
      command = "psql #{psql_options} --file=#{backup}"
      puts "Running #{command}..."
      `#{command}`
      puts "Deleting #{backup}..."
      `rm #{backup}`
    end
  else
    puts "Invalid backup number"
  end
end
postgres_export(file = "export.sql") click to toggle source
# File lib/dokku_installer/postgres.rb, line 94
def postgres_export(file = "export.sql")
  command = "postgres:dump #{app_name} > #{file}"
  run_command(command)
end
postgres_import(file) click to toggle source
# File lib/dokku_installer/postgres.rb, line 100
def postgres_import(file)
  command = "postgres:restore #{app_name} < #{file}"
  run_command(command)
end
ssl_add(*args) click to toggle source
# File lib/dokku_installer/ssl.rb, line 5
def ssl_add(*args)
  key = nil
  certificate = nil
  intermediate_certificates = []

  args.each do |arg|
    file_contents = File.read(arg)
    if file_contents.include?("KEY")
      key = file_contents
    elsif file_contents.include?("BEGIN CERTIFICATE")
      certificate = file_contents
    elsif file_contents.include?("NEW CERTIFICATE REQUEST")
      intermediate_certificates << file_contents
    end
  end

  if key.nil?
    puts "Missing SSL private key.\nSpecify the key, certificate, and any intermediate certificates."
    exit
  elsif certificate.nil?
    puts "Missing SSL certificate.\nSpecify the key, certificate, and any intermediate certificates."
    exit
  end

  puts "Adding SSL private key..."
  command = "echo \"#{key}\" | ssh dokku@#{domain} ssl:key #{app_name}"
  result = `#{command}`

  puts "Adding SSL certificate..."
  combined_certificate = certificate
  if intermediate_certificates.length > 0
    combined_certificate += "#{intermediate_certificates.join("\n")}\n"
  end
  command = "echo \"#{combined_certificate}\" | ssh dokku@#{domain} ssl:certificate #{app_name}"
  exec(command)
end
ssl_force(domain = nil) click to toggle source
# File lib/dokku_installer/ssl.rb, line 43
def ssl_force(domain = nil)
  if domain.nil?
    puts "Specify a domain to force SSL."
    exit
  end

  run_command "ssl:force #{app_name} #{domain}"
end
ssl_remove() click to toggle source
# File lib/dokku_installer/ssl.rb, line 53
def ssl_remove
  run_command "ssl:delete #{domain} #{app_name}"
end
update() click to toggle source
# File lib/dokku_installer/version.rb, line 18
def update
  command = "gem install dokku-installer-cli"
  puts "Running #{command}..."
  exec(command)
end
upgrade() click to toggle source
# File lib/dokku_installer/version.rb, line 11
def upgrade
  command = "ssh -t root@#{domain} \"rm -rf /var/lib/dokku/plugins; cd /root/dokku; git pull origin master; make install\""
  puts "Running #{command}..."
  exec(command)
end
version() click to toggle source
# File lib/dokku_installer/version.rb, line 25
def version
  gem_version = "v#{DokkuInstaller::VERSION}"

  # Grab the latest version of the RubyGem
  rubygems_json = Net::HTTP.get("rubygems.org", "/api/v1/gems/dokku-installer-cli.json")
  rubygems_version = "v#{JSON.parse(rubygems_json)["version"].strip}"

  # Grab the version of the remote Dokku install
  command = "ssh -t dokku@#{domain} version"
  remote_version = `#{command}`.strip

  upgrade_message = ""
  if gem_version != rubygems_version
    upgrade_message = " Run `dokku update` to install"
  end

  puts
  puts "Dokku Installer CLI"
  puts "  Installed: #{gem_version}"
  puts "  Latest:    #{rubygems_version}#{upgrade_message}"
  puts

  upgrade_message = ""
  if remote_version != rubygems_version
    upgrade_message = " Run `dokku upgrade` to install"
  end

  puts "Remote Dokku Version"
  puts "  Installed: #{remote_version}"
  puts "  Latest:    #{rubygems_version}#{upgrade_message}"
  puts
end

Private Instance Methods

backup_filename(number) click to toggle source
# File lib/dokku_installer/postgres.rb, line 107
def backup_filename(number)
  # Make sure the number is valid or use 1
  number = number.to_s.gsub(/\D/, "").to_i
  number = 1 if number < 1

  # Get the file name for the numbered backup
  puts "Getting list of backups..."
  command = "ssh -t dokku@#{domain} postgres:backups #{app_name}"
  backups = `#{command}`
  index = number - 1
  if filename = backups.split("\n").select{|backup| backup != "" }.reverse[index]
    filename.strip
  else
    nil
  end
end
psql_options() click to toggle source
# File lib/dokku_installer/postgres.rb, line 124
def psql_options
  @psql_options ||= begin
    restore_options = nil

    if File.exist?("./config/database.yml")
      if development_config = YAML::load(IO.read("./config/database.yml"))["development"]
        restore_options = "--host=#{development_config['host']} --dbname=#{development_config['database']}"

        if username = development_config["username"]
          restore_options += " --username=#{username}"
        end

        if port = development_config["port"]
          restore_options += " --port=#{port}"
        end
      else
        puts "Missing database.yml config for development environment"
      end
    else
      puts "Missing file config/database.yml"
    end

    restore_options
  end
end