Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #17 from hmrc/ADD_APPEND_ARGS_TO_CMD
Browse files Browse the repository at this point in the history
ADD_APPEND_ARGS_TO_CMD Allowing the command line to take appendArgs too
  • Loading branch information
vaughansharman committed Dec 2, 2014
2 parents aaf126d + d636839 commit b8079cf
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
7 changes: 4 additions & 3 deletions bin/sm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def _process_command():
parser.add_argument('--autorefresh', nargs='*', help='Get the status of a single service or a list of services and refreshes until killed')
parser.add_argument('-n', '--shownotrunning', action='store_true', help='Used with status command to show services that are not running')
parser.add_argument('--proxy', type=str, help='Use with start to start those items with a proxy, i.e. add at the end --proxy localhost:8888')
parser.add_argument('--appendArgs', type=json.loads, help='A map of args to append for services you are starting. i.e. \'{"SERVICE_NAME":["-DFoo=Bar","SOMETHING"],"SERVICE_TWO":["APPEND_THIS"]}\'')
parser.add_argument('-o', '--offline', action='store_true', help='Offline mode: uses existing binaries instead of looking for an updated version online')
parser.add_argument('--start', type=str, nargs='*', help='Starts a single service/profile, or a list of services/profiles').completer = ServiceCompleter
parser.add_argument('--restart', type=str, nargs='*', help='Restarts a single service/profile or a list of services/profiles').completer = ServiceCompleter
Expand Down Expand Up @@ -89,10 +90,10 @@ def _process_command():
time.sleep(5)
for service_name in service_resolver.resolve_services_from_array(args.restart):
if context.has_service(service_name):
actions.start_one(context, service_name, args.fatjar, args.release, args.proxy, actions.overridden_port(args.restart, args.port))
actions.start_one(context, service_name, args.fatjar, args.release, args.proxy, actions.overridden_port(args.restart, args.port), args.appendArgs)

if args.start:
actions.start_and_wait(service_resolver, context, args.start, args.fatjar, args.release, args.proxy, args.port, args.wait)
actions.start_and_wait(service_resolver, context, args.start, args.fatjar, args.release, args.proxy, args.port, args.wait, args.appendArgs)

if args.cleanlogs:
for service_name in context.application.services:
Expand Down Expand Up @@ -147,7 +148,7 @@ def _process_command():
print "The parameters you provided would start the following services using the following commands:"
for service_name in service_resolver.resolve_services_from_array(args.showcmdfor):
if context.has_service(service_name):
cmd = actions.get_start_cmd(context, service_name, args.fatjar, args.release, args.proxy, actions.overridden_port(args.showcmdfor, args.port))
cmd = actions.get_start_cmd(context, service_name, args.fatjar, args.release, args.proxy, actions.overridden_port(args.showcmdfor, args.port), args.appendArgs)
print service_name
print " ".join(cmd)
else:
Expand Down
15 changes: 9 additions & 6 deletions servicemanager/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from servicemanager.smprocess import SmProcess
from servicemanager.service.smplayservice import SmPlayService

def start_one(context, service_name, fatjar, release, proxy, port=None):
def start_one(context, service_name, fatjar, release, proxy, port=None, appendArgs=None):
if release:
run_from = "RELEASE"
elif fatjar:
Expand All @@ -27,14 +27,14 @@ def start_one(context, service_name, fatjar, release, proxy, port=None):
print "There is already: '" + str(len(existing_service_status)) + "' instance(s) of the service: '" + service_name + "' running"
return False

if context.start_service(service_name, run_from, proxy, port=port, version=version):
if context.start_service(service_name, run_from, proxy, port=port, version=version, appendArgs=appendArgs):
if context.get_service(service_name).is_started_on_default_port():
print "Started: " + service_name
return True

return False

def get_start_cmd(context, service_name, fatjar, release, proxy, port=None):
def get_start_cmd(context, service_name, fatjar, release, proxy, port=None, appendArgs=None):
if release:
run_from = "RELEASE"
elif fatjar:
Expand All @@ -46,7 +46,7 @@ def get_start_cmd(context, service_name, fatjar, release, proxy, port=None):
if version == "LATEST":
version = None

starter = context.get_service_starter(service_name, run_from, proxy, port=port, version=version)
starter = context.get_service_starter(service_name, run_from, proxy, port=port, version=version, appendArgs=appendArgs)
return starter.get_start_command(run_from)

def stop_profile(context, profile):
Expand Down Expand Up @@ -163,12 +163,15 @@ def display_info(context, service_name):
print "| " + comments


def start_and_wait(service_resolver, context, start, fatjar, release, proxy, port, seconds_to_wait):
def start_and_wait(service_resolver, context, start, fatjar, release, proxy, port, seconds_to_wait, append_args):

all_services = service_resolver.resolve_services_from_array(start)
for service_name in all_services:
if context.has_service(service_name):
start_one(context, service_name, fatjar, release, proxy, overridden_port(start, port))
append_args_for_this_service = None
if append_args is not None:
append_args_for_this_service = append_args.get(service_name, None)
start_one(context, service_name, fatjar, release, proxy, overridden_port(start, port), append_args_for_this_service)
else:
print "The requested service %s does not exist" % service_name

Expand Down
6 changes: 4 additions & 2 deletions servicemanager/smprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ def _is_pycharm_related_process(pid):
ps_command = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
ps_output = ps_command.stdout.read()
ps_pid_str = ps_output.strip()
if ps_pid_str and int(ps_pid_str) == pid:
return True
for process in ps_pid_str.split("\n"):
if process and int(process) == pid:
return True

return False


Expand Down
71 changes: 63 additions & 8 deletions test/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,15 @@ def test_start_and_stop_one(self):
config_dir_override = os.path.join(os.path.dirname(__file__), "conf")
context = SmContext(SmApplication(config_dir_override), None, False, False)
actions.start_one(context, "TEST_ONE", True, False, None, port=None)
self.assertIsNotNone(context.get_service("TEST_ONE").status())
self.assertEquals(len(context.get_service("TEST_ONE").status()), 1)
context.kill("TEST_ONE")
self.assertEqual(context.get_service("TEST_ONE").status(), [])

def test_start_and_stop_one_with_append_args(self):
config_dir_override = os.path.join(os.path.dirname(__file__), "conf")
context = SmContext(SmApplication(config_dir_override), None, False, False)
actions.start_one(context, "TEST_ONE", True, False, None, None, ["; echo 'Fin du sleep!!'"])
self.assertEquals(len(context.get_service("TEST_ONE").status()), 2) # it is two in this case because the append creates a forked process
context.kill("TEST_ONE")
self.assertEqual(context.get_service("TEST_ONE").status(), [])

Expand All @@ -101,7 +109,7 @@ def test_dropwizard_from_source(self):
service_resolver = ServiceResolver(sm_application)

servicetostart = "DROPWIZARD_NEXUS_END_TO_END_TEST"
actions.start_and_wait(service_resolver, context, [servicetostart], False, False, None, port=None, seconds_to_wait=90)
actions.start_and_wait(service_resolver, context, [servicetostart], False, False, None, port=None, seconds_to_wait=90, append_args=None)
self.assertIsNotNone(context.get_service(servicetostart).status())
context.kill(servicetostart)
self.assertEqual(context.get_service(servicetostart).status(), [])
Expand All @@ -118,7 +126,7 @@ def test_dropwizard_from_jar(self):
time.sleep(5)

servicetostart = "DROPWIZARD_NEXUS_END_TO_END_TEST"
actions.start_and_wait(service_resolver, context, [servicetostart], True, False, None, port=None, seconds_to_wait=90)
actions.start_and_wait(service_resolver, context, [servicetostart], True, False, None, port=None, seconds_to_wait=90, append_args=None)
self.assertIsNotNone(context.get_service(servicetostart).status())
context.kill(servicetostart)
context.kill("FAKE_NEXUS")
Expand All @@ -133,7 +141,10 @@ def test_play_from_source(self):
service_resolver = ServiceResolver(sm_application)

servicetostart = "PLAY_NEXUS_END_TO_END_TEST"
actions.start_and_wait(service_resolver, context, [servicetostart], False, False, None, port=None, seconds_to_wait=90)
port = None
secondsToWait = 90
append_args = None
actions.start_and_wait(service_resolver, context, [servicetostart], False, False, None, port, secondsToWait, append_args)
self.assertIsNotNone(context.get_service(servicetostart).status())
context.kill(servicetostart)
self.assertEqual(context.get_service(servicetostart).status(), [])
Expand All @@ -152,9 +163,48 @@ def test_successful_play_from_jar_without_waiting(self):
self.assertIsNotNone(context.get_service("FAKE_NEXUS").status())
time.sleep(5)

fatJar = True
release = False
proxy = None
port = None
seconds_to_wait = None
append_args = None

try:
servicetostart = ["PLAY_NEXUS_END_TO_END_TEST"]
actions.start_and_wait(service_resolver, context, servicetostart, fatjar=True, release=False, proxy=None, port=None, seconds_to_wait=None)
actions.start_and_wait(service_resolver, context, servicetostart, fatJar, release, proxy, port, seconds_to_wait, append_args)
finally:
context.kill_everything()

def test_successful_play_from_jar_without_waiting_with_append_args(self):
config_dir_override = os.path.join(os.path.dirname(__file__), "conf")
sm_application = SmApplication(config_dir_override)
context = SmContext(sm_application, None, False, False)
service_resolver = ServiceResolver(sm_application)

context.kill_everything()
time.sleep(5)

response1 = actions.start_one(context, "FAKE_NEXUS", True, False, None, None, None)
self.assertTrue(response1)
self.assertIsNotNone(context.get_service("FAKE_NEXUS").status())
time.sleep(5)

servicetostart = ["PLAY_NEXUS_END_TO_END_TEST"]
appendArgs = {"PLAY_NEXUS_END_TO_END_TEST": ["-DFoo=Bar"]}
fatJar = True
release = False
proxy = None
port = None
seconds_to_wait = None

try:
actions.start_and_wait(service_resolver, context, servicetostart, fatJar, release, proxy, port, seconds_to_wait, appendArgs)
time.sleep(5)
service = SmPlayService(context, "PLAY_NEXUS_END_TO_END_TEST")
processes = SmProcess.processes_matching(service.pattern)
self.assertEqual(len(processes), 1)
self.assertTrue("-DFoo=Bar" in processes[0].args)
finally:
context.kill_everything()

Expand All @@ -175,7 +225,7 @@ def test_failing_play_from_jar(self):

try:
servicetostart = ["BROKEN_PLAY_PROJECT"]
actions.start_and_wait(service_resolver, context, servicetostart, fatjar=True, release=False, proxy=None, port=None, seconds_to_wait=2)
actions.start_and_wait(service_resolver, context, servicetostart, fatjar=True, release=False, proxy=None, port=None, seconds_to_wait=2, append_args=None)
self.fail("Did not expect the project to startup.")
except ServiceManagerException as sme:
self.assertEqual("Timed out starting service(s): BROKEN_PLAY_PROJECT", sme.message)
Expand Down Expand Up @@ -237,7 +287,10 @@ def test_wait_on_assets_server(self):
self.assertIsNotNone(context.get_service("FAKE_NEXUS").status())
time.sleep(5)

actions.start_and_wait(service_resolver, context, ["PYTHON_SIMPLE_SERVER_ASSETS_FRONTEND"], True, False, None, port=None, seconds_to_wait=5)
port = None
seconds_to_wait = 5
append_args = None
actions.start_and_wait(service_resolver, context, ["PYTHON_SIMPLE_SERVER_ASSETS_FRONTEND"], True, False, None, port, seconds_to_wait, append_args)
self.assertIsNotNone(context.get_service("PYTHON_SIMPLE_SERVER_ASSETS_FRONTEND").status())
context.kill("PYTHON_SIMPLE_SERVER_ASSETS_FRONTEND")
context.kill("FAKE_NEXUS")
Expand All @@ -249,7 +302,9 @@ def test_wait_on_assets_server(self):
def test_python_server_offline(self):
config_dir_override = os.path.join(os.path.dirname(__file__), "conf")
context = SmContext(SmApplication(config_dir_override), None, True, False)
actions.start_one(context, "PYTHON_SIMPLE_SERVER_ASSETS_FRONTEND", True, False, None, port=None)
port = None
append_args = None
actions.start_one(context, "PYTHON_SIMPLE_SERVER_ASSETS_FRONTEND", True, False, None, port, append_args)
self.assertIsNotNone(context.get_service("PYTHON_SIMPLE_SERVER_ASSETS_FRONTEND").status())
context.kill("PYTHON_SIMPLE_SERVER_ASSETS_FRONTEND")
time.sleep(5)
Expand Down

0 comments on commit b8079cf

Please sign in to comment.