Implement deployment-create
The deployment-create command creates a derived config from the supplied config, then creates a deployment with that. Supported signal transports are TEMP_URL_SIGNAL and NO_SIGNAL (CFN_SIGNAL and HEAT_SIGNAL are not possible as they require the deployment to be associated with a stack resource). The transport TEMP_URL_SIGNAL requires that a swift object (with TempURL) be created to receive the signal, so heatclient now depends on swiftclient so that this object can be created. This change implements the heatclient portion of blueprint software-config-trigger. Implements-Blueprint: software-config-trigger Change-Id: I6d73ba1f0cb9abc3331b5c3d3c788678ad88ff20
This commit is contained in:
147
heatclient/common/deployment_utils.py
Normal file
147
heatclient/common/deployment_utils.py
Normal file
@@ -0,0 +1,147 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import copy
|
||||
import six
|
||||
from six.moves.urllib import parse as urlparse
|
||||
from swiftclient import client as sc
|
||||
from swiftclient import utils as swiftclient_utils
|
||||
import uuid
|
||||
|
||||
from heatclient import exc
|
||||
from heatclient.openstack.common._i18n import _
|
||||
|
||||
|
||||
def build_derived_config_params(action, source, name, input_values,
|
||||
server_id, signal_transport, signal_id=None):
|
||||
|
||||
input_values = input_values or {}
|
||||
inputs = copy.deepcopy(source.inputs) or []
|
||||
|
||||
for inp in inputs:
|
||||
input_key = inp['name']
|
||||
inp['value'] = input_values.pop(input_key, inp.get('default'))
|
||||
|
||||
# for any input values that do not have a declared input, add
|
||||
# a derived declared input so that they can be used as config
|
||||
# inputs
|
||||
for inpk, inpv in input_values.items():
|
||||
inputs.append({
|
||||
'name': inpk,
|
||||
'type': 'String',
|
||||
'value': inpv
|
||||
})
|
||||
|
||||
inputs.extend([{
|
||||
'name': 'deploy_server_id',
|
||||
'description': _('ID of the server being deployed to'),
|
||||
'type': 'String',
|
||||
'value': server_id
|
||||
}, {
|
||||
'name': 'deploy_action',
|
||||
'description': _('Name of the current action being deployed'),
|
||||
'type': 'String',
|
||||
'value': action
|
||||
}, {
|
||||
'name': 'deploy_signal_transport',
|
||||
'description': _('How the server should signal to heat with '
|
||||
'the deployment output values.'),
|
||||
'type': 'String',
|
||||
'value': signal_transport
|
||||
}])
|
||||
|
||||
if signal_transport == 'TEMP_URL_SIGNAL':
|
||||
inputs.append({
|
||||
'name': 'deploy_signal_id',
|
||||
'description': _('ID of signal to use for signaling '
|
||||
'output values'),
|
||||
'type': 'String',
|
||||
'value': signal_id
|
||||
})
|
||||
inputs.append({
|
||||
'name': 'deploy_signal_verb',
|
||||
'description': _('HTTP verb to use for signaling '
|
||||
'output values'),
|
||||
'type': 'String',
|
||||
'value': 'PUT'
|
||||
})
|
||||
elif signal_transport != 'NO_SIGNAL':
|
||||
raise exc.CommandError(
|
||||
_('Unsupported signal transport %s') % signal_transport)
|
||||
|
||||
return {
|
||||
'group': source.group or 'Heat::Ungrouped',
|
||||
'config': source.config or '',
|
||||
'options': source.options,
|
||||
'inputs': inputs,
|
||||
'outputs': source.outputs,
|
||||
'name': name
|
||||
}
|
||||
|
||||
|
||||
def create_temp_url(swift_client, name, timeout, container=None):
|
||||
|
||||
container = container or '%(name)s-%(uuid)s' % {
|
||||
'name': name, 'uuid': uuid.uuid4()}
|
||||
object_name = str(uuid.uuid4())
|
||||
|
||||
swift_client.put_container(container)
|
||||
key_header = 'x-account-meta-temp-url-key'
|
||||
if key_header not in swift_client.head_account():
|
||||
swift_client.post_account({
|
||||
key_header: six.text_type(uuid.uuid4())[:32]})
|
||||
|
||||
key = swift_client.head_account()[key_header]
|
||||
project_path = swift_client.url.split('/')[-1]
|
||||
path = '/v1/%s/%s/%s' % (project_path, container, object_name)
|
||||
timeout_secs = timeout * 60
|
||||
tempurl = swiftclient_utils.generate_temp_url(path, timeout_secs, key,
|
||||
'PUT')
|
||||
sw_url = urlparse.urlparse(swift_client.url)
|
||||
put_url = '%s://%s%s' % (sw_url.scheme, sw_url.netloc, tempurl)
|
||||
swift_client.put_object(container, object_name, '')
|
||||
return put_url
|
||||
|
||||
|
||||
def build_signal_id(hc, args):
|
||||
if args.signal_transport != 'TEMP_URL_SIGNAL':
|
||||
return
|
||||
|
||||
if args.os_no_client_auth:
|
||||
raise exc.CommandError(_(
|
||||
'Cannot use --os-no-client-auth, auth required to create '
|
||||
'a Swift TempURL.'))
|
||||
swift_client = create_swift_client(
|
||||
hc.http_client.auth, hc.http_client.session, args)
|
||||
|
||||
return create_temp_url(swift_client, args.name, args.timeout)
|
||||
|
||||
|
||||
def create_swift_client(auth, session, args):
|
||||
auth_token = auth.get_token(session)
|
||||
endpoint = auth.get_endpoint(session,
|
||||
service_type='object-store',
|
||||
region_name=args.os_region_name)
|
||||
project_name = args.os_project_name or args.os_tenant_name
|
||||
swift_args = {
|
||||
'auth_version': '2.0',
|
||||
'tenant_name': project_name,
|
||||
'user': args.os_username,
|
||||
'key': None,
|
||||
'authurl': None,
|
||||
'preauthtoken': auth_token,
|
||||
'preauthurl': endpoint,
|
||||
'cacert': args.os_cacert,
|
||||
'insecure': args.insecure
|
||||
}
|
||||
|
||||
return sc.Connection(**swift_args)
|
327
heatclient/tests/test_deployment_utils.py
Normal file
327
heatclient/tests/test_deployment_utils.py
Normal file
@@ -0,0 +1,327 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import mock
|
||||
import six
|
||||
import swiftclient.client
|
||||
import testscenarios
|
||||
import testtools
|
||||
|
||||
from heatclient.common import deployment_utils
|
||||
from heatclient import exc
|
||||
from heatclient.v1 import software_configs
|
||||
from testtools import matchers
|
||||
|
||||
|
||||
load_tests = testscenarios.load_tests_apply_scenarios
|
||||
|
||||
|
||||
def mock_sc(group=None, config=None, options=None,
|
||||
inputs=None, outputs=None):
|
||||
return software_configs.SoftwareConfig(None, {
|
||||
'group': group,
|
||||
'config': config,
|
||||
'options': options or {},
|
||||
'inputs': inputs or [],
|
||||
'outputs': outputs or [],
|
||||
}, True)
|
||||
|
||||
|
||||
class DerivedConfigTest(testtools.TestCase):
|
||||
|
||||
scenarios = [
|
||||
('defaults', dict(
|
||||
action='UPDATE',
|
||||
source=mock_sc(),
|
||||
name='s1',
|
||||
input_values=None,
|
||||
server_id='1234',
|
||||
signal_transport='NO_SIGNAL',
|
||||
signal_id=None,
|
||||
result={
|
||||
'config': '',
|
||||
'group': 'Heat::Ungrouped',
|
||||
'inputs': [{
|
||||
'description': 'ID of the server being deployed to',
|
||||
'name': 'deploy_server_id',
|
||||
'type': 'String',
|
||||
'value': '1234'
|
||||
}, {
|
||||
'description': 'Name of the current action '
|
||||
'being deployed',
|
||||
'name': 'deploy_action',
|
||||
'type': 'String',
|
||||
'value': 'UPDATE'
|
||||
}, {
|
||||
'description': 'How the server should signal to '
|
||||
'heat with the deployment output values.',
|
||||
'name': 'deploy_signal_transport',
|
||||
'type': 'String',
|
||||
'value': 'NO_SIGNAL'}],
|
||||
'name': 's1',
|
||||
'options': {},
|
||||
'outputs': []})),
|
||||
('config_values', dict(
|
||||
action='UPDATE',
|
||||
source=mock_sc(
|
||||
group='puppet',
|
||||
config='do the foo',
|
||||
inputs=[
|
||||
{'name': 'one', 'default': '1'},
|
||||
{'name': 'two'}],
|
||||
options={'option1': 'value'},
|
||||
outputs=[
|
||||
{'name': 'output1'},
|
||||
{'name': 'output2'}],
|
||||
),
|
||||
name='s2',
|
||||
input_values={'one': 'foo', 'two': 'bar', 'three': 'baz'},
|
||||
server_id='1234',
|
||||
signal_transport='NO_SIGNAL',
|
||||
signal_id=None,
|
||||
result={
|
||||
'config': 'do the foo',
|
||||
'group': 'puppet',
|
||||
'inputs': [{
|
||||
'name': 'one',
|
||||
'default': '1',
|
||||
'value': 'foo'
|
||||
}, {
|
||||
'name': 'two',
|
||||
'value': 'bar'
|
||||
}, {
|
||||
'name': 'three',
|
||||
'type': 'String',
|
||||
'value': 'baz'
|
||||
}, {
|
||||
'description': 'ID of the server being deployed to',
|
||||
'name': 'deploy_server_id',
|
||||
'type': 'String',
|
||||
'value': '1234'
|
||||
}, {
|
||||
'description': 'Name of the current action '
|
||||
'being deployed',
|
||||
'name': 'deploy_action',
|
||||
'type': 'String',
|
||||
'value': 'UPDATE'
|
||||
}, {
|
||||
'description': 'How the server should signal to '
|
||||
'heat with the deployment output values.',
|
||||
'name': 'deploy_signal_transport',
|
||||
'type': 'String',
|
||||
'value': 'NO_SIGNAL'
|
||||
}],
|
||||
'name': 's2',
|
||||
'options': {'option1': 'value'},
|
||||
'outputs': [
|
||||
{'name': 'output1'},
|
||||
{'name': 'output2'}]})),
|
||||
('temp_url', dict(
|
||||
action='UPDATE',
|
||||
source=mock_sc(),
|
||||
name='s1',
|
||||
input_values=None,
|
||||
server_id='1234',
|
||||
signal_transport='TEMP_URL_SIGNAL',
|
||||
signal_id='http://192.0.2.1:8080/foo',
|
||||
result={
|
||||
'config': '',
|
||||
'group': 'Heat::Ungrouped',
|
||||
'inputs': [{
|
||||
'description': 'ID of the server being deployed to',
|
||||
'name': 'deploy_server_id',
|
||||
'type': 'String',
|
||||
'value': '1234'
|
||||
}, {
|
||||
'description': 'Name of the current action '
|
||||
'being deployed',
|
||||
'name': 'deploy_action',
|
||||
'type': 'String',
|
||||
'value': 'UPDATE'
|
||||
}, {
|
||||
'description': 'How the server should signal to '
|
||||
'heat with the deployment output values.',
|
||||
'name': 'deploy_signal_transport',
|
||||
'type': 'String',
|
||||
'value': 'TEMP_URL_SIGNAL'
|
||||
}, {
|
||||
'description': 'ID of signal to use for signaling '
|
||||
'output values',
|
||||
'name': 'deploy_signal_id',
|
||||
'type': 'String',
|
||||
'value': 'http://192.0.2.1:8080/foo'
|
||||
}, {
|
||||
'description': 'HTTP verb to use for signaling '
|
||||
'output values',
|
||||
'name': 'deploy_signal_verb',
|
||||
'type': 'String',
|
||||
'value': 'PUT'}],
|
||||
'name': 's1',
|
||||
'options': {},
|
||||
'outputs': []})),
|
||||
('unsupported', dict(
|
||||
action='UPDATE',
|
||||
source=mock_sc(),
|
||||
name='s1',
|
||||
input_values=None,
|
||||
server_id='1234',
|
||||
signal_transport='ASDF',
|
||||
signal_id=None,
|
||||
result_error=exc.CommandError,
|
||||
result_error_msg='Unsupported signal transport ASDF',
|
||||
result=None)),
|
||||
]
|
||||
|
||||
def test_build_derived_config_params(self):
|
||||
try:
|
||||
self.assertEqual(
|
||||
self.result,
|
||||
deployment_utils.build_derived_config_params(
|
||||
action=self.action,
|
||||
source=self.source,
|
||||
name=self.name,
|
||||
input_values=self.input_values,
|
||||
server_id=self.server_id,
|
||||
signal_transport=self.signal_transport,
|
||||
signal_id=self.signal_id))
|
||||
except Exception as e:
|
||||
if not self.result_error:
|
||||
raise e
|
||||
self.assertIsInstance(e, self.result_error)
|
||||
self.assertEqual(self.result_error_msg, six.text_type(e))
|
||||
|
||||
|
||||
class TempURLSignalTest(testtools.TestCase):
|
||||
|
||||
@mock.patch.object(swiftclient.client, 'Connection')
|
||||
def test_create_swift_client(self, sc_conn):
|
||||
auth = mock.MagicMock()
|
||||
auth.get_token.return_value = '1234'
|
||||
auth.get_endpoint.return_value = 'http://192.0.2.1:8080'
|
||||
|
||||
session = mock.MagicMock()
|
||||
|
||||
args = mock.MagicMock()
|
||||
args.os_region_name = 'Region1'
|
||||
args.os_project_name = 'project'
|
||||
args.os_username = 'user'
|
||||
args.os_cacert = None
|
||||
args.insecure = True
|
||||
|
||||
sc_conn.return_value = mock.MagicMock()
|
||||
|
||||
sc = deployment_utils.create_swift_client(auth, session, args)
|
||||
|
||||
self.assertEqual(sc_conn.return_value, sc)
|
||||
|
||||
self.assertEqual(
|
||||
mock.call(session),
|
||||
auth.get_token.call_args)
|
||||
|
||||
self.assertEqual(
|
||||
mock.call(
|
||||
session,
|
||||
service_type='object-store',
|
||||
region_name='Region1'),
|
||||
auth.get_endpoint.call_args)
|
||||
|
||||
self.assertEqual(
|
||||
mock.call(
|
||||
cacert=None,
|
||||
insecure=True,
|
||||
key=None,
|
||||
tenant_name='project',
|
||||
preauthtoken='1234',
|
||||
authurl=None,
|
||||
user='user',
|
||||
preauthurl='http://192.0.2.1:8080',
|
||||
auth_version='2.0'),
|
||||
sc_conn.call_args)
|
||||
|
||||
def test_create_temp_url(self):
|
||||
swift_client = mock.MagicMock()
|
||||
swift_client.url = ("http://fake-host.com:8080/v1/AUTH_demo")
|
||||
swift_client.head_account = mock.Mock(return_value={
|
||||
'x-account-meta-temp-url-key': '123456'})
|
||||
swift_client.post_account = mock.Mock()
|
||||
|
||||
uuid_pattern = ('[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB]'
|
||||
'[a-f0-9]{3}-[a-f0-9]{12}')
|
||||
url = deployment_utils.create_temp_url(swift_client, 'bar', 60)
|
||||
self.assertFalse(swift_client.post_account.called)
|
||||
regexp = ("http://fake-host.com:8080/v1/AUTH_demo/bar-%s"
|
||||
"/%s\?temp_url_sig=[0-9a-f]{40}&"
|
||||
"temp_url_expires=[0-9]{10}" % (uuid_pattern, uuid_pattern))
|
||||
self.assertThat(url, matchers.MatchesRegex(regexp))
|
||||
|
||||
timeout = int(url.split('=')[-1])
|
||||
self.assertTrue(timeout < 2147483647)
|
||||
|
||||
def test_get_temp_url_no_account_key(self):
|
||||
swift_client = mock.MagicMock()
|
||||
swift_client.url = ("http://fake-host.com:8080/v1/AUTH_demo")
|
||||
head_account = {}
|
||||
|
||||
def post_account(data):
|
||||
head_account.update(data)
|
||||
|
||||
swift_client.head_account = mock.Mock(return_value=head_account)
|
||||
swift_client.post_account = post_account
|
||||
|
||||
self.assertNotIn('x-account-meta-temp-url-key', head_account)
|
||||
deployment_utils.create_temp_url(swift_client, 'bar', 60, 'foo')
|
||||
self.assertIn('x-account-meta-temp-url-key', head_account)
|
||||
|
||||
def test_build_signal_id_no_signal(self):
|
||||
hc = mock.MagicMock()
|
||||
args = mock.MagicMock()
|
||||
args.signal_transport = 'NO_SIGNAL'
|
||||
self.assertIsNone(deployment_utils.build_signal_id(hc, args))
|
||||
|
||||
def test_build_signal_id_no_client_auth(self):
|
||||
hc = mock.MagicMock()
|
||||
args = mock.MagicMock()
|
||||
args.os_no_client_auth = True
|
||||
args.signal_transport = 'TEMP_URL_SIGNAL'
|
||||
e = self.assertRaises(exc.CommandError,
|
||||
deployment_utils.build_signal_id, hc, args)
|
||||
self.assertEqual((
|
||||
'Cannot use --os-no-client-auth, auth required to create '
|
||||
'a Swift TempURL.'),
|
||||
six.text_type(e))
|
||||
|
||||
@mock.patch.object(deployment_utils, 'create_temp_url')
|
||||
@mock.patch.object(deployment_utils, 'create_swift_client')
|
||||
def test_build_signal_id(self, csc, ctu):
|
||||
hc = mock.MagicMock()
|
||||
args = mock.MagicMock()
|
||||
args.name = 'foo'
|
||||
args.timeout = 60
|
||||
args.os_no_client_auth = False
|
||||
args.signal_transport = 'TEMP_URL_SIGNAL'
|
||||
csc.return_value = mock.MagicMock()
|
||||
temp_url = (
|
||||
'http://fake-host.com:8080/v1/AUTH_demo/foo/'
|
||||
'a81a74d5-c395-4269-9670-ddd0824fd696'
|
||||
'?temp_url_sig=6a68371d602c7a14aaaa9e3b3a63b8b85bd9a503'
|
||||
'&temp_url_expires=1425270977')
|
||||
ctu.return_value = temp_url
|
||||
|
||||
self.assertEqual(
|
||||
temp_url, deployment_utils.build_signal_id(hc, args))
|
||||
self.assertEqual(
|
||||
mock.call(hc.http_client.auth, hc.http_client.session, args),
|
||||
csc.call_args)
|
||||
self.assertEqual(
|
||||
mock.call(csc.return_value, 'foo', 60),
|
||||
ctu.call_args)
|
@@ -21,6 +21,7 @@ import yaml
|
||||
from oslo.serialization import jsonutils
|
||||
from oslo.utils import strutils
|
||||
|
||||
from heatclient.common import deployment_utils
|
||||
from heatclient.common import template_format
|
||||
from heatclient.common import template_utils
|
||||
from heatclient.common import utils
|
||||
@@ -1044,6 +1045,65 @@ def do_config_delete(hc, args):
|
||||
"configs."))
|
||||
|
||||
|
||||
@utils.arg('-i', '--input-value', metavar='<KEY=VALUE>',
|
||||
help=_('Input value to set on the deployment. '
|
||||
'This can be specified multiple times.'),
|
||||
action='append')
|
||||
@utils.arg('-a', '--action', metavar='<ACTION>', default='UPDATE',
|
||||
help=_('Name of action for this deployment. '
|
||||
'Can be a custom action, or one of: '
|
||||
'CREATE, UPDATE, DELETE, SUSPEND, RESUME'))
|
||||
@utils.arg('-c', '--config', metavar='<CONFIG>',
|
||||
help=_('ID of the configuration to deploy.'))
|
||||
@utils.arg('-s', '--server', metavar='<SERVER>',
|
||||
help=_('ID of the server being deployed to.'))
|
||||
@utils.arg('-t', '--signal-transport',
|
||||
default='TEMP_URL_SIGNAL',
|
||||
metavar='<TRANSPORT>',
|
||||
help=_('How the server should signal to heat with the deployment '
|
||||
'output values. TEMP_URL_SIGNAL will create a '
|
||||
'Swift TempURL to be signaled via HTTP PUT. NO_SIGNAL will '
|
||||
'result in the resource going to the COMPLETE state '
|
||||
'without waiting for any signal.'))
|
||||
@utils.arg('--container', metavar='<CONTAINER_NAME>',
|
||||
help=_('Optional name of container to store TEMP_URL_SIGNAL '
|
||||
'objects in. If not specified a container will be created '
|
||||
'with a name derived from the DEPLOY_NAME'))
|
||||
@utils.arg('--timeout', metavar='<TIMEOUT>',
|
||||
type=int,
|
||||
default=60,
|
||||
help=_('Deployment timeout in minutes.'))
|
||||
@utils.arg('name', metavar='<DEPLOY_NAME>',
|
||||
help=_('Name of the derived config associated with this '
|
||||
'deployment. This is used to apply a sort order to the '
|
||||
'list of configurations currently deployed to the server.'))
|
||||
def do_deployment_create(hc, args):
|
||||
try:
|
||||
config = hc.software_configs.get(config_id=args.config)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError(_('Configuration not found: %s') % args.id)
|
||||
|
||||
derrived_params = deployment_utils.build_derived_config_params(
|
||||
action=args.action,
|
||||
source=config,
|
||||
name=args.name,
|
||||
input_values=utils.format_parameters(args.input_value, False),
|
||||
server_id=args.server,
|
||||
signal_transport=args.signal_transport,
|
||||
signal_id=deployment_utils.build_signal_id(hc, args)
|
||||
)
|
||||
derived_config = hc.software_configs.create(**derrived_params)
|
||||
|
||||
sd = hc.software_deployments.create(
|
||||
tenant_id='asdf',
|
||||
config_id=derived_config.id,
|
||||
server_id=args.server,
|
||||
action=args.action,
|
||||
status='IN_PROGRESS'
|
||||
)
|
||||
print(jsonutils.dumps(sd.to_dict(), indent=2))
|
||||
|
||||
|
||||
@utils.arg('id', metavar='<ID>',
|
||||
help=_('ID of the deployment.'))
|
||||
def do_deployment_show(hc, args):
|
||||
@@ -1051,7 +1111,7 @@ def do_deployment_show(hc, args):
|
||||
try:
|
||||
sd = hc.software_deployments.get(deployment_id=args.id)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Deployment not found: %s' % args.id)
|
||||
raise exc.CommandError(_('Deployment not found: %s') % args.id)
|
||||
else:
|
||||
print(jsonutils.dumps(sd.to_dict(), indent=2))
|
||||
|
||||
|
@@ -11,6 +11,7 @@ oslo.i18n>=1.3.0 # Apache-2.0
|
||||
oslo.serialization>=1.2.0 # Apache-2.0
|
||||
oslo.utils>=1.2.0 # Apache-2.0
|
||||
python-keystoneclient>=1.1.0
|
||||
python-swiftclient>=2.2.0
|
||||
PyYAML>=3.1.0
|
||||
requests>=2.2.0,!=2.4.0
|
||||
six>=1.9.0
|
||||
|
Reference in New Issue
Block a user