# Copyright 2010 Cloudkick, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. module Cloudkick class Node < Base attr_reader :agent_state, :color, :id, :ipaddress, :name attr_reader :provider_id, :provider_name, :status, :tags def initialize(agent_state, color, id, ipaddress, name, provider_id, provider_name, status, tags) @agent_state = agent_state @color = color @id = id @ipaddress = ipaddress @name = name @provider_id = provider_id @provider_name = provider_name @status = status @tags = tags end def check(type=nil) resp, data = access_token.get("/api/node/#{@id}/check/mem") Crack::JSON.parse(data) end end class Nodes < Base attr_reader :nodes, :query def initialize(query=nil) @query = query @nodes = get end def each @nodes.each { |node| yield node } end def get if @query resp, data = access_token.get("/api/nodes?query=#{query}") else resp, data = access_token.get("/api/nodes") end hash = Crack::JSON.parse(data) nodes = hash.map do |node| Node.new(node['agent_state'], node['color'], node['id'], node['ipaddress'], node['name'], node['provider_id'], node['provider_name'], node['status'], node['tags']) end end def pssh(command) file = File.new('pssh-ips.txt', 'w') @nodes.each do |node| file.puts node.ipaddress end "run: pssh -h pssh-ips.txt -o /tmp/ck \'#{command}\'" end end end