Class: CM::Resource::AWS

Inherits:
Object
  • Object
show all
Defined in:
lib/CM/resource/AWS.rb

Instance Method Summary collapse

Instance Method Details

#computeObject




39
40
41
# File 'lib/CM/resource/AWS.rb', line 39

def compute
  @compute ||= init_compute
end

#create_keypair(name, data, suppress_info = false) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/CM/resource/AWS.rb', line 122

def create_keypair(name, data, suppress_info = false)
  begin
    info('create_keypair', { :id => id, :name => parameters[:Name] }) unless suppress_info

    private_key_file = key_file(name)
    result = compute.create_key_pair(name)

    Nucleon::Util::Disk.write(private_key_file, result.body['keyMaterial'])
    FileUtils.chmod(0600, private_key_file)

    aws_keypair = fetch_keypair(name, true)
    data[:Fingerprint] = aws_keypair[:fingerprint]
    data[:Content] = result.body['keyMaterial']
    data[:File] = aws_keypair[:file]

  rescue => error
    myself.status = code.aws_request_failed
    raise error
  end
end

#create_resourceObject


Operations



52
53
54
55
56
57
58
59
60
61
# File 'lib/CM/resource/AWS.rb', line 52

def create_resource
  super do |data|
    if template == :keypair
      create_keypair(parameters[:Name], data)
    else
      #info('create_stack', { :id => id, :name => template, :prefix => false })
      #create_stack(template, parameters, data)
    end
  end
end

#create_stack(name, parameters = {}, data) ⇒ Object



195
196
197
# File 'lib/CM/resource/AWS.rb', line 195

def create_stack(name, parameters = {}, data)

end

#delete_keypair(name, data) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/CM/resource/AWS.rb', line 174

def delete_keypair(name, data)
  begin
    info('delete_keypair', { :id => id, :name => parameters[:Name] })

    if aws_keypair = fetch_keypair(name)
      result = compute.delete_key_pair(name)
      File.delete(aws_keypair[:file]) if File.exist?(aws_keypair[:file])
    end
  rescue => error
    myself.status = code.aws_request_failed
    raise error
  end
end

#delete_resourceObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/CM/resource/AWS.rb', line 85

def delete_resource
  super do |data|
    if template == :keypair
      delete_keypair(parameters[:Name], data)
    else
      #info('delete_stack', { :id => id, :name => template, :prefix => false })
      #delete_stack(template, data)
    end
  end
end

#delete_stack(name, data) ⇒ Object



203
204
205
# File 'lib/CM/resource/AWS.rb', line 203

def delete_stack(name, data)

end

#fetch_keypair(name, reset = false) ⇒ Object


Keypair related functionality



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/CM/resource/AWS.rb', line 99

def fetch_keypair(name, reset = false)
  if reset || !@keypair
    begin
      aws_keypair = nil

      result = compute.describe_key_pairs({ 'key-name' => [name] })
      aws_keypair = result.body['keySet'].first['keyFingerprint'] if result.body['keySet'].length == 1

      if aws_keypair
        @keypair = {
          :name => name.to_sym,
          :fingerprint => aws_keypair,
          :file => key_file(name)
        }
      end
    rescue => error
      # Placeholder for logging in the future
      raise error
    end
  end
  @keypair
end

#fetch_stack(name, data) ⇒ Object


Stack related functionality



191
192
193
# File 'lib/CM/resource/AWS.rb', line 191

def fetch_stack(name, data)

end

#init_computeObject


Utilities



210
211
212
213
214
215
216
217
218
219
# File 'lib/CM/resource/AWS.rb', line 210

def init_compute
  require 'fog/aws'

  Fog::Compute.new({
    :provider => 'AWS',
    :region => manifest_config[:aws][:Region],
    :aws_access_key_id => manifest_config[:aws][:AccessKey],
    :aws_secret_access_key => manifest_config[:aws][:SecretAccessKey]
  })
end

#initialized?(options = {}) ⇒ Boolean


Checks

Returns:

  • (Boolean)


20
21
22
# File 'lib/CM/resource/AWS.rb', line 20

def initialized?(options = {})
  true
end

#key_file(name) ⇒ Object




45
46
47
# File 'lib/CM/resource/AWS.rb', line 45

def key_file(name)
  File.join(plan.key_directory, "#{name}.pem")
end

#manifest_configObject




33
34
35
# File 'lib/CM/resource/AWS.rb', line 33

def manifest_config
  @manifest_config ||= plan.manifest_config
end

#normalize(reload) ⇒ Object


Plugin interface



9
10
11
12
13
14
15
# File 'lib/CM/resource/AWS.rb', line 9

def normalize(reload)
  super

  codes :aws_request_failed

  yield if block_given?
end

#retrieve_resourceObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/CM/resource/AWS.rb', line 63

def retrieve_resource
  resource = nil
  if template == :keypair
    resource = fetch_keypair(parameters[:Name])
  else
    #resource = fetch_stack(template)
    #dbg(resource, 'retrieval results')
  end
  resource
end

#templateObject


Property accessors / modifiers



27
28
29
# File 'lib/CM/resource/AWS.rb', line 27

def template
  settings[:template].to_sym
end

#update_keypair(name, data) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/CM/resource/AWS.rb', line 143

def update_keypair(name, data)
  begin
    info('check_keypair', { :id => id, :name => parameters[:Name] })

    aws_keypair = fetch_keypair(name)
    private_key_file = aws_keypair[:file]
    update = false

    if File.exist?(private_key_file)
      local_keypair = `openssl pkcs8 -in #{private_key_file} -inform PEM -outform DER -topk8 -nocrypt | openssl sha1 -c`.strip.sub(/^[^\s]+\s+/, '')

      if aws_keypair[:fingerprint] != local_keypair
        File.delete(private_key_file)
        update = true
      end
    else
      update = true
    end

    if update
      info('update_keypair', { :id => id, :name => parameters[:Name] })

      compute.delete_key_pair(name)
      create_keypair(name, data, true)
    end
  rescue => error
    myself.status = code.aws_request_failed
    raise error
  end
end

#update_resourceObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/CM/resource/AWS.rb', line 74

def update_resource
  super do |data|
    if template == :keypair
      update_keypair(parameters[:Name], data)
    else
      #info('update_stack', { :id => id, :name => template, :prefix => false })
      #update_stack(template, parameters, data)
    end
  end
end

#update_stack(name, parameters = {}, data) ⇒ Object



199
200
201
# File 'lib/CM/resource/AWS.rb', line 199

def update_stack(name, parameters = {}, data)

end