Last active
December 27, 2015 17:39
-
-
Save sergiopena/7363440 to your computer and use it in GitHub Desktop.
Tiny script that connects to Abiquo API Outbound, listen for deploy events and refresh novnc token file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require 'rubygems' | |
require 'rest_client' | |
require 'nokogiri' | |
require 'digest/md5' | |
require 'em-eventsource' | |
require 'base64' | |
rescue LoadError | |
puts "Some dependencies are missing. | |
Check for availabilty of rubygems, rest-client, nokogiri, getoptlong, digest/md5, em-eventsource, base64 | |
Try again once dependencies are met. | |
" | |
end | |
# IP where the abiquo API and m are running | |
@host = "10.60.13.34" | |
# user with enterprise access to retrieve events and VM information | |
@user = "admin" | |
@pass = "xabiquo" | |
# output file where the websockify expects the configuration | |
# Usually /opt/websockify/config.vnc | |
@outputfile = "./outputtokens" | |
# --- DO NOT MODIFY AFTER THIS LINE --- | |
@auth = Base64.encode64("#{@user}:#{@pass}").to_s | |
def get_all_vms() | |
@tokens=[] | |
begin | |
url = "http://#{@host}/api/admin/enterprises" | |
entxml = RestClient::Request.new(:method => :get, :url => url, :user => @user, :password => @pass).execute | |
Nokogiri::XML.parse(entxml).xpath('//enterprises/enterprise').each do |ent| | |
ent.xpath('./link[@rel="virtualmachines"]').each do |entvm| | |
url = entvm.attribute("href").to_s | |
vmxml = RestClient::Request.new(:method => :get, :url => url, :user => @user, :password => @pass).execute | |
Nokogiri::XML.parse(vmxml).xpath('//virtualMachines/virtualMachine').each do |vm| | |
unless vm.at('vdrpIP').nil? or vm.at('vdrpPort').nil? | |
conn = "#{vm.at('vdrpIP').to_str}:#{vm.at('vdrpPort').to_str}" | |
digest = Digest::MD5.hexdigest(conn) | |
line = "#{digest}: #{conn}" | |
@tokens << line | |
#An error occurred, dir not writable etc. | |
end | |
end | |
end | |
end | |
rescue SocketError | |
puts "Cannot connect to specified @host." | |
exit 1 | |
end | |
begin | |
file = File.open(@outputfile, "w") | |
@tokens.each { |line| file.write("#{line}\n")} | |
rescue IOError => e | |
#An error occurred, dir not writable etc. | |
ensure | |
file.close | |
end | |
end | |
EM.run do | |
source = EM::EventSource.new("http://#{@host}/m/stream", | |
{ 'X-Atmosphere-Transport' => 'sse', | |
'X-Atmosphere-Framework' => '1.0', | |
'action' => 'DEPLOY,UNDEPLOY'}, | |
{ 'Authorization' => "Basic #{@auth.strip}" } | |
) | |
source.message do |message| | |
puts "Received event #{message}" | |
get_all_vms | |
end | |
source.error do |error| | |
puts "error #{error}" | |
end | |
source.start | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment