Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Updates to MCP #10

Merged
merged 10 commits into from

1 participant

@jaredbischof

Added mlog component to MCP.
Updated code to read config file in json format.
Updated components' 'state' for MCP_API to be more consistent across all components.
Added start and stop methods to mcp_api component.
Allowing more arguments (2 to 4) to be passed to component methods for more complex commands (e.g. MCP mlog set mgrast 4 would set the log level for the mgrast component to 4). This needs to be added to documentation.

@jaredbischof jaredbischof merged commit 3fee5e8 into MG-RAST:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 18, 2013
  1. @jaredbischof
  2. @jaredbischof
  3. @jaredbischof
  4. @jaredbischof

    Changed config file from ini format to json so had to edit code here.…

    jaredbischof authored
    … Also, instead of allowing only 2 arguments to be passed to MCP components, I'm not allowing 2, 3, or 4. Documentation will have to be updated to reflect this change.
  5. @jaredbischof

    Updated code to read config file in json format. Updated component 's…

    jaredbischof authored
    …tate' to be more consistent across all components. Added stop and restart methods to mcp_api component.
  6. @jaredbischof

    Updated code to read config file in json format. Updated component 's…

    jaredbischof authored
    …tate' to be more consistent across all components.
  7. @jaredbischof

    Updated code to read config file in json format. Updated component 's…

    jaredbischof authored
    …tate' to be more consistent across all components.
  8. @jaredbischof
  9. @jaredbischof
  10. @jaredbischof
This page is out of date. Refresh to see the latest.
View
25 MCP
@@ -2,10 +2,10 @@
import os
import inspect
+import json
import sys, shlex, socket
import getpass, commands, subprocess
import re
-from ConfigParser import SafeConfigParser
# Getting paths
MCP_path = os.path.realpath(__file__)
@@ -16,8 +16,8 @@ MCP_dir = matchObj.group(0)
path = list(sys.path)
sys.path.insert(0, MCP_dir+"mcp_modules/")
-parser = SafeConfigParser()
-parser.read(MCP_dir+'conf/conf.ini')
+json_conf_file = open(MCP_dir+'conf/conf.json')
+json_conf = json.load(json_conf_file)
__doc__ = """
MG-RAST Control Program (MCP)
@@ -27,25 +27,25 @@ SERVICE \tACTIONS\t\tUSER
service_name_to_module = {}
service_action_to_req_user = {}
-for module_name in parser.get('global', 'services').split(','):
+for module_name in json_conf["global"]["services"]:
module_name = module_name.strip()
module = __import__(module_name)
service_name_to_module[module_name] = module
for name, obj in inspect.getmembers(module):
if inspect.isclass(obj) and name == module_name and hasattr(obj, 'actions'):
userhost = ""
- if parser.has_option(module_name, 'user') and parser.has_option(module_name, 'host'):
- userhost = parser.get(module_name, 'user') + '@' + parser.get(module_name, 'host')
+ if 'user' in json_conf[module_name] and 'host' in json_conf[module_name]:
+ userhost = json_conf[module_name]['user'] + '@' + json_conf[module_name]['host']
for action in obj.actions.split(','):
service_action_to_req_user[name + " " + action.strip()] = userhost
- __doc__ += name + " \t" + obj.actions + " \t" + userhost + "\n"
+ __doc__ += name + " \t" + obj.actions + " \t" + userhost + "\n"
def main(args):
usage = "\nUsage: MCP service action\n" + __doc__
# Checking number of inputs.
- if len(sys.argv) != 3:
+ if len(sys.argv) < 3:
sys.stderr.write(usage)
sys.stderr.write("\n")
else:
@@ -66,7 +66,7 @@ def main(args):
host = socket.gethostname()
if service_action_to_req_user[service_action] != "" and (user + "@" + host) != service_action_to_req_user[service_action]:
print "Handing off command to run as: " + service_action_to_req_user[service_action]
- array = commands.getstatusoutput("sudo -s ssh " + service_action_to_req_user[service_action] + " " + MCP_path + " " + service_action)
+ array = commands.getstatusoutput("sudo -s ssh " + service_action_to_req_user[service_action] + " " + MCP_path + " " + " ".join(map(str,sys.argv[1:])))
print array[1]
return 0
@@ -78,7 +78,12 @@ def main(args):
# Retrieve the appropriate "action" python method
myaction = getattr(myservice, action)
# Run the python method
- myaction()
+ if(len(sys.argv) == 3):
+ myaction()
+ elif(len(sys.argv) == 4):
+ myaction(sys.argv[3])
+ elif(len(sys.argv) == 5):
+ myaction(sys.argv[3], sys.argv[4])
return 0
View
5 conf/config_template.ini
@@ -1,5 +0,0 @@
-[hosts]
-memhost = hostname:port
-
-[urls]
-apiurl = url
View
32 conf/config_template.json
@@ -0,0 +1,32 @@
+{
+ "mlog": {
+ "user": "",
+ "host": "",
+ "log_levels": {
+ "api": 3,
+ "mgrast": 3
+ }
+ },
+ "queue": {
+ "host": "",
+ "user": ""
+ },
+ "mcp_api": {
+ "user": "",
+ "host": "",
+ "dir": "",
+ "version": 1
+ },
+ "global": {
+ "apiurl": "",
+ "services": [
+ "memcache",
+ "mcp_api",
+ "mlog",
+ "queue"
+ ]
+ },
+ "memcache": {
+ "memhost": ""
+ }
+}
View
8 mcp_modules/__init__.py
@@ -1,8 +0,0 @@
-"""
-MCP modules
-
-"""
-__author__ = 'Jared Bischof'
-__version__ = '0.1'
-__description__ = 'MCP (MG-RAST Control Program) modules including services and base classes'
-__all__ = ["mcp_base", "memcache", "nagasaki"]
View
25 mcp_modules/mcp_api.py
@@ -1,17 +1,18 @@
-import glob, json, sys, time
+import glob, json, os, sys, time
from subsystem import subsystem
class mcp_api(subsystem):
- actions = "start"
+ actions = "start, stop, restart"
def __init__(self, MCP_dir):
subsystem.__init__(self, MCP_dir)
self.state = { 'resource':self.__class__.__name__,
- 'date':time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
+ '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__,
'status':'online'
}
- self.apidir = self.parser.get('mcp_api', 'dir') + "/" + self.parser.get('mcp_api', 'version')
- self.services = self.parser.get('global', 'services')
+ self.apidir = self.json_conf['mcp_api']['dir'] + "/" + str(self.json_conf['mcp_api']['version'])
+ self.services = self.json_conf['global']['services']
def start(self):
files = glob.glob(self.apidir+"/*")
@@ -19,7 +20,7 @@ def start(self):
sys.stderr.write("ERROR: Cannot initialize mcp_api because API directory (" + self.apidir + ") is not empty\n")
return 0
- for service in self.services.split(','):
+ for service in self.services:
service = service.strip();
module = __import__(service)
myclass = getattr(module, service)
@@ -28,3 +29,15 @@ def start(self):
jstate = json.dumps(myservice.get_state())
f = open(self.apidir + "/" + service, 'w')
f.write(jstate)
+
+ def stop(self):
+ for file_object in os.listdir(self.apidir):
+ file_object_path = os.path.join(self.apidir, file_object)
+ if os.path.isfile(file_object_path):
+ os.unlink(file_object_path)
+ else:
+ shutil.rmtree(file_object_path)
+
+ def restart(self):
+ self.stop()
+ self.start()
View
5 mcp_modules/memcache.py
@@ -7,9 +7,10 @@ class memcache(subsystem):
def __init__(self, MCP_dir):
subsystem.__init__(self, MCP_dir)
self.state = { 'resource':self.__class__.__name__,
- 'date':time.strftime("%Y-%m-%d %H:%M:%S")
+ '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")
}
- self.memhost = self.parser.get('memcache', 'memhost')
+ self.memhost = self.json_conf['memcache']['memhost']
def clear(self):
print "Clearing memcache:"
View
36 mcp_modules/mlog.py
@@ -0,0 +1,36 @@
+import glob, json, sys, time
+from subsystem import subsystem
+from mcp_api import mcp_api
+
+class mlog(subsystem):
+ actions = "set"
+
+ 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]
+
+ self.apidir = self.json_conf['mcp_api']['dir'] + "/" + str(self.json_conf['mcp_api']['version'])
+
+ 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
View
3  mcp_modules/queue.py
@@ -17,7 +17,8 @@ def __init__(self, MCP_dir):
fast_status = 'online' if lines[len(lines)-1].split()[3] == 'yes' else 'offline'
self.state = { 'resource':self.__class__.__name__,
- 'date':time.strftime("%Y-%m-%d %H:%M:%S"),
+ '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__,
'queues': [ { 'name': 'batch',
'status': batch_status
},
View
6 mcp_modules/subsystem.py
@@ -1,12 +1,12 @@
+import json
import shlex, subprocess
-from ConfigParser import SafeConfigParser
class subsystem (object):
def __init__(self, MCP_dir):
self.MCP_dir = MCP_dir
self.state = {}
- self.parser = SafeConfigParser()
- self.parser.read(self.MCP_dir+'conf/conf.ini')
+ json_conf_file = open(self.MCP_dir+'conf/conf.json')
+ self.json_conf = json.load(json_conf_file)
def get_state(self):
return self.state
View
4 mlog_libs/mlog.py
@@ -101,8 +101,8 @@ def _update_api_defined_log_levels(self):
print " Alternatively, you can set log levels for components with mlog using:"
print " set_log_level(integer level, string component)"
else:
- for components in data['components']:
- self.api_defined_log_levels[components['name']] = components['log_level']
+ for component in data['log_levels']:
+ self.api_defined_log_levels[component] = data['log_levels'][component]
def _get_log_level(self, component):
if(component in self.user_defined_log_levels):
View
9 test_scripts/test_mlog_py_lib.py
@@ -1,17 +1,10 @@
#!/soft/packages/python/2.6/bin/python
-import os
import sys
-import re, time
-
-# Getting paths
-MCP_path = os.path.realpath(__file__)
-matchObj = re.match(r'(^.*\/)', MCP_path)
-MCP_dir = matchObj.group(0)
# Adding modules directory to path
path = list(sys.path)
-sys.path.insert(0, MCP_dir+"mlog_libs/")
+sys.path.insert(0, ".")
from mlog import mlog
Something went wrong with that request. Please try again.