Sha256: 93919a90f1300d9f81c7c2ef1cd6e7f6976848bd44974fe31b6d963ecf0a7441

Contents?: true

Size: 1021 Bytes

Versions: 2

Compression:

Stored size: 1021 Bytes

Contents

module Kontena::Plugin::Packet
  module SshKeyOption

    DEFAULT_PATH = File.join(Dir.home, '.ssh', 'id_rsa.pub')

    def self.included(base)
      base.option "--ssh-key", "PATH", "Path to ssh public key", attribute_name: :ssh_key_path, default: DEFAULT_PATH
      base.class_eval do
        def ssh_key
          if ssh_key_path
            begin
              return File.read(ssh_key_path).strip
            rescue => ex
              unless ssh_key_path == DEFAULT_PATH
                raise ex
              end
            end
          end

          require 'packet'
          client = Packet::Client.new(self.token || (self.respond_to?(:default_token) && self.default_token))

          keys = client.list_ssh_keys

          if keys.empty?
            prompt.ask('SSH public key: (enter an ssh key in OpenSSH format "ssh-xxx xxxxx key_name")') do |q|
              q.validate /^ssh-rsa \S+ \S+$/
            end
          else
            keys.first.key
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kontena-plugin-packet-0.2.7 lib/kontena/plugin/packet/ssh_key_option.rb
kontena-plugin-packet-0.2.7.rc1 lib/kontena/plugin/packet/ssh_key_option.rb