Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Razor API Image Hack
require 'rake'
require 'net/ssh'
require 'net/scp'
require "net/http"
require "json"
USER = PASSWORD = 'vagrant'
HOSTS = {
'gold' => '172.16.0.2',
}
def ssh(host, user = USER, password = PASSWORD)
(@ssh_connections ||= {})[host.to_s] ||= begin
Net::SSH.start(HOSTS[host.to_s], user, :password => password)
end
end
def scp(host, local_path, remote_path, user = USER, password = PASSWORD)
ssh(host, user, password).scp.upload!(local_path, remote_path)
end
def razor(*a)
ssh(:gold).exec!(['sudo /opt/razor/bin/razor', *a].join(' '))
end
def razor_api(*a) # As of 2012.06.27 this image api isn't implemented
if(a.first == "image")
return ghetto_api(a)
end
# We create a URI object with our IP, Port, and the path to the Slice (Node)
uri = URI "http://#{HOSTS['gold']}:8026/razor/api/#{a.join("/")}"
# We run an HTTP GET against our Node Slice API
res = Net::HTTP.get(uri)
# This returns
response_hash = JSON.parse(res)
end
#XXX Hack until images api works
def ghetto_api(*a)
result = razor(a)
slice = a.first.first.capitalize
# Each object is separated by a blank line
# Plus an extra blank link at the end we need to trim
objects = result.split(/^$/)[0...-1]
objects.map! do |object|
# Break it into lines and select the hash rocket ones
# Giving us an Array of stuff that looks like
# UUID => 6LlBHrlbmHAEM8XRwjbQjB
# Type => OS Install
attributes = object.split("\n").select{ |i| i.include?( "=>" ) }
hash = attributes.each_with_object({}) do |line,hash|
key,value = line.split("=>").map { |i| i.strip } # remove leading and trailing spaces on keys / values
hash[key] = value
end
end
{"resource"=>"ProjectRazor::Slice::#{slice}", "command"=>"#{slice.downcase}_query_all", "result"=>"Ok", "http_err_code"=>200, "errcode"=>0, "response"=> objects}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment