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):
pass
def start(self):
def stop(self, name, *args, **kargs):
raise NotImplementedError()
def stop(self, pkgs):
def start(self, name, program, *args, **kargs):
raise NotImplementedError()

View File

@ -164,18 +164,6 @@ def mkdirslist(pth):
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):
with open(fn, "a") as f:
f.write(text)

View File

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

View File

@ -13,9 +13,55 @@
# License for the specific language governing permissions and limitations
# under the License.
import time
import os
import re
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):
def __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