Skip to content

Instantly share code, notes, and snippets.

@mndoci
Forked from geemus/0_rackspace.rb
Created February 26, 2011 20:43
Show Gist options
  • Save mndoci/845599 to your computer and use it in GitHub Desktop.
Save mndoci/845599 to your computer and use it in GitHub Desktop.
Forked from Geemus
compute.import_key_pair(
'id_rsa.pub',
File.read('~/.ssh/id_rsa.pub')
)
compute.authorize_security_group_ingress(
'CidrIp' => '0.0.0.0/0',
'FromPort' => 22,
'IpProtocol' => 'tcp',
'GroupName' => 'default',
'ToPort' => 22
)
server_data = compute.run_instances(
'ami-1a837773',
1,
1,
'InstanceType' => 'm1.small',
'KeyName' => 'id_rsa.pub',
'SecurityGroup' => 'default'
).body['instancesSet'].first
until compute.describe_instances(
'instance-id' => server_data['instanceId']
).body['reservationSet'].first['instancesSet'].first['instanceState']['name'] == 'running'
end
sleep(300)
Net::SSH.start(
server_data['ipAddress'],
'ubuntu',
:key_data => [File.read('~/.ssh/id_rsa')]
) do |ssh|
commands = [
%{'mkdir .ssh'},
%{'echo #{File.read('~/.ssh/id_rsa.pub')} >> ~/.ssh/authorized_keys'},
%{passwd -l root},
]
commands.each do |command|
ssh.open_channel do |ssh_channel|
ssh_channel.request_pty
ssh_channel.exec(%{bash -lc '#{command}'})
ssh.loop
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment