Add --set argument to prepare command

This new argument will allow special ceph variables to be added to the
template file overcloud_containers.yaml.j2. For example if the
following existed in the template file:
  {% set ceph_namespace=ceph_namespace or "ceph" %}
  {% set ceph_daemon=ceph_daemon or "daemon" %}
  {% set ceph_tag=ceph_tag or "tag-build-master-jewel-centos-7" %}
  - imagename: "{{ ceph_namespace }}/{{ ceph_daemon }}:{{ ceph_tag }}"

Then the --set argument will allow making substitutions on these
values, for example:
  openstack overcloud container image prepare \
      --namespace tripleoupstream \
      --tag latest \
      --images-file overcloud_containers.yaml \
      --set ceph_namespace=myceph \
      --set ceph_tag=latest \
      --env-file $HOME/docker_registry.yaml

Change-Id: Ie12dbab0f4d7f20d14a764abb9095fd8b326c700
This commit is contained in:
Steve Baker 2017-07-31 16:06:48 +12:00
parent 66c902a2b6
commit b1df493d43
2 changed files with 31 additions and 1 deletions

View File

@ -120,6 +120,12 @@ class TestContainerImagePrepare(TestPluginV1):
images_file, images_file,
'--env-file', '--env-file',
env_file, env_file,
'--set',
'ceph_namespace=myceph',
'--set',
'ceph_image=mydaemon',
'--set',
'ceph_tag=mytag',
] ]
self.cmd.app.command_options = arglist self.cmd.app.command_options = arglist
verifylist = [] verifylist = []
@ -144,7 +150,10 @@ class TestContainerImagePrepare(TestPluginV1):
name_prefix='os-', name_prefix='os-',
name_suffix='foo', name_suffix='foo',
namespace='tripleo', namespace='tripleo',
tag='passed-ci' tag='passed-ci',
ceph_image='mydaemon',
ceph_namespace='myceph',
ceph_tag='mytag'
) )
ci_data = { ci_data = {
'container_images': [{ 'container_images': [{

View File

@ -22,6 +22,7 @@ import sys
import tempfile import tempfile
from osc_lib.command import command from osc_lib.command import command
from osc_lib import exceptions as oscexc
from osc_lib.i18n import _ from osc_lib.i18n import _
import yaml import yaml
@ -218,6 +219,13 @@ class PrepareImageFiles(command.Command):
help=_("Override the default name suffix substitution.\n" help=_("Override the default name suffix substitution.\n"
"Default is empty."), "Default is empty."),
) )
parser.add_argument(
'--set',
metavar='<variable=value>',
action='append',
help=_('Set the value of a variable in the template, even if it '
'has no dedicated argument such as "--suffix".')
)
parser.add_argument( parser.add_argument(
"--exclude", "--exclude",
dest="excludes", dest="excludes",
@ -244,6 +252,18 @@ class PrepareImageFiles(command.Command):
) )
return parser return parser
def parse_set_values(self, subs, set_values):
if not set_values:
return
for s in set_values:
try:
(n, v) = s.split(('='), 1)
subs[n] = v
except ValueError:
msg = _('Malformed --set(%s). '
'Use the variable=value format.') % s
raise oscexc.CommandError(msg)
def write_env_file(self, params, env_file): def write_env_file(self, params, env_file):
with os.fdopen(os.open(env_file, with os.fdopen(os.open(env_file,
@ -265,6 +285,7 @@ class PrepareImageFiles(command.Command):
'name_prefix': parsed_args.prefix, 'name_prefix': parsed_args.prefix,
'name_suffix': parsed_args.suffix, 'name_suffix': parsed_args.suffix,
} }
self.parse_set_values(subs, parsed_args.set)
def ffunc(entry): def ffunc(entry):
imagename = entry.get('imagename', '') imagename = entry.get('imagename', '')