Class: CFnDK::KeyPair
- Inherits:
-
Object
- Object
- CFnDK::KeyPair
- Defined in:
- lib/cfndk/key_pair.rb
Instance Attribute Summary collapse
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#key_file ⇒ Object
readonly
Returns the value of attribute key_file.
-
#post_command ⇒ Object
readonly
Returns the value of attribute post_command.
-
#pre_command ⇒ Object
readonly
Returns the value of attribute pre_command.
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(name, data, option, global_config, credentials) ⇒ KeyPair
constructor
A new instance of KeyPair.
- #name ⇒ Object
- #original_name ⇒ Object
- #post_command_execute ⇒ Object
- #pre_command_execute ⇒ Object
Constructor Details
#initialize(name, data, option, global_config, credentials) ⇒ KeyPair
Returns a new instance of KeyPair.
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 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 |
Instance Attribute Details
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
3 4 5 |
# File 'lib/cfndk/key_pair.rb', line 3 def enabled @enabled end |
#key_file ⇒ Object (readonly)
Returns the value of attribute key_file.
3 4 5 |
# File 'lib/cfndk/key_pair.rb', line 3 def key_file @key_file end |
#post_command ⇒ Object (readonly)
Returns the value of attribute post_command.
3 4 5 |
# File 'lib/cfndk/key_pair.rb', line 3 def post_command @post_command end |
#pre_command ⇒ Object (readonly)
Returns the value of attribute pre_command.
3 4 5 |
# File 'lib/cfndk/key_pair.rb', line 3 def pre_command @pre_command end |
Instance Method Details
#create ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# 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 ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# 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? ⇒ Boolean
42 43 44 45 46 47 48 49 50 |
# 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 ⇒ Object
52 53 54 |
# File 'lib/cfndk/key_pair.rb', line 52 def name [@name, @option[:uuid]].compact.join('-') end |
#original_name ⇒ Object
56 57 58 |
# File 'lib/cfndk/key_pair.rb', line 56 def original_name @name end |
#post_command_execute ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# 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 ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 |