Sha256: 38bb8031d0d1f713d2f6b634560c22a053a172c236b24b97136986c481897f2e

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

# -*- coding: utf-8 -*-

require 'tmpdir'

module Dcmgr::Models
  # SSH Key database for account.
  class SshKeyPair < AccountResource
    taggable 'ssh'

    inheritable_schema do
      String :name, :size=>100, :null=>false
      Text :public_key, :null=>false
      Text :private_key, :null=>true
      
      index [:account_id, :name], {:unique=>true}
    end
    with_timestamps

    def validate
    end

    def before_destroy
      # TODO: check running instances which are associated to ssh key
      # pairs. reject deletion if exist.
    end

    # 
    # @return [Hash] {:private_key=>'pkey string',
    #                 :public_key=>'pubkey string'}
    def self.generate_key_pair()
      pkey = File.expand_path(randstr, Dir.tmpdir)
      pubkey = pkey + '.pub'
      begin
        system("ssh-keygen -q -t rsa -C '' -N '' -f %s >/dev/null" % [pkey])
        unless $?.exitstatus == 0
          raise "Failed to run ssh-keygen: exitcode=#{$?.exitstatus}"
        end
        
        {:private_key=>IO.read(pkey),
          :public_key=>IO.read(pubkey)}
      rescue
        # clean up tmp key files
        [pkey, pubkey].each { |i|
          File.unlink(i) if File.exist?(i)
        }
      end
    end

    private
    def self.randstr
      Array.new(10) {  (('a'..'z').to_a + (0..9).to_a)[rand(36)] }.join
    end
    
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
wakame-vdc-dcmgr-10.12.0 lib/dcmgr/models/ssh_key_pair.rb
wakame-vdc-agents-10.12.0 lib/dcmgr/models/ssh_key_pair.rb
wakame-vdc-dcmgr-10.11.0 lib/dcmgr/models/ssh_key_pair.rb
wakame-vdc-agents-10.11.0 lib/dcmgr/models/ssh_key_pair.rb