#!/usr/bin/env ruby # grid.rb # Grid Helper require 'ap' require 'watirgrid' require 'json' require 'rest_client' require 'highline/import' class Grid ## # This is a helper method to control a grid based on watir. # It involves some general block thuggery and could # honestly benefit from some brutal refactoring... def self.control(params = {}, &block) log = Logger.new(STDOUT, 'daily') log.level = params[:loglevel] || Logger::DEBUG grid = Watir::Grid.new(params) grid.start(:take_all => true) log.debug("Grid size : #{grid.size}") log.debug("Grid rampup : #{rampup(grid.size, params)} secs") threads = [] grid.browsers.each_with_index do |browser, index| sleep rampup(grid.size, params) threads << Thread.new do start = ::Time.now log.debug("Browser #{index+1}##{Thread.current.object_id} start : #{::Time.now}") log.debug("Browser #{index+1}##{Thread.current.object_id} architecture : #{browser[:architecture]}") log.debug("Browser #{index+1}##{Thread.current.object_id} type : #{browser[:browser_type]}") log.debug("Browser #{index+1}##{Thread.current.object_id} hostname : #{browser[:hostname]}") @browser = browser[:object].new_browser yield @browser, "#{index+1}##{Thread.current.object_id}" log.debug("Browser #{index+1}##{Thread.current.object_id} stop : #{::Time.now}") log.debug("Browser #{index+1}##{Thread.current.object_id} elapsed : #{(::Time.now - start).to_i} secs") end end threads.each {|thread| thread.join} grid.release_tuples end class Helper def initialize(params = {}) @grid_id = params[:grid_id] @uri = params[:uri] || "http://gridin.it/api/v0" logfile = STDOUT @log = Logger.new(logfile, 'daily') @log.level = Logger::DEBUG @log.datetime_format = "%Y-%m-%d %H:%M:%S " @token = get_token(params) @log.debug("Current token: #{@token}") end def get_password(prompt='Password: ', mask='*') ask(prompt) { |q| q.echo = mask } end def get_token(params) if params[:token] params[:token] else HighLine.track_eof = false @email = params[:email] || ask('Email: ') @password = params[:password] || get_password response = RestClient.post "#{@uri}/token.json" , { :email => @email, :password => @password } (JSON.parse(response))["token"] end end def list @log.debug("Listing grids ...") begin response = RestClient.get "#{@uri}/grid.json" , { :token => @token } ap JSON.parse(response) rescue => e @log.error("#{e}") end end def show @grid_id = ask("Grid ID: ") unless @grid_id @log.debug("Listing grid ...") begin response = RestClient.get "#{@uri}/grid/#{@grid_id}.json" , { :token => @token } ap JSON.parse(response) rescue => e @log.error("#{e}") end end def create_grid(name, description) begin @log.debug("Creating grid ...") response = RestClient.post "#{@uri}/grid.json" , { :token => @token, :name => name, :description => description } (JSON.parse(response)).first[1]["id"] rescue => e @log.error("#{e}") end end def create_intranode(grid_id, browser_type, location, nodes) begin @log.debug("Creating intranode ...") response = RestClient.post "#{@uri}/grid/#{grid_id.to_s}/intranode.json", { :token => @token, :browser_type => browser_type, :location => location, :nodes => nodes } rescue => e @log.error("#{e}") end end def delete_intranode begin @log.debug("Deleting intranode ...") response = RestClient.delete "#{@uri}/grid/intranode/#{@intranode_id}.json", { :token => @token } rescue => e @log.error("#{e}") end end def create name = ask("Grid name: ") { |q| q.default = "Grid" } description = ask("Grid description: ") { |q| q.default = "Grid created #{Time.now}" } nodes = ask("Number of nodes: ") { |q| q.default = 5 } location = ask("Location of nodes: ") { |q| q.default = "us-east" } browser_type = ask("Node type: ") { |q| q.default = "webdriver" } @grid_id = create_grid(name, description) create_intranode(@grid_id, browser_type, location, nodes) show end def add @grid_id = ask("Grid ID: ") unless @grid_id nodes = ask("Number of nodes: ") { |q| q.default = 5 } location = ask("Location of nodes: ") { |q| q.default = "us-east" } browser_type = ask("Node type: ") { |q| q.default = "webdriver" } create_intranode(@grid_id, browser_type, location, nodes) show end def delete @grid_id = ask("Grid ID: ") unless @grid_id @intranode_id= ask("Intranode ID: ") unless @intranode_id delete_intranode show end def start @grid_id = ask("Grid ID: ") unless @grid_id @log.debug("Starting grid ...") begin response = RestClient.put "#{@uri}/grid/#{@grid_id.to_s}/start.json", {:token => @token} rescue => e @log.error("#{e}") end show end def stop @grid_id = ask("Grid ID: ") unless @grid_id @log.debug("Stopping grid ...") begin response = RestClient.put "#{@uri}/grid/#{@grid_id.to_s}/stop.json", {:token => @token} rescue => e @log.error("#{e}") end show end def restart @grid_id = ask("Grid ID: ") unless @grid_id @log.debug("Restarting grid ...") begin response = RestClient.put "#{@uri}/grid/#{@grid_id.to_s}/restart.json", {:token => @token} rescue => e @log.error("#{e}") end show end def status @grid_id = ask("Grid ID: ") unless @grid_id @log.debug("Updating status of grid ...") begin response = RestClient.put "#{@uri}/grid/#{@grid_id.to_s}/status.json", {:token => @token} rescue => e @log.error("#{e}") end show end end end