foreman_digitalocean 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,16 +16,30 @@ module FogExtensions
16
16
  @flavor ||= service.flavors.get(flavor_id.to_i)
17
17
  end
18
18
 
19
+ def flavor_name
20
+ requires :flavor
21
+ @flavor_name ||= @flavor.try(:name)
22
+ end
23
+
19
24
  def image
20
25
  requires :image_id
21
26
  @image ||= service.images.get(image_id.to_i)
22
27
  end
23
28
 
29
+ def image_name
30
+ @image_name ||= @image.try(:name)
31
+ end
32
+
24
33
  def region
25
34
  requires :region_id
26
35
  @region ||= service.regions.get(region_id.to_i)
27
36
  end
28
37
 
38
+ def region_name
39
+ requires :region
40
+ @region_name ||= @region.try(:name)
41
+ end
42
+
29
43
  def ip_addresses
30
44
  [public_ip_address, private_ip_address].flatten.select(&:present?)
31
45
  end
@@ -6,6 +6,10 @@ module ForemanDigitalocean
6
6
  validates :user, :password, :presence => true
7
7
  before_create :test_connection
8
8
 
9
+ after_create :setup_key_pair
10
+ after_destroy :destroy_key_pair
11
+
12
+
9
13
  # Not sure why it would need a url, but OK (copied from ec2)
10
14
  alias_attribute :region, :url
11
15
 
@@ -32,6 +36,7 @@ module ForemanDigitalocean
32
36
  end
33
37
 
34
38
  def create_vm(args = { })
39
+ args["ssh_keys"] = [ssh_key] if ssh_key
35
40
  super(args)
36
41
  rescue Fog::Errors::Error => e
37
42
  logger.error "Unhandled DigitalOcean error: #{e.class}:#{e.message}\n " + e.backtrace.join("\n ")
@@ -101,5 +106,52 @@ module ForemanDigitalocean
101
106
  )
102
107
  end
103
108
 
109
+
110
+ # this method creates a new key pair for each new DigitalOcean compute resource
111
+ # it should create the key and upload it to DigitalOcean
112
+ def setup_key_pair
113
+ public_key, private_key = generate_key
114
+ key_name = "foreman-#{id}#{Foreman.uuid}"
115
+ key = client.create_ssh_key key_name, public_key
116
+ KeyPair.create! :name => key_name, :compute_resource_id => self.id, :secret => private_key
117
+ rescue => e
118
+ logger.warn "failed to generate key pair"
119
+ logger.error e.message
120
+ logger.error e.backtrace.join("\n")
121
+ destroy_key_pair
122
+ raise
123
+ end
124
+
125
+ def destroy_key_pair
126
+ return unless key_pair
127
+ logger.info "removing DigitalOcean key #{key_pair.name}"
128
+ client.destroy_ssh_key(ssh_key.id) if ssh_key
129
+ key_pair.destroy
130
+ true
131
+ rescue => e
132
+ logger.warn "failed to delete key pair from DigitalOcean, you might need to cleanup manually : #{e}"
133
+ end
134
+
135
+ def ssh_key
136
+ @ssh_key ||= begin
137
+ key = client.list_ssh_keys.data[:body]["ssh_keys"].select{|i| i["name"] == key_pair.name}.first
138
+ if key
139
+ #the vm creator expects objects which respond to id, OpenStruct is the shortest solution.
140
+ OpenStruct.new(key)
141
+ else
142
+ nil
143
+ end
144
+ end
145
+ end
146
+
147
+ def generate_key
148
+ key = OpenSSL::PKey::RSA.new 2048
149
+ type = key.ssh_type
150
+ data = [ key.to_blob ].pack('m0')
151
+
152
+ openssh_format_public_key = "#{type} #{data}"
153
+ [openssh_format_public_key, key.to_pem]
154
+ end
155
+
104
156
  end
105
157
  end
@@ -6,9 +6,8 @@
6
6
  <%= prop :private_ip_address %>
7
7
  <%= prop :state %>
8
8
  <%= prop :created_at, 'Created' %>
9
- <%= prop :image_id, 'Image ID' %>
10
- <%= prop @vm.image.name, 'Image' if @vm.image.present? %>
11
- <%= prop :flavor_id, 'Flavor ID' %>
12
- <%= prop :vm_description, "Type (flavor)" %>
9
+ <%= prop :image_name, 'Image' if @vm.image.present? %>
10
+ <%= prop :flavor_name, "Type (flavor)" %>
11
+ <%= prop :region_name, "Region" %>
13
12
  </table>
14
13
  </div>
@@ -1,3 +1,3 @@
1
1
  module ForemanDigitalocean
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,18 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_digitalocean
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Tommy McNeely, Daniel Lobato
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-12-10 00:00:00.000000000 Z
12
+ date: 2015-02-13 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rubocop
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ~>
18
20
  - !ruby/object:Gem::Version
@@ -20,6 +22,7 @@ dependencies:
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - ~>
25
28
  - !ruby/object:Gem::Version
@@ -31,46 +34,46 @@ executables: []
31
34
  extensions: []
32
35
  extra_rdoc_files: []
33
36
  files:
34
- - LICENSE
35
- - README.md
36
- - app/models/concerns/fog_extensions/digitalocean/image.rb
37
- - app/models/concerns/fog_extensions/digitalocean/server.rb
38
- - app/models/foreman_digitalocean/digitalocean.rb
39
- - app/views/api/v1/compute_resources/digitalocean.json
37
+ - app/views/compute_resources_vms/index/_digitalocean.html.erb
38
+ - app/views/compute_resources_vms/form/_digitalocean.html.erb
39
+ - app/views/compute_resources_vms/show/_digitalocean.html.erb
40
40
  - app/views/api/v2/compute_resources/digitalocean.json
41
+ - app/views/api/v1/compute_resources/digitalocean.json
41
42
  - app/views/compute_resources/form/_digitalocean.html.erb
42
43
  - app/views/compute_resources/show/_digitalocean.html.erb
43
- - app/views/compute_resources_vms/form/_digitalocean.html.erb
44
- - app/views/compute_resources_vms/index/_digitalocean.html.erb
45
- - app/views/compute_resources_vms/show/_digitalocean.html.erb
46
44
  - app/views/images/form/_digitalocean.html.erb
45
+ - app/models/foreman_digitalocean/digitalocean.rb
46
+ - app/models/concerns/fog_extensions/digitalocean/server.rb
47
+ - app/models/concerns/fog_extensions/digitalocean/image.rb
47
48
  - lib/foreman_digitalocean.rb
48
- - lib/foreman_digitalocean/engine.rb
49
49
  - lib/foreman_digitalocean/version.rb
50
+ - lib/foreman_digitalocean/engine.rb
50
51
  - locale/Makefile
52
+ - LICENSE
53
+ - README.md
51
54
  homepage: http://github.com/theforeman/foreman-digitalocean
52
55
  licenses:
53
56
  - GPL-3
54
- metadata: {}
55
57
  post_install_message:
56
58
  rdoc_options: []
57
59
  require_paths:
58
60
  - lib
59
61
  required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
60
63
  requirements:
61
- - - '>='
64
+ - - ! '>='
62
65
  - !ruby/object:Gem::Version
63
66
  version: '0'
64
67
  required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
65
69
  requirements:
66
- - - '>='
70
+ - - ! '>='
67
71
  - !ruby/object:Gem::Version
68
72
  version: '0'
69
73
  requirements: []
70
74
  rubyforge_project:
71
- rubygems_version: 2.2.2
75
+ rubygems_version: 1.8.23
72
76
  signing_key:
73
- specification_version: 4
77
+ specification_version: 3
74
78
  summary: Provision and manage DigitalOcean droplets from Foreman
75
79
  test_files: []
76
- has_rdoc:
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 9c6b46a2acbef2577139e5b143a3338744c93812
4
- data.tar.gz: 5082619fa7124d599f454a125f4977926e34b922
5
- SHA512:
6
- metadata.gz: 5150238855d5ee6091b40bfa0d49eb9bf7b0ec21c2845bf447ca54b4f645639b6b707889528b305ee03ba9004cf857d30eee779d3448144e0a208a619b16a630
7
- data.tar.gz: 3493421989e84cddfa600b2f808158e457160342d7b9cab08441c2e993a6fa18756f0775eb8444c72a7bb7dcb0afaf3b124f03670c815240762f3af5c8d83cba