Skip to content

Instantly share code, notes, and snippets.

@chojayr
Last active May 31, 2017 13:04
Show Gist options
  • Save chojayr/8c28fa76ae6b37e4a209 to your computer and use it in GitHub Desktop.
Save chojayr/8c28fa76ae6b37e4a209 to your computer and use it in GitHub Desktop.
racktables-propel

racktables-propel

racktables-propel is created to automate the task of adding the server(physical | virtual) information to the Racktables, information like system serial number, CPU count, memory, operating system installed and version, kernel release, system model, uuid etc. this will eliminate the manual input of the server information on Racktables and also avoid to missed and forget the task of including the server(and its info) on the Racktables(which is always happened) especially when there's a machine that's freshly build.

racktables-propel is a script created using Ruby and the following library(gems)

facter - to extract the system information yaml - to convert the extracted information on yaml and parse the converted yaml info logger - to create ang provide logs(/var/log/racktables.log) json - to parse the json formatted Racktables api

racktables-propel depends on:

  • only works on forked and release Racktables-api by Ian Bettinger
  • Tested on Ruby 1.9.x

Note:

* Still doing some improvements and tests to make it more reliable
* As of now this is applicable for freshly build machine or VM's that is not yet included on the Racktables
* Other features such as Delete and Modify is not yet functional
* Tested only on previous version of Racktables which is 0.20.3 and 0.20.6 - latest version is 0.20.7
* Other server information will be added soon 
You can use and modify the script anyway you want
you can just run the "ruby racktables-porpel.rb" for testing
and for much automated way you can include it as a module for your favorite configuration management framework
I created a Ansible playbook for this you can check out here [ansible-racktables-propel](https://github.com/chojayr/ansible-racktables-propel)
note:
The account(username) that going to be use should have a admin privilege on the racktables server for you to be able update the server info
This script is kind a bit "lame" or totally "lame"
so your patience and understanding is much appreciated
:p chojayr
#!/usr/bin/env ruby
##
## Script to add server info (physical\virtual) to Racktables using Racktables api
## @chojayr :p
require 'rubygems'
require 'facter'
require 'json'
require "net/http"
require "uri"
require 'yaml'
require 'logger'
@log = Logger.new('/var/log/racktables.log')
@object_id = ""
def generateFacts()
@log.info "Generate facts... dump on /tmp/facts_generated.yaml"
rejected_facts = ["sshdsakey", "sshrsakey"]
@outputfile = "/tmp/facts_generated.yaml"
facts = Facter.to_hash.reject { |k,v| rejected_facts.include? k }
File.open(@outputfile, "w") { |fh| fh.write(facts.to_yaml) }
end
def getAllFacters()
@log.info "Reading facts.."
ymlres = YAML.load(open('/tmp/facts_generated.yaml'))
@fq = ymlres["fqdn"]
@dom = ymlres["domain"]
@hname = ymlres["hostname"]
@uid = ymlres["uuid"]
@os = ymlres["operatingsystem"]
@osrel = ymlres["operatingsystemrelease"]
@kern = ymlres["kernelrelease"]
@kernver = ymlres["kernelversion"]
@procnt = ymlres["processorcount"]
@mem = ymlres["memorysize"].match(/[0-9|\.]+/)[0]
virt = ymlres["virtual"]
if virt == "physical" then
@object_type_id = "4"
else
@object_type_id = "1504"
end
end
def httpConnect( url )
uri = URI.parse(url)
user = "username"
passwd = "password"
http = Net::HTTP.new(uri.host, uri.port)
#set true if you're using ssl
http.use_ssl = {{ use_ssl }}
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth(user, passwd)
@log.info "Authenticating and verifying api request..."
response = http.request(request)
@log.info "http request sent. [#{url}]"
return response
end
def add_object()
begin
@log.info "Creating new object in racktables."
@log.info "Object name : #{@hname}"
@log.info "Object label : #{@hname}"
#endpoint
url="http://example.racktables.com/racktables/api.php?method=add_object&object_type_id=#{@object_type_id}&object_name=#{@hname}&object_label=#{@hname}"
#authenticate and send POST
response = httpConnect(url)
#get objectId of newly added object
@objectId = response['location'].match(/([0-9]{1,5})/)[0]
@log.info "machine added : http://example.racktables.com/racktables/api.php?method=get_object&object_id=#{@objectId}&include_attrs"
return @objectId
rescue StandardError => e
@log.warn "machine already existed"
return "NOTOK"
end
end
#def generate_objectId()
# @log.info "Generating host object_id for future use ..."
# File.open(@outputfile, "a") { |fh| fh.write("\n object_id: #{@objectId}") }
#end
def update_object()
obj = YAML.load(open('/tmp/facts_generated.yaml'))
object_id = obj["object_id"]
begin
@log.info "Adding other details of the machine..."
#endpoint
url="http://example.racktables.com/racktables/api.php?method=edit_object&#{"object_id=#{@objectId}&object_type_id=#{@object_type_id}&object_name=#{@hname}&object_label=#{@hname}&object_asset_no=&object_comment=#{@object_comment}&attr_3=#{@fq}&attr_25=#{@uid}&attr_10004=#{@dom}&attr_10043=#{@os}&attr_10044=#{@osrel}&attr_10040=#{@kern}&attr_10041=#{@kernver}&attr_10050=#{@procnt}&attr_10005=#{@mem}"}"
#puts url
#authenticate and send POST
response = httpConnect(url)
return response
end
end
##################
# #
# Main Program #
# #
##################
@log.info "Starting-----------------------------------"
generateFacts()
getAllFacters()
@object_comment = "created_by_racktables_propel"
add_object()
#generate_objectId()
update_object()
@log.info "End."
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment