Starting to add screen runner.

This commit is contained in:
Joshua Harlow 2012-01-17 10:27:54 -08:00
parent 7bceb807a5
commit b2606956b6
4 changed files with 77 additions and 21 deletions

View File

@ -27,8 +27,8 @@ class Runner():
def __init__(self): def __init__(self):
pass pass
def start(self): def stop(self, name, *args, **kargs):
raise NotImplementedError() raise NotImplementedError()
def stop(self, pkgs): def start(self, name, program, *args, **kargs):
raise NotImplementedError() raise NotImplementedError()

View File

@ -164,18 +164,6 @@ def mkdirslist(pth):
return dirsmade return dirsmade
def load_json(fn):
data = load_file(fn)
lines = data.splitlines()
nlines = list()
for line in lines:
if(line.lstrip().startswith('#')):
continue
nlines.append(line)
data = os.linesep.join(nlines)
return json.loads(data)
def append_file(fn, text, flush=True): def append_file(fn, text, flush=True):
with open(fn, "a") as f: with open(fn, "a") as f:
f.write(text) f.write(text)

View File

@ -18,13 +18,15 @@ from termcolor import colored
import os import os
import platform import platform
import re import re
import json
import subprocess import subprocess
import netifaces
from Exceptions import (BadRegexException, from Exceptions import (BadRegexException,
NoReplacementException, NoReplacementException,
FileException) FileException)
import Logger import Logger
from Shell import (joinpths, load_json, execute) from Shell import (joinpths, load_file, execute)
#constant goodies #constant goodies
VERSION = 0x2 VERSION = 0x2
@ -47,6 +49,7 @@ IPV4 = 'IPv4'
IPV6 = 'IPv6' IPV6 = 'IPv6'
DEFAULT_NET_INTERFACE = 'eth0' DEFAULT_NET_INTERFACE = 'eth0'
DEFAULT_NET_INTERFACE_IP_VERSION = IPV4 DEFAULT_NET_INTERFACE_IP_VERSION = IPV4
PARAM_SUB_REGEX = "%([\\w\\d]+?)%"
#component name mappings #component name mappings
NOVA = "nova" NOVA = "nova"
@ -194,6 +197,7 @@ def execute_template(*cmds, **kargs):
return return
params_replacements = kargs.pop('params') params_replacements = kargs.pop('params')
ignore_missing = kargs.pop('ignore_missing', False) ignore_missing = kargs.pop('ignore_missing', False)
outs = dict()
for cmdinfo in cmds: for cmdinfo in cmds:
cmd_to_run_templ = cmdinfo.get("cmd") cmd_to_run_templ = cmdinfo.get("cmd")
cmd_to_run = list() cmd_to_run = list()
@ -213,7 +217,7 @@ def execute_template(*cmds, **kargs):
ignore_missing=ignore_missing)) ignore_missing=ignore_missing))
else: else:
stdin_full.append(piece) stdin_full.append(piece)
stdin = joinlinesep(stdin_full) stdin = joinlinesep(*stdin_full)
root_run = cmdinfo.get('run_as_root', False) root_run = cmdinfo.get('run_as_root', False)
execute(*cmd_to_run, run_as_root=root_run, process_input=stdin, **kargs) execute(*cmd_to_run, run_as_root=root_run, process_input=stdin, **kargs)
@ -243,6 +247,18 @@ def component_pths(root, compnent_type):
return out return out
def load_json(fn):
data = load_file(fn)
lines = data.splitlines()
new_lines = list()
for line in lines:
if(line.lstrip().startswith('#')):
continue
new_lines.append(line)
data = joinlinesep(*new_lines)
return json.loads(data)
def get_host_ip(cfg=None): def get_host_ip(cfg=None):
ip = None ip = None
if(cfg): if(cfg):
@ -261,7 +277,6 @@ def get_host_ip(cfg=None):
def get_interfaces(): def get_interfaces():
import netifaces
interfaces = dict() interfaces = dict()
for intfc in netifaces.interfaces(): for intfc in netifaces.interfaces():
interface_info = dict() interface_info = dict()
@ -328,6 +343,7 @@ def get_pip_list(distro, component):
if(distro_pkgs and len(distro_pkgs)): if(distro_pkgs and len(distro_pkgs)):
combined = dict(all_pkgs) combined = dict(all_pkgs)
for (pkgname, pkginfo) in distro_pkgs.items(): for (pkgname, pkginfo) in distro_pkgs.items():
#we currently just overwrite
combined[pkgname] = pkginfo combined[pkgname] = pkginfo
all_pkgs = combined all_pkgs = combined
return all_pkgs return all_pkgs
@ -352,7 +368,7 @@ def get_pkg_list(distro, component):
for (infokey, infovalue) in pkginfo.items(): for (infokey, infovalue) in pkginfo.items():
#this is expected to be a list of cmd actions #this is expected to be a list of cmd actions
#so merge that accordingly #so merge that accordingly
if(infokey == 'pre-install' or infokey == 'post-install'): if(infokey == PRE_INSTALL or infokey == POST_INSTALL):
oldinstalllist = oldpkginfo.get(infokey) or [] oldinstalllist = oldpkginfo.get(infokey) or []
infovalue = oldinstalllist + infovalue infovalue = oldinstalllist + infovalue
newpkginfo[infokey] = infovalue newpkginfo[infokey] = infovalue
@ -364,10 +380,17 @@ def get_pkg_list(distro, component):
def joinlinesep(*pieces): def joinlinesep(*pieces):
return os.linesep.join(*pieces) return os.linesep.join(pieces)
def param_replace(text, replacements, ignore_missing=False): def param_replace(text, replacements, ignore_missing=False):
if(not replacements or len(replacements) == 0):
return text
if(len(text) == 0):
return text
if(ignore_missing): if(ignore_missing):
LOG.debug("Performing parameter replacements (ignoring missing) on %s" % (text)) LOG.debug("Performing parameter replacements (ignoring missing) on %s" % (text))
else: else:
@ -386,8 +409,7 @@ def param_replace(text, replacements, ignore_missing=False):
LOG.debug("Replacing [%s] with [%s]" % (org, str(v))) LOG.debug("Replacing [%s] with [%s]" % (org, str(v)))
return str(v) return str(v)
ntext = re.sub("%([\\w\\d]+?)%", replacer, text) return re.sub(PARAM_SUB_REGEX, replacer, text)
return ntext
def welcome(program_action): def welcome(program_action):

View File

@ -13,9 +13,55 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import time
import os
import re
import Runner import Runner
import Logger
from Exceptions import (StartException, StopException)
from Util import (execute_template)
from Shell import (execute)
LOG = Logger.getLogger("install.screen")
SCREEN_MAKE = ['screen', '-d', '-m', '-S', '%NAME%', '-t', '%NAME%']
NAME_POSTFIX = ".devstack"
class Screen(Runner.Runner): class Screen(Runner.Runner):
def __init__(self): def __init__(self):
Runner.Runner.__init__(self) Runner.Runner.__init__(self)
def stop(self, name, *args, **kargs):
real_name = name + NAME_POSTFIX
list_cmd = ['screen', '-list']
(sysout, stderr) = execute(*list_cmd)
lines = sysout.splitlines()
entries = list()
lookfor = r"^(\d+\." + re.escape(real_name) + r")\s+(.*)$"
for line in lines:
cleaned_line = line.strip()
if(len(cleaned_line) == 0):
continue
mtch = re.match(lookfor, cleaned_line)
if(not mtch):
continue
kill_entry = mtch.group(1)
entries.append(kill_entry)
for entry in entries:
kill_cmd = ['screen', '-r', entry, '-X', 'kill']
execute(*kill_cmd)
time.sleep(2)
quit_cmd = ['screen', '-r', entry, '-X', 'quit']
execute(*quit_cmd)
def start(self, name, program, *args, **kargs):
app_dir = kargs.get('app_dir')
params = dict()
params['NAME'] = name + NAME_POSTFIX
runcmd = SCREEN_MAKE + [program] + list(args)
cmds = [{'cmd':runcmd}]
execute_template(*cmds, params=params, cwd=app_dir, **kargs)
return None