mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
clean/fix bootstrap methods
This commit is contained in:
parent
1ff62d60d3
commit
a986736523
3 changed files with 41 additions and 33 deletions
|
@ -33,7 +33,7 @@ module Fog
|
|||
attribute :user_data
|
||||
|
||||
attr_accessor :password, :username
|
||||
attr_writer :private_key_path, :public_key_path
|
||||
attr_writer :private_key, :private_key_path, :public_key, :public_key_path
|
||||
|
||||
def initialize(attributes={})
|
||||
@groups ||= ["default"] unless attributes[:subnet_id]
|
||||
|
@ -79,7 +79,7 @@ module Fog
|
|||
end
|
||||
|
||||
def key_pair=(new_keypair)
|
||||
@key_name = new_keypair.name
|
||||
@key_name = new_keypair && new_keypair.name
|
||||
end
|
||||
|
||||
def monitoring=(new_monitoring)
|
||||
|
@ -99,13 +99,20 @@ module Fog
|
|||
end
|
||||
|
||||
def private_key_path
|
||||
@private_key_path ||= Fog.credentials[:private_key_path]
|
||||
File.expand_path(@private_key_path ||= Fog.credentials[:private_key_path])
|
||||
end
|
||||
|
||||
def private_key
|
||||
@private_key ||= File.read(private_key_path)
|
||||
end
|
||||
|
||||
def public_key_path
|
||||
@public_key_path ||= Fog.credentials[:public_key_path]
|
||||
File.expand_path(@public_key_path ||= Fog.credentials[:public_key_path])
|
||||
end
|
||||
|
||||
def public_key
|
||||
@public_key ||= File.read(public_key_path)
|
||||
end
|
||||
def ready?
|
||||
@state == 'running'
|
||||
end
|
||||
|
@ -147,11 +154,11 @@ module Fog
|
|||
end
|
||||
|
||||
def setup(credentials = {})
|
||||
requires :ip_address, :identity, :public_key_path, :username
|
||||
requires :identity, :ip_address, :public_key, :username
|
||||
sleep(10) # takes a bit before EC2 instances will play nice
|
||||
Fog::SSH.new(ip_address, username, credentials).run([
|
||||
%{mkdir .ssh},
|
||||
%{echo "#{File.read(public_key_path)}" >> ~/.ssh/authorized_keys},
|
||||
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
|
||||
%{passwd -l root},
|
||||
%{echo "#{attributes.to_json}" >> ~/attributes.json}
|
||||
])
|
||||
|
@ -161,8 +168,8 @@ module Fog
|
|||
end
|
||||
|
||||
def ssh(commands)
|
||||
requires :identity, :ip_address, :private_key_path, :username
|
||||
@ssh ||= Fog::SSH.new(ip_address, username, :keys => [private_key_path])
|
||||
requires :identity, :ip_address, :private_key, :username
|
||||
@ssh ||= Fog::SSH.new(ip_address, username, :key_data => [private_key])
|
||||
@ssh.run(commands)
|
||||
end
|
||||
|
||||
|
|
|
@ -9,8 +9,6 @@ module Fog
|
|||
|
||||
attribute :server_id
|
||||
|
||||
attr_writer :private_key_path, :public_key_path
|
||||
|
||||
model Fog::AWS::Compute::Server
|
||||
|
||||
def initialize(attributes)
|
||||
|
@ -31,10 +29,14 @@ module Fog
|
|||
end
|
||||
|
||||
def bootstrap(new_attributes = {})
|
||||
server = connection.servers.new(new_attributes)
|
||||
|
||||
# first or create fog_#{credential} keypair
|
||||
unless key_pair = connection.key_pairs.get("fog_#{Fog.credential}")
|
||||
public_key = File.read(public_key_path)
|
||||
key_pair = connection.key_pairs.create(:name => "fog_#{Fog.credential}", :public_key => public_key)
|
||||
unless server.key_pair = connection.key_pairs.get("fog_#{Fog.credential}")
|
||||
server.key_pair = connection.key_pairs.create(
|
||||
:name => "fog_#{Fog.credential}",
|
||||
:public_key => server.public_key
|
||||
)
|
||||
end
|
||||
|
||||
# make sure port 22 is open in the first security group
|
||||
|
@ -49,11 +51,9 @@ module Fog
|
|||
security_group.authorize_port_range(22..22)
|
||||
end
|
||||
|
||||
server.save
|
||||
server.wait_for { ready? }
|
||||
private_key = File.read(private_key_path)
|
||||
server.setup(:key_data => [private_key])
|
||||
|
||||
server.merge_attributes(:private_key_path => private_key_path, :public_key_path => public_key_path)
|
||||
server.setup(:key_data => [server.private_key])
|
||||
server
|
||||
end
|
||||
|
||||
|
@ -65,14 +65,6 @@ module Fog
|
|||
nil
|
||||
end
|
||||
|
||||
def private_key_path
|
||||
@private_key_path ||= Fog.credentials[:private_key_path]
|
||||
end
|
||||
|
||||
def public_key_path
|
||||
@public_key_path ||= Fog.credentials[:public_key_path]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -18,7 +18,8 @@ module Fog
|
|||
attribute :progress
|
||||
attribute :status
|
||||
|
||||
attr_accessor :password, :private_key_path, :public_key_path, :username
|
||||
attr_accessor :password, :username
|
||||
attr_writer :private_key, :private_key_path, :public_key, :public_key_path
|
||||
|
||||
def initialize(attributes={})
|
||||
@flavor_id ||= 1
|
||||
|
@ -57,11 +58,19 @@ module Fog
|
|||
end
|
||||
|
||||
def private_key_path
|
||||
@private_key_path ||= Fog.credentials[:private_key_path]
|
||||
File.expand_path(@private_key_path ||= Fog.credentials[:private_key_path])
|
||||
end
|
||||
|
||||
def private_key
|
||||
@private_key ||= File.read(private_key_path)
|
||||
end
|
||||
|
||||
def public_key_path
|
||||
@public_key_path ||= Fog.credentials[:public_key_path]
|
||||
File.expand_path(@public_key_path ||= Fog.credentials[:public_key_path])
|
||||
end
|
||||
|
||||
def public_key
|
||||
@public_key ||= File.read(public_key_path)
|
||||
end
|
||||
|
||||
def save
|
||||
|
@ -78,10 +87,10 @@ module Fog
|
|||
end
|
||||
|
||||
def setup(credentials = {})
|
||||
requires :addresses, :identity, :public_key_path, :username
|
||||
requires :addresses, :identity, :public_key, :username
|
||||
Fog::SSH.new(addresses['public'].first, username, credentials).run([
|
||||
%{mkdir .ssh},
|
||||
%{echo "#{File.read(File.expand_path(public_key_path))}" >> ~/.ssh/authorized_keys},
|
||||
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
|
||||
%{passwd -l root},
|
||||
%{echo "#{attributes.to_json}" >> ~/attributes.json},
|
||||
%{echo "#{metadata.to_json}" >> ~/metadata.json}
|
||||
|
@ -92,13 +101,13 @@ module Fog
|
|||
end
|
||||
|
||||
def ssh(commands)
|
||||
requires :addresses, :identity, :private_key_path, :username
|
||||
@ssh ||= Fog::SSH.new(addresses['public'].first, username, :keys => [private_key_path])
|
||||
requires :addresses, :identity, :private_key, :username
|
||||
@ssh ||= Fog::SSH.new(addresses['public'].first, username, :key_data => [private_key])
|
||||
@ssh.run(commands)
|
||||
end
|
||||
|
||||
def username
|
||||
@username ||= root
|
||||
@username ||= 'root'
|
||||
end
|
||||
|
||||
private
|
||||
|
|
Loading…
Add table
Reference in a new issue