Debian Codegraph @

heatclient/common/deployment_utils.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import copy
14 import uuid
15
16 from swiftclient import client as sc
17 from swiftclient import utils as swiftclient_utils
18 from urllib import parse as urlparse
19
20 from heatclient._i18n import _
21 from heatclient import exc
22 from heatclient.v1 import software_configs
23
24
25 def build_derived_config_params(action, source, name, input_values,
26 server_id, signal_transport, signal_id=None):
27
28 if isinstance(source, software_configs.SoftwareConfig):
29 source = source.to_dict()
30 input_values = input_values or {}
31 inputs = copy.deepcopy(source.get('inputs')) or []
32
33 for inp in inputs:
34 input_key = inp['name']
35 inp['value'] = input_values.pop(input_key, inp.get('default'))
36
37 # for any input values that do not have a declared input, add
38 # a derived declared input so that they can be used as config
39 # inputs
40 for inpk, inpv in input_values.items():
41 inputs.append({
42 'name': inpk,
43 'type': 'String',
44 'value': inpv
45 })
46
47 inputs.extend([{
48 'name': 'deploy_server_id',
49 'description': _('ID of the server being deployed to'),
50 'type': 'String',
51 'value': server_id
52 }, {
53 'name': 'deploy_action',
54 'description': _('Name of the current action being deployed'),
55 'type': 'String',
56 'value': action
57 }, {
58 'name': 'deploy_signal_transport',
59 'description': _('How the server should signal to heat with '
60 'the deployment output values.'),
61 'type': 'String',
62 'value': signal_transport
63 }])
64
65 if signal_transport == 'TEMP_URL_SIGNAL':
66 inputs.append({
67 'name': 'deploy_signal_id',
68 'description': _('ID of signal to use for signaling '
69 'output values'),
70 'type': 'String',
71 'value': signal_id
72 })
73 inputs.append({
74 'name': 'deploy_signal_verb',
75 'description': _('HTTP verb to use for signaling '
76 'output values'),
77 'type': 'String',
78 'value': 'PUT'
79 })
80 elif signal_transport != 'NO_SIGNAL':
81 raise exc.CommandError(
82 _('Unsupported signal transport %s') % signal_transport)
83
84 return {
85 'group': source.get('group') or 'Heat::Ungrouped',
86 'config': source.get('config') or '',
87 'options': source.get('options') or {},
88 'inputs': inputs,
89 'outputs': source.get('outputs') or [],
90 'name': name
91 }
92
93
94 def create_temp_url(swift_client, name, timeout, container=None):
95
96 container = container or '{name}-{uuid}'.format(
97 name=name, uuid=uuid.uuid4())
98 object_name = str(uuid.uuid4())
99
100 swift_client.put_container(container)
101 key_header = 'x-account-meta-temp-url-key'
102 if key_header not in swift_client.head_account():
103 swift_client.post_account({
104 key_header: str(uuid.uuid4())[:32]})
105
106 key = swift_client.head_account()[key_header]
107 project_path = swift_client.url.split('/')[-1]
108 path = f'/v1/{project_path}/{container}/{object_name}'
109 timeout_secs = timeout * 60
110 tempurl = swiftclient_utils.generate_temp_url(path, timeout_secs, key,
111 'PUT')
112 sw_url = urlparse.urlparse(swift_client.url)
113 put_url = f'{sw_url.scheme}://{sw_url.netloc}{tempurl}'
114 swift_client.put_object(container, object_name, '')
115 return put_url
116
117
118 def build_signal_id(hc, args):
119 if args.signal_transport != 'TEMP_URL_SIGNAL':
120 return
121
122 # NOTE(pas-ha) only heatclient has os-no-client-auth arg,
123 # osc plugin does not have it
124 if getattr(args, 'os_no_client_auth', False):
125 raise exc.CommandError(_(
126 'Cannot use --os-no-client-auth, auth required to create '
127 'a Swift TempURL.'))
128 swift_client = create_swift_client(
129 hc.http_client.auth, hc.http_client.session, args)
130
131 return create_temp_url(swift_client, args.name, args.timeout)
132
133
134 def create_swift_client(auth, session, args):
135 auth_token = auth.get_token(session)
136 endpoint = auth.get_endpoint(session,
137 service_type='object-store',
138 region_name=args.os_region_name)
139 project_name = args.os_project_name or args.os_tenant_name
140 swift_args = {
141 'auth_version': '2.0',
142 'tenant_name': project_name,
143 'user': args.os_username,
144 'key': None,
145 'authurl': None,
146 'preauthtoken': auth_token,
147 'preauthurl': endpoint,
148 'cacert': args.os_cacert,
149 'insecure': args.insecure
150 }
151
152 return sc.Connection(**swift_args)