comparison piecrust/main.py @ 612:2edaefcb82cd

chef: Add `--pid-file` option.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 04 Feb 2016 08:03:52 -0800
parents 1eda551ee681
children 9dd2f68f243b
comparison
equal deleted inserted replaced
611:906cc2520773 612:2edaefcb82cd
1 import os
2 import os.path
1 import io 3 import io
2 import sys 4 import sys
3 import time 5 import time
4 import os.path
5 import logging 6 import logging
6 import argparse 7 import argparse
7 import colorama 8 import colorama
8 from piecrust import APP_VERSION 9 from piecrust import APP_VERSION
9 from piecrust.app import ( 10 from piecrust.app import (
116 action='store_true') 117 action='store_true')
117 parser.add_argument( 118 parser.add_argument(
118 '--no-color', 119 '--no-color',
119 help="Don't use colorized output.", 120 help="Don't use colorized output.",
120 action='store_true') 121 action='store_true')
122 parser.add_argument(
123 '--pid-file',
124 dest='pid_file',
125 help="Write a PID file for the current process.")
121 126
122 127
123 def _pre_parse_chef_args(argv): 128 def _pre_parse_chef_args(argv):
124 # We need to parse some arguments before we can build the actual argument 129 # We need to parse some arguments before we can build the actual argument
125 # parser, because it can affect which plugins will be loaded. Also, log- 130 # parser, because it can affect which plugins will be loaded. Also, log-
164 file_handler = logging.FileHandler(res.log_file, mode='w') 169 file_handler = logging.FileHandler(res.log_file, mode='w')
165 root_logger.addHandler(file_handler) 170 root_logger.addHandler(file_handler)
166 if res.log_debug: 171 if res.log_debug:
167 file_handler.setLevel(logging.DEBUG) 172 file_handler.setLevel(logging.DEBUG)
168 173
174 # PID file.
175 if res.pid_file:
176 try:
177 pid_file_dir = os.path.dirname(res.pid_file)
178 if pid_file_dir and not os.path.isdir(pid_file_dir):
179 os.makedirs(pid_file_dir)
180
181 with open(res.pid_file, 'w') as fp:
182 fp.write(str(os.getpid()))
183 except OSError as ex:
184 raise Exception("Can't write PID file: %s" % res.pid_file) from ex
185
169 return res 186 return res
170 187
171 188
172 def _run_chef(pre_args, argv): 189 def _run_chef(pre_args, argv):
173 # Setup the app. 190 # Setup the app.