Class: Grid::Helper
- Inherits:
-
Object
- Object
- Grid::Helper
- Defined in:
- lib/grid/grid.rb
Instance Method Summary collapse
- #create(args = nil) ⇒ Object
- #create_grid(name, description, driver, location, total_nodes) ⇒ Object
- #credits(args = nil) ⇒ Object
- #get_password(prompt = 'Password: ', mask = '*') ⇒ Object
- #get_token(params = {}) ⇒ Object
- #home ⇒ Object
-
#initialize(params = {}) ⇒ Helper
constructor
A new instance of Helper.
- #launch(args = nil) ⇒ Object
- #list(args = nil) ⇒ Object
- #restart(grid_id = nil) ⇒ Object
- #show(grid_id = nil) ⇒ Object
- #start(grid_id = nil) ⇒ Object
- #status(grid_id = nil) ⇒ Object
- #stop(grid_id = nil) ⇒ Object
- #token(args = nil) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Helper
Returns a new instance of Helper.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/grid/grid.rb', line 101 def initialize(params = {}) @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 " if home if File.file? ENV[home]+'/.gridrc' @token = open(ENV[home]+'/.gridrc') { |f| f.read }.split(':')[1] end end @token = get_token(params) unless @token @log.debug("Current token: #{@token}") end |
Instance Method Details
#create(args = nil) ⇒ Object
199 200 201 202 203 204 205 206 207 |
# File 'lib/grid/grid.rb', line 199 def create(args=nil) name = ask("Grid name: ") { |q| q.default = "Grid" } description = ask("Grid description: ") { |q| q.default = "Grid created #{Time.now}" } total_nodes = ask("Number of nodes: ") { |q| q.default = 5 } location = ask("Location of nodes: ") { |q| q.default = "us-east" } driver = ask("Driver: ") { |q| q.default = "webdriver" } unless @api @grid_id = create_grid(name, description, driver, location, total_nodes) show(@grid_id) end |
#create_grid(name, description, driver, location, total_nodes) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/grid/grid.rb', line 182 def create_grid(name, description, driver, location, total_nodes) begin @log.debug("Creating grid ...") response = RestClient.post "#{@uri}/grid.json" , { :token => @token, :name => name, :description => description, :driver => driver, :location => location, :total_nodes => total_nodes } (JSON.parse(response)).first[1]["id"] rescue => e @log.error("#{e}") end end |
#credits(args = nil) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/grid/grid.rb', line 145 def credits(args=nil) @log.debug("Listing credits available ...") begin response = RestClient.get "#{@uri}/credit.json" , { :token => @token } ap JSON.parse(response) rescue => e @log.error("#{e}") end end |
#get_password(prompt = 'Password: ', mask = '*') ⇒ Object
116 117 118 |
# File 'lib/grid/grid.rb', line 116 def get_password(prompt='Password: ', mask='*') ask(prompt) { |q| q.echo = mask } end |
#get_token(params = {}) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/grid/grid.rb', line 120 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 |
#home ⇒ Object
134 135 136 137 |
# File 'lib/grid/grid.rb', line 134 def home homes = ["HOME", "HOMEPATH"] homes.detect {|h| ENV[h] != nil} end |
#launch(args = nil) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/grid/grid.rb', line 209 def launch(args=nil) name = ask("Grid name: ") { |q| q.default = "Grid" } description = ask("Grid description: ") { |q| q.default = "Grid created #{Time.now}" } total_nodes = ask("Number of nodes: ") { |q| q.default = 5 } location = ask("Location of nodes: ") { |q| q.default = "us-east" } driver = ask("Driver: ") { |q| q.default = "webdriver" } unless @api @grid_id = create_grid(name, description, driver, location, total_nodes) @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(@grid_id) end |
#list(args = nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/grid/grid.rb', line 157 def list(args=nil) @log.debug("Listing grids ...") begin response = RestClient.get "#{@uri}/grid.json" , { :token => @token } ap JSON.parse(response) rescue => e @log.error("#{e}") end end |
#restart(grid_id = nil) ⇒ Object
247 248 249 250 251 252 253 254 255 256 |
# File 'lib/grid/grid.rb', line 247 def restart(grid_id=nil) @grid_id = ( grid_id ? grid_id : ask("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(@grid_id) end |
#show(grid_id = nil) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/grid/grid.rb', line 169 def show(grid_id=nil) @grid_id = ( grid_id ? grid_id : ask("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 |
#start(grid_id = nil) ⇒ Object
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/grid/grid.rb', line 225 def start(grid_id=nil) @grid_id = ( grid_id ? grid_id : ask("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(@grid_id) end |
#status(grid_id = nil) ⇒ Object
258 259 260 261 262 263 264 265 266 267 |
# File 'lib/grid/grid.rb', line 258 def status(grid_id=nil) @grid_id = ( grid_id ? grid_id : ask("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(@grid_id) end |
#stop(grid_id = nil) ⇒ Object
236 237 238 239 240 241 242 243 244 245 |
# File 'lib/grid/grid.rb', line 236 def stop(grid_id=nil) @grid_id = ( grid_id ? grid_id : ask("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(@grid_id) end |
#token(args = nil) ⇒ Object
139 140 141 142 143 |
# File 'lib/grid/grid.rb', line 139 def token(args=nil) @token = get_token open(ENV[home]+'/.gridrc', 'w') { |f| f << "TOKEN:#{@token}" } if home and @token @log.debug("New token : #{@token}") end |