Move script files to root to ease path use, and use of shell scripts

This commit is contained in:
Insane Turkey 2016-01-04 22:04:16 -05:00
parent b174d304d7
commit a348705885
5 changed files with 136 additions and 116 deletions

View file

@ -2,7 +2,7 @@ require 'twitter'
require './services.rb'
module ServiceQuery
class ServiceQuery
# Path to service definition file
SERVICE_CONFIG = './conf/services.json'
@ -11,10 +11,10 @@ module ServiceQuery
# Query services and post any updates to twitter
def run
def self.run
# Load configuration
twitter_client = get_twitter_client
service_defs = get_service_defs
service_defs = load_service_defs
# Query services
query_services(twitter_client, service_defs)
@ -25,19 +25,19 @@ module ServiceQuery
# Load service definitions
def load_service_defs
TribesNext.read_config(SERVICE_CONFIG)
def self.load_service_defs
TribesNext::Services.read_config(SERVICE_CONFIG)
end
# Save service definitions
def write_service_defs(service_defs)
TribesNext.write_config(SERVICE_CONFIG, services)
def self.write_service_defs(service_defs)
TribesNext::Services.write_config(SERVICE_CONFIG, service_defs)
end
# Query services, and post updates if any have changed availability
def query_services(twitter_client, service_defs)
def self.query_services(twitter_client, service_defs)
workers = []
# Create background worker for each service
@ -52,32 +52,32 @@ module ServiceQuery
# Query a service, and post an update if it has changed availability
def query_service(twitter_client, service_def)
def self.query_service(twitter_client, service_def)
begin
plural? = ('s' === service_def['name'])
available?, changed? = TribesNext.query(service_def)
is_plural = ('s' === service_def['name'])
is_available, is_changed = TribesNext::Services.query_service(service_def)
# if state changed: post update
if changed? then
if available? then
if is_changed then
if is_available then
state = 'AVAILABLE'
verb = if plural? then 'are' else 'is' end
verb = if is_plural then 'are' else 'is' end
else
state = 'UNAVAILABLE'
verb = if plural? then 'appear to be' else 'appears to be' end
verb = if is_plural then 'appear to be' else 'appears to be' end
end
twitter_client.update("#{service_def['name']} #{verb} #{state}.")
end
rescue Exception => ex
puts "Exception encountered while querying #{service['name']}: #{e.message}"
puts e.backtrace.inspect if debug?
puts "Exception encountered while querying #{service_def['name']}: #{ex.message}"
puts ex.backtrace.inspect
end
end
# Create a client for interacting with a twitter account
def get_twitter_client
def self.get_twitter_client
# Load config file
opts = JSON.parse(File.read(TWITTER_CONFIG))
# create client

7
runQuery.rbEnv.sh Normal file
View file

@ -0,0 +1,7 @@
#!/bin/sh
unset GEM_HOME
unset GEM_PATH
export PATH=~/.rbenv/bin:"$PATH"
~/.rbenv/shims/ruby main.rb

2
runQuery.sh Normal file
View file

@ -0,0 +1,2 @@
#!/bin/sh
/usr/bin/ruby main.rb

109
services.rb Normal file
View file

@ -0,0 +1,109 @@
require 'json'
require 'open-uri'
require 'socket'
module TribesNext
class Services
# Read service definitions from a coniguration file
def self.read_config(config_file)
contents = File.read(config_file)
services = JSON.parse(contents)
return services
end
# Write service definitions to configuration file
def self.write_config(config_file, services)
contents = JSON.pretty_generate(services)
File.write(config_file, contents)
end
# Query a service
def self.query_service(service)
url = service['url']
available = case service['type']
when 'auth' then AccountService.query(url)
when 'list' then ListService.query(url)
when 'community' then CommunityService.query(url)
else false
end
changed = (available != service['available'])
service['changed'] = changed
service['available'] = available
return [available, changed]
end
end
class ServiceBase
# Query an HTTP service
def self.query_http_service(url)
begin
# Fetch details over HTTP
url = URI.parse(url)
info = url.read
# Assume online if we get a 200/OK response
return '200' === info.status[0]
rescue
return false
end
end
end
class AccountService < ServiceBase
# Query the account service to determine its status
def self.query(url)
begin
# Discover host
auth_host, auth_port = discover(url)
unless auth_host.nil? then
# Query host
sock = TCPSocket.new auth_host, auth_port
sock.puts 'AVAIL'
status = sock.gets
sock.close
return 'AVAIL' === status.strip
else
return false
end
rescue
return false
end
end
# Discover the account service host/port
def self.discover(url)
# Fetch details over HTTP
url = URI.parse(url)
info = url.read
# Parse out host:port
if '200' === info.status[0] then
info.split(/[:\s]/)
else
return nil
end
end
end
class ListService < ServiceBase
# Query the listing service to determine its status
def self.query(url)
return query_http_service(url)
end
end
class CommunityService < ServiceBase
# Query the community service to determine its status
def self.query(url)
return query_http_service(url)
end
end
end

View file

@ -1,98 +0,0 @@
require 'json'
require 'open-uri'
require 'socket'
module TribesNext
# Read service definitions from a coniguration file
def read_config(config_file)
contents = File.read(config_file)
services = JSON.parse(contents)
return services
end
# Write service definitions to configuration file
def write_config(config_file, services)
contents = JSON.pretty_generate(services)
File.write(config_file, contents)
end
# Query a service
def query_service(service)
url = service['url']
available? = case service['type']
when 'auth' then TribesNext.Account.query(url)
when 'list' then TribesNext.Listing.query(url)
when 'community' then TribesNext.Community.query(url)
else false
end
changed? = (available? != service['available'])
service['changed'] = changed?
service['available'] = available?
return [available? changed?]
end
# Query an HTTP service
def query_http_service(url)
# Fetch details over HTTP
url = URI.parse(url)
info = url.read
# Assume online if we get a 200/OK response
return '200' === info.status[0]
end
module Account
# Query the account service to determine its status
def query(url)
# Discover host
auth_host, auth_port = discover(service)
unless auth_host.nil? then
# Query host
sock = TCPSocket.new auth_host, auth_port
sock.puts 'AVAIL'
status = sock.gets
sock.close
return 'AVAIL' === status.strip
else
return false
end
end
# Discover the account service host/port
def discover(url)
# Fetch details over HTTP
url = URI.parse(url)
info = url.read
# Parse out host:port
if '200' === info.status[0] then
info.split(/[:\s]/)
else
return nil
end
end
end
module Listing
# Query the listing service to determine its status
def query(url)
return TribesNext.query_http(url)
end
end
module Community
# Query the community service to determine its status
def query(url)
return TribesNext.query_http(url)
end
end
end