Created
January 1, 2010 15:36
-
-
Save julienXX/267143 to your computer and use it in GitHub Desktop.
teempush.rb
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
#!/opt/ruby-enterprise/bin/ruby | |
# | |
# you need to install : | |
# ruby | |
# ruby-dev | |
# libopenssl-ruby libxml2-dev libxslt1-dev (ubuntu/debian) | |
# lsb_release package (named redhat-lsb on centos and fedora) | |
# rubygems : | |
## gem install uuidtools activeresource sysinfo nokogiri | |
## | |
# | |
require 'rubygems' | |
require 'uuidtools' | |
require 'ftools' | |
require 'active_resource' | |
require 'sysinfo' | |
require 'nokogiri' | |
# set to true if you want publish your host's infos to everyone | |
PUBLIC=true | |
API_KEY= '' | |
URL = 'http://www.teem.it/' | |
class ActiveResource::Base | |
# Method inspired by ActiveRecord | |
# it is very simple, and probably won't react as ActiveRecord#update_attributes for complex cases and option uses | |
def update_attributes(attributes) | |
load(attributes).save | |
end | |
end | |
class Host < ActiveResource::Base | |
self.site = URL | |
self.user = API_KEY | |
self.timeout = 5 | |
end | |
class Users < ActiveResource::Base | |
self.site = URL | |
self.user = API_KEY | |
end | |
sysinfo = SysInfo.new | |
# get sys infos for OS | |
if File.exist?('/usr/sbin/system_profiler') # system is OSX | |
whoami = %x(whoami).gsub(/\n/,'') | |
%x(system_profiler -detailLevel basic -xml > /Users/#{whoami}/system.xml) | |
doc = Nokogiri::XML(File.open("/Users/#{whoami}/system.xml").read) | |
software = doc.xpath("//dict/key[text()='kernel_version']/ancestor::node()/string").collect(&:text) | |
os_name = software[7].split(" ")[0..2].join(" ") | |
os_codename = case software[7].split(" ")[3][0..3] | |
when "10.6" then "Snow Leopard" | |
when "10.5" then "Leopard" | |
when "10.4" then "Tiger" | |
when "10.3" then "Panther" | |
when "10.2" then "Jaguar" | |
when "10.1" then "Puma" | |
when "10.0" then "Cheetah" | |
end | |
os_release = software[7].split(" ")[3..4].join(" ") | |
else # system is Linux | |
os_name =`lsb_release -i`.split(":")[1].strip | |
os_codename =`lsb_release -c`.split(":")[1].strip | |
os_release =`lsb_release -r`.split(":")[1].strip | |
end | |
# get uuid | |
if File.exist?('/etc/sysconfig/hw-uuid') | |
uuid = `cat /etc/sysconfig/hw-uuid`.strip | |
elsif File.exist?(File.expand_path("~/.teemit/hw-uuid")) | |
uuid = `cat ~/.teemit/hw-uuid`.strip | |
else | |
# generate an universal identifier id | |
uuid = UUIDTools::UUID.timestamp_create.to_s | |
File.makedirs(File.expand_path("~/.teemit/")) | |
f = File.new(File.expand_path("~/.teemit/")+"/hw-uuid", "w") | |
f.write(uuid) | |
f.close | |
end | |
if os_name | |
begin | |
h = Host.find(:one, :from => :search, :params => {:uuid => uuid}) | |
h.update_attributes( | |
:os_name => os_name, | |
:os_codename => os_codename, | |
:os_release => os_release, | |
:os_type => sysinfo.impl.to_s, | |
:os_base => sysinfo.os.to_s, | |
:hostname => sysinfo.hostname, | |
:uptime => sysinfo.uptime.to_s, | |
:shell => sysinfo.shell.to_s, | |
:user => sysinfo.user, | |
:architecture => sysinfo.arch.to_s, | |
:public => PUBLIC | |
) | |
p h | |
rescue ActiveResource::ResourceNotFound => e | |
p "Host not found, creating" | |
begin | |
h = Host.create(:os_name => os_name, :os_codename => os_codename, :os_release => os_release, :os_type => sysinfo.impl.to_s, :os_base => sysinfo.os.to_s, :hostname => sysinfo.hostname, :uptime => sysinfo.uptime.to_s, :shell => sysinfo.shell.to_s, :user => sysinfo.user, :architecture => sysinfo.arch.to_s, :uuid => uuid, :public => PUBLIC) | |
p h | |
rescue ActiveResource::ResourceConflict => e | |
p "Error, host conflict. It seems that your host's uuid is blocked or registered by another user." | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment