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