1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# Copyright 2012 OpenStack LLC.
# All Rights Reserved.
#
# 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.
#
# vim: tabstop=4 shiftwidth=4 softtabstop=4
"""
Server action implementations
"""
import logging
import os
from cliff import lister
from cliff import show
from openstackclient.common import command
from openstackclient.common import utils
def _format_servers_list_networks(server):
"""Return a string containing the networks a server is attached to.
:param server: a single Server resource
"""
output = []
for (network, addresses) in server.networks.items():
if not addresses:
continue
addresses_csv = ', '.join(addresses)
group = "%s=%s" % (network, addresses_csv)
output.append(group)
return '; '.join(output)
class ListServer(command.OpenStackCommand, lister.Lister):
"""List server command"""
api = 'compute'
log = logging.getLogger(__name__ + '.ListServer')
def get_parser(self, prog_name):
parser = super(ListServer, self).get_parser(prog_name)
parser.add_argument(
'--reservation-id',
help='only return instances that match the reservation',
)
parser.add_argument(
'--ip',
help='regular expression to match IP address',
)
parser.add_argument(
'--ip6',
help='regular expression to match IPv6 address',
)
parser.add_argument(
'--name',
help='regular expression to match name',
)
parser.add_argument(
'--instance-name',
help='regular expression to match instance name',
)
parser.add_argument(
'--status',
help='search by server status',
# FIXME(dhellmann): Add choices?
)
parser.add_argument(
'--flavor',
help='search by flavor ID',
)
parser.add_argument(
'--image',
help='search by image ID',
)
parser.add_argument(
'--host',
metavar='HOSTNAME',
help='search by hostname',
)
parser.add_argument(
'--all-tenants',
action='store_true',
default=bool(int(os.environ.get("ALL_TENANTS", 0))),
help='display information from all tenants (admin only)',
)
return parser
def get_data(self, parsed_args):
self.log.debug('get_data(%s)' % parsed_args)
nova_client = self.app.client_manager.compute
search_opts = {
'all_tenants': parsed_args.all_tenants,
'reservation_id': parsed_args.reservation_id,
'ip': parsed_args.ip,
'ip6': parsed_args.ip6,
'name': parsed_args.name,
'image': parsed_args.image,
'flavor': parsed_args.flavor,
'status': parsed_args.status,
'host': parsed_args.host,
'instance_name': parsed_args.instance_name,
}
self.log.debug('search options: %s', search_opts)
# FIXME(dhellmann): Consider adding other columns
columns = ('ID', 'Name', 'Status', 'Networks')
data = nova_client.servers.list(search_opts=search_opts)
return (columns,
(utils.get_item_properties(
s, columns,
formatters={'Networks': _format_servers_list_networks},
) for s in data),
)
class ShowServer(command.OpenStackCommand, show.ShowOne):
"""Show server command"""
api = 'compute'
log = logging.getLogger(__name__ + '.ShowServer')
def get_parser(self, prog_name):
parser = super(ShowServer, self).get_parser(prog_name)
parser.add_argument(
'server',
metavar='<server>',
help='Name or ID of server to display')
return parser
def get_data(self, parsed_args):
self.log.debug('get_data(%s)' % parsed_args)
nova_client = self.app.client_manager.compute
server = utils.find_resource(nova_client.servers, parsed_args.server)
info = {}
info.update(server._info)
# Convert the flavor blob to a name
flavor_info = info.get('flavor', {})
flavor_id = flavor_info.get('id', '')
flavor = utils.find_resource(nova_client.flavors, flavor_id)
info['flavor'] = flavor.name
# Convert the image blob to a name
image_info = info.get('image', {})
image_id = image_info.get('id', '')
image = utils.find_resource(nova_client.images, image_id)
info['image'] = image.name
# Format addresses in a useful way
info['addresses'] = _format_servers_list_networks(server)
# Remove a couple of values that are long and not too useful
info.pop('links', None)
return zip(*sorted(info.iteritems()))
|