class VagrantPlugins::DigitalOcean::Actions::SetupKey

Public Class Methods

new(app, env) click to toggle source
# File lib/vagrant-digitalocean/actions/setup_key.rb, line 9
def initialize(app, env)
  @app = app
  @machine = env[:machine]
  @client = client
  @logger = Log4r::Logger.new('vagrant::digitalocean::setup_key')
end

Public Instance Methods

call(env) click to toggle source

TODO check the content of the key to see if it has changed

# File lib/vagrant-digitalocean/actions/setup_key.rb, line 17
def call(env)
  ssh_key_name = @machine.provider_config.ssh_key_name

  begin
    # assigns existing ssh key id to env for use by other commands
    env[:ssh_key_id] = @client
      .request('/v2/account/keys')
      .find_id(:ssh_keys, :name => ssh_key_name)

    env[:ui].info I18n.t('vagrant_digital_ocean.info.using_key', {
      :name => ssh_key_name
    })
  rescue Errors::ResultMatchError
    env[:ssh_key_id] = create_ssh_key(ssh_key_name, env)
  end

  @app.call(env)
end

Private Instance Methods

create_ssh_key(name, env) click to toggle source
# File lib/vagrant-digitalocean/actions/setup_key.rb, line 38
def create_ssh_key(name, env)
  # assumes public key exists on the same path as private key with .pub ext
  path = @machine.config.ssh.private_key_path
  path = path[0] if path.is_a?(Array)
  path = File.expand_path(path, @machine.env.root_path)
  pub_key = DigitalOcean.public_key(path)

  env[:ui].info I18n.t('vagrant_digital_ocean.info.creating_key', {
    :name => name
  }) 

  result = @client.post('/v2/account/keys', {
    :name => name,
    :public_key => pub_key
  })
  result['ssh_key']['id']
end