class CFnDK::KeyPair

Attributes

enabled[R]
key_file[R]
post_command[R]
pre_command[R]

Public Class Methods

new(name, data, option, global_config, credentials) click to toggle source
# File lib/cfndk/key_pair.rb, line 4
def initialize(name, data, option, global_config, credentials)
  @global_config = global_config
  @name = name
  data = {} unless data
  @key_file = data['key_file'] || nil
  @region = data['region'] || @global_config.region
  @pre_command = data['pre_command'] || nil
  @post_command = data['post_command'] || nil
  @enabled = true
  @enabled = false if data['enabled'] === false
  @option = option
  @client = Aws::EC2::Client.new(credentials: credentials, region: @region)
end

Public Instance Methods

create() click to toggle source
# File lib/cfndk/key_pair.rb, line 18
def create
  return unless @enabled
  CFnDK.logger.info(('creating keypair: ' + name).color(:green))
  key_pair = @client.create_key_pair(
    key_name: name
  )
  CFnDK.logger.info(('created keypair: ' + name).color(:green))

  create_key_file(key_pair)
end
destroy() click to toggle source
# File lib/cfndk/key_pair.rb, line 29
def destroy
  return unless @enabled
  if exists?
    CFnDK.logger.info(('deleting keypair: ' + name).color(:green))
    @client.delete_key_pair(
      key_name: name
    )
    CFnDK.logger.info(('deleted keypair: ' + name).color(:green))
  else
    CFnDK.logger.info(('do not delete keypair: ' + name).color(:red))
  end
end
exists?() click to toggle source
# File lib/cfndk/key_pair.rb, line 42
def exists?
  !@client.describe_key_pairs(
    key_names: [
      name,
    ]
  ).key_pairs.empty?
rescue Aws::EC2::Errors::InvalidKeyPairNotFound
  false
end
name() click to toggle source
# File lib/cfndk/key_pair.rb, line 52
def name
  [@name, @option[:uuid]].compact.join('-')
end
original_name() click to toggle source
# File lib/cfndk/key_pair.rb, line 56
def original_name
  @name
end
post_command_execute() click to toggle source
# File lib/cfndk/key_pair.rb, line 73
def post_command_execute
  return unless @enabled
  if @post_command
    CFnDK.logger.info(('execute post command: ' + @post_command).color(:green))
    IO.popen(@post_command, :err => [:child, :out]) do |io|
      io.each_line do |line|
        CFnDK.logger.info((line).color(:green))
      end
    end
    raise 'post command is error. status: ' + $?.exitstatus.to_s + ' command: ' + @post_command if $?.exitstatus != 0
  end
end
pre_command_execute() click to toggle source
# File lib/cfndk/key_pair.rb, line 60
def pre_command_execute
  return unless @enabled
  if @pre_command
    CFnDK.logger.info(('execute pre command: ' + @pre_command).color(:green))
    IO.popen(@pre_command, :err => [:child, :out]) do |io|
      io.each_line do |line|
        CFnDK.logger.info((line).color(:green))
      end
    end
    raise 'pre command is error. status: ' + $?.exitstatus.to_s + ' command: ' + @pre_command if $?.exitstatus != 0
  end
end

Private Instance Methods

create_key_file(key_pair) click to toggle source
# File lib/cfndk/key_pair.rb, line 88
def create_key_file(key_pair)
  return unless @key_file
  key_file = CFnDK::ErbString.new(@key_file, @option).value
  CFnDK.logger.info(('create key file: ' + key_file).color(:green))
  FileUtils.mkdir_p(File.dirname(key_file))
  File.write(key_file, key_pair.key_material)
end