1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 CLI ``services`` sub-commands.
21 from StringIO import StringIO
23 from . import service_templates
24 from .. import helptexts
27 from ..core import aria
28 from ...core import Core
29 from ...modeling import exceptions as modeling_exceptions
30 from ...storage import exceptions as storage_exceptions
31 from ...parser import consumption
32 from ...utils import (formatting, collections, console)
33 from ...orchestrator import topology
36 DESCRIPTION_FIELD_LENGTH_LIMIT = 20
37 SERVICE_COLUMNS = ('id', 'name', 'description', 'service_template_name', 'created_at', 'updated_at')
40 @aria.group(name='services')
41 @aria.options.verbose()
49 @services.command(name='show',
50 short_help='Show information for a service')
51 @aria.argument('service-name')
52 @aria.options.verbose()
53 @aria.options.service_mode_full
54 @aria.options.mode_graph
55 @aria.options.format_json
56 @aria.options.format_yaml
57 @aria.pass_model_storage
59 def show(service_name, model_storage, mode_full, mode_graph, format_json, format_yaml, logger):
61 Show information for a service
63 SERVICE_NAME is the unique name of the service.
65 service = model_storage.service.get_by_name(service_name)
67 if format_json or format_yaml:
71 consumption.ConsumptionContext()
73 console.puts(formatting.json_dumps(collections.prune(service.as_raw)))
75 console.puts(formatting.yaml_dumps(collections.prune(service.as_raw)))
77 console.puts(topology.Topology().dump(service))
79 console.puts(topology.Topology().dump_graph(service))
81 logger.info('Showing service {0}...'.format(service_name))
82 service_dict = service.to_dict()
83 columns = SERVICE_COLUMNS
85 dict(description=table.trim_formatter_generator(DESCRIPTION_FIELD_LENGTH_LIMIT))
86 table.print_data(columns, service_dict, 'Service:',
87 column_formatters=column_formatters, col_max_width=50)
89 if service_dict['description'] is not None:
90 logger.info('Description:')
91 logger.info('{0}{1}'.format(service_dict['description'].encode('UTF-8') or '',
95 @services.command(name='list', short_help='List services')
96 @aria.options.service_template_name()
97 @aria.options.sort_by()
98 @aria.options.descending
99 @aria.options.verbose()
100 @aria.pass_model_storage
102 def list(service_template_name,
110 If `--service-template-name` is provided, list services based on that service template.
111 Otherwise, list all services.
113 if service_template_name:
114 logger.info('Listing services for service template {0}...'.format(
115 service_template_name))
116 service_template = model_storage.service_template.get_by_name(service_template_name)
117 filters = dict(service_template=service_template)
119 logger.info('Listing all services...')
122 services_list = model_storage.service.list(
123 sort=utils.storage_sort_param(sort_by=sort_by, descending=descending),
125 table.print_data(SERVICE_COLUMNS, services_list, 'Services:')
128 @services.command(name='create',
129 short_help='Create a service')
130 @aria.argument('service-name', required=False)
131 @aria.options.service_template_name(required=True)
132 @aria.options.inputs(help=helptexts.SERVICE_INPUTS)
133 @aria.options.verbose()
134 @aria.pass_model_storage
135 @aria.pass_resource_storage
136 @aria.pass_plugin_manager
138 def create(service_template_name,
140 inputs, # pylint: disable=redefined-outer-name
148 SERVICE_NAME is the unique name to give to the service.
150 logger.info('Creating new service from service template {0}...'.format(
151 service_template_name))
152 core = Core(model_storage, resource_storage, plugin_manager)
153 service_template = model_storage.service_template.get_by_name(service_template_name)
156 service = core.create_service(service_template.id, inputs, service_name)
157 except storage_exceptions.StorageError as e:
158 utils.check_overriding_storage_exceptions(e, 'service', service_name)
160 except modeling_exceptions.ParameterException:
161 service_templates.print_service_template_inputs(model_storage, service_template_name,
164 logger.info("Service created. The service's name is {0}".format(service.name))
167 @services.command(name='delete',
168 short_help='Delete a service')
169 @aria.argument('service-name')
170 @aria.options.force(help=helptexts.IGNORE_AVAILABLE_NODES)
171 @aria.options.verbose()
172 @aria.pass_model_storage
173 @aria.pass_resource_storage
174 @aria.pass_plugin_manager
176 def delete(service_name, force, model_storage, resource_storage, plugin_manager, logger):
180 SERVICE_NAME is the unique name of the service.
182 logger.info('Deleting service {0}...'.format(service_name))
183 service = model_storage.service.get_by_name(service_name)
184 core = Core(model_storage, resource_storage, plugin_manager)
185 core.delete_service(service.id, force=force)
186 logger.info('Service {0} deleted'.format(service_name))
189 @services.command(name='outputs',
190 short_help='Show service outputs')
191 @aria.argument('service-name')
192 @aria.options.verbose()
193 @aria.pass_model_storage
195 def outputs(service_name, model_storage, logger):
199 SERVICE_NAME is the unique name of the service.
201 logger.info('Showing outputs for service {0}...'.format(service_name))
202 service = model_storage.service.get_by_name(service_name)
205 outputs_string = StringIO()
206 for output_name, output in service.outputs.iteritems():
207 outputs_string.write(' - "{0}":{1}'.format(output_name, os.linesep))
208 outputs_string.write(' Description: {0}{1}'.format(output.description, os.linesep))
209 outputs_string.write(' Value: {0}{1}'.format(output.value, os.linesep))
210 logger.info(outputs_string.getvalue())
212 logger.info('\tNo outputs')
215 @services.command(name='inputs',
216 short_help='Show service inputs')
217 @aria.argument('service-name')
218 @aria.options.verbose()
219 @aria.pass_model_storage
221 def inputs(service_name, model_storage, logger):
225 SERVICE_NAME is the unique name of the service.
227 logger.info('Showing inputs for service {0}...'.format(service_name))
228 service = model_storage.service.get_by_name(service_name)
231 inputs_string = StringIO()
232 for input_name, input_ in service.inputs.iteritems():
233 inputs_string.write(' - "{0}":{1}'.format(input_name, os.linesep))
234 inputs_string.write(' Description: {0}{1}'.format(input_.description, os.linesep))
235 inputs_string.write(' Value: {0}{1}'.format(input_.value, os.linesep))
236 logger.info(inputs_string.getvalue())
238 logger.info('\tNo inputs')