OpenStack Compute (Nova) Client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shell.py 101KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115
  1. # Copyright 2010 Jacob Kaplan-Moss
  2. # Copyright 2011 OpenStack Foundation
  3. # Copyright 2013 IBM Corp.
  4. # All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  7. # not use this file except in compliance with the License. You may obtain
  8. # a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. # License for the specific language governing permissions and limitations
  16. # under the License.
  17. import argparse
  18. import copy
  19. import datetime
  20. import getpass
  21. import locale
  22. import os
  23. import sys
  24. import time
  25. from novaclient import exceptions
  26. from novaclient.openstack.common import strutils
  27. from novaclient.openstack.common import timeutils
  28. from novaclient import utils
  29. from novaclient.v1_1 import availability_zones
  30. from novaclient.v1_1 import quotas
  31. from novaclient.v1_1 import servers
  32. def _key_value_pairing(text):
  33. try:
  34. (k, v) = text.split('=', 1)
  35. return (k, v)
  36. except ValueError:
  37. msg = "%r is not in the format of key=value" % text
  38. raise argparse.ArgumentTypeError(msg)
  39. def _match_image(cs, wanted_properties):
  40. image_list = cs.images.list()
  41. images_matched = []
  42. match = set(wanted_properties)
  43. for img in image_list:
  44. try:
  45. if match == match.intersection(set(img.metadata.items())):
  46. images_matched.append(img)
  47. except AttributeError:
  48. pass
  49. return images_matched
  50. def _boot(cs, args, reservation_id=None, min_count=None, max_count=None):
  51. """Boot a new server."""
  52. if min_count is None:
  53. min_count = 1
  54. if max_count is None:
  55. max_count = min_count
  56. if min_count > max_count:
  57. raise exceptions.CommandError("min_instances should be <= "
  58. "max_instances")
  59. if not min_count or not max_count:
  60. raise exceptions.CommandError("min_instances nor max_instances should"
  61. "be 0")
  62. if args.image:
  63. image = _find_image(cs, args.image)
  64. else:
  65. image = None
  66. if not image and args.image_with:
  67. images = _match_image(cs, args.image_with)
  68. if images:
  69. # TODO(harlowja): log a warning that we
  70. # are selecting the first of many?
  71. image = images[0]
  72. if not image and not args.block_device_mapping:
  73. raise exceptions.CommandError("you need to specify an Image ID "
  74. "or a block device mapping "
  75. "or provide a set of properties to match"
  76. " against an image")
  77. if not args.flavor:
  78. raise exceptions.CommandError("you need to specify a Flavor ID ")
  79. if args.num_instances is not None:
  80. if args.num_instances <= 1:
  81. raise exceptions.CommandError("num_instances should be > 1")
  82. max_count = args.num_instances
  83. flavor = _find_flavor(cs, args.flavor)
  84. meta = dict(v.split('=', 1) for v in args.meta)
  85. files = {}
  86. for f in args.files:
  87. dst, src = f.split('=', 1)
  88. try:
  89. files[dst] = open(src)
  90. except IOError as e:
  91. raise exceptions.CommandError("Can't open '%s': %s" % (src, e))
  92. # use the os-keypair extension
  93. key_name = None
  94. if args.key_name is not None:
  95. key_name = args.key_name
  96. if args.user_data:
  97. try:
  98. userdata = open(args.user_data)
  99. except IOError as e:
  100. raise exceptions.CommandError("Can't open '%s': %s" %
  101. (args.user_data, e))
  102. else:
  103. userdata = None
  104. if args.availability_zone:
  105. availability_zone = args.availability_zone
  106. else:
  107. availability_zone = None
  108. if args.security_groups:
  109. security_groups = args.security_groups.split(',')
  110. else:
  111. security_groups = None
  112. block_device_mapping = {}
  113. for bdm in args.block_device_mapping:
  114. device_name, mapping = bdm.split('=', 1)
  115. block_device_mapping[device_name] = mapping
  116. nics = []
  117. for nic_str in args.nics:
  118. nic_info = {"net-id": "", "v4-fixed-ip": "", "port-id": ""}
  119. for kv_str in nic_str.split(","):
  120. k, v = kv_str.split("=", 1)
  121. nic_info[k] = v
  122. nics.append(nic_info)
  123. hints = {}
  124. if args.scheduler_hints:
  125. for hint in args.scheduler_hints:
  126. key, _sep, value = hint.partition('=')
  127. # NOTE(vish): multiple copies of the same hint will
  128. # result in a list of values
  129. if key in hints:
  130. if isinstance(hints[key], basestring):
  131. hints[key] = [hints[key]]
  132. hints[key] += [value]
  133. else:
  134. hints[key] = value
  135. boot_args = [args.name, image, flavor]
  136. if str(args.config_drive).lower() in ("true", "1"):
  137. config_drive = True
  138. elif str(args.config_drive).lower() in ("false", "0", "", "none"):
  139. config_drive = None
  140. else:
  141. config_drive = args.config_drive
  142. boot_kwargs = dict(
  143. meta=meta,
  144. files=files,
  145. key_name=key_name,
  146. reservation_id=reservation_id,
  147. min_count=min_count,
  148. max_count=max_count,
  149. userdata=userdata,
  150. availability_zone=availability_zone,
  151. security_groups=security_groups,
  152. block_device_mapping=block_device_mapping,
  153. nics=nics,
  154. scheduler_hints=hints,
  155. config_drive=config_drive)
  156. return boot_args, boot_kwargs
  157. @utils.arg('--flavor',
  158. default=None,
  159. metavar='<flavor>',
  160. help="Flavor ID (see 'nova flavor-list').")
  161. @utils.arg('--image',
  162. default=None,
  163. metavar='<image>',
  164. help="Image ID (see 'nova image-list'). ")
  165. @utils.arg('--image-with',
  166. default=[],
  167. type=_key_value_pairing,
  168. action='append',
  169. metavar='<key=value>',
  170. help="Image metadata property (see 'nova image-show'). ")
  171. @utils.arg('--num-instances',
  172. default=None,
  173. type=int,
  174. metavar='<number>',
  175. help="boot multi instances at a time")
  176. @utils.arg('--meta',
  177. metavar="<key=value>",
  178. action='append',
  179. default=[],
  180. help="Record arbitrary key/value metadata to /meta.js "
  181. "on the new server. Can be specified multiple times.")
  182. @utils.arg('--file',
  183. metavar="<dst-path=src-path>",
  184. action='append',
  185. dest='files',
  186. default=[],
  187. help="Store arbitrary files from <src-path> locally to <dst-path> "
  188. "on the new server. You may store up to 5 files.")
  189. @utils.arg('--key-name',
  190. metavar='<key-name>',
  191. help="Key name of keypair that should be created earlier with \
  192. the command keypair-add")
  193. @utils.arg('--key_name',
  194. help=argparse.SUPPRESS)
  195. @utils.arg('name', metavar='<name>', help='Name for the new server')
  196. @utils.arg('--user-data',
  197. default=None,
  198. metavar='<user-data>',
  199. help="user data file to pass to be exposed by the metadata server.")
  200. @utils.arg('--user_data',
  201. help=argparse.SUPPRESS)
  202. @utils.arg('--availability-zone',
  203. default=None,
  204. metavar='<availability-zone>',
  205. help="The availability zone for instance placement.")
  206. @utils.arg('--availability_zone',
  207. help=argparse.SUPPRESS)
  208. @utils.arg('--security-groups',
  209. default=None,
  210. metavar='<security-groups>',
  211. help="Comma separated list of security group names.")
  212. @utils.arg('--security_groups',
  213. help=argparse.SUPPRESS)
  214. @utils.arg('--block-device-mapping',
  215. metavar="<dev-name=mapping>",
  216. action='append',
  217. default=[],
  218. help="Block device mapping in the format "
  219. "<dev-name>=<id>:<type>:<size(GB)>:<delete-on-terminate>.")
  220. @utils.arg('--block_device_mapping',
  221. action='append',
  222. help=argparse.SUPPRESS)
  223. @utils.arg('--hint',
  224. action='append',
  225. dest='scheduler_hints',
  226. default=[],
  227. metavar='<key=value>',
  228. help="Send arbitrary key/value pairs to the scheduler for custom use.")
  229. @utils.arg('--nic',
  230. metavar="<net-id=net-uuid,v4-fixed-ip=ip-addr,port-id=port-uuid>",
  231. action='append',
  232. dest='nics',
  233. default=[],
  234. help="Create a NIC on the server.\n"
  235. "Specify option multiple times to create multiple NICs.\n"
  236. "net-id: attach NIC to network with this UUID (optional)\n"
  237. "v4-fixed-ip: IPv4 fixed address for NIC (optional).\n"
  238. "port-id: attach NIC to port with this UUID (optional)")
  239. @utils.arg('--config-drive',
  240. metavar="<value>",
  241. dest='config_drive',
  242. default=False,
  243. help="Enable config drive")
  244. @utils.arg('--poll',
  245. dest='poll',
  246. action="store_true",
  247. default=False,
  248. help='Blocks while instance builds so progress can be reported.')
  249. def do_boot(cs, args):
  250. """Boot a new server."""
  251. boot_args, boot_kwargs = _boot(cs, args)
  252. extra_boot_kwargs = utils.get_resource_manager_extra_kwargs(do_boot, args)
  253. boot_kwargs.update(extra_boot_kwargs)
  254. server = cs.servers.create(*boot_args, **boot_kwargs)
  255. # Keep any information (like adminPass) returned by create
  256. info = server._info
  257. server = cs.servers.get(info['id'])
  258. info.update(server._info)
  259. flavor = info.get('flavor', {})
  260. flavor_id = flavor.get('id', '')
  261. info['flavor'] = _find_flavor(cs, flavor_id).name
  262. image = info.get('image', {})
  263. if image:
  264. image_id = image.get('id', '')
  265. info['image'] = _find_image(cs, image_id).name
  266. else: # Booting from volume
  267. info['image'] = "Attempt to boot from volume - no image supplied"
  268. info.pop('links', None)
  269. info.pop('addresses', None)
  270. utils.print_dict(info)
  271. if args.poll:
  272. _poll_for_status(cs.servers.get, info['id'], 'building', ['active'])
  273. def do_cloudpipe_list(cs, _args):
  274. """Print a list of all cloudpipe instances."""
  275. cloudpipes = cs.cloudpipe.list()
  276. columns = ['Project Id', "Public IP", "Public Port", "Internal IP"]
  277. utils.print_list(cloudpipes, columns)
  278. @utils.arg('project', metavar='<project>', help='Name of the project.')
  279. def do_cloudpipe_create(cs, args):
  280. """Create a cloudpipe instance for the given project"""
  281. cs.cloudpipe.create(args.project)
  282. @utils.arg('address', metavar='<ip address>', help='New IP Address.')
  283. @utils.arg('port', metavar='<port>', help='New Port.')
  284. def do_cloudpipe_configure(cs, args):
  285. """Update the VPN IP/port of a cloudpipe instance"""
  286. cs.cloudpipe.update(args.address, args.port)
  287. def _poll_for_status(poll_fn, obj_id, action, final_ok_states,
  288. poll_period=5, show_progress=True,
  289. status_field="status", silent=False):
  290. """Block while an action is being performed, periodically printing
  291. progress.
  292. """
  293. def print_progress(progress):
  294. if show_progress:
  295. msg = ('\rInstance %(action)s... %(progress)s%% complete'
  296. % dict(action=action, progress=progress))
  297. else:
  298. msg = '\rInstance %(action)s...' % dict(action=action)
  299. sys.stdout.write(msg)
  300. sys.stdout.flush()
  301. if not silent:
  302. print
  303. while True:
  304. obj = poll_fn(obj_id)
  305. status = getattr(obj, status_field)
  306. if status:
  307. status = status.lower()
  308. progress = getattr(obj, 'progress', None) or 0
  309. if status in final_ok_states:
  310. if not silent:
  311. print_progress(100)
  312. print("\nFinished")
  313. break
  314. elif status == "error":
  315. if not silent:
  316. print("\nError %(action)s instance" % locals())
  317. break
  318. if not silent:
  319. print_progress(progress)
  320. time.sleep(poll_period)
  321. def _translate_keys(collection, convert):
  322. for item in collection:
  323. keys = item.__dict__.keys()
  324. for from_key, to_key in convert:
  325. if from_key in keys and to_key not in keys:
  326. setattr(item, to_key, item._info[from_key])
  327. def _translate_extended_states(collection):
  328. power_states = [
  329. 'NOSTATE', # 0x00
  330. 'Running', # 0x01
  331. '', # 0x02
  332. 'Paused', # 0x03
  333. 'Shutdown', # 0x04
  334. '', # 0x05
  335. 'Crashed', # 0x06
  336. 'Suspended' # 0x07
  337. ]
  338. for item in collection:
  339. try:
  340. setattr(item, 'power_state',
  341. power_states[getattr(item, 'power_state')]
  342. )
  343. except AttributeError:
  344. setattr(item, 'power_state', "N/A")
  345. try:
  346. getattr(item, 'task_state')
  347. except AttributeError:
  348. setattr(item, 'task_state', "N/A")
  349. def _translate_flavor_keys(collection):
  350. _translate_keys(collection, [('ram', 'memory_mb')])
  351. def _print_flavor_extra_specs(flavor):
  352. try:
  353. return flavor.get_keys()
  354. except exceptions.NotFound:
  355. return "N/A"
  356. def _print_flavor_list(flavors, show_extra_specs=False):
  357. _translate_flavor_keys(flavors)
  358. headers = [
  359. 'ID',
  360. 'Name',
  361. 'Memory_MB',
  362. 'Disk',
  363. 'Ephemeral',
  364. 'Swap',
  365. 'VCPUs',
  366. 'RXTX_Factor',
  367. 'Is_Public',
  368. ]
  369. if show_extra_specs:
  370. formatters = {'extra_specs': _print_flavor_extra_specs}
  371. headers.append('extra_specs')
  372. else:
  373. formatters = {}
  374. utils.print_list(flavors, headers, formatters)
  375. @utils.arg('--extra-specs',
  376. dest='extra_specs',
  377. action='store_true',
  378. default=False,
  379. help='Get extra-specs of each flavor.')
  380. @utils.arg('--all',
  381. dest='all',
  382. action='store_true',
  383. default=False,
  384. help='Display all flavors (Admin only).')
  385. def do_flavor_list(cs, args):
  386. """Print a list of available 'flavors' (sizes of servers)."""
  387. if args.all:
  388. flavors = cs.flavors.list(is_public=None)
  389. else:
  390. flavors = cs.flavors.list()
  391. _print_flavor_list(flavors, args.extra_specs)
  392. @utils.arg('flavor',
  393. metavar='<flavor>',
  394. help="Name or ID of the flavor to delete")
  395. def do_flavor_delete(cs, args):
  396. """Delete a specific flavor"""
  397. flavorid = _find_flavor(cs, args.flavor)
  398. cs.flavors.delete(flavorid)
  399. _print_flavor_list([flavorid])
  400. @utils.arg('flavor',
  401. metavar='<flavor>',
  402. help="Name or ID of flavor")
  403. def do_flavor_show(cs, args):
  404. """Show details about the given flavor."""
  405. flavor = _find_flavor(cs, args.flavor)
  406. _print_flavor(flavor)
  407. @utils.arg('name',
  408. metavar='<name>',
  409. help="Name of the new flavor")
  410. @utils.arg('id',
  411. metavar='<id>',
  412. help="Unique ID (integer or UUID) for the new flavor."
  413. " If specifying 'auto', a UUID will be generated as id")
  414. @utils.arg('ram',
  415. metavar='<ram>',
  416. help="Memory size in MB")
  417. @utils.arg('disk',
  418. metavar='<disk>',
  419. help="Disk size in GB")
  420. @utils.arg('--ephemeral',
  421. metavar='<ephemeral>',
  422. help="Ephemeral space size in GB (default 0)",
  423. default=0)
  424. @utils.arg('vcpus',
  425. metavar='<vcpus>',
  426. help="Number of vcpus")
  427. @utils.arg('--swap',
  428. metavar='<swap>',
  429. help="Swap space size in MB (default 0)",
  430. default=0)
  431. @utils.arg('--rxtx-factor',
  432. metavar='<factor>',
  433. help="RX/TX factor (default 1)",
  434. default=1.0)
  435. @utils.arg('--is-public',
  436. metavar='<is-public>',
  437. help="Make flavor accessible to the public (default true)",
  438. type=utils.bool_from_str,
  439. default=True)
  440. def do_flavor_create(cs, args):
  441. """Create a new flavor"""
  442. f = cs.flavors.create(args.name, args.ram, args.vcpus, args.disk, args.id,
  443. args.ephemeral, args.swap, args.rxtx_factor,
  444. args.is_public)
  445. _print_flavor_list([f])
  446. @utils.arg('flavor',
  447. metavar='<flavor>',
  448. help="Name or ID of flavor")
  449. @utils.arg('action',
  450. metavar='<action>',
  451. choices=['set', 'unset'],
  452. help="Actions: 'set' or 'unset'")
  453. @utils.arg('metadata',
  454. metavar='<key=value>',
  455. nargs='+',
  456. action='append',
  457. default=[],
  458. help='Extra_specs to set/unset (only key is necessary on unset)')
  459. def do_flavor_key(cs, args):
  460. """Set or unset extra_spec for a flavor."""
  461. flavor = _find_flavor(cs, args.flavor)
  462. keypair = _extract_metadata(args)
  463. if args.action == 'set':
  464. flavor.set_keys(keypair)
  465. elif args.action == 'unset':
  466. flavor.unset_keys(keypair.keys())
  467. @utils.arg('--flavor',
  468. metavar='<flavor>',
  469. help="Filter results by flavor name or ID.")
  470. @utils.arg('--tenant', metavar='<tenant_id>',
  471. help='Filter results by tenant ID.')
  472. def do_flavor_access_list(cs, args):
  473. """Print access information about the given flavor."""
  474. if args.flavor and args.tenant:
  475. raise exceptions.CommandError("Unable to filter results by "
  476. "both --flavor and --tenant.")
  477. elif args.flavor:
  478. flavor = _find_flavor(cs, args.flavor)
  479. if flavor.is_public:
  480. raise exceptions.CommandError("Failed to get access list "
  481. "for public flavor type.")
  482. kwargs = {'flavor': flavor}
  483. elif args.tenant:
  484. kwargs = {'tenant': args.tenant}
  485. else:
  486. raise exceptions.CommandError("Unable to get all access lists. "
  487. "Specify --flavor or --tenant")
  488. try:
  489. access_list = cs.flavor_access.list(**kwargs)
  490. except NotImplementedError as e:
  491. raise exceptions.CommandError("%s" % str(e))
  492. columns = ['Flavor_ID', 'Tenant_ID']
  493. utils.print_list(access_list, columns)
  494. @utils.arg('flavor',
  495. metavar='<flavor>',
  496. help="Filter results by flavor name or ID.")
  497. @utils.arg('tenant', metavar='<tenant_id>',
  498. help='Filter results by tenant ID.')
  499. def do_flavor_access_add(cs, args):
  500. """Add flavor access for the given tenant."""
  501. flavor = _find_flavor(cs, args.flavor)
  502. access_list = cs.flavor_access.add_tenant_access(flavor, args.tenant)
  503. columns = ['Flavor_ID', 'Tenant_ID']
  504. utils.print_list(access_list, columns)
  505. @utils.arg('flavor',
  506. metavar='<flavor>',
  507. help="Filter results by flavor name or ID.")
  508. @utils.arg('tenant', metavar='<tenant_id>',
  509. help='Filter results by tenant ID.')
  510. def do_flavor_access_remove(cs, args):
  511. """Remove flavor access for the given tenant."""
  512. flavor = _find_flavor(cs, args.flavor)
  513. access_list = cs.flavor_access.remove_tenant_access(flavor, args.tenant)
  514. columns = ['Flavor_ID', 'Tenant_ID']
  515. utils.print_list(access_list, columns)
  516. @utils.arg('project_id', metavar='<project_id>',
  517. help='The ID of the project.')
  518. def do_scrub(cs, args):
  519. """Deletes data associated with the project"""
  520. networks_list = cs.networks.list()
  521. networks_list = [network for network in networks_list
  522. if getattr(network, 'project_id', '') == args.project_id]
  523. search_opts = {'all_tenants': 1}
  524. groups = cs.security_groups.list(search_opts)
  525. groups = [group for group in groups
  526. if group.tenant_id == args.project_id]
  527. for network in networks_list:
  528. cs.networks.disassociate(network)
  529. for group in groups:
  530. cs.security_groups.delete(group)
  531. def do_network_list(cs, _args):
  532. """Print a list of available networks."""
  533. network_list = cs.networks.list()
  534. columns = ['ID', 'Label', 'Cidr']
  535. utils.print_list(network_list, columns)
  536. @utils.arg('network',
  537. metavar='<network>',
  538. help="uuid or label of network")
  539. def do_network_show(cs, args):
  540. """Show details about the given network."""
  541. network = utils.find_resource(cs.networks, args.network)
  542. utils.print_dict(network._info)
  543. @utils.arg('--host-only',
  544. dest='host_only',
  545. metavar='<0|1>',
  546. nargs='?',
  547. type=int,
  548. const=1,
  549. default=0)
  550. @utils.arg('--project-only',
  551. dest='project_only',
  552. metavar='<0|1>',
  553. nargs='?',
  554. type=int,
  555. const=1,
  556. default=0)
  557. @utils.arg('network',
  558. metavar='<network>',
  559. help="uuid of network")
  560. def do_network_disassociate(cs, args):
  561. """Disassociate host and/or project from the given network."""
  562. if args.host_only:
  563. cs.networks.disassociate(args.network, True, False)
  564. elif args.project_only:
  565. cs.networks.disassociate(args.network, False, True)
  566. else:
  567. cs.networks.disassociate(args.network, True, True)
  568. @utils.arg('network',
  569. metavar='<network>',
  570. help="uuid of network")
  571. @utils.arg('host',
  572. metavar='<host>',
  573. help="Name of host")
  574. def do_network_associate_host(cs, args):
  575. """Associate host with network."""
  576. cs.networks.associate_host(args.network, args.host)
  577. @utils.arg('network',
  578. metavar='<network>',
  579. help="uuid of network")
  580. def do_network_associate_project(cs, args):
  581. """Associate project with network."""
  582. cs.networks.associate_project(args.network)
  583. def _filter_network_create_options(args):
  584. valid_args = ['label', 'cidr', 'vlan_start', 'vpn_start', 'cidr_v6',
  585. 'gateway', 'gateway_v6', 'bridge', 'bridge_interface',
  586. 'multi_host', 'dns1', 'dns2', 'uuid', 'fixed_cidr',
  587. 'project_id', 'priority']
  588. kwargs = {}
  589. for k, v in args.__dict__.items():
  590. if k in valid_args and v is not None:
  591. kwargs[k] = v
  592. return kwargs
  593. @utils.arg('label',
  594. metavar='<network_label>',
  595. help="Label for network")
  596. @utils.arg('--fixed-range-v4',
  597. dest='cidr',
  598. metavar='<x.x.x.x/yy>',
  599. help="IPv4 subnet (ex: 10.0.0.0/8)")
  600. @utils.arg('--fixed-range-v6',
  601. dest="cidr_v6",
  602. help='IPv6 subnet (ex: fe80::/64')
  603. @utils.arg('--vlan',
  604. dest='vlan_start',
  605. metavar='<vlan id>',
  606. help="vlan id")
  607. @utils.arg('--vpn',
  608. dest='vpn_start',
  609. metavar='<vpn start>',
  610. help="vpn start")
  611. @utils.arg('--gateway',
  612. dest="gateway",
  613. help='gateway')
  614. @utils.arg('--gateway-v6',
  615. dest="gateway_v6",
  616. help='ipv6 gateway')
  617. @utils.arg('--bridge',
  618. dest="bridge",
  619. metavar='<bridge>',
  620. help='VIFs on this network are connected to this bridge')
  621. @utils.arg('--bridge-interface',
  622. dest="bridge_interface",
  623. metavar='<bridge interface>',
  624. help='the bridge is connected to this interface')
  625. @utils.arg('--multi-host',
  626. dest="multi_host",
  627. metavar="<'T'|'F'>",
  628. help='Multi host')
  629. @utils.arg('--dns1',
  630. dest="dns1",
  631. metavar="<DNS Address>", help='First DNS')
  632. @utils.arg('--dns2',
  633. dest="dns2",
  634. metavar="<DNS Address>",
  635. help='Second DNS')
  636. @utils.arg('--uuid',
  637. dest="uuid",
  638. metavar="<network uuid>",
  639. help='Network UUID')
  640. @utils.arg('--fixed-cidr',
  641. dest="fixed_cidr",
  642. metavar='<x.x.x.x/yy>',
  643. help='IPv4 subnet for fixed IPS (ex: 10.20.0.0/16)')
  644. @utils.arg('--project-id',
  645. dest="project_id",
  646. metavar="<project id>",
  647. help='Project id')
  648. @utils.arg('--priority',
  649. dest="priority",
  650. metavar="<number>",
  651. help='Network interface priority')
  652. def do_network_create(cs, args):
  653. """Create a network."""
  654. if not (args.cidr or args.cidr_v6):
  655. raise exceptions.CommandError(
  656. "Must specify eith fixed_range_v4 or fixed_range_v6")
  657. kwargs = _filter_network_create_options(args)
  658. if args.multi_host is not None:
  659. kwargs['multi_host'] = bool(args.multi_host == 'T' or
  660. strutils.bool_from_string(args.multi_host))
  661. cs.networks.create(**kwargs)
  662. @utils.arg('--limit',
  663. dest="limit",
  664. metavar="<limit>",
  665. help='number of images to return per request')
  666. def do_image_list(cs, _args):
  667. """Print a list of available images to boot from."""
  668. limit = _args.limit
  669. image_list = cs.images.list(limit=limit)
  670. def parse_server_name(image):
  671. try:
  672. return image.server['id']
  673. except (AttributeError, KeyError):
  674. return ''
  675. fmts = {'Server': parse_server_name}
  676. utils.print_list(image_list, ['ID', 'Name', 'Status', 'Server'],
  677. fmts, sortby_index=1)
  678. @utils.arg('image',
  679. metavar='<image>',
  680. help="Name or ID of image")
  681. @utils.arg('action',
  682. metavar='<action>',
  683. choices=['set', 'delete'],
  684. help="Actions: 'set' or 'delete'")
  685. @utils.arg('metadata',
  686. metavar='<key=value>',
  687. nargs='+',
  688. action='append',
  689. default=[],
  690. help='Metadata to add/update or delete (only key is necessary on delete)')
  691. def do_image_meta(cs, args):
  692. """Set or Delete metadata on an image."""
  693. image = _find_image(cs, args.image)
  694. metadata = _extract_metadata(args)
  695. if args.action == 'set':
  696. cs.images.set_meta(image, metadata)
  697. elif args.action == 'delete':
  698. cs.images.delete_meta(image, metadata.keys())
  699. def _extract_metadata(args):
  700. metadata = {}
  701. for metadatum in args.metadata[0]:
  702. # Can only pass the key in on 'delete'
  703. # So this doesn't have to have '='
  704. if metadatum.find('=') > -1:
  705. (key, value) = metadatum.split('=', 1)
  706. else:
  707. key = metadatum
  708. value = None
  709. metadata[key] = value
  710. return metadata
  711. def _print_image(image):
  712. info = image._info.copy()
  713. # ignore links, we don't need to present those
  714. info.pop('links')
  715. # try to replace a server entity to just an id
  716. server = info.pop('server', None)
  717. try:
  718. info['server'] = server['id']
  719. except (KeyError, TypeError):
  720. pass
  721. # break up metadata and display each on its own row
  722. metadata = info.pop('metadata', {})
  723. try:
  724. for key, value in metadata.items():
  725. _key = 'metadata %s' % key
  726. info[_key] = value
  727. except AttributeError:
  728. pass
  729. utils.print_dict(info)
  730. def _print_flavor(flavor):
  731. info = flavor._info.copy()
  732. # ignore links, we don't need to present those
  733. info.pop('links')
  734. info.update({"extra_specs": _print_flavor_extra_specs(flavor)})
  735. utils.print_dict(info)
  736. @utils.arg('image',
  737. metavar='<image>',
  738. help="Name or ID of image")
  739. def do_image_show(cs, args):
  740. """Show details about the given image."""
  741. image = _find_image(cs, args.image)
  742. _print_image(image)
  743. @utils.arg('image', metavar='<image>', nargs='+',
  744. help='Name or ID of image(s).')
  745. def do_image_delete(cs, args):
  746. """Delete specified image(s)."""
  747. for image in args.image:
  748. try:
  749. _find_image(cs, image).delete()
  750. except Exception as e:
  751. print "Delete for image %s failed: %s" % (image, e)
  752. @utils.arg('--reservation-id',
  753. dest='reservation_id',
  754. metavar='<reservation-id>',
  755. default=None,
  756. help='Only return instances that match reservation-id.')
  757. @utils.arg('--reservation_id',
  758. help=argparse.SUPPRESS)
  759. @utils.arg('--ip',
  760. dest='ip',
  761. metavar='<ip-regexp>',
  762. default=None,
  763. help='Search with regular expression match by IP address (Admin only).')
  764. @utils.arg('--ip6',
  765. dest='ip6',
  766. metavar='<ip6-regexp>',
  767. default=None,
  768. help='Search with regular expression match by IPv6 address (Admin only).')
  769. @utils.arg('--name',
  770. dest='name',
  771. metavar='<name-regexp>',
  772. default=None,
  773. help='Search with regular expression match by name')
  774. @utils.arg('--instance-name',
  775. dest='instance_name',
  776. metavar='<name-regexp>',
  777. default=None,
  778. help='Search with regular expression match by instance name (Admin only).')
  779. @utils.arg('--instance_name',
  780. help=argparse.SUPPRESS)
  781. @utils.arg('--status',
  782. dest='status',
  783. metavar='<status>',
  784. default=None,
  785. help='Search by server status')
  786. @utils.arg('--flavor',
  787. dest='flavor',
  788. metavar='<flavor>',
  789. default=None,
  790. help='Search by flavor name or ID')
  791. @utils.arg('--image',
  792. dest='image',
  793. metavar='<image>',
  794. default=None,
  795. help='Search by image name or ID')
  796. @utils.arg('--host',
  797. dest='host',
  798. metavar='<hostname>',
  799. default=None,
  800. help='Search instances by hostname to which they are assigned '
  801. '(Admin only).')
  802. @utils.arg('--all-tenants',
  803. dest='all_tenants',
  804. metavar='<0|1>',
  805. nargs='?',
  806. type=int,
  807. const=1,
  808. default=int(utils.bool_from_str(os.environ.get("ALL_TENANTS", 'false'))),
  809. help='Display information from all tenants (Admin only).')
  810. @utils.arg('--all_tenants',
  811. nargs='?',
  812. type=int,
  813. const=1,
  814. help=argparse.SUPPRESS)
  815. @utils.arg('--tenant',
  816. #nova db searches by project_id
  817. dest='tenant',
  818. metavar='<tenant>',
  819. nargs='?',
  820. help='Display information from single tenant (Admin only).')
  821. @utils.arg('--fields',
  822. default=None,
  823. metavar='<fields>',
  824. help='Comma-separated list of fields to display. '
  825. 'Use the show command to see which fields are available.')
  826. def do_list(cs, args):
  827. """List active servers."""
  828. imageid = None
  829. flavorid = None
  830. if args.image:
  831. imageid = _find_image(cs, args.image).id
  832. if args.flavor:
  833. flavorid = _find_flavor(cs, args.flavor).id
  834. search_opts = {
  835. 'all_tenants': args.all_tenants,
  836. 'reservation_id': args.reservation_id,
  837. 'ip': args.ip,
  838. 'ip6': args.ip6,
  839. 'name': args.name,
  840. 'image': imageid,
  841. 'flavor': flavorid,
  842. 'status': args.status,
  843. 'tenant_id': args.tenant,
  844. 'host': args.host,
  845. 'instance_name': args.instance_name}
  846. filters = {'flavor': lambda f: f['id'],
  847. 'security_groups': utils._format_security_groups}
  848. formatters = {}
  849. field_titles = []
  850. if args.fields:
  851. for field in args.fields.split(','):
  852. field_title, formatter = utils._make_field_formatter(field,
  853. filters)
  854. field_titles.append(field_title)
  855. formatters[field_title] = formatter
  856. id_col = 'ID'
  857. servers = cs.servers.list(search_opts=search_opts)
  858. convert = [('OS-EXT-SRV-ATTR:host', 'host'),
  859. ('OS-EXT-STS:task_state', 'task_state'),
  860. ('OS-EXT-SRV-ATTR:instance_name', 'instance_name'),
  861. ('OS-EXT-STS:power_state', 'power_state'),
  862. ('hostId', 'host_id')]
  863. _translate_keys(servers, convert)
  864. _translate_extended_states(servers)
  865. if field_titles:
  866. columns = [id_col] + field_titles
  867. else:
  868. columns = [
  869. id_col,
  870. 'Name',
  871. 'Status',
  872. 'Task State',
  873. 'Power State',
  874. 'Networks'
  875. ]
  876. formatters['Networks'] = utils._format_servers_list_networks
  877. utils.print_list(servers, columns,
  878. formatters, sortby_index=1)
  879. @utils.arg('--hard',
  880. dest='reboot_type',
  881. action='store_const',
  882. const=servers.REBOOT_HARD,
  883. default=servers.REBOOT_SOFT,
  884. help='Perform a hard reboot (instead of a soft one).')
  885. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  886. @utils.arg('--poll',
  887. dest='poll',
  888. action="store_true",
  889. default=False,
  890. help='Blocks while instance is rebooting.')
  891. def do_reboot(cs, args):
  892. """Reboot a server."""
  893. server = _find_server(cs, args.server)
  894. server.reboot(args.reboot_type)
  895. if args.poll:
  896. _poll_for_status(cs.servers.get, server.id, 'rebooting', ['active'],
  897. show_progress=False)
  898. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  899. @utils.arg('image', metavar='<image>', help="Name or ID of new image.")
  900. @utils.arg('--rebuild-password',
  901. dest='rebuild_password',
  902. metavar='<rebuild-password>',
  903. default=False,
  904. help="Set the provided password on the rebuild instance.")
  905. @utils.arg('--rebuild_password',
  906. help=argparse.SUPPRESS)
  907. @utils.arg('--poll',
  908. dest='poll',
  909. action="store_true",
  910. default=False,
  911. help='Blocks while instance rebuilds so progress can be reported.')
  912. @utils.arg('--minimal',
  913. dest='minimal',
  914. action="store_true",
  915. default=False,
  916. help='Skips flavor/image lookups when showing instances')
  917. def do_rebuild(cs, args):
  918. """Shutdown, re-image, and re-boot a server."""
  919. server = _find_server(cs, args.server)
  920. image = _find_image(cs, args.image)
  921. if args.rebuild_password is not False:
  922. _password = args.rebuild_password
  923. else:
  924. _password = None
  925. kwargs = utils.get_resource_manager_extra_kwargs(do_rebuild, args)
  926. s = server.rebuild(image, _password, **kwargs)
  927. _print_server(cs, args)
  928. if args.poll:
  929. _poll_for_status(cs.servers.get, server.id, 'rebuilding', ['active'])
  930. @utils.arg('server', metavar='<server>',
  931. help='Name (old name) or ID of server.')
  932. @utils.arg('name', metavar='<name>', help='New name for the server.')
  933. def do_rename(cs, args):
  934. """Rename a server."""
  935. _find_server(cs, args.server).update(name=args.name)
  936. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  937. @utils.arg('flavor', metavar='<flavor>', help="Name or ID of new flavor.")
  938. @utils.arg('--poll',
  939. dest='poll',
  940. action="store_true",
  941. default=False,
  942. help='Blocks while instance resizes so progress can be reported.')
  943. def do_resize(cs, args):
  944. """Resize a server."""
  945. server = _find_server(cs, args.server)
  946. flavor = _find_flavor(cs, args.flavor)
  947. kwargs = utils.get_resource_manager_extra_kwargs(do_resize, args)
  948. server.resize(flavor, **kwargs)
  949. if args.poll:
  950. _poll_for_status(cs.servers.get, server.id, 'resizing',
  951. ['active', 'verify_resize'])
  952. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  953. def do_resize_confirm(cs, args):
  954. """Confirm a previous resize."""
  955. _find_server(cs, args.server).confirm_resize()
  956. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  957. def do_resize_revert(cs, args):
  958. """Revert a previous resize (and return to the previous VM)."""
  959. _find_server(cs, args.server).revert_resize()
  960. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  961. @utils.arg('--poll',
  962. dest='poll',
  963. action="store_true",
  964. default=False,
  965. help='Blocks while instance migrates so progress can be reported.')
  966. def do_migrate(cs, args):
  967. """Migrate a server. The new host will be selected by the scheduler."""
  968. server = _find_server(cs, args.server)
  969. server.migrate()
  970. if args.poll:
  971. _poll_for_status(cs.servers.get, server.id, 'migrating',
  972. ['active', 'verify_resize'])
  973. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  974. def do_pause(cs, args):
  975. """Pause a server."""
  976. _find_server(cs, args.server).pause()
  977. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  978. def do_unpause(cs, args):
  979. """Unpause a server."""
  980. _find_server(cs, args.server).unpause()
  981. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  982. def do_stop(cs, args):
  983. """Stop a server."""
  984. _find_server(cs, args.server).stop()
  985. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  986. def do_start(cs, args):
  987. """Start a server."""
  988. _find_server(cs, args.server).start()
  989. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  990. def do_lock(cs, args):
  991. """Lock a server."""
  992. _find_server(cs, args.server).lock()
  993. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  994. def do_unlock(cs, args):
  995. """Unlock a server."""
  996. _find_server(cs, args.server).unlock()
  997. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  998. def do_suspend(cs, args):
  999. """Suspend a server."""
  1000. _find_server(cs, args.server).suspend()
  1001. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1002. def do_resume(cs, args):
  1003. """Resume a server."""
  1004. _find_server(cs, args.server).resume()
  1005. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1006. def do_rescue(cs, args):
  1007. """Rescue a server."""
  1008. utils.print_dict(_find_server(cs, args.server).rescue()[1])
  1009. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1010. def do_unrescue(cs, args):
  1011. """Unrescue a server."""
  1012. _find_server(cs, args.server).unrescue()
  1013. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1014. def do_diagnostics(cs, args):
  1015. """Retrieve server diagnostics."""
  1016. server = _find_server(cs, args.server)
  1017. utils.print_dict(cs.servers.diagnostics(server)[1])
  1018. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1019. def do_root_password(cs, args):
  1020. """
  1021. Change the root password for a server.
  1022. """
  1023. server = _find_server(cs, args.server)
  1024. p1 = getpass.getpass('New password: ')
  1025. p2 = getpass.getpass('Again: ')
  1026. if p1 != p2:
  1027. raise exceptions.CommandError("Passwords do not match.")
  1028. server.change_password(p1)
  1029. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1030. @utils.arg('name', metavar='<name>', help='Name of snapshot.')
  1031. @utils.arg('--poll',
  1032. dest='poll',
  1033. action="store_true",
  1034. default=False,
  1035. help='Blocks while instance snapshots so progress can be reported.')
  1036. def do_image_create(cs, args):
  1037. """Create a new image by taking a snapshot of a running server."""
  1038. server = _find_server(cs, args.server)
  1039. image_uuid = cs.servers.create_image(server, args.name)
  1040. if args.poll:
  1041. _poll_for_status(cs.images.get, image_uuid, 'snapshotting',
  1042. ['active'])
  1043. # NOTE(sirp): A race-condition exists between when the image finishes
  1044. # uploading and when the servers's `task_state` is cleared. To account
  1045. # for this, we need to poll a second time to ensure the `task_state` is
  1046. # cleared before returning, ensuring that a snapshot taken immediately
  1047. # after this function returns will succeed.
  1048. #
  1049. # A better long-term solution will be to separate 'snapshotting' and
  1050. # 'image-uploading' in Nova and clear the task-state once the VM
  1051. # snapshot is complete but before the upload begins.
  1052. task_state_field = "OS-EXT-STS:task_state"
  1053. if hasattr(server, task_state_field):
  1054. _poll_for_status(cs.servers.get, server.id, 'image_snapshot',
  1055. [None], status_field=task_state_field,
  1056. show_progress=False, silent=True)
  1057. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1058. @utils.arg('name', metavar='<name>', help='Name of the backup image.')
  1059. @utils.arg('backup_type', metavar='<backup-type>',
  1060. help='The backup type, like "daily" or "weekly".')
  1061. @utils.arg('rotation', metavar='<rotation>',
  1062. help='Int parameter representing how many backups to keep around.')
  1063. def do_backup(cs, args):
  1064. """ Backup a instance by create a 'backup' type snapshot """
  1065. _find_server(cs, args.server).backup(args.name,
  1066. args.backup_type,
  1067. args.rotation)
  1068. @utils.arg('server',
  1069. metavar='<server>',
  1070. help="Name or ID of server")
  1071. @utils.arg('action',
  1072. metavar='<action>',
  1073. choices=['set', 'delete'],
  1074. help="Actions: 'set' or 'delete'")
  1075. @utils.arg('metadata',
  1076. metavar='<key=value>',
  1077. nargs='+',
  1078. action='append',
  1079. default=[],
  1080. help='Metadata to set or delete (only key is necessary on delete)')
  1081. def do_meta(cs, args):
  1082. """Set or Delete metadata on a server."""
  1083. server = _find_server(cs, args.server)
  1084. metadata = _extract_metadata(args)
  1085. if args.action == 'set':
  1086. cs.servers.set_meta(server, metadata)
  1087. elif args.action == 'delete':
  1088. cs.servers.delete_meta(server, metadata.keys())
  1089. def _print_server(cs, args):
  1090. # By default when searching via name we will do a
  1091. # findall(name=blah) and due a REST /details which is not the same
  1092. # as a .get() and doesn't get the information about flavors and
  1093. # images. This fix it as we redo the call with the id which does a
  1094. # .get() to get all informations.
  1095. server = _find_server(cs, args.server)
  1096. networks = server.networks
  1097. info = server._info.copy()
  1098. for network_label, address_list in networks.items():
  1099. info['%s network' % network_label] = ', '.join(address_list)
  1100. flavor = info.get('flavor', {})
  1101. flavor_id = flavor.get('id', '')
  1102. if args.minimal:
  1103. info['flavor'] = flavor_id
  1104. else:
  1105. info['flavor'] = '%s (%s)' % (_find_flavor(cs, flavor_id).name,
  1106. flavor_id)
  1107. image = info.get('image', {})
  1108. if image:
  1109. image_id = image.get('id', '')
  1110. if args.minimal:
  1111. info['image'] = image_id
  1112. else:
  1113. try:
  1114. info['image'] = '%s (%s)' % (_find_image(cs, image_id).name,
  1115. image_id)
  1116. except Exception:
  1117. info['image'] = '%s (%s)' % ("Image not found", image_id)
  1118. else: # Booted from volume
  1119. info['image'] = "Attempt to boot from volume - no image supplied"
  1120. info.pop('links', None)
  1121. info.pop('addresses', None)
  1122. utils.print_dict(info)
  1123. @utils.arg('--minimal',
  1124. dest='minimal',
  1125. action="store_true",
  1126. default=False,
  1127. help='Skips flavor/image lookups when showing instances')
  1128. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1129. def do_show(cs, args):
  1130. """Show details about the given server."""
  1131. _print_server(cs, args)
  1132. @utils.arg('server', metavar='<server>', nargs='+',
  1133. help='Name or ID of server(s).')
  1134. def do_delete(cs, args):
  1135. """Immediately shut down and delete specified server(s)."""
  1136. for server in args.server:
  1137. try:
  1138. _find_server(cs, server).delete()
  1139. except Exception as e:
  1140. print(e)
  1141. def _find_server(cs, server):
  1142. """Get a server by name or ID."""
  1143. return utils.find_resource(cs.servers, server)
  1144. def _find_image(cs, image):
  1145. """Get an image by name or ID."""
  1146. return utils.find_resource(cs.images, image)
  1147. def _find_flavor(cs, flavor):
  1148. """Get a flavor by name, ID, or RAM size."""
  1149. try:
  1150. return utils.find_resource(cs.flavors, flavor)
  1151. except exceptions.NotFound:
  1152. return cs.flavors.find(ram=flavor)
  1153. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1154. @utils.arg('network_id',
  1155. metavar='<network-id>',
  1156. help='Network ID.')
  1157. def do_add_fixed_ip(cs, args):
  1158. """Add new IP address on a network to server."""
  1159. server = _find_server(cs, args.server)
  1160. server.add_fixed_ip(args.network_id)
  1161. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1162. @utils.arg('address', metavar='<address>', help='IP Address.')
  1163. def do_remove_fixed_ip(cs, args):
  1164. """Remove an IP address from a server."""
  1165. server = _find_server(cs, args.server)
  1166. server.remove_fixed_ip(args.address)
  1167. def _find_volume(cs, volume):
  1168. """Get a volume by name or ID."""
  1169. return utils.find_resource(cs.volumes, volume)
  1170. def _find_volume_snapshot(cs, snapshot):
  1171. """Get a volume snapshot by name or ID."""
  1172. return utils.find_resource(cs.volume_snapshots, snapshot)
  1173. def _print_volume(volume):
  1174. utils.print_dict(volume._info)
  1175. def _print_volume_snapshot(snapshot):
  1176. utils.print_dict(snapshot._info)
  1177. def _translate_volume_keys(collection):
  1178. _translate_keys(collection,
  1179. [('displayName', 'display_name'),
  1180. ('volumeType', 'volume_type')])
  1181. def _translate_volume_snapshot_keys(collection):
  1182. _translate_keys(collection,
  1183. [('displayName', 'display_name'),
  1184. ('volumeId', 'volume_id')])
  1185. def _translate_availability_zone_keys(collection):
  1186. _translate_keys(collection,
  1187. [('zoneName', 'name'), ('zoneState', 'status')])
  1188. @utils.arg('--all-tenants',
  1189. dest='all_tenants',
  1190. metavar='<0|1>',
  1191. nargs='?',
  1192. type=int,
  1193. const=1,
  1194. default=int(utils.bool_from_str(os.environ.get("ALL_TENANTS", 'false'))),
  1195. help='Display information from all tenants (Admin only).')
  1196. @utils.arg('--all_tenants',
  1197. nargs='?',
  1198. type=int,
  1199. const=1,
  1200. help=argparse.SUPPRESS)
  1201. @utils.service_type('volume')
  1202. def do_volume_list(cs, args):
  1203. """List all the volumes."""
  1204. search_opts = {'all_tenants': args.all_tenants}
  1205. volumes = cs.volumes.list(search_opts=search_opts)
  1206. _translate_volume_keys(volumes)
  1207. # Create a list of servers to which the volume is attached
  1208. for vol in volumes:
  1209. servers = [s.get('server_id') for s in vol.attachments]
  1210. setattr(vol, 'attached_to', ','.join(map(str, servers)))
  1211. utils.print_list(volumes, ['ID', 'Status', 'Display Name',
  1212. 'Size', 'Volume Type', 'Attached to'])
  1213. @utils.arg('volume', metavar='<volume>', help='Name or ID of the volume.')
  1214. @utils.service_type('volume')
  1215. def do_volume_show(cs, args):
  1216. """Show details about a volume."""
  1217. volume = _find_volume(cs, args.volume)
  1218. _print_volume(volume)
  1219. @utils.arg('size',
  1220. metavar='<size>',
  1221. type=int,
  1222. help='Size of volume in GB')
  1223. @utils.arg('--snapshot-id',
  1224. metavar='<snapshot-id>',
  1225. default=None,
  1226. help='Optional snapshot id to create the volume from. (Default=None)')
  1227. @utils.arg('--snapshot_id',
  1228. help=argparse.SUPPRESS)
  1229. @utils.arg('--image-id',
  1230. metavar='<image-id>',
  1231. help='Optional image id to create the volume from. (Default=None)',
  1232. default=None)
  1233. @utils.arg('--display-name',
  1234. metavar='<display-name>',
  1235. default=None,
  1236. help='Optional volume name. (Default=None)')
  1237. @utils.arg('--display_name',
  1238. help=argparse.SUPPRESS)
  1239. @utils.arg('--display-description',
  1240. metavar='<display-description>',
  1241. default=None,
  1242. help='Optional volume description. (Default=None)')
  1243. @utils.arg('--display_description',
  1244. help=argparse.SUPPRESS)
  1245. @utils.arg('--volume-type',
  1246. metavar='<volume-type>',
  1247. default=None,
  1248. help='Optional volume type. (Default=None)')
  1249. @utils.arg('--volume_type',
  1250. help=argparse.SUPPRESS)
  1251. @utils.arg('--availability-zone', metavar='<availability-zone>',
  1252. help='Optional Availability Zone for volume. (Default=None)',
  1253. default=None)
  1254. @utils.service_type('volume')
  1255. def do_volume_create(cs, args):
  1256. """Add a new volume."""
  1257. volume = cs.volumes.create(args.size,
  1258. args.snapshot_id,
  1259. args.display_name,
  1260. args.display_description,
  1261. args.volume_type,
  1262. args.availability_zone,
  1263. imageRef=args.image_id)
  1264. _print_volume(volume)
  1265. @utils.arg('volume',
  1266. metavar='<volume>',
  1267. help='Name or ID of the volume to delete.')
  1268. @utils.service_type('volume')
  1269. def do_volume_delete(cs, args):
  1270. """Remove a volume."""
  1271. volume = _find_volume(cs, args.volume)
  1272. volume.delete()
  1273. @utils.arg('server',
  1274. metavar='<server>',
  1275. help='Name or ID of server.')
  1276. @utils.arg('volume',
  1277. metavar='<volume>',
  1278. help='ID of the volume to attach.')
  1279. @utils.arg('device', metavar='<device>',
  1280. help='Name of the device e.g. /dev/vdb. '
  1281. 'Use "auto" for autoassign (if supported)')
  1282. def do_volume_attach(cs, args):
  1283. """Attach a volume to a server."""
  1284. if args.device == 'auto':
  1285. args.device = None
  1286. volume = cs.volumes.create_server_volume(_find_server(cs, args.server).id,
  1287. args.volume,
  1288. args.device)
  1289. _print_volume(volume)
  1290. @utils.arg('server',
  1291. metavar='<server>',
  1292. help='Name or ID of server.')
  1293. @utils.arg('attachment_id',
  1294. metavar='<volume>',
  1295. help='Attachment ID of the volume.')
  1296. def do_volume_detach(cs, args):
  1297. """Detach a volume from a server."""
  1298. cs.volumes.delete_server_volume(_find_server(cs, args.server).id,
  1299. args.attachment_id)
  1300. @utils.service_type('volume')
  1301. def do_volume_snapshot_list(cs, _args):
  1302. """List all the snapshots."""
  1303. snapshots = cs.volume_snapshots.list()
  1304. _translate_volume_snapshot_keys(snapshots)
  1305. utils.print_list(snapshots, ['ID', 'Volume ID', 'Status', 'Display Name',
  1306. 'Size'])
  1307. @utils.arg('snapshot',
  1308. metavar='<snapshot>',
  1309. help='Name or ID of the snapshot.')
  1310. @utils.service_type('volume')
  1311. def do_volume_snapshot_show(cs, args):
  1312. """Show details about a snapshot."""
  1313. snapshot = _find_volume_snapshot(cs, args.snapshot)
  1314. _print_volume_snapshot(snapshot)
  1315. @utils.arg('volume_id',
  1316. metavar='<volume-id>',
  1317. help='ID of the volume to snapshot')
  1318. @utils.arg('--force',
  1319. metavar='<True|False>',
  1320. help='Optional flag to indicate whether to snapshot a volume even if its '
  1321. 'attached to an instance. (Default=False)',
  1322. default=False)
  1323. @utils.arg('--display-name',
  1324. metavar='<display-name>',
  1325. default=None,
  1326. help='Optional snapshot name. (Default=None)')
  1327. @utils.arg('--display_name',
  1328. help=argparse.SUPPRESS)
  1329. @utils.arg('--display-description',
  1330. metavar='<display-description>',
  1331. default=None,
  1332. help='Optional snapshot description. (Default=None)')
  1333. @utils.arg('--display_description',
  1334. help=argparse.SUPPRESS)
  1335. @utils.service_type('volume')
  1336. def do_volume_snapshot_create(cs, args):
  1337. """Add a new snapshot."""
  1338. snapshot = cs.volume_snapshots.create(args.volume_id,
  1339. args.force,
  1340. args.display_name,
  1341. args.display_description)
  1342. _print_volume_snapshot(snapshot)
  1343. @utils.arg('snapshot',
  1344. metavar='<snapshot>',
  1345. help='Name or ID of the snapshot to delete.')
  1346. @utils.service_type('volume')
  1347. def do_volume_snapshot_delete(cs, args):
  1348. """Remove a snapshot."""
  1349. snapshot = _find_volume_snapshot(cs, args.snapshot)
  1350. snapshot.delete()
  1351. def _print_volume_type_list(vtypes):
  1352. utils.print_list(vtypes, ['ID', 'Name'])
  1353. @utils.service_type('volume')
  1354. def do_volume_type_list(cs, args):
  1355. """Print a list of available 'volume types'."""
  1356. vtypes = cs.volume_types.list()
  1357. _print_volume_type_list(vtypes)
  1358. @utils.arg('name',
  1359. metavar='<name>',
  1360. help="Name of the new flavor")
  1361. @utils.service_type('volume')
  1362. def do_volume_type_create(cs, args):
  1363. """Create a new volume type."""
  1364. vtype = cs.volume_types.create(args.name)
  1365. _print_volume_type_list([vtype])
  1366. @utils.arg('id',
  1367. metavar='<id>',
  1368. help="Unique ID of the volume type to delete")
  1369. @utils.service_type('volume')
  1370. def do_volume_type_delete(cs, args):
  1371. """Delete a specific flavor"""
  1372. cs.volume_types.delete(args.id)
  1373. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1374. @utils.arg('console_type',
  1375. metavar='<console-type>',
  1376. help='Type of vnc console ("novnc" or "xvpvnc").')
  1377. def do_get_vnc_console(cs, args):
  1378. """Get a vnc console to a server."""
  1379. server = _find_server(cs, args.server)
  1380. data = server.get_vnc_console(args.console_type)
  1381. class VNCConsole:
  1382. def __init__(self, console_dict):
  1383. self.type = console_dict['type']
  1384. self.url = console_dict['url']
  1385. utils.print_list([VNCConsole(data['console'])], ['Type', 'Url'])
  1386. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1387. @utils.arg('console_type',
  1388. metavar='<console-type>',
  1389. help='Type of spice console ("spice-html5").')
  1390. def do_get_spice_console(cs, args):
  1391. """Get a spice console to a server."""
  1392. server = _find_server(cs, args.server)
  1393. data = server.get_spice_console(args.console_type)
  1394. class SPICEConsole:
  1395. def __init__(self, console_dict):
  1396. self.type = console_dict['type']
  1397. self.url = console_dict['url']
  1398. utils.print_list([SPICEConsole(data['console'])], ['Type', 'Url'])
  1399. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1400. @utils.arg('private_key',
  1401. metavar='<private-key>',
  1402. help='Private key (used locally to decrypt password).')
  1403. def do_get_password(cs, args):
  1404. """Get password for a server."""
  1405. server = _find_server(cs, args.server)
  1406. data = server.get_password(args.private_key)
  1407. print(data)
  1408. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1409. def do_clear_password(cs, args):
  1410. """Clear password for a server."""
  1411. server = _find_server(cs, args.server)
  1412. data = server.clear_password()
  1413. def _print_floating_ip_list(floating_ips):
  1414. utils.print_list(floating_ips, ['Ip', 'Instance Id', 'Fixed Ip', 'Pool'])
  1415. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1416. @utils.arg('--length',
  1417. metavar='<length>',
  1418. default=None,
  1419. help='Length in lines to tail.')
  1420. def do_console_log(cs, args):
  1421. """Get console log output of a server."""
  1422. server = _find_server(cs, args.server)
  1423. data = server.get_console_output(length=args.length)
  1424. print(data)
  1425. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1426. @utils.arg('address', metavar='<address>', help='IP Address.')
  1427. def do_add_floating_ip(cs, args):
  1428. """Add a floating IP address to a server."""
  1429. server = _find_server(cs, args.server)
  1430. server.add_floating_ip(args.address)
  1431. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1432. @utils.arg('address', metavar='<address>', help='IP Address.')
  1433. def do_remove_floating_ip(cs, args):
  1434. """Remove a floating IP address from a server."""
  1435. server = _find_server(cs, args.server)
  1436. server.remove_floating_ip(args.address)
  1437. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1438. @utils.arg('secgroup', metavar='<secgroup>', help='Name of Security Group.')
  1439. def do_add_secgroup(cs, args):
  1440. """Add a Security Group to a server."""
  1441. server = _find_server(cs, args.server)
  1442. server.add_security_group(args.secgroup)
  1443. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1444. @utils.arg('secgroup', metavar='<secgroup>', help='Name of Security Group.')
  1445. def do_remove_secgroup(cs, args):
  1446. """Remove a Security Group from a server."""
  1447. server = _find_server(cs, args.server)
  1448. server.remove_security_group(args.secgroup)
  1449. @utils.arg('pool',
  1450. metavar='<floating-ip-pool>',
  1451. help='Name of Floating IP Pool. (Optional)',
  1452. nargs='?',
  1453. default=None)
  1454. def do_floating_ip_create(cs, args):
  1455. """Allocate a floating IP for the current tenant."""
  1456. _print_floating_ip_list([cs.floating_ips.create(pool=args.pool)])
  1457. @utils.arg('address', metavar='<address>', help='IP of Floating Ip.')
  1458. def do_floating_ip_delete(cs, args):
  1459. """De-allocate a floating IP."""
  1460. floating_ips = cs.floating_ips.list()
  1461. for floating_ip in floating_ips:
  1462. if floating_ip.ip == args.address:
  1463. return cs.floating_ips.delete(floating_ip.id)
  1464. raise exceptions.CommandError("Floating ip %s not found." % args.address)
  1465. def do_floating_ip_list(cs, _args):
  1466. """List floating ips for this tenant."""
  1467. _print_floating_ip_list(cs.floating_ips.list())
  1468. def do_floating_ip_pool_list(cs, _args):
  1469. """List all floating ip pools."""
  1470. utils.print_list(cs.floating_ip_pools.list(), ['name'])
  1471. @utils.arg('--host', dest='host', metavar='<host>', default=None,
  1472. help='Filter by host')
  1473. def do_floating_ip_bulk_list(cs, args):
  1474. """List all floating ips"""
  1475. utils.print_list(cs.floating_ips_bulk.list(args.host), ['project_id',
  1476. 'address',
  1477. 'instance_uuid',
  1478. 'pool',
  1479. 'interface'])
  1480. @utils.arg('ip_range', metavar='<range>', help='Address range to create')
  1481. @utils.arg('--pool', dest='pool', metavar='<pool>', default=None,
  1482. help='Pool for new Floating IPs')
  1483. @utils.arg('--interface', metavar='<interface>', default=None,
  1484. help='Interface for new Floating IPs')
  1485. def do_floating_ip_bulk_create(cs, args):
  1486. """Bulk create floating ips by range"""
  1487. cs.floating_ips_bulk.create(args.ip_range, args.pool, args.interface)
  1488. @utils.arg('ip_range', metavar='<range>', help='Address range to delete')
  1489. def do_floating_ip_bulk_delete(cs, args):
  1490. """Bulk delete floating ips by range"""
  1491. cs.floating_ips_bulk.delete(args.ip_range)
  1492. def _print_dns_list(dns_entries):
  1493. utils.print_list(dns_entries, ['ip', 'name', 'domain'])
  1494. def _print_domain_list(domain_entries):
  1495. utils.print_list(domain_entries, ['domain', 'scope',
  1496. 'project', 'availability_zone'])
  1497. def do_dns_domains(cs, args):
  1498. """Print a list of available dns domains."""
  1499. domains = cs.dns_domains.domains()
  1500. _print_domain_list(domains)
  1501. @utils.arg('domain', metavar='<domain>', help='DNS domain')
  1502. @utils.arg('--ip', metavar='<ip>', help='ip address', default=None)
  1503. @utils.arg('--name', metavar='<name>', help='DNS name', default=None)
  1504. def do_dns_list(cs, args):
  1505. """List current DNS entries for domain and ip or domain and name."""
  1506. if not (args.ip or args.name):
  1507. raise exceptions.CommandError(
  1508. "You must specify either --ip or --name")
  1509. if args.name:
  1510. entry = cs.dns_entries.get(args.domain, args.name)
  1511. _print_dns_list([entry])
  1512. else:
  1513. entries = cs.dns_entries.get_for_ip(args.domain,
  1514. ip=args.ip)
  1515. _print_dns_list(entries)
  1516. @utils.arg('ip', metavar='<ip>', help='ip address')
  1517. @utils.arg('name', metavar='<name>', help='DNS name')
  1518. @utils.arg('domain', metavar='<domain>', help='DNS domain')
  1519. @utils.arg('--type', metavar='<type>', help='dns type (e.g. "A")', default='A')
  1520. def do_dns_create(cs, args):
  1521. """Create a DNS entry for domain, name and ip."""
  1522. cs.dns_entries.create(args.domain, args.name, args.ip, args.type)
  1523. @utils.arg('domain', metavar='<domain>', help='DNS domain')
  1524. @utils.arg('name', metavar='<name>', help='DNS name')
  1525. def do_dns_delete(cs, args):
  1526. """Delete the specified DNS entry."""
  1527. cs.dns_entries.delete(args.domain, args.name)
  1528. @utils.arg('domain', metavar='<domain>', help='DNS domain')
  1529. def do_dns_delete_domain(cs, args):
  1530. """Delete the specified DNS domain."""
  1531. cs.dns_domains.delete(args.domain)
  1532. @utils.arg('domain', metavar='<domain>', help='DNS domain')
  1533. @utils.arg('--availability-zone',
  1534. metavar='<availability-zone>',
  1535. default=None,
  1536. help='Limit access to this domain to instances '
  1537. 'in the specified availability zone.')
  1538. @utils.arg('--availability_zone',
  1539. help=argparse.SUPPRESS)
  1540. def do_dns_create_private_domain(cs, args):
  1541. """Create the specified DNS domain."""
  1542. cs.dns_domains.create_private(args.domain,
  1543. args.availability_zone)
  1544. @utils.arg('domain', metavar='<domain>', help='DNS domain')
  1545. @utils.arg('--project', metavar='<project>',
  1546. help='Limit access to this domain to users '
  1547. 'of the specified project.',
  1548. default=None)
  1549. def do_dns_create_public_domain(cs, args):
  1550. """Create the specified DNS domain."""
  1551. cs.dns_domains.create_public(args.domain,
  1552. args.project)
  1553. def _print_secgroup_rules(rules):
  1554. class FormattedRule:
  1555. def __init__(self, obj):
  1556. items = (obj if isinstance(obj, dict) else obj._info).items()
  1557. for k, v in items:
  1558. if k == 'ip_range':
  1559. v = v.get('cidr')
  1560. elif k == 'group':
  1561. k = 'source_group'
  1562. v = v.get('name')
  1563. if v is None:
  1564. v = ''
  1565. setattr(self, k, v)
  1566. rules = [FormattedRule(rule) for rule in rules]
  1567. utils.print_list(rules, ['IP Protocol', 'From Port', 'To Port',
  1568. 'IP Range', 'Source Group'])
  1569. def _print_secgroups(secgroups):
  1570. utils.print_list(secgroups, ['Name', 'Description'])
  1571. def _get_secgroup(cs, secgroup):
  1572. match_found = False
  1573. for s in cs.security_groups.list():
  1574. encoding = (locale.getpreferredencoding() or
  1575. sys.stdin.encoding or
  1576. 'UTF-8')
  1577. s.name = s.name.encode(encoding)
  1578. if secgroup == s.name:
  1579. if match_found != False:
  1580. msg = ("Multiple security group matches found for name"
  1581. " '%s', use an ID to be more specific." % secgroup)
  1582. raise exceptions.NoUniqueMatch(msg)
  1583. match_found = s
  1584. if match_found is False:
  1585. raise exceptions.CommandError("Secgroup %s not found" % secgroup)
  1586. return match_found
  1587. @utils.arg('secgroup', metavar='<secgroup>', help='ID of security group.')
  1588. @utils.arg('ip_proto',
  1589. metavar='<ip-proto>',
  1590. help='IP protocol (icmp, tcp, udp).')
  1591. @utils.arg('from_port',
  1592. metavar='<from-port>',
  1593. help='Port at start of range.')
  1594. @utils.arg('to_port',
  1595. metavar='<to-port>',
  1596. help='Port at end of range.')
  1597. @utils.arg('cidr', metavar='<cidr>', help='CIDR for address range.')
  1598. def do_secgroup_add_rule(cs, args):
  1599. """Add a rule to a security group."""
  1600. secgroup = _get_secgroup(cs, args.secgroup)
  1601. rule = cs.security_group_rules.create(secgroup.id,
  1602. args.ip_proto,
  1603. args.from_port,
  1604. args.to_port,
  1605. args.cidr)
  1606. _print_secgroup_rules([rule])
  1607. @utils.arg('secgroup', metavar='<secgroup>', help='ID of security group.')
  1608. @utils.arg('ip_proto',
  1609. metavar='<ip-proto>',
  1610. help='IP protocol (icmp, tcp, udp).')
  1611. @utils.arg('from_port',
  1612. metavar='<from-port>',
  1613. help='Port at start of range.')
  1614. @utils.arg('to_port',
  1615. metavar='<to-port>',
  1616. help='Port at end of range.')
  1617. @utils.arg('cidr', metavar='<cidr>', help='CIDR for address range.')
  1618. def do_secgroup_delete_rule(cs, args):
  1619. """Delete a rule from a security group."""
  1620. secgroup = _get_secgroup(cs, args.secgroup)
  1621. for rule in secgroup.rules:
  1622. if (rule['ip_protocol'] and
  1623. rule['ip_protocol'].upper() == args.ip_proto.upper() and
  1624. rule['from_port'] == int(args.from_port) and
  1625. rule['to_port'] == int(args.to_port) and
  1626. rule['ip_range']['cidr'] == args.cidr):
  1627. return cs.security_group_rules.delete(rule['id'])
  1628. raise exceptions.CommandError("Rule not found")
  1629. @utils.arg('name', metavar='<name>', help='Name of security group.')
  1630. @utils.arg('description', metavar='<description>',
  1631. help='Description of security group.')
  1632. def do_secgroup_create(cs, args):
  1633. """Create a security group."""
  1634. _print_secgroups([cs.security_groups.create(args.name, args.description)])
  1635. @utils.arg('secgroup', metavar='<secgroup>', help='Name of security group.')
  1636. def do_secgroup_delete(cs, args):
  1637. """Delete a security group."""
  1638. cs.security_groups.delete(_get_secgroup(cs, args.secgroup))
  1639. @utils.arg('--all-tenants',
  1640. dest='all_tenants',
  1641. metavar='<0|1>',
  1642. nargs='?',
  1643. type=int,
  1644. const=1,
  1645. default=int(utils.bool_from_str(os.environ.get("ALL_TENANTS", 'false'))),
  1646. help='Display information from all tenants (Admin only).')
  1647. @utils.arg('--all_tenants',
  1648. nargs='?',
  1649. type=int,
  1650. const=1,
  1651. help=argparse.SUPPRESS)
  1652. def do_secgroup_list(cs, args):
  1653. """List security groups for the current tenant."""
  1654. search_opts = {'all_tenants': args.all_tenants}
  1655. columns = ['Name', 'Description']
  1656. if args.all_tenants:
  1657. columns.append('Tenant_ID')
  1658. groups = cs.security_groups.list(search_opts=search_opts)
  1659. utils.print_list(groups, columns)
  1660. @utils.arg('secgroup', metavar='<secgroup>', help='Name of security group.')
  1661. def do_secgroup_list_rules(cs, args):
  1662. """List rules for a security group."""
  1663. secgroup = _get_secgroup(cs, args.secgroup)
  1664. _print_secgroup_rules(secgroup.rules)
  1665. @utils.arg('secgroup', metavar='<secgroup>', help='ID of security group.')
  1666. @utils.arg('source_group',
  1667. metavar='<source-group>',
  1668. help='ID of source group.')
  1669. @utils.arg('ip_proto',
  1670. metavar='<ip-proto>',
  1671. help='IP protocol (icmp, tcp, udp).')
  1672. @utils.arg('from_port',
  1673. metavar='<from-port>',
  1674. help='Port at start of range.')
  1675. @utils.arg('to_port',
  1676. metavar='<to-port>',
  1677. help='Port at end of range.')
  1678. def do_secgroup_add_group_rule(cs, args):
  1679. """Add a source group rule to a security group."""
  1680. secgroup = _get_secgroup(cs, args.secgroup)
  1681. source_group = _get_secgroup(cs, args.source_group)
  1682. params = {}
  1683. params['group_id'] = source_group.id
  1684. if args.ip_proto or args.from_port or args.to_port:
  1685. if not (args.ip_proto and args.from_port and args.to_port):
  1686. raise exceptions.CommandError("ip_proto, from_port, and to_port"
  1687. " must be specified together")
  1688. params['ip_protocol'] = args.ip_proto.upper()
  1689. params['from_port'] = args.from_port
  1690. params['to_port'] = args.to_port
  1691. rule = cs.security_group_rules.create(secgroup.id, **params)
  1692. _print_secgroup_rules([rule])
  1693. @utils.arg('secgroup', metavar='<secgroup>', help='ID of security group.')
  1694. @utils.arg('source_group',
  1695. metavar='<source-group>',
  1696. help='ID of source group.')
  1697. @utils.arg('ip_proto',
  1698. metavar='<ip-proto>',
  1699. help='IP protocol (icmp, tcp, udp).')
  1700. @utils.arg('from_port',
  1701. metavar='<from-port>',
  1702. help='Port at start of range.')
  1703. @utils.arg('to_port',
  1704. metavar='<to-port>',
  1705. help='Port at end of range.')
  1706. def do_secgroup_delete_group_rule(cs, args):
  1707. """Delete a source group rule from a security group."""
  1708. secgroup = _get_secgroup(cs, args.secgroup)
  1709. source_group = _get_secgroup(cs, args.source_group)
  1710. params = {}
  1711. params['group_name'] = source_group.name
  1712. if args.ip_proto or args.from_port or args.to_port:
  1713. if not (args.ip_proto and args.from_port and args.to_port):
  1714. raise exceptions.CommandError("ip_proto, from_port, and to_port"
  1715. " must be specified together")
  1716. params['ip_protocol'] = args.ip_proto.upper()
  1717. params['from_port'] = int(args.from_port)
  1718. params['to_port'] = int(args.to_port)
  1719. for rule in secgroup.rules:
  1720. if (rule.get('ip_protocol').upper() == params.get(
  1721. 'ip_protocol').upper() and
  1722. rule.get('from_port') == params.get('from_port') and
  1723. rule.get('to_port') == params.get('to_port') and
  1724. rule.get('group', {}).get('name') ==
  1725. params.get('group_name')):
  1726. return cs.security_group_rules.delete(rule['id'])
  1727. raise exceptions.CommandError("Rule not found")
  1728. @utils.arg('name', metavar='<name>', help='Name of key.')
  1729. @utils.arg('--pub-key',
  1730. metavar='<pub-key>',
  1731. default=None,
  1732. help='Path to a public ssh key.')
  1733. @utils.arg('--pub_key',
  1734. help=argparse.SUPPRESS)
  1735. def do_keypair_add(cs, args):
  1736. """Create a new key pair for use with instances"""
  1737. name = args.name
  1738. pub_key = args.pub_key
  1739. if pub_key:
  1740. try:
  1741. with open(os.path.expanduser(pub_key)) as f:
  1742. pub_key = f.read()
  1743. except IOError as e:
  1744. raise exceptions.CommandError("Can't open or read '%s': %s" %
  1745. (pub_key, e))
  1746. keypair = cs.keypairs.create(name, pub_key)
  1747. if not pub_key:
  1748. private_key = keypair.private_key
  1749. print(private_key)
  1750. @utils.arg('name', metavar='<name>', help='Keypair name to delete.')
  1751. def do_keypair_delete(cs, args):
  1752. """Delete keypair by its name"""
  1753. name = args.name
  1754. cs.keypairs.delete(name)
  1755. def do_keypair_list(cs, args):
  1756. """Print a list of keypairs for a user"""
  1757. keypairs = cs.keypairs.list()
  1758. columns = ['Name', 'Fingerprint']
  1759. utils.print_list(keypairs, columns)
  1760. @utils.arg('--reserved',
  1761. dest='reserved',
  1762. action='store_true',
  1763. default=False,
  1764. help='Include reservations count.')
  1765. def do_absolute_limits(cs, args):
  1766. """Print a list of absolute limits for a user"""
  1767. limits = cs.limits.get(args.reserved).absolute
  1768. columns = ['Name', 'Value']
  1769. utils.print_list(limits, columns)
  1770. def do_rate_limits(cs, args):
  1771. """Print a list of rate limits for a user"""
  1772. limits = cs.limits.get().rate
  1773. columns = ['Verb', 'URI', 'Value', 'Remain', 'Unit', 'Next_Available']
  1774. utils.print_list(limits, columns)
  1775. @utils.arg('--start', metavar='<start>',
  1776. help='Usage range start date ex 2012-01-20 (default: 4 weeks ago)',
  1777. default=None)
  1778. @utils.arg('--end', metavar='<end>',
  1779. help='Usage range end date, ex 2012-01-20 (default: tomorrow) ',
  1780. default=None)
  1781. def do_usage_list(cs, args):
  1782. """List usage data for all tenants"""
  1783. dateformat = "%Y-%m-%d"
  1784. rows = ["Tenant ID", "Instances", "RAM MB-Hours", "CPU Hours",
  1785. "Disk GB-Hours"]
  1786. now = timeutils.utcnow()
  1787. if args.start:
  1788. start = datetime.datetime.strptime(args.start, dateformat)
  1789. else:
  1790. start = now - datetime.timedelta(weeks=4)
  1791. if args.end:
  1792. end = datetime.datetime.strptime(args.end, dateformat)
  1793. else:
  1794. end = now + datetime.timedelta(days=1)
  1795. def simplify_usage(u):
  1796. simplerows = map(lambda x: x.lower().replace(" ", "_"), rows)
  1797. setattr(u, simplerows[0], u.tenant_id)
  1798. setattr(u, simplerows[1], "%d" % len(u.server_usages))
  1799. setattr(u, simplerows[2], "%.2f" % u.total_memory_mb_usage)
  1800. setattr(u, simplerows[3], "%.2f" % u.total_vcpus_usage)
  1801. setattr(u, simplerows[4], "%.2f" % u.total_local_gb_usage)
  1802. usage_list = cs.usage.list(start, end, detailed=True)
  1803. print("Usage from %s to %s:" % (start.strftime(dateformat),
  1804. end.strftime(dateformat)))
  1805. for usage in usage_list:
  1806. simplify_usage(usage)
  1807. utils.print_list(usage_list, rows)
  1808. @utils.arg('--start', metavar='<start>',
  1809. help='Usage range start date ex 2012-01-20 (default: 4 weeks ago)',
  1810. default=None)
  1811. @utils.arg('--end', metavar='<end>',
  1812. help='Usage range end date, ex 2012-01-20 (default: tomorrow) ',
  1813. default=None)
  1814. @utils.arg('--tenant', metavar='<tenant-id>',
  1815. default=None,
  1816. help='UUID or name of tenant to get usage for.')
  1817. def do_usage(cs, args):
  1818. """Show usage data for a single tenant"""
  1819. dateformat = "%Y-%m-%d"
  1820. rows = ["Instances", "RAM MB-Hours", "CPU Hours", "Disk GB-Hours"]
  1821. now = timeutils.utcnow()
  1822. if args.start:
  1823. start = datetime.datetime.strptime(args.start, dateformat)
  1824. else:
  1825. start = now - datetime.timedelta(weeks=4)
  1826. if args.end:
  1827. end = datetime.datetime.strptime(args.end, dateformat)
  1828. else:
  1829. end = now + datetime.timedelta(days=1)
  1830. def simplify_usage(u):
  1831. simplerows = map(lambda x: x.lower().replace(" ", "_"), rows)
  1832. setattr(u, simplerows[0], "%d" % len(u.server_usages))
  1833. setattr(u, simplerows[1], "%.2f" % u.total_memory_mb_usage)
  1834. setattr(u, simplerows[2], "%.2f" % u.total_vcpus_usage)
  1835. setattr(u, simplerows[3], "%.2f" % u.total_local_gb_usage)
  1836. if args.tenant:
  1837. usage = cs.usage.get(args.tenant, start, end)
  1838. else:
  1839. usage = cs.usage.get(cs.client.tenant_id, start, end)
  1840. print("Usage from %s to %s:" % (start.strftime(dateformat),
  1841. end.strftime(dateformat)))
  1842. if getattr(usage, 'total_vcpus_usage', None):
  1843. simplify_usage(usage)
  1844. utils.print_list([usage], rows)
  1845. else:
  1846. print('None')
  1847. @utils.arg('pk_filename',
  1848. metavar='<private-key-filename>',
  1849. nargs='?',
  1850. default='pk.pem',
  1851. help='Filename for the private key [Default: pk.pem]')
  1852. @utils.arg('cert_filename',
  1853. metavar='<x509-cert-filename>',
  1854. nargs='?',
  1855. default='cert.pem',
  1856. help='Filename for the X.509 certificate [Default: cert.pem]')
  1857. def do_x509_create_cert(cs, args):
  1858. """Create x509 cert for a user in tenant"""
  1859. if os.path.exists(args.pk_filename):
  1860. raise exceptions.CommandError("Unable to write privatekey - %s exists."
  1861. % args.pk_filename)
  1862. if os.path.exists(args.cert_filename):
  1863. raise exceptions.CommandError("Unable to write x509 cert - %s exists."
  1864. % args.cert_filename)
  1865. certs = cs.certs.create()
  1866. try:
  1867. old_umask = os.umask(0o377)
  1868. with open(args.pk_filename, 'w') as private_key:
  1869. private_key.write(certs.private_key)
  1870. print("Wrote private key to %s" % args.pk_filename)
  1871. finally:
  1872. os.umask(old_umask)
  1873. with open(args.cert_filename, 'w') as cert:
  1874. cert.write(certs.data)
  1875. print("Wrote x509 certificate to %s" % args.cert_filename)
  1876. @utils.arg('filename',
  1877. metavar='<filename>',
  1878. nargs='?',
  1879. default='cacert.pem',
  1880. help='Filename to write the x509 root cert.')
  1881. def do_x509_get_root_cert(cs, args):
  1882. """Fetches the x509 root cert."""
  1883. if os.path.exists(args.filename):
  1884. raise exceptions.CommandError("Unable to write x509 root cert - \
  1885. %s exists." % args.filename)
  1886. with open(args.filename, 'w') as cert:
  1887. cacert = cs.certs.get()
  1888. cert.write(cacert.data)
  1889. print("Wrote x509 root cert to %s" % args.filename)
  1890. @utils.arg('--hypervisor', metavar='<hypervisor>', default=None,
  1891. help='type of hypervisor.')
  1892. def do_agent_list(cs, args):
  1893. """List all builds"""
  1894. result = cs.agents.list(args.hypervisor)
  1895. columns = ["Agent_id", "Hypervisor", "OS", "Architecture", "Version",
  1896. 'Md5hash', 'Url']
  1897. utils.print_list(result, columns)
  1898. @utils.arg('os', metavar='<os>', help='type of os.')
  1899. @utils.arg('architecture', metavar='<architecture>',
  1900. help='type of architecture')
  1901. @utils.arg('version', metavar='<version>', help='version')
  1902. @utils.arg('url', metavar='<url>', help='url')
  1903. @utils.arg('md5hash', metavar='<md5hash>', help='md5 hash')
  1904. @utils.arg('hypervisor', metavar='<hypervisor>', default='xen',
  1905. help='type of hypervisor.')
  1906. def do_agent_create(cs, args):
  1907. """Creates a new agent build."""
  1908. result = cs.agents.create(args.os, args.architecture,
  1909. args.version, args.url,
  1910. args.md5hash, args.hypervisor)
  1911. utils.print_dict(result._info.copy())
  1912. @utils.arg('id', metavar='<id>', help='id of the agent-build')
  1913. def do_agent_delete(cs, args):
  1914. """Deletes an existing agent build."""
  1915. cs.agents.delete(args.id)
  1916. @utils.arg('id', metavar='<id>', help='id of the agent-build')
  1917. @utils.arg('version', metavar='<version>', help='version')
  1918. @utils.arg('url', metavar='<url>', help='url')
  1919. @utils.arg('md5hash', metavar='<md5hash>', help='md5hash')
  1920. def do_agent_modify(cs, args):
  1921. """Modify an existing agent build."""
  1922. result = cs.agents.update(args.id, args.version,
  1923. args.url, args.md5hash)
  1924. utils.print_dict(result._info)
  1925. def do_aggregate_list(cs, args):
  1926. """Print a list of all aggregates."""
  1927. aggregates = cs.aggregates.list()
  1928. columns = ['Id', 'Name', 'Availability Zone']
  1929. utils.print_list(aggregates, columns)
  1930. @utils.arg('name', metavar='<name>', help='Name of aggregate.')
  1931. @utils.arg('availability_zone',
  1932. metavar='<availability-zone>',
  1933. default=None,
  1934. nargs='?',
  1935. help='The availability zone of the aggregate (optional).')
  1936. def do_aggregate_create(cs, args):
  1937. """Create a new aggregate with the specified details."""
  1938. aggregate = cs.aggregates.create(args.name, args.availability_zone)
  1939. _print_aggregate_details(aggregate)
  1940. @utils.arg('id', metavar='<id>', help='Aggregate id to delete.')
  1941. def do_aggregate_delete(cs, args):
  1942. """Delete the aggregate by its id."""
  1943. cs.aggregates.delete(args.id)
  1944. print("Aggregate %s has been successfully deleted." % args.id)
  1945. @utils.arg('id', metavar='<id>', help='Aggregate id to update.')
  1946. @utils.arg('name', metavar='<name>', help='Name of aggregate.')
  1947. @utils.arg('availability_zone',
  1948. metavar='<availability-zone>',
  1949. nargs='?',
  1950. default=None,
  1951. help='The availability zone of the aggregate.')
  1952. def do_aggregate_update(cs, args):
  1953. """Update the aggregate's name and optionally availability zone."""
  1954. updates = {"name": args.name}
  1955. if args.availability_zone:
  1956. updates["availability_zone"] = args.availability_zone
  1957. aggregate = cs.aggregates.update(args.id, updates)
  1958. print("Aggregate %s has been successfully updated." % args.id)
  1959. _print_aggregate_details(aggregate)
  1960. @utils.arg('id', metavar='<id>', help='Aggregate id to update.')
  1961. @utils.arg('metadata',
  1962. metavar='<key=value>',
  1963. nargs='+',
  1964. action='append',
  1965. default=[],
  1966. help='Metadata to add/update to aggregate')
  1967. def do_aggregate_set_metadata(cs, args):
  1968. """Update the metadata associated with the aggregate."""
  1969. metadata = _extract_metadata(args)
  1970. aggregate = cs.aggregates.set_metadata(args.id, metadata)
  1971. print("Aggregate %s has been successfully updated." % args.id)
  1972. _print_aggregate_details(aggregate)
  1973. @utils.arg('id', metavar='<id>', help='Aggregate id.')
  1974. @utils.arg('host', metavar='<host>', help='The host to add to the aggregate.')
  1975. def do_aggregate_add_host(cs, args):
  1976. """Add the host to the specified aggregate."""
  1977. aggregate = cs.aggregates.add_host(args.id, args.host)
  1978. print("Aggregate %s has been successfully updated." % args.id)
  1979. _print_aggregate_details(aggregate)
  1980. @utils.arg('id', metavar='<id>', help='Aggregate id.')
  1981. @utils.arg('host', metavar='<host>',
  1982. help='The host to remove from the aggregate.')
  1983. def do_aggregate_remove_host(cs, args):
  1984. """Remove the specified host from the specified aggregate."""
  1985. aggregate = cs.aggregates.remove_host(args.id, args.host)
  1986. print("Aggregate %s has been successfully updated." % args.id)
  1987. _print_aggregate_details(aggregate)
  1988. @utils.arg('id', metavar='<id>', help='Aggregate id.')
  1989. def do_aggregate_details(cs, args):
  1990. """Show details of the specified aggregate."""
  1991. _print_aggregate_details(cs.aggregates.get_details(args.id))
  1992. def _print_aggregate_details(aggregate):
  1993. columns = ['Id', 'Name', 'Availability Zone', 'Hosts', 'Metadata']
  1994. utils.print_list([aggregate], columns)
  1995. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  1996. @utils.arg('host', metavar='<host>', default=None, nargs='?',
  1997. help='destination host name.')
  1998. @utils.arg('--block-migrate',
  1999. action='store_true',
  2000. dest='block_migrate',
  2001. default=False,
  2002. help='True in case of block_migration.\
  2003. (Default=False:live_migration)')
  2004. @utils.arg('--block_migrate',
  2005. action='store_true',
  2006. help=argparse.SUPPRESS)
  2007. @utils.arg('--disk-over-commit',
  2008. action='store_true',
  2009. dest='disk_over_commit',
  2010. default=False,
  2011. help='Allow overcommit.(Default=False)')
  2012. @utils.arg('--disk_over_commit',
  2013. action='store_true',
  2014. help=argparse.SUPPRESS)
  2015. def do_live_migration(cs, args):
  2016. """Migrates a running instance to a new machine."""
  2017. _find_server(cs, args.server).live_migrate(args.host,
  2018. args.block_migrate,
  2019. args.disk_over_commit)
  2020. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  2021. @utils.arg('--active', action='store_const', dest='state',
  2022. default='error', const='active',
  2023. help='Request the instance be reset to "active" state instead '
  2024. 'of "error" state (the default).')
  2025. def do_reset_state(cs, args):
  2026. """Reset the state of an instance"""
  2027. _find_server(cs, args.server).reset_state(args.state)
  2028. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  2029. def do_reset_network(cs, args):
  2030. """Reset network of an instance."""
  2031. _find_server(cs, args.server).reset_network()
  2032. @utils.arg('--host', metavar='<hostname>', default=None,
  2033. help='Name of host.')
  2034. @utils.arg('--binary', metavar='<binary>', default=None,
  2035. help='Service binary.')
  2036. def do_service_list(cs, args):
  2037. """Show a list of all running services. Filter by host & binary."""
  2038. result = cs.services.list(host=args.host, binary=args.binary)
  2039. columns = ["Binary", "Host", "Zone", "Status", "State", "Updated_at"]
  2040. utils.print_list(result, columns)
  2041. @utils.arg('host', metavar='<hostname>', help='Name of host.')
  2042. @utils.arg('binary', metavar='<binary>', help='Service binary.')
  2043. def do_service_enable(cs, args):
  2044. """Enable the service"""
  2045. result = cs.services.enable(args.host, args.binary)
  2046. utils.print_list([result], ['Host', 'Binary', 'Status'])
  2047. @utils.arg('host', metavar='<hostname>', help='Name of host.')
  2048. @utils.arg('binary', metavar='<binary>', help='Service binary.')
  2049. def do_service_disable(cs, args):
  2050. """Disable the service"""
  2051. result = cs.services.disable(args.host, args.binary)
  2052. utils.print_list([result], ['Host', 'Binary', 'Status'])
  2053. @utils.arg('fixed_ip', metavar='<fixed_ip>', help='Fixed IP Address.')
  2054. def do_fixed_ip_get(cs, args):
  2055. """Get info on a fixed ip"""
  2056. result = cs.fixed_ips.get(args.fixed_ip)
  2057. utils.print_list([result], ['address', 'cidr', 'hostname', 'host'])
  2058. @utils.arg('fixed_ip', metavar='<fixed_ip>', help='Fixed IP Address.')
  2059. def do_fixed_ip_reserve(cs, args):
  2060. """Reserve a fixed ip"""
  2061. cs.fixed_ips.reserve(args.fixed_ip)
  2062. @utils.arg('fixed_ip', metavar='<fixed_ip>', help='Fixed IP Address.')
  2063. def do_fixed_ip_unreserve(cs, args):
  2064. """Unreserve a fixed ip"""
  2065. cs.fixed_ips.unreserve(args.fixed_ip)
  2066. @utils.arg('host', metavar='<hostname>', help='Name of host.')
  2067. def do_host_describe(cs, args):
  2068. """Describe a specific host"""
  2069. result = cs.hosts.get(args.host)
  2070. columns = ["HOST", "PROJECT", "cpu", "memory_mb", "disk_gb"]
  2071. utils.print_list(result, columns)
  2072. @utils.arg('--zone', metavar='<zone>', default=None,
  2073. help='Filters the list, returning only those '
  2074. 'hosts in the availability zone <zone>.')
  2075. def do_host_list(cs, args):
  2076. """List all hosts by service"""
  2077. columns = ["host_name", "service", "zone"]
  2078. result = cs.hosts.list_all(args.zone)
  2079. utils.print_list(result, columns)
  2080. @utils.arg('host', metavar='<hostname>', help='Name of host.')
  2081. @utils.arg('--status', metavar='<enable|disable>', default=None, dest='status',
  2082. help='Either enable or disable a host.')
  2083. @utils.arg('--maintenance',
  2084. metavar='<enable|disable>',
  2085. default=None,
  2086. dest='maintenance',
  2087. help='Either put or resume host to/from maintenance.')
  2088. def do_host_update(cs, args):
  2089. """Update host settings."""
  2090. updates = {}
  2091. columns = ["HOST"]
  2092. if args.status:
  2093. updates['status'] = args.status
  2094. columns.append("status")
  2095. if args.maintenance:
  2096. updates['maintenance_mode'] = args.maintenance
  2097. columns.append("maintenance_mode")
  2098. result = cs.hosts.update(args.host, updates)
  2099. utils.print_list([result], columns)
  2100. @utils.arg('host', metavar='<hostname>', help='Name of host.')
  2101. @utils.arg('--action', metavar='<action>', dest='action',
  2102. choices=['startup', 'shutdown', 'reboot'],
  2103. help='A power action: startup, reboot, or shutdown.')
  2104. def do_host_action(cs, args):
  2105. """Perform a power action on a host."""
  2106. result = cs.hosts.host_action(args.host, args.action)
  2107. utils.print_list([result], ['HOST', 'power_action'])
  2108. @utils.arg('--combine',
  2109. dest='combine',
  2110. action="store_true",
  2111. default=False,
  2112. help='Generate a single report for all services.')
  2113. def do_coverage_start(cs, args):
  2114. """Start Nova coverage reporting"""
  2115. cs.coverage.start(combine=args.combine)
  2116. print("Coverage collection started")
  2117. def do_coverage_stop(cs, args):
  2118. """Stop Nova coverage reporting"""
  2119. out = cs.coverage.stop()
  2120. print("Coverage data file path: %s" % out[-1]['path'])
  2121. @utils.arg('filename', metavar='<filename>', help='report filename')
  2122. @utils.arg('--html',
  2123. dest='html',
  2124. action="store_true",
  2125. default=False,
  2126. help='Generate HTML reports instead of text ones.')
  2127. @utils.arg('--xml',
  2128. dest='xml',
  2129. action="store_true",
  2130. default=False,
  2131. help='Generate XML reports instead of text ones.')
  2132. def do_coverage_report(cs, args):
  2133. """Generate a coverage report"""
  2134. if args.html == True and args.xml == True:
  2135. raise exceptions.CommandError("--html and --xml must not be "
  2136. "specified together.")
  2137. cov = cs.coverage.report(args.filename, xml=args.xml, html=args.html)
  2138. print("Report path: %s" % cov[-1]['path'])
  2139. def do_coverage_reset(cs, args):
  2140. """Reset coverage data."""
  2141. cs.coverage.reset()
  2142. print("Coverage data reset")
  2143. @utils.arg('--matching', metavar='<hostname>', default=None,
  2144. help='List hypervisors matching the given <hostname>.')
  2145. def do_hypervisor_list(cs, args):
  2146. """List hypervisors."""
  2147. columns = ['ID', 'Hypervisor hostname']
  2148. if args.matching:
  2149. utils.print_list(cs.hypervisors.search(args.matching), columns)
  2150. else:
  2151. # Since we're not outputting detail data, choose
  2152. # detailed=False for server-side efficiency
  2153. utils.print_list(cs.hypervisors.list(False), columns)
  2154. @utils.arg('hostname', metavar='<hostname>',
  2155. help='The hypervisor hostname (or pattern) to search for.')
  2156. def do_hypervisor_servers(cs, args):
  2157. """List instances belonging to specific hypervisors."""
  2158. hypers = cs.hypervisors.search(args.hostname, servers=True)
  2159. class InstanceOnHyper(object):
  2160. def __init__(self, **kwargs):
  2161. self.__dict__.update(kwargs)
  2162. # Massage the result into a list to be displayed
  2163. instances = []
  2164. for hyper in hypers:
  2165. hyper_host = hyper.hypervisor_hostname
  2166. hyper_id = hyper.id
  2167. if hasattr(hyper, 'servers'):
  2168. instances.extend([InstanceOnHyper(id=serv['uuid'],
  2169. name=serv['name'],
  2170. hypervisor_hostname=hyper_host,
  2171. hypervisor_id=hyper_id)
  2172. for serv in hyper.servers])
  2173. # Output the data
  2174. utils.print_list(instances, ['ID', 'Name', 'Hypervisor ID',
  2175. 'Hypervisor Hostname'])
  2176. @utils.arg('hypervisor_id',
  2177. metavar='<hypervisor-id>',
  2178. help='The ID of the hypervisor to show the details of.')
  2179. def do_hypervisor_show(cs, args):
  2180. """Display the details of the specified hypervisor."""
  2181. hyper = utils.find_resource(cs.hypervisors, args.hypervisor_id)
  2182. # Build up the dict
  2183. info = hyper._info.copy()
  2184. info['service_id'] = info['service']['id']
  2185. info['service_host'] = info['service']['host']
  2186. del info['service']
  2187. utils.print_dict(info)
  2188. @utils.arg('hypervisor_id',
  2189. metavar='<hypervisor-id>',
  2190. help='The ID of the hypervisor to show the uptime of.')
  2191. def do_hypervisor_uptime(cs, args):
  2192. """Display the uptime of the specified hypervisor."""
  2193. hyper = cs.hypervisors.uptime(args.hypervisor_id)
  2194. # Output the uptime information
  2195. utils.print_dict(hyper._info.copy())
  2196. def do_hypervisor_stats(cs, args):
  2197. """Get hypervisor statistics over all compute nodes."""
  2198. stats = cs.hypervisors.statistics()
  2199. utils.print_dict(stats._info.copy())
  2200. def ensure_service_catalog_present(cs):
  2201. if not hasattr(cs.client, 'service_catalog'):
  2202. # Turn off token caching and re-auth
  2203. cs.client.unauthenticate()
  2204. cs.client.use_token_cache(False)
  2205. cs.client.authenticate()
  2206. def do_endpoints(cs, _args):
  2207. """Discover endpoints that get returned from the authenticate services"""
  2208. ensure_service_catalog_present(cs)
  2209. catalog = cs.client.service_catalog.catalog
  2210. for e in catalog['access']['serviceCatalog']:
  2211. utils.print_dict(e['endpoints'][0], e['name'])
  2212. @utils.arg('--wrap', dest='wrap', metavar='<integer>', default=64,
  2213. help='wrap PKI tokens to a specified length, or 0 to disable')
  2214. def do_credentials(cs, _args):
  2215. """Show user credentials returned from auth"""
  2216. ensure_service_catalog_present(cs)
  2217. catalog = cs.client.service_catalog.catalog
  2218. utils.print_dict(catalog['access']['user'], "User Credentials",
  2219. wrap=int(_args.wrap))
  2220. utils.print_dict(catalog['access']['token'], "Token", wrap=int(_args.wrap))
  2221. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  2222. @utils.arg('--port',
  2223. dest='port',
  2224. action='store',
  2225. type=int,
  2226. default=22,
  2227. help='Optional flag to indicate which port to use for ssh. '
  2228. '(Default=22)')
  2229. @utils.arg('--private',
  2230. dest='private',
  2231. action='store_true',
  2232. default=False,
  2233. help='Optional flag to indicate whether to use private address '
  2234. 'attached to an instance. (Default=False)')
  2235. @utils.arg('--ipv6',
  2236. dest='ipv6',
  2237. action='store_true',
  2238. default=False,
  2239. help='Optional flag to indicate whether to use an IPv6 address '
  2240. 'attached to an instance. (Defaults to IPv4 address)')
  2241. @utils.arg('--login', metavar='<login>', help='Login to use.', default="root")
  2242. @utils.arg('-i', '--identity',
  2243. dest='identity',
  2244. help='Private key file, same as the -i option to the ssh command.',
  2245. default='')
  2246. @utils.arg('--extra-opts',
  2247. dest='extra',
  2248. help='Extra options to pass to ssh. see: man ssh',
  2249. default='')
  2250. def do_ssh(cs, args):
  2251. """SSH into a server."""
  2252. addresses = _find_server(cs, args.server).addresses
  2253. address_type = "private" if args.private else "public"
  2254. version = 6 if args.ipv6 else 4
  2255. if address_type not in addresses:
  2256. print("ERROR: No %s addresses found for '%s'." % (address_type,
  2257. args.server))
  2258. return
  2259. ip_address = None
  2260. for address in addresses[address_type]:
  2261. if address['version'] == version:
  2262. ip_address = address['addr']
  2263. break
  2264. identity = '-i %s' % args.identity if len(args.identity) else ''
  2265. if ip_address:
  2266. os.system("ssh -%d -p%d %s %s@%s %s" % (version, args.port, identity,
  2267. args.login, ip_address,
  2268. args.extra))
  2269. else:
  2270. pretty_version = "IPv%d" % version
  2271. print("ERROR: No %s %s address found." % (address_type,
  2272. pretty_version))
  2273. return
  2274. _quota_resources = ['instances', 'cores', 'ram', 'volumes', 'gigabytes',
  2275. 'floating_ips', 'fixed_ips', 'metadata_items',
  2276. 'injected_files', 'key_pairs',
  2277. 'injected_file_content_bytes', 'injected_file_path_bytes',
  2278. 'security_groups', 'security_group_rules']
  2279. def _quota_show(quotas):
  2280. quota_dict = {}
  2281. for resource in _quota_resources:
  2282. try:
  2283. quota_dict[resource] = getattr(quotas, resource)
  2284. except AttributeError:
  2285. pass
  2286. utils.print_dict(quota_dict)
  2287. def _quota_update(manager, identifier, args):
  2288. updates = {}
  2289. for resource in _quota_resources:
  2290. val = getattr(args, resource, None)
  2291. if val is not None:
  2292. updates[resource] = val
  2293. if updates:
  2294. force_update = getattr(args, 'force', False)
  2295. if isinstance(manager, quotas.QuotaSetManager):
  2296. manager.update(identifier, force_update, **updates)
  2297. else:
  2298. manager.update(identifier, **updates)
  2299. @utils.arg('--tenant',
  2300. metavar='<tenant-id>',
  2301. default=None,
  2302. help='ID of tenant to list the quotas for.')
  2303. def do_quota_show(cs, args):
  2304. """List the quotas for a tenant."""
  2305. if not args.tenant:
  2306. _quota_show(cs.quotas.get(cs.client.tenant_id))
  2307. else:
  2308. _quota_show(cs.quotas.get(args.tenant))
  2309. @utils.arg('--tenant',
  2310. metavar='<tenant-id>',
  2311. default=None,
  2312. help='ID of tenant to list the default quotas for.')
  2313. def do_quota_defaults(cs, args):
  2314. """List the default quotas for a tenant."""
  2315. if not args.tenant:
  2316. _quota_show(cs.quotas.defaults(cs.client.tenant_id))
  2317. else:
  2318. _quota_show(cs.quotas.defaults(args.tenant))
  2319. @utils.arg('tenant',
  2320. metavar='<tenant-id>',
  2321. help='ID of tenant to set the quotas for.')
  2322. @utils.arg('--instances',
  2323. metavar='<instances>',
  2324. type=int, default=None,
  2325. help='New value for the "instances" quota.')
  2326. @utils.arg('--cores',
  2327. metavar='<cores>',
  2328. type=int, default=None,
  2329. help='New value for the "cores" quota.')
  2330. @utils.arg('--ram',
  2331. metavar='<ram>',
  2332. type=int, default=None,
  2333. help='New value for the "ram" quota.')
  2334. @utils.arg('--volumes',
  2335. metavar='<volumes>',
  2336. type=int, default=None,
  2337. help='New value for the "volumes" quota.')
  2338. @utils.arg('--gigabytes',
  2339. metavar='<gigabytes>',
  2340. type=int, default=None,
  2341. help='New value for the "gigabytes" quota.')
  2342. @utils.arg('--floating-ips',
  2343. metavar='<floating-ips>',
  2344. type=int,
  2345. default=None,
  2346. help='New value for the "floating-ips" quota.')
  2347. @utils.arg('--floating_ips',
  2348. type=int,
  2349. help=argparse.SUPPRESS)
  2350. @utils.arg('--fixed-ips',
  2351. metavar='<fixed-ips>',
  2352. type=int,
  2353. default=None,
  2354. help='New value for the "fixed-ips" quota.')
  2355. @utils.arg('--metadata-items',
  2356. metavar='<metadata-items>',
  2357. type=int,
  2358. default=None,
  2359. help='New value for the "metadata-items" quota.')
  2360. @utils.arg('--metadata_items',
  2361. type=int,
  2362. help=argparse.SUPPRESS)
  2363. @utils.arg('--injected-files',
  2364. metavar='<injected-files>',
  2365. type=int,
  2366. default=None,
  2367. help='New value for the "injected-files" quota.')
  2368. @utils.arg('--injected_files',
  2369. type=int,
  2370. help=argparse.SUPPRESS)
  2371. @utils.arg('--injected-file-content-bytes',
  2372. metavar='<injected-file-content-bytes>',
  2373. type=int,
  2374. default=None,
  2375. help='New value for the "injected-file-content-bytes" quota.')
  2376. @utils.arg('--injected_file_content_bytes',
  2377. type=int,
  2378. help=argparse.SUPPRESS)
  2379. @utils.arg('--injected-file-path-bytes',
  2380. metavar='<injected-file-path-bytes>',
  2381. type=int,
  2382. default=None,
  2383. help='New value for the "injected-file-path-bytes" quota.')
  2384. @utils.arg('--key-pairs',
  2385. metavar='<key-pairs>',
  2386. type=int,
  2387. default=None,
  2388. help='New value for the "key-pairs" quota.')
  2389. @utils.arg('--security-groups',
  2390. metavar='<security-groups>',
  2391. type=int,
  2392. default=None,
  2393. help='New value for the "security-groups" quota.')
  2394. @utils.arg('--security-group-rules',
  2395. metavar='<security-group-rules>',
  2396. type=int,
  2397. default=None,
  2398. help='New value for the "security-group-rules" quota.')
  2399. @utils.arg('--force',
  2400. dest='force',
  2401. action="store_true",
  2402. default=False,
  2403. help='Whether force update the quota even if the already used'
  2404. ' and reserved exceeds the new quota')
  2405. def do_quota_update(cs, args):
  2406. """Update the quotas for a tenant."""
  2407. _quota_update(cs.quotas, args.tenant, args)
  2408. @utils.arg('class_name',
  2409. metavar='<class>',
  2410. help='Name of quota class to list the quotas for.')
  2411. def do_quota_class_show(cs, args):
  2412. """List the quotas for a quota class."""
  2413. _quota_show(cs.quota_classes.get(args.class_name))
  2414. @utils.arg('class_name',
  2415. metavar='<class>',
  2416. help='Name of quota class to set the quotas for.')
  2417. @utils.arg('--instances',
  2418. metavar='<instances>',
  2419. type=int, default=None,
  2420. help='New value for the "instances" quota.')
  2421. @utils.arg('--cores',
  2422. metavar='<cores>',
  2423. type=int, default=None,
  2424. help='New value for the "cores" quota.')
  2425. @utils.arg('--ram',
  2426. metavar='<ram>',
  2427. type=int, default=None,
  2428. help='New value for the "ram" quota.')
  2429. @utils.arg('--volumes',
  2430. metavar='<volumes>',
  2431. type=int, default=None,
  2432. help='New value for the "volumes" quota.')
  2433. @utils.arg('--gigabytes',
  2434. metavar='<gigabytes>',
  2435. type=int, default=None,
  2436. help='New value for the "gigabytes" quota.')
  2437. @utils.arg('--floating-ips',
  2438. metavar='<floating-ips>',
  2439. type=int,
  2440. default=None,
  2441. help='New value for the "floating-ips" quota.')
  2442. @utils.arg('--floating_ips',
  2443. type=int,
  2444. help=argparse.SUPPRESS)
  2445. @utils.arg('--metadata-items',
  2446. metavar='<metadata-items>',
  2447. type=int,
  2448. default=None,
  2449. help='New value for the "metadata-items" quota.')
  2450. @utils.arg('--metadata_items',
  2451. type=int,
  2452. help=argparse.SUPPRESS)
  2453. @utils.arg('--injected-files',
  2454. metavar='<injected-files>',
  2455. type=int,
  2456. default=None,
  2457. help='New value for the "injected-files" quota.')
  2458. @utils.arg('--injected_files',
  2459. type=int,
  2460. help=argparse.SUPPRESS)
  2461. @utils.arg('--injected-file-content-bytes',
  2462. metavar='<injected-file-content-bytes>',
  2463. type=int,
  2464. default=None,
  2465. help='New value for the "injected-file-content-bytes" quota.')
  2466. @utils.arg('--injected_file_content_bytes',
  2467. type=int,
  2468. help=argparse.SUPPRESS)
  2469. @utils.arg('--injected-file-path-bytes',
  2470. metavar='<injected-file-path-bytes>',
  2471. type=int,
  2472. default=None,
  2473. help='New value for the "injected-file-path-bytes" quota.')
  2474. @utils.arg('--key-pairs',
  2475. metavar='<key-pairs>',
  2476. type=int,
  2477. default=None,
  2478. help='New value for the "key-pairs" quota.')
  2479. @utils.arg('--security-groups',
  2480. metavar='<security-groups>',
  2481. type=int,
  2482. default=None,
  2483. help='New value for the "security-groups" quota.')
  2484. @utils.arg('--security-group-rules',
  2485. metavar='<security-group-rules>',
  2486. type=int,
  2487. default=None,
  2488. help='New value for the "security-group-rules" quota.')
  2489. def do_quota_class_update(cs, args):
  2490. """Update the quotas for a quota class."""
  2491. _quota_update(cs.quota_classes, args.class_name, args)
  2492. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  2493. @utils.arg('host', metavar='<host>', help='Name or ID of target host.')
  2494. @utils.arg('--password',
  2495. dest='password',
  2496. metavar='<password>',
  2497. default=None,
  2498. help="Set the provided password on the evacuated instance. Not applicable "
  2499. "with on-shared-storage flag")
  2500. @utils.arg('--on-shared-storage',
  2501. dest='on_shared_storage',
  2502. action="store_true",
  2503. default=False,
  2504. help='Specifies whether instance files located on shared storage')
  2505. def do_evacuate(cs, args):
  2506. """Evacuate server from failed host to specified one."""
  2507. server = _find_server(cs, args.server)
  2508. res = server.evacuate(args.host, args.on_shared_storage, args.password)[1]
  2509. if type(res) is dict:
  2510. utils.print_dict(res)
  2511. def _print_interfaces(interfaces):
  2512. columns = ['Port State', 'Port ID', 'Net ID', 'IP addresses',
  2513. 'MAC Address']
  2514. class FormattedInterface(object):
  2515. def __init__(self, interface):
  2516. for col in columns:
  2517. key = col.lower().replace(" ", "_")
  2518. if hasattr(interface, key):
  2519. setattr(self, key, getattr(interface, key))
  2520. self.ip_addresses = ",".join([fip['ip_address']
  2521. for fip in interface.fixed_ips])
  2522. utils.print_list([FormattedInterface(i) for i in interfaces], columns)
  2523. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  2524. def do_interface_list(cs, args):
  2525. """List interfaces attached to an instance."""
  2526. server = _find_server(cs, args.server)
  2527. res = server.interface_list()
  2528. if type(res) is list:
  2529. _print_interfaces(res)
  2530. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  2531. @utils.arg('--port-id', metavar='<port_id>', help='Port ID.', dest="port_id")
  2532. @utils.arg('--net-id', metavar='<fixed_ip>', help='Network ID',
  2533. default=None, dest="net_id")
  2534. @utils.arg('--fixed-ip', metavar='<fixed_ip>', help='Requested fixed IP.',
  2535. default=None, dest="fixed_ip")
  2536. def do_interface_attach(cs, args):
  2537. """Attach a network interface to an instance."""
  2538. server = _find_server(cs, args.server)
  2539. res = server.interface_attach(args.port_id, args.net_id, args.fixed_ip)
  2540. if type(res) is dict:
  2541. utils.print_dict(res)
  2542. @utils.arg('server', metavar='<server>', help='Name or ID of server.')
  2543. @utils.arg('port_id', metavar='<port_id>', help='Port ID.')
  2544. def do_interface_detach(cs, args):
  2545. """Detach a network interface from an instance."""
  2546. server = _find_server(cs, args.server)
  2547. res = server.interface_detach(args.port_id)
  2548. if type(res) is dict:
  2549. utils.print_dict(res)
  2550. def _treeizeAvailabilityZone(zone):
  2551. """Build a tree view for availability zones"""
  2552. AvailabilityZone = availability_zones.AvailabilityZone
  2553. az = AvailabilityZone(zone.manager,
  2554. copy.deepcopy(zone._info), zone._loaded)
  2555. result = []
  2556. # Zone tree view item
  2557. az.zoneName = zone.zoneName
  2558. az.zoneState = ('available'
  2559. if zone.zoneState['available'] else 'not available')
  2560. az._info['zoneName'] = az.zoneName
  2561. az._info['zoneState'] = az.zoneState
  2562. result.append(az)
  2563. if zone.hosts is not None:
  2564. for (host, services) in zone.hosts.items():
  2565. # Host tree view item
  2566. az = AvailabilityZone(zone.manager,
  2567. copy.deepcopy(zone._info), zone._loaded)
  2568. az.zoneName = '|- %s' % host
  2569. az.zoneState = ''
  2570. az._info['zoneName'] = az.zoneName
  2571. az._info['zoneState'] = az.zoneState
  2572. result.append(az)
  2573. for (svc, state) in services.items():
  2574. # Service tree view item
  2575. az = AvailabilityZone(zone.manager,
  2576. copy.deepcopy(zone._info), zone._loaded)
  2577. az.zoneName = '| |- %s' % svc
  2578. az.zoneState = '%s %s %s' % (
  2579. 'enabled' if state['active'] else 'disabled',
  2580. ':-)' if state['available'] else 'XXX',
  2581. state['updated_at'])
  2582. az._info['zoneName'] = az.zoneName
  2583. az._info['zoneState'] = az.zoneState
  2584. result.append(az)
  2585. return result
  2586. @utils.service_type('compute')
  2587. def do_availability_zone_list(cs, _args):
  2588. """List all the availability zones."""
  2589. try:
  2590. availability_zones = cs.availability_zones.list()
  2591. except exceptions.Forbidden as e: # policy doesn't allow probably
  2592. try:
  2593. availability_zones = cs.availability_zones.list(detailed=False)
  2594. except:
  2595. raise e
  2596. result = []
  2597. for zone in availability_zones:
  2598. result += _treeizeAvailabilityZone(zone)
  2599. _translate_availability_zone_keys(result)
  2600. utils.print_list(result, ['Name', 'Status'],
  2601. sortby_index=None)