Skip to content

Instantly share code, notes, and snippets.

@geemus
Created December 6, 2010 07:46
Show Gist options
  • Save geemus/729992 to your computer and use it in GitHub Desktop.
Save geemus/729992 to your computer and use it in GitHub Desktop.
fog or: How I Learned to Stop Worrying and Love the Cloud (examples)
server_data = compute.create_server(
1,
49
).body['server']
until compute.get_server_details(
server_data['id']
).body['server']['status'] == 'ACTIVE'
end
commands = [
%{'mkdir .ssh'},
%{'echo #{File.read('~/.ssh/id_rsa.pub')} >> ~/.ssh/authorized_keys'},
%{passwd -l root},
]
Net::SSH.start(
server_data['addresses'].first,
'root',
:password => server_data['password']
) do |ssh|
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
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
require 'rubygems'
require 'fog'
def pinger(credentials, server_attributes, target)
Formatador.display_line # top padding
# setup a connection to the service
compute = Fog::Compute.new(credentials)
Formatador.display_line("Bootstrapping server...")
# boot server and setup ssh keys
server = compute.servers.bootstrap(server_attributes)
Formatador.display_line("Pinging [bold]#{target}[/]...")
# ping target 10 times
ssh_results = server.ssh("ping -c 10 #{target}")
stdout = ssh_results.first.stdout
# parse result, last line is summary
# round-trip min/avg/max/stddev = A.A/B.B/C.C/D.D ms
stats = stdout.split("/n").last.split(' ')[-2]
min, avg, max, stddev = stats.split('/')
Formatador.display_line("Shutting down server...")
# shutdown the server
server.destroy
Formatador.display_line # bottom padding
# return the data as a hash
{ :min => min, :avg => avg, :max => max, :stddev => stddev }
end
require 'rubygems'
require 'fog'
def aggregator(credentials, directory_name, file_path)
Formatador.display_line # top padding
# setup a connection to the service
storage = Fog::Storage.new(credentials)
Formatador.display_line("Creating directory [bold]#{directory_name}[/]")
# create a directory
directory = storage.directories.create(
:key => directory_name,
:public => true
)
# expand path and get name
path = File.expand_path(file_path)
name = File.basename(path)
Formatador.display_line("Creating file [bold]#{name}[/]")
# store the file
file = directory.files.create(
:body => File.open(path),
:key => name,
:public => true
)
Formatador.display_line # bottom padding
# return the public url for the file
file.public_url
end
require 'rubygems'
require 'fog'
def freemium(credentials, domain_name, company_name)
Formatador.display_line # top padding
# setup a connection to the service
dns = Fog::DNS.new(credentials)
Formatador.display_line("Creating zone [bold]#{domain_name}[/]")
# create a zone
zone = dns.zones.create(
:domain => domain_name,
:email => "admin@#{domain_name}"
)
Formatador.display_line("Creating record [bold]#{company_name}[/]")
# create the record
record = zone.records.create(
:ip => '1.2.3.4',
:name => "#{company_name}.#{domain_name}",
:type => 'A'
)
Formatador.display_line # bottom padding
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment