Use argparse as optparse is deprecated.
Also change the --validate option to match the variable name use in the program, as it makes it easier for a user to map intentions to actual code.
This commit is contained in:
parent
a5f65f4bfe
commit
c3c32a170b
|
@ -5,14 +5,15 @@ import os
|
|||
import pystache
|
||||
import sys
|
||||
import tempfile
|
||||
from optparse import OptionParser
|
||||
from argparse import ArgumentParser
|
||||
from pystache.context import KeyNotFoundError
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
def install_config(config_path, template_root, output_path, write):
|
||||
|
||||
def install_config(config_path, template_root, output_path, validate):
|
||||
config = read_config(config_path)
|
||||
tree = build_tree( template_paths(template_root), config )
|
||||
if write:
|
||||
if not validate:
|
||||
for path, contents in tree.items():
|
||||
write_file( os.path.join(output_path, strip_prefix('/', path)), contents)
|
||||
|
||||
|
@ -72,25 +73,29 @@ def strip_prefix(prefix, s):
|
|||
return s[len(prefix):] if s.startswith(prefix) else s
|
||||
|
||||
def parse_opts():
|
||||
parser = OptionParser(usage="os-config-applier -t TEMPLATE_ROOT [-m METADATA_FILE] [-o OUT_DIR]")
|
||||
parser.add_option('-t', '--templates', dest='template_root', help='path to template root directory')
|
||||
parser.add_option('-o', '--output', dest='out_root', help='root directory for output (default: /)',
|
||||
default='/')
|
||||
parser.add_option('-m', '--metadata', dest='metadata_path', help='path to metadata file',
|
||||
default='/var/lib/cloud/data/cfn-init-data')
|
||||
parser.add_option('-v', '--validate', dest='write', help='validate only. do not write files',
|
||||
default=True, action='store_false')
|
||||
(opts, args) = parser.parse_args()
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument('-t', '--templates', metavar='TEMPLATE_ROOT',
|
||||
help='path to template root directory')
|
||||
parser.add_argument('-o', '--output', metavar='OUT_DIR',
|
||||
help='root directory for output (default: /)',
|
||||
default='/')
|
||||
parser.add_argument('-m', '--metadata', metavar='METADATA_FILE',
|
||||
help='path to metadata file',
|
||||
default='/var/lib/cloud/data/cfn-init-data')
|
||||
parser.add_argument('-v', '--validate', help='validate only. do not write files',
|
||||
default=False, action='store_true')
|
||||
opts = parser.parse_args()
|
||||
|
||||
if opts.template_root is None: raise ConfigException('missing option --templates')
|
||||
if not os.access(opts.out_root, os.W_OK):
|
||||
raise ConfigException("you don't have permission to write to '%s'" % opts.out_root)
|
||||
if opts.templates is None: raise ConfigException('missing option --templates')
|
||||
if not os.access(opts.output, os.W_OK):
|
||||
raise ConfigException("you don't have permission to write to '%s'" % opts.output)
|
||||
return opts
|
||||
|
||||
def main():
|
||||
try:
|
||||
opts = parse_opts()
|
||||
install_config(opts.metadata_path, opts.template_root, opts.out_root, opts.write)
|
||||
install_config(opts.metadata, opts.templates, opts.output,
|
||||
opts.validate)
|
||||
logger.info("success")
|
||||
except ConfigException as e:
|
||||
logger.error(e)
|
||||
|
|
|
@ -40,7 +40,7 @@ def test_install_config():
|
|||
t.write(json.dumps(CONFIG))
|
||||
t.flush()
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
install_config(t.name, TEMPLATES, tmpdir, True)
|
||||
install_config(t.name, TEMPLATES, tmpdir, False)
|
||||
for path, contents in OUTPUT.items():
|
||||
full_path = os.path.join(tmpdir, path[1:])
|
||||
assert os.path.exists(full_path)
|
||||
|
@ -71,7 +71,10 @@ def test_render_executable_failure():
|
|||
|
||||
def test_template_paths():
|
||||
expected = map(lambda p: (template(p), p), TEMPLATE_PATHS)
|
||||
assert_equals( template_paths(TEMPLATES), expected)
|
||||
actual = template_paths(TEMPLATES)
|
||||
expected.sort(key=lambda tup: tup[1])
|
||||
actual.sort(key=lambda tup: tup[1])
|
||||
assert_equals( actual , expected)
|
||||
|
||||
def test_read_config():
|
||||
with tempfile.NamedTemporaryFile() as t:
|
||||
|
|
Loading…
Reference in New Issue