#!/usr/bin/env ruby #onering # list [options] field [key:value ..] # search [options] [key:value .. ] # provision [options] [key:value .. ] [pxe profile | ] require 'json' require 'rubygems' require 'subcommander' require 'onering' require 'yaml' require 'pp' include Subcommander def print_format(data, format=nil) case format when :text if data.is_a?(Hash) pp data elsif data.is_a?(Array) puts data.join("\n") else puts data.to_s end else puts YAML.dump(data) end end subcommander.version = ::Gem.loaded_specs['onering-client'].version.to_s subcommander.desc = ::Gem.loaded_specs['onering-client'].description #subcommander.opt :server, '-s', '--server', 'Specify the Onering server URL' subcommand :devices, "Operations related to Onering's assets database" do |devices| api = Onering::API::Devices api.connect ENV['ONERING_URL'] def _field(action, field, value, opts={}) rv = [] ids = [] # append IDs from filter ids += Onering::API::Devices.list('id', { :filter => opts[:filter] }) if opts[:filter] # add specific ID ids << opts[:id] if opts[:id] ids.each do |id| case action when :get rv << Onering::API::Devices.get_field(id, field) when :set rv << Onering::API::Devices.set_field(id, field, value) end end rv end # SHOW devices.subcommand :show, "Print out a single node by ID" do |sc| sc.usage = "onering devices show ID" sc.exec do id = sc[:args].first print_format(api.get(id)) end end # GET [FIELD] devices.subcommand :get, "Get a named field from one or more devices" do |sc| sc.usage = "onering devices get FIELD" sc.opt :filter, '-f', '--filter FILTER', "A urlquery filter string" sc.opt :as_txt, '-t', '--as-text', "Return the results as text" sc.opt :id, '-i', '--id ID', "A specific node ID" sc.exec do rv = _field(:get, sc[:args].first, nil, sc) print_format(rv, (sc[:as_txt] ? :text : nil)) end end # SET [FIELD] devices.subcommand :set, "Set a named field for one or more devices" do |sc| sc.usage = "onering devices set FIELD VALUE" sc.opt :filter, '-f', '--filter FILTER', "A urlquery filter string" sc.opt :as_txt, '-t', '--as-text', "Return the results as text" sc.opt :id, '-i', '--id ID', "A specific node ID" sc.exec do rv = _field(:set, sc[:args].first, sc[:args].last, sc) print_format(rv, (sc[:as_txt] ? :text : nil)) end end # LIST devices.subcommand :list, "List field values" do |sc| sc.usage = "onering devices list [-f FILTER] FIELD" sc.opt :filter, '-f', '--filter FILTER', "A urlquery filter string" sc.opt :as_txt, '-t', '--as-text', "Return the results as text" sc.exec do field = sc[:args].first filter = sc[:filter] print_format(api.list(field, { :filter => filter }), (sc[:as_txt] ? :text : nil)) end end # FIND devices.subcommand :find, "Finds all nodes that match a urlquery filter string" do |sc| sc.arity = 1 sc.usage = "onering devices find FILTER" sc.exec do print_format(api.find(sc[:args].first)) end end # SAVE devices.subcommand :save, "Creates or updates a new device in Onering, reading a JSON document from standard input" do |sc| sc.usage = "cat device.json | onering devices save [ID]" sc.exec do unless STDIN.tty? begin json = ::JSON.load(STDIN.read) raise "Input document must specify an ID" if sc[:args].empty? and not json['id'] print_format(api.save((sc[:args].first || json['id']), json)) rescue Exception => e STDERR.puts "#{e.class.name}: #{e.message}" exit 1 end end end end end subcommander.go!