| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
"""Orchestration v1 Software Deployment action implementations""" |
| 15 |
|
| 16 |
import logging |
| 17 |
|
| 18 |
from osc_lib.command import command |
| 19 |
from osc_lib import exceptions as exc |
| 20 |
from osc_lib import utils |
| 21 |
from oslo_serialization import jsonutils |
| 22 |
|
| 23 |
from heatclient._i18n import _ |
| 24 |
from heatclient.common import deployment_utils |
| 25 |
from heatclient.common import format_utils |
| 26 |
from heatclient.common import utils as heat_utils |
| 27 |
from heatclient import exc as heat_exc |
| 28 |
|
| 29 |
|
| 30 |
class CreateDeployment(format_utils.YamlFormat): |
| 31 |
"""Create a software deployment.""" |
| 32 |
|
| 33 |
log = logging.getLogger(__name__ + '.CreateDeployment') |
| 34 |
|
| 35 |
def get_parser(self, prog_name): |
| 36 |
parser = super().get_parser(prog_name) |
| 37 |
parser.add_argument( |
| 38 |
'name', |
| 39 |
metavar='<deployment-name>', |
| 40 |
help=_('Name of the derived config associated with this ' |
| 41 |
'deployment. This is used to apply a sort order to the ' |
| 42 |
'list of configurations currently deployed to the server.') |
| 43 |
) |
| 44 |
parser.add_argument( |
| 45 |
'--input-value', |
| 46 |
metavar='<key=value>', |
| 47 |
action='append', |
| 48 |
help=_('Input value to set on the deployment. This can be ' |
| 49 |
'specified multiple times.') |
| 50 |
) |
| 51 |
parser.add_argument( |
| 52 |
'--action', |
| 53 |
metavar='<action>', |
| 54 |
default='UPDATE', |
| 55 |
help=_('Name of an action for this deployment. This can be a ' |
| 56 |
'custom action, or one of CREATE, UPDATE, DELETE, SUSPEND, ' |
| 57 |
'RESUME. Default is UPDATE') |
| 58 |
) |
| 59 |
parser.add_argument( |
| 60 |
'--config', |
| 61 |
metavar='<config>', |
| 62 |
help=_('ID of the configuration to deploy') |
| 63 |
) |
| 64 |
parser.add_argument( |
| 65 |
'--signal-transport', |
| 66 |
metavar='<signal-transport>', |
| 67 |
default='TEMP_URL_SIGNAL', |
| 68 |
help=_('How the server should signal to heat with the deployment ' |
| 69 |
'output values. TEMP_URL_SIGNAL will create a Swift ' |
| 70 |
'TempURL to be signaled via HTTP PUT. ZAQAR_SIGNAL will ' |
| 71 |
'create a dedicated zaqar queue to be signaled using the ' |
| 72 |
'provided keystone credentials.NO_SIGNAL will result in ' |
| 73 |
'the resource going to the COMPLETE state without waiting ' |
| 74 |
'for any signal') |
| 75 |
) |
| 76 |
parser.add_argument( |
| 77 |
'--container', |
| 78 |
metavar='<container>', |
| 79 |
help=_('Optional name of container to store TEMP_URL_SIGNAL ' |
| 80 |
'objects in. If not specified a container will be created ' |
| 81 |
'with a name derived from the DEPLOY_NAME') |
| 82 |
) |
| 83 |
parser.add_argument( |
| 84 |
'--timeout', |
| 85 |
metavar='<timeout>', |
| 86 |
type=int, |
| 87 |
default=60, |
| 88 |
help=_('Deployment timeout in minutes') |
| 89 |
) |
| 90 |
parser.add_argument( |
| 91 |
'--server', |
| 92 |
metavar='<server>', |
| 93 |
required=True, |
| 94 |
help=_('ID of the server being deployed to') |
| 95 |
) |
| 96 |
return parser |
| 97 |
|
| 98 |
def take_action(self, parsed_args): |
| 99 |
self.log.debug('take_action(%s)', parsed_args) |
| 100 |
|
| 101 |
client = self.app.client_manager.orchestration |
| 102 |
|
| 103 |
config = {} |
| 104 |
if parsed_args.config: |
| 105 |
try: |
| 106 |
config = client.software_configs.get(parsed_args.config) |
| 107 |
except heat_exc.HTTPNotFound: |
| 108 |
msg = (_('Software configuration not found: %s') % |
| 109 |
parsed_args.config) |
| 110 |
raise exc.CommandError(msg) |
| 111 |
|
| 112 |
derived_params = deployment_utils.build_derived_config_params( |
| 113 |
parsed_args.action, |
| 114 |
config, |
| 115 |
parsed_args.name, |
| 116 |
heat_utils.format_parameters(parsed_args.input_value, False), |
| 117 |
parsed_args.server, |
| 118 |
parsed_args.signal_transport, |
| 119 |
signal_id=deployment_utils.build_signal_id(client, parsed_args) |
| 120 |
) |
| 121 |
derived_config = client.software_configs.create(**derived_params) |
| 122 |
|
| 123 |
sd = client.software_deployments.create( |
| 124 |
config_id=derived_config.id, |
| 125 |
server_id=parsed_args.server, |
| 126 |
action=parsed_args.action, |
| 127 |
status='IN_PROGRESS' |
| 128 |
) |
| 129 |
|
| 130 |
return zip(*sorted(sd.to_dict().items())) |
| 131 |
|
| 132 |
|
| 133 |
class DeleteDeployment(command.Command): |
| 134 |
"""Delete software deployment(s) and correlative config(s).""" |
| 135 |
|
| 136 |
log = logging.getLogger(__name__ + '.DeleteDeployment') |
| 137 |
|
| 138 |
def get_parser(self, prog_name): |
| 139 |
parser = super().get_parser(prog_name) |
| 140 |
parser.add_argument( |
| 141 |
'deployment', |
| 142 |
metavar='<deployment>', |
| 143 |
nargs='+', |
| 144 |
help=_('ID of the deployment(s) to delete.') |
| 145 |
) |
| 146 |
return parser |
| 147 |
|
| 148 |
def take_action(self, parsed_args): |
| 149 |
self.log.debug("take_action(%s)", parsed_args) |
| 150 |
hc = self.app.client_manager.orchestration |
| 151 |
failure_count = 0 |
| 152 |
|
| 153 |
for deploy_id in parsed_args.deployment: |
| 154 |
try: |
| 155 |
sd = hc.software_deployments.get(deployment_id=deploy_id) |
| 156 |
hc.software_deployments.delete( |
| 157 |
deployment_id=deploy_id) |
| 158 |
except Exception as e: |
| 159 |
if isinstance(e, heat_exc.HTTPNotFound): |
| 160 |
print(_('Deployment with ID %s not found') % deploy_id) |
| 161 |
else: |
| 162 |
print(_('Deployment with ID %s failed to delete') |
| 163 |
% deploy_id) |
| 164 |
failure_count += 1 |
| 165 |
continue |
| 166 |
|
| 167 |
try: |
| 168 |
config_id = getattr(sd, 'config_id') |
| 169 |
hc.software_configs.delete(config_id=config_id) |
| 170 |
except Exception: |
| 171 |
print(_('Failed to delete the correlative config' |
| 172 |
' %(config_id)s of deployment %(deploy_id)s') % |
| 173 |
{'config_id': config_id, 'deploy_id': deploy_id}) |
| 174 |
|
| 175 |
if failure_count: |
| 176 |
raise exc.CommandError(_('Unable to delete %(count)s of the ' |
| 177 |
'%(total)s deployments.') % |
| 178 |
{'count': failure_count, |
| 179 |
'total': len(parsed_args.deployment)}) |
| 180 |
|
| 181 |
|
| 182 |
class ListDeployment(command.Lister): |
| 183 |
"""List software deployments.""" |
| 184 |
|
| 185 |
log = logging.getLogger(__name__ + '.ListDeployment') |
| 186 |
|
| 187 |
def get_parser(self, prog_name): |
| 188 |
parser = super().get_parser(prog_name) |
| 189 |
parser.add_argument( |
| 190 |
'--server', |
| 191 |
metavar='<server>', |
| 192 |
help=_('ID of the server to fetch deployments for') |
| 193 |
) |
| 194 |
parser.add_argument( |
| 195 |
'--long', |
| 196 |
action='store_true', |
| 197 |
help=_('List more fields in output') |
| 198 |
) |
| 199 |
return parser |
| 200 |
|
| 201 |
def take_action(self, parsed_args): |
| 202 |
self.log.debug("take_action(%s)", parsed_args) |
| 203 |
|
| 204 |
heat_client = self.app.client_manager.orchestration |
| 205 |
return _list_deployment(heat_client, args=parsed_args) |
| 206 |
|
| 207 |
|
| 208 |
def _list_deployment(heat_client, args=None): |
| 209 |
kwargs = {'server_id': args.server} if args.server else {} |
| 210 |
columns = ['id', 'config_id', 'server_id', 'action', 'status'] |
| 211 |
if args.long: |
| 212 |
columns.append('creation_time') |
| 213 |
columns.append('status_reason') |
| 214 |
|
| 215 |
deployments = heat_client.software_deployments.list(**kwargs) |
| 216 |
return ( |
| 217 |
columns, |
| 218 |
(utils.get_item_properties(s, columns) for s in deployments) |
| 219 |
) |
| 220 |
|
| 221 |
|
| 222 |
class ShowDeployment(command.ShowOne): |
| 223 |
"""Show SoftwareDeployment Details.""" |
| 224 |
|
| 225 |
log = logging.getLogger(__name__ + ".ShowSoftwareDeployment") |
| 226 |
|
| 227 |
def get_parser(self, prog_name): |
| 228 |
parser = super().get_parser(prog_name) |
| 229 |
parser.add_argument( |
| 230 |
'deployment', |
| 231 |
metavar='<deployment>', |
| 232 |
help=_('ID of the deployment') |
| 233 |
) |
| 234 |
parser.add_argument( |
| 235 |
'--long', |
| 236 |
action='store_true', |
| 237 |
help=_('Show more fields in output') |
| 238 |
) |
| 239 |
return parser |
| 240 |
|
| 241 |
def take_action(self, parsed_args): |
| 242 |
self.log.debug("take_action(%s)", parsed_args) |
| 243 |
|
| 244 |
heat_client = self.app.client_manager.orchestration |
| 245 |
try: |
| 246 |
data = heat_client.software_deployments.get( |
| 247 |
deployment_id=parsed_args.deployment) |
| 248 |
except heat_exc.HTTPNotFound: |
| 249 |
raise exc.CommandError( |
| 250 |
_('Software Deployment not found: %s') |
| 251 |
% parsed_args.deployment) |
| 252 |
else: |
| 253 |
columns = [ |
| 254 |
'id', |
| 255 |
'server_id', |
| 256 |
'config_id', |
| 257 |
'creation_time', |
| 258 |
'updated_time', |
| 259 |
'status', |
| 260 |
'status_reason', |
| 261 |
'input_values', |
| 262 |
'action', |
| 263 |
] |
| 264 |
if parsed_args.long: |
| 265 |
columns.append('output_values') |
| 266 |
return columns, utils.get_item_properties(data, columns) |
| 267 |
|
| 268 |
|
| 269 |
class ShowMetadataDeployment(command.Command): |
| 270 |
"""Get deployment configuration metadata for the specified server.""" |
| 271 |
|
| 272 |
log = logging.getLogger(__name__ + '.ShowMetadataDeployment') |
| 273 |
|
| 274 |
def get_parser(self, prog_name): |
| 275 |
parser = super().get_parser(prog_name) |
| 276 |
parser.add_argument( |
| 277 |
'server', |
| 278 |
metavar='<server>', |
| 279 |
help=_('ID of the server to fetch deployments for') |
| 280 |
) |
| 281 |
return parser |
| 282 |
|
| 283 |
def take_action(self, parsed_args): |
| 284 |
self.log.debug("take_action(%s)", parsed_args) |
| 285 |
heat_client = self.app.client_manager.orchestration |
| 286 |
md = heat_client.software_deployments.metadata( |
| 287 |
server_id=parsed_args.server) |
| 288 |
print(jsonutils.dumps(md, indent=2)) |
| 289 |
|
| 290 |
|
| 291 |
class ShowOutputDeployment(command.Command): |
| 292 |
"""Show a specific deployment output.""" |
| 293 |
|
| 294 |
log = logging.getLogger(__name__ + '.ShowOutputDeployment') |
| 295 |
|
| 296 |
def get_parser(self, prog_name): |
| 297 |
parser = super().get_parser(prog_name) |
| 298 |
parser.add_argument( |
| 299 |
'deployment', |
| 300 |
metavar='<deployment>', |
| 301 |
help=_('ID of deployment to show the output for') |
| 302 |
) |
| 303 |
parser.add_argument( |
| 304 |
'output', |
| 305 |
metavar='<output-name>', |
| 306 |
nargs='?', |
| 307 |
default=None, |
| 308 |
help=_('Name of an output to display') |
| 309 |
) |
| 310 |
parser.add_argument( |
| 311 |
'--all', |
| 312 |
default=False, |
| 313 |
action='store_true', |
| 314 |
help=_('Display all deployment outputs') |
| 315 |
) |
| 316 |
parser.add_argument( |
| 317 |
'--long', |
| 318 |
action='store_true', |
| 319 |
default=False, |
| 320 |
help='Show full deployment logs in output', |
| 321 |
) |
| 322 |
return parser |
| 323 |
|
| 324 |
def take_action(self, parsed_args): |
| 325 |
self.log.debug("take_action(%s)", parsed_args) |
| 326 |
|
| 327 |
heat_client = self.app.client_manager.orchestration |
| 328 |
if (not parsed_args.all and parsed_args.output is None or |
| 329 |
parsed_args.all and parsed_args.output is not None): |
| 330 |
raise exc.CommandError( |
| 331 |
_('Error: either %(output)s or %(all)s argument is needed.') |
| 332 |
% {'output': '<output-name>', 'all': '--all'}) |
| 333 |
try: |
| 334 |
sd = heat_client.software_deployments.get( |
| 335 |
deployment_id=parsed_args.deployment) |
| 336 |
except heat_exc.HTTPNotFound: |
| 337 |
raise exc.CommandError(_('Deployment not found: %s') |
| 338 |
% parsed_args.deployment) |
| 339 |
outputs = sd.output_values |
| 340 |
if outputs: |
| 341 |
if parsed_args.all: |
| 342 |
print('output_values:\n') |
| 343 |
for k in outputs: |
| 344 |
format_utils.print_software_deployment_output( |
| 345 |
data=outputs, name=k, long=parsed_args.long) |
| 346 |
else: |
| 347 |
if parsed_args.output not in outputs: |
| 348 |
msg = (_('Output %(output)s does not exist in deployment' |
| 349 |
' %(deployment)s') |
| 350 |
% {'output': parsed_args.output, |
| 351 |
'deployment': parsed_args.deployment}) |
| 352 |
raise exc.CommandError(msg) |
| 353 |
else: |
| 354 |
print('output_value:\n') |
| 355 |
format_utils.print_software_deployment_output( |
| 356 |
data=outputs, name=parsed_args.output) |