changeset 612:2edaefcb82cd

chef: Add `--pid-file` option.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 04 Feb 2016 08:03:52 -0800
parents 906cc2520773
children e2e955a3bb25
files piecrust/main.py
diffstat 1 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/main.py	Thu Feb 04 08:03:33 2016 -0800
+++ b/piecrust/main.py	Thu Feb 04 08:03:52 2016 -0800
@@ -1,7 +1,8 @@
+import os
+import os.path
 import io
 import sys
 import time
-import os.path
 import logging
 import argparse
 import colorama
@@ -118,6 +119,10 @@
             '--no-color',
             help="Don't use colorized output.",
             action='store_true')
+    parser.add_argument(
+            '--pid-file',
+            dest='pid_file',
+            help="Write a PID file for the current process.")
 
 
 def _pre_parse_chef_args(argv):
@@ -166,6 +171,18 @@
         if res.log_debug:
             file_handler.setLevel(logging.DEBUG)
 
+    # PID file.
+    if res.pid_file:
+        try:
+            pid_file_dir = os.path.dirname(res.pid_file)
+            if pid_file_dir and not os.path.isdir(pid_file_dir):
+                os.makedirs(pid_file_dir)
+
+            with open(res.pid_file, 'w') as fp:
+                fp.write(str(os.getpid()))
+        except OSError as ex:
+            raise Exception("Can't write PID file: %s" % res.pid_file) from ex
+
     return res