Skip to content

Instantly share code, notes, and snippets.

@JanDeDobbeleer
Last active April 1, 2016 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JanDeDobbeleer/6df1ffd1ae5ae522ab45c8783ab85ea5 to your computer and use it in GitHub Desktop.
Save JanDeDobbeleer/6df1ffd1ae5ae522ab45c8783ab85ea5 to your computer and use it in GitHub Desktop.
Dashing - Bamboo Agent Widget

Description

Display a Bamboo Agent status. It can show if it is Offline, Idle or Busy building.

Installation

To use this widget, copy bamboo_agent.html, bamboo_agent.coffee, and bamboo_agent.scss into the /widgets/bamboo_agent directory. Put the bamboo_agent.rb file in your /jobs folder.

You'll also need a nice little icon for the widget. Use whatever you want, name it bamboo-status.png and put it in your /assets/images folder.

To include the widget in a dashboard, add the snippet from dashboard.erb to the dashboard layout file.

Configuration

You need and admin account to be able to access Bamboo agent details. Add the credentials to the job's configuration as well as your Bamboo instance's URL.

The bamboo agent ID's can be found on the agents detail page. The url looks something like this /admin/agent/viewAgent.action?agentId=12648450, take the ID you find in there.

You can add the agent ID's to the configuration like this: [agent1 agent2 agent3] and add 1 snippet per agent from dashboard.erb to your dashboard's erb file. Adjust the agent id so the information can be sent to that widget. You do not need to specify a title, that will be set by the job.

In case you need a different refresh rate, that can also be adjusted.

class Dashing.AgentStatus extends Dashing.Widget
@accessor 'value', Dashing.AnimatedValue
constructor: ->
super
refreshWidgetState: =>
node = $(@node)
node.removeClass('offline idle building')
node.addClass(@get('state').toLowerCase())
ready: ->
@refreshWidgetState()
onData: (data) ->
@refreshWidgetState()
<div class="header">Bamboo Agent</div>
<h1 class="title" data-bind="title"></h1>
<div class="label">Status</div>
<h2 class="state" data-bind="state"></h2>
require 'net/https'
require 'json'
require 'nokogiri'
#--------------------------------------------------------------------------------
# Configuration
#--------------------------------------------------------------------------------
configuration = {
:bamboo_uri => '',
:credentials => {
:username => '',
:password => ''
},
:refresh_rate => '10s',
:agent_ids => %w[agent1 agent2]
}
#--------------------------------------------------------------------------------
class BambooAgents
def initialize(base_uri, rest_endpoint, credentials)
@base_uri = base_uri
@rest_endpoint = rest_endpoint
@credentials = credentials
end
def agents_status(id)
begin
uri = URI.parse(@base_uri)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new(agent_endpoint(id))
if @credentials && @credentials[:username].length > 0
request.basic_auth @credentials[:username], @credentials[:password]
end
response = http.request(request)
doc = Nokogiri::HTML(response.body)
state = doc.at_xpath('//div[@class="form-view"]/div/div[2]/div/span/img/@alt').to_s.split(' ')[0].strip
title = doc.at_xpath('//section[@class="aui-page-panel-content"]/h1/text()').to_s.strip[1..-1]
# Idle Offline
return {
:state => state,
:title => title
}
rescue => e
puts "Error getting bamboo agent status: #{e}"
return {
:state => 'Offline',
:title => 'Unknown'
}
end
end
private
def agent_endpoint(agent_id)
"/admin/agent/viewAgent.action?agentId=#{agent_id}"
end
end
def get_agents_status(bamboo_uri, rest_endpoint, credentials, id)
agents = BambooAgents.new(bamboo_uri, rest_endpoint, credentials)
agents.agents_status(id)
end
SCHEDULER.every configuration[:refresh_rate], :first_in => 0 do |_|
configuration[:agent_ids].each { |id|
state = get_agents_status(configuration[:bamboo_uri], configuration[:rest_endpoint], configuration[:credentials], id)
puts "#{id} => #{state}\n"
send_event(id, state)
}
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$state-color: #fff;
$background-failed-color: #f44336;
$background-successful-color: #00C176;
$background-busy-color: #12b0c5;
$background-unknown-color: #ff9800;
$title-color: rgba(255, 255, 255, 1);
$label-color: rgba(255, 255, 255, 0.7);
$duration-color: rgba(255, 255, 255, 0.7);
$finished-color: rgba(0, 0, 0, 0.3);
.widget-agent-status {
background: url("/assets/bamboo-status.png") no-repeat center;
background-size: contain;
vertical-align: top;
.title {
color: $title-color;
font-weight: normal;
opacity: .9;
text-align: top;
}
.label {
font-size: 22px;
font-weight: normal;
}
.header {
font-size: 30px;
font-weight: bolder;
}
.state {
text-align: center;
display: block;
color: $state-color;
font-size: 60px;
word-wrap: break-word;
font-weight: bold;
}
&.offline {
background-color: $background-failed-color;
}
&.idle {
background-color: $background-successful-color;
}
&.building {
background-color: $background-busy-color;
}
}
<li data-row="3" data-col="4" data-sizex="1" data-sizey="1">
<div data-id="agent1" data-view="AgentStatus"></div>
</li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment