Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from jaredbischof/master
Mostly moving arg parsing around and adding to the base subsystem class.
- Loading branch information
Showing
7 changed files
with
144 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
import time | ||
from subsystem import subsystem | ||
|
||
class memcache(subsystem): | ||
actions = "clear" | ||
actions = [ 'clear', 'log' ] | ||
|
||
def __init__(self, MCP_dir): | ||
subsystem.__init__(self, MCP_dir) | ||
self.state = { 'resource':self.__class__.__name__, | ||
'url':self.json_conf['global']['apiurl'] + "/" + str(self.json_conf['mcp_api']['version']) + "/" + self.__class__.__name__, | ||
'updated':time.strftime("%Y-%m-%d %H:%M:%S") | ||
} | ||
def __init__(self, MCP_path): | ||
subsystem.__init__(self, MCP_path) | ||
self.memhost = self.json_conf['memcache']['memhost'] | ||
|
||
def clear(self): | ||
print "Clearing memcache:" | ||
sout, serr = self.run_cmd(self.MCP_dir + "bin/clear_memcache.pl " + self.memhost) | ||
sout = self.run_cmd(self.MCP_dir + "bin/clear_memcache.pl " + self.memhost) | ||
print "memcache cleared!" | ||
return 0 | ||
return 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,8 @@ | ||
import json, sys, time | ||
import json, sys | ||
from subsystem import subsystem | ||
|
||
class mlog(subsystem): | ||
actions = "set" | ||
actions = [ 'log' ] | ||
|
||
def __init__(self, MCP_dir): | ||
subsystem.__init__(self, MCP_dir) | ||
self.state = { 'resource':self.__class__.__name__, | ||
'updated':time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), | ||
'url':self.json_conf['global']['apiurl'] + "/" + str(self.json_conf['mcp_api']['version']) + "/" + self.__class__.__name__, | ||
'log_levels':{} | ||
} | ||
|
||
for component in self.json_conf['mlog']['log_levels']: | ||
self.state['log_levels'][component] = self.json_conf['mlog']['log_levels'][component] | ||
|
||
def set(self, component, level): | ||
if component not in self.json_conf['mlog']['log_levels']: | ||
sys.stderr.write("ERROR: '" + component + "' is not a valid logging component.\n") | ||
return 0 | ||
else: | ||
try: | ||
int(level) | ||
except ValueError: | ||
sys.stderr.write("ERROR: '" + level + "' is not an integer value.\n") | ||
return 0 | ||
self.state['log_levels'][component] = int(level) | ||
self.state['updated'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) | ||
jstate = json.dumps(self.get_state()) | ||
f = open(self.apidir + "/" + self.__class__.__name__, 'w') | ||
f.write(jstate) | ||
return 1 | ||
def __init__(self, MCP_path): | ||
subsystem.__init__(self, MCP_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,30 @@ | ||
import time | ||
from subsystem import subsystem | ||
|
||
class queue(subsystem): | ||
actions = "start, stop" | ||
actions = [ 'start', 'stop', 'log' ] | ||
|
||
def __init__(self, MCP_dir): | ||
subsystem.__init__(self, MCP_dir) | ||
def __init__(self, MCP_path): | ||
subsystem.__init__(self, MCP_path) | ||
|
||
# getting status of queues | ||
sout, serr = self.run_cmd("/usr/local/bin/qstat -Q batch") | ||
sout = self.run_cmd("/usr/local/bin/qstat -Q batch") | ||
lines = sout.splitlines() | ||
batch_status = 'online' if lines[len(lines)-1].split()[3] == 'yes' else 'offline' | ||
|
||
sout, serr = self.run_cmd("/usr/local/bin/qstat -Q fast") | ||
sout = self.run_cmd("/usr/local/bin/qstat -Q fast") | ||
lines = sout.splitlines() | ||
fast_status = 'online' if lines[len(lines)-1].split()[3] == 'yes' else 'offline' | ||
|
||
self.state = { 'resource':self.__class__.__name__, | ||
'updated':time.strftime("%Y-%m-%d %H:%M:%S"), | ||
'url':self.json_conf['global']['apiurl'] + "/" + str(self.json_conf['mcp_api']['version']) + "/" + self.__class__.__name__, | ||
'status': { 'batch': batch_status, | ||
'fast': fast_status | ||
} | ||
} | ||
self.state['status'] = { 'batch': batch_status, 'fast': fast_status } | ||
|
||
def start(self): | ||
print "Starting nagasaki pipeline:" | ||
sout, serr = self.run_cmd("/usr/local/bin/qstart batch") | ||
sout = self.run_cmd("/usr/local/bin/qstart batch") | ||
print "nagasaki pipeline started!" | ||
return 0 | ||
return 1 | ||
|
||
def stop(self): | ||
print "Stopping nagasaki pipeline:" | ||
sout, serr = self.run_cmd("/usr/local/bin/qstop batch") | ||
sout = self.run_cmd("/usr/local/bin/qstop batch") | ||
print "nagasaki pipeline stopped!" | ||
return 0 | ||
return 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,88 @@ | ||
import argparse | ||
import commands | ||
import json | ||
import shlex, subprocess | ||
import getpass, re, socket | ||
import shlex, shutil, subprocess | ||
import sys, time | ||
|
||
class subsystem (object): | ||
def __init__(self, MCP_dir): | ||
self.MCP_dir = MCP_dir | ||
self.state = {} | ||
def __init__(self, MCP_path): | ||
self.MCP_path = MCP_path | ||
matchObj = re.match(r'(^.*\/)', self.MCP_path) | ||
self.MCP_dir = matchObj.group(0) | ||
|
||
self.subsystem = self.__class__.__name__ | ||
json_conf_file = open(self.MCP_dir+'conf/conf.json') | ||
self.json_conf = json.load(json_conf_file) | ||
self.apidir = self.json_conf['mcp_api']['dir'] + "/" + str(self.json_conf['mcp_api']['version']) | ||
self.log_level_max = self.json_conf['global']['log_level_max'] | ||
self.log_level_min = self.json_conf['global']['log_level_min'] | ||
|
||
self.req_host = "" | ||
if 'req_host' in self.json_conf[self.subsystem]: | ||
self.req_host = self.json_conf[self.subsystem]['req_host'] | ||
|
||
self.req_user = "" | ||
if 'req_user' in self.json_conf[self.subsystem]: | ||
self.req_user = self.json_conf[self.subsystem]['req_user'] | ||
|
||
self.state = { 'log_level':self.json_conf['global']['default_log_level'], | ||
'subsystem':self.subsystem, | ||
'updated':time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), | ||
'url':self.json_conf['global']['apiurl'] + "/" + str(self.json_conf['mcp_api']['version']) + "/" + self.subsystem | ||
} | ||
|
||
def run(self, params): | ||
parser = argparse.ArgumentParser(prog='MCP ' + self.subsystem) | ||
parser.add_argument('action', metavar='action', | ||
help="An action for the subsystem '" + self.subsystem + "' to perform. Available actions include: '" + "', '".join(self.actions) + "'") | ||
parser.add_argument('params', metavar='params', nargs=argparse.REMAINDER, | ||
help="Additional arguments should specify an action followed by action parameters if required.") | ||
args = parser.parse_args(params) | ||
action = args.action | ||
params = args.params | ||
|
||
user = getpass.getuser() | ||
host = socket.gethostname() | ||
req_login = self.req_user + "@" + self.req_host | ||
if self.req_host != "" and self.req_user != "" and (user + "@" + host) != req_login: | ||
print "Handing off command to run as: " + req_login | ||
sout = self.run_cmd("sudo -s ssh " + " ".join([req_login, self.MCP_path, self.subsystem, action]) + " " + " ".join(params)) | ||
return 1 | ||
|
||
myaction = getattr(self, action) | ||
if params: | ||
myaction(params) | ||
else: | ||
myaction() | ||
|
||
def log(self, level): | ||
try: | ||
level = int(level) | ||
except ValueError: | ||
sys.stderr.write("ERROR: '" + level + "' is not an integer value.\n") | ||
return 0 | ||
|
||
if level < self.log_level_min or level > self.log_level_max: | ||
sys.stderr.write("ERROR: '" + str(level) + "' is not a valid logging level.\n") | ||
return 0 | ||
|
||
self.state['log_level'] = level | ||
self.state['updated'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) | ||
jstate = json.dumps(self.state) | ||
fname = self.apidir + "/" + self.__class__.__name__ | ||
f = open(fname + ".tmp", 'w') | ||
f.write(jstate) | ||
shutil.move(fname + ".tmp", fname) | ||
|
||
def get_state(self): | ||
return self.state | ||
return 1 | ||
|
||
def run_cmd(self, cmd_str): | ||
cmd = shlex.split(str(cmd_str)) | ||
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
stdout, stderr = proc.communicate() | ||
if proc.returncode != 0: | ||
raise IOError("%s\n%s"%(" ".join(cmd), stderr)) | ||
sout, serr = proc.communicate() | ||
if proc.returncode != 0 or serr != "": | ||
raise IOError("%s\n%s"%(" ".join(cmd), serr)) | ||
return 0 | ||
|
||
return stdout, stderr | ||
return sout |
Oops, something went wrong.