Commit d793312f authored by Cool Fire's avatar Cool Fire

Now able to recveive results of jobs

parent c2add876
{
"daemonize":true,
"host":"localhost",
"max_skew":1,
"user":"guest",
"pass":"rabbitmq_pass",
"channel_commands":"ServerName",
"channel_results":"Results",
"private_keyfile":"config/private.pem",
"encryption_pass":"password_for_private.pem",
"api_url":"http://localhost:3000/api/index"
"api_pass":"some_secret_key"
}
"daemonize":true,
"host":"localhost",
"max_skew":1,
"user":"guest",
"pass":"rabbitmq_pass",
"channel_commands":"ServerName",
"channel_results":"Results",
"private_keyfile":"config/private.pem",
"encryption_pass":"password_for_private.pem",
"api_url":"http://localhost:3000/api/index",
"api_pass":"some_secret_key"
}
\ No newline at end of file
class ApiController < ApplicationController
def index
end
skip_before_filter :verify_authenticity_token, only: [:index]
def index
@result = update_job(params)
end
private
def command_params
params.require(:job_update).permit(:id, :timestamp, :exitcode, :message, :key)
end
def update_job(j)
# Check key
config = loadconfig()
if(config['api_pass'] != j[:key])
return "Invalid key"
end
# Get job
job = Job.find(j[:id])
# Update values
job.timestampcompleted = j[:timestamp]
job.state = j[:exitcode]
job.result = j[:message]
job.save
end
end
class JobsController < ApplicationController
def index
@jobs = Job.all
end
def new
# Check if parameters are set
begin
......@@ -60,20 +64,8 @@ class JobsController < ApplicationController
def queue_job(job)
# Load configuration
configfile = 'config/rabbitmq.conf'
if(File.exists?(configfile))
configdata = ''
File.open(configfile) do |file|
file.each do |line|
configdata << line
end
end
config = JSON.parse(configdata)
else
raise "Config file '#{configfile}' does not exist."
end
config = loadconfig()
# Read encryption key
private_key = OpenSSL::PKey::RSA.new(File.read(config['private_keyfile']), config['encryption_pass'])
......
<h1>Api#index</h1>
<p>Find me in app/views/api/index.html.erb</p>
<%= @result.inspect %>
\ No newline at end of file
Jobs index.
\ No newline at end of file
<div class='pagetitle'>
Jobs
</div>
<div class='list'>
<table>
<tr class='listtiles'>
<td>Job ID</td>
<td>Started</td>
<td>Finished</td>
<td>Server</td>
<td>Command</td>
<td>Arguments</td>
<td>Result</td>
</tr>
<% @jobs.reverse.each do |job| %>
<tr class='jobstatus<%= job.state %>'>
<td><%= job.id %></td>
<td><%= showtime(job.timestampstarted) %></td>
<td><%= showtime(job.timestampcompleted) %></td>
<td><%= link_to job.server.shortname, server_path(job.server) %></td>
<td><%= link_to job.command.name, job_path(job) %></td>
<td><%= job.args %></td>
<td><%= job.result %></td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
......@@ -29,11 +29,11 @@
</tr>
<tr>
<td>Start time</td>
<td><%= DateTime.strptime(@job.timestampstarted.to_s,'%s').strftime('%F %T') %></td>
<td><%= showtime(@job.timestampstarted) %></td>
</tr>
<tr>
<td>End time</td>
<td><%= @job.timestampcompleted ? DateTime.strptime(@job.timestampcompleted.to_s,'%s').strftime('%F %T') : "Not completed yet" %></td>
<td><%= showtime(@job.timestampcompleted) %></td>
</tr>
</table>
</div>
\ No newline at end of file
<div class='pagetitle'>
Servers
</div>
<div class='list'>
<table>
<% @servers.each do |server| %>
......
......@@ -6,7 +6,28 @@
</div>
<div class='servertasks'>
<% if @jobcount > 0 %>
Has jobs
<div class='list'>
<table>
<tr class='listtitles'>
<td>Job ID</td>
<td>Started</td>
<td>Finished</td>
<td>Command</td>
<td>Arguments</td>
<td>Result</td>
</tr>
<% @server.jobs.reverse.each do |job| %>
<tr class='jobstatus<%= job.state %>'>
<td><%= job.id %></td>
<td><%= showtime(job.timestampstarted) %></td>
<td><%= showtime(job.timestampcompleted) %></td>
<td><%= link_to job.command.name, job_path(job) %></td>
<td><%= job.args %></td>
<td><%= job.result %></td>
</tr>
<% end %>
</table>
</div>
<% else %>
No jobs on this server.
<% end %>
......
......@@ -3,3 +3,26 @@ require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
# Show time just the way we like it
def showtime(timestamp)
timestamp ? Time.at(timestamp).strftime('%F %T') : "-"
end
def loadconfig()
configfile = 'config/rabbitmq.conf'
if(File.exists?(configfile))
configdata = ''
File.open(configfile) do |file|
file.each do |line|
configdata << line
end
end
config = JSON.parse(configdata)
else
raise "Config file '#{configfile}' does not exist."
end
return config
end
{
"host":"localhost",
"user":"guest",
"pass":"rabbitmq_pass",
"private_keyfile":"config/private.pem",
"encryption_pass":"password_for_private.pem"
"host":"localhost",
"user":"guest",
"pass":"rabbitmq_pass",
"private_keyfile":"config/private.pem",
"encryption_pass":"password_for_private.pem",
"api_pass":"some_secret_key"
}
\ No newline at end of file
Rails.application.routes.draw do
get 'api/index'
get 'api/index'
post 'api/index'
resources :commands, :servers, :jobs
......
......@@ -7,6 +7,8 @@ require 'json'
require 'optparse'
require 'openssl'
require 'time'
require "net/http"
require "uri"
# Function to report errors (to syslog or to stderr)
def report(message)
......@@ -88,7 +90,6 @@ q.subscribe(:block => true) do |delivery_info, properties, payload|
# Try to decrypt and parse message
begin
puts "Received anything at all"
message = JSON.parse(private_key.private_decrypt(payload))
# Check if the message seems to be authentic and sane
......@@ -97,6 +98,17 @@ q.subscribe(:block => true) do |delivery_info, properties, payload|
report("Rejected message #{message['id']}: Timestamp too old. (#{delivery_info[:channel].connection.host})")
else
puts message
uri = URI.parse(config['api_url'])
response = Net::HTTP.post_form(uri,
{
'id' => message['id'],
'timestamp' => message['timestamp'],
'exitcode' => message['exitcode'],
'message' => message['message'],
'key' => config['api_pass']
}
)
puts response.body
end
rescue Exception => e
report("Could not decode message. (#{delivery_info[:channel].connection.host})")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment