Skip to content

Instantly share code, notes, and snippets.

@sergiopena
Created December 4, 2012 23:27
Show Gist options
  • Save sergiopena/4210226 to your computer and use it in GitHub Desktop.
Save sergiopena/4210226 to your computer and use it in GitHub Desktop.
List tasks of a virtual machine and change last task state
#!/usr/bin/env ruby
require 'rubygems'
require 'trollop'
require 'redis'
require 'taulukko'
require 'date'
=begin
Description
===========
Script to list tasks of a virtual machine, by default only shows the last task.
Options:
--vm, -v <s>: Virtual machine database identifier
--fix, -f : Set last task state to finish_successfully
--host, -h <s>: Redis host (default: localhost)
--port, -p <i>: Redis port (default: 6379)
--all, -a: Show all tasks
--version, -e: Print version and exit
--help, -l: Show this message
Example
=======
./vmtasks.rb --vm 1326 --host 10.60.20.30 --all
Task e0fd075a-aa4a-4565-9e82-cee39ba916ea
=========================================
Id: e0fd075a-aa4a-4565-9e82-cee39ba916ea
Type: DEPLOY
State: FINISHED_SUCCESSFULLY
Jobs:
Id Type State Rollback state
------------------------------------------------------------------------- --------- ----- --------------
e0fd075a-aa4a-4565-9e82-cee39ba916ea.5adfc1b2-2b65-4c52-9c4b-b28ca7c3ce54 CONFIGURE DONE UNKNOWN
e0fd075a-aa4a-4565-9e82-cee39ba916ea.6a52472c-df1f-427b-82db-f71dd96c0927 POWER_ON DONE UNKNOWN
Dependencies
============
gem install trollop
gem install redis
gem install taulukko
gem install SystemTimer (Ruby < 1.9)
=end
options = Trollop::options do
version "v0.0.1"
opt :vm, "Virtual machine database identifier", :type => String
opt :host, "Redis host", :type => String, :default => 'localhost'
opt :port, "Redis port", :type => :int, :default => 6379
opt :all, "Show all tasks", :default => false
end
Trollop::die :vm, "is a mandatory option" if !options[:vm]
REDIS = Redis.new(:host => options[:host], :port => options[:port])
if !REDIS.ping
puts "Unable to connect redis at #{options[:host]}:#{options[:port]}"
exit 1
end
ID = options[:vm]
if options[:all]
tasks = REDIS.lrange "Owner:VirtualMachine:#{ID}", 0, -1
else
tasks = REDIS.lrange "Owner:VirtualMachine:#{ID}", 0, 0
end
if tasks.empty?
puts "There are no tasks for virtual machine #{ID}"
exit 0
end
tasks.each do |taskkey|
task = REDIS.hgetall taskkey
puts
puts heading = "Task #{task['taskId']}"
puts "".ljust(heading.size, "=")
puts
puts "Id: #{task['taskId']}"
puts "Type: #{task['type']}"
puts "State: #{task['state']}"
puts "Timestamp: #{Time.at(task['timestamp'].to_i)} (#{task['timestamp']})"
puts "Jobs:"
puts
jobs = Taulukko.new
jobs.headings = ["Id", "Type", "State", "Rollback state", "Timestamp"]
REDIS.lrange(task['jobs'], 0, -1).each do |jobkey|
job = REDIS.hgetall jobkey
jobs << [job['id'], job['type'], job['state'], job['rollbackState'], "#{Time.at(job['timestamp'].to_i)} (#{task['timestamp']})"]
end
puts jobs
if options[:fix]
if #{task['state']} == "FINISHED_UNSUCCESSFULLY"
REDIS.hset( taskkey, "state", "FINISHED_SUCCESSFULLY")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment