~ubuntu-branches/ubuntu/vivid/sahara/vivid-proposed

« back to all changes in this revision

Viewing changes to sahara/service/edp/resources/launch_command.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2014-09-24 16:34:46 UTC
  • Revision ID: package-import@ubuntu.com-20140924163446-8gu3zscu5e3n9lr2
Tags: upstream-2014.2~b3
ImportĀ upstreamĀ versionĀ 2014.2~b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Copyright (c) 2014 OpenStack Foundation
 
4
#
 
5
# Licensed under the Apache License, Version 2.0 (the "License");
 
6
# you may not use this file except in compliance with the License.
 
7
# You may obtain a copy of the License at
 
8
#
 
9
#    http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
# Unless required by applicable law or agreed to in writing, software
 
12
# distributed under the License is distributed on an "AS IS" BASIS,
 
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
14
# implied.
 
15
# See the License for the specific language governing permissions and
 
16
# limitations under the License.
 
17
 
 
18
import logging
 
19
import signal
 
20
import subprocess
 
21
import sys
 
22
 
 
23
log = logging.getLogger()
 
24
hdlr = logging.FileHandler(sys.argv[0]+".log")
 
25
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
 
26
hdlr.setFormatter(formatter)
 
27
log.addHandler(hdlr)
 
28
log.setLevel(logging.DEBUG)
 
29
 
 
30
 
 
31
def make_handler(a):
 
32
    def handle_signal(signum, stack):
 
33
        a.send_signal(signum)
 
34
        log.info("Sent SIGINT to subprocess")
 
35
    return handle_signal
 
36
 
 
37
log.info("Running %s" % ' '.join(sys.argv[1:]))
 
38
 
 
39
try:
 
40
    # "Unignore" SIGINT before the subprocess is launched
 
41
    # in case this process is running in the background
 
42
    # (background processes ignore SIGINT)
 
43
    signal.signal(signal.SIGINT, signal.SIG_DFL)
 
44
 
 
45
    # Interpret all command line args as the command to run
 
46
    a = subprocess.Popen(sys.argv[1:],
 
47
                         stdout=open("stdout", "w"),
 
48
                         stderr=open("stderr", "w"))
 
49
 
 
50
    # Set our handler to trap SIGINT and propagate to the child
 
51
    # The expectation is that the child process handles SIGINT
 
52
    # and exits.
 
53
    signal.signal(signal.SIGINT, make_handler(a))
 
54
 
 
55
    # Write out the childpid just in case there is a
 
56
    # need to send special signals directly to the child process
 
57
    open("childpid", "w").write("%s\n" % a.pid)
 
58
 
 
59
    # Wait for child to exit and write result file
 
60
    log.info("Waiting for subprocess %s" % a.pid)
 
61
    ret = a.wait()
 
62
    log.info("Subprocess exit status %s" % ret)
 
63
    open("result", "w").write("%s\n" % ret)
 
64
 
 
65
except Exception as e:
 
66
    log.exception(e)