OpenStack Compute (Nova)
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.

manage.py 98KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  1. # Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
  2. # Copyright 2010 United States Government as represented by the
  3. # Administrator of the National Aeronautics and Space Administration.
  4. # All Rights Reserved.
  5. # Copyright 2013 Red Hat, Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. # not use this file except in compliance with the License. You may obtain
  9. # a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. # License for the specific language governing permissions and limitations
  17. # under the License.
  18. """
  19. CLI interface for nova management.
  20. """
  21. from __future__ import print_function
  22. import functools
  23. import re
  24. import sys
  25. import traceback
  26. from dateutil import parser as dateutil_parser
  27. import decorator
  28. from keystoneauth1 import exceptions as ks_exc
  29. import netaddr
  30. from oslo_config import cfg
  31. from oslo_db import exception as db_exc
  32. from oslo_log import log as logging
  33. import oslo_messaging as messaging
  34. from oslo_utils import encodeutils
  35. from oslo_utils import importutils
  36. from oslo_utils import uuidutils
  37. import prettytable
  38. import six
  39. import six.moves.urllib.parse as urlparse
  40. from sqlalchemy.engine import url as sqla_url
  41. from nova.cmd import common as cmd_common
  42. from nova.compute import api as compute_api
  43. import nova.conf
  44. from nova import config
  45. from nova import context
  46. from nova.db import api as db
  47. from nova.db import migration
  48. from nova.db.sqlalchemy import api as sa_db
  49. from nova import exception
  50. from nova.i18n import _
  51. from nova import objects
  52. from nova.objects import block_device as block_device_obj
  53. from nova.objects import build_request as build_request_obj
  54. from nova.objects import compute_node as compute_node_obj
  55. from nova.objects import host_mapping as host_mapping_obj
  56. from nova.objects import instance as instance_obj
  57. from nova.objects import instance_mapping as instance_mapping_obj
  58. from nova.objects import quotas as quotas_obj
  59. from nova.objects import virtual_interface as virtual_interface_obj
  60. from nova import quota
  61. from nova import rpc
  62. from nova.scheduler.client import report
  63. from nova.scheduler import utils as scheduler_utils
  64. from nova import utils
  65. from nova import version
  66. from nova.virt import ironic
  67. CONF = nova.conf.CONF
  68. LOG = logging.getLogger(__name__)
  69. QUOTAS = quota.QUOTAS
  70. # Keep this list sorted and one entry per line for readability.
  71. _EXTRA_DEFAULT_LOG_LEVELS = ['oslo_concurrency=INFO',
  72. 'oslo_db=INFO',
  73. 'oslo_policy=INFO']
  74. # Decorators for actions
  75. args = cmd_common.args
  76. action_description = cmd_common.action_description
  77. def mask_passwd_in_url(url):
  78. parsed = urlparse.urlparse(url)
  79. safe_netloc = re.sub(':.*@', ':****@', parsed.netloc)
  80. new_parsed = urlparse.ParseResult(
  81. parsed.scheme, safe_netloc,
  82. parsed.path, parsed.params,
  83. parsed.query, parsed.fragment)
  84. return urlparse.urlunparse(new_parsed)
  85. class FloatingIpCommands(object):
  86. """Class for managing floating IP."""
  87. # TODO(stephenfin): Remove these when we remove cells v1
  88. description = ('DEPRECATED: Floating IP commands are deprecated since '
  89. 'nova-network is deprecated in favor of Neutron. The '
  90. 'floating IP commands will be removed in an upcoming '
  91. 'release.')
  92. @staticmethod
  93. def address_to_hosts(addresses):
  94. """Iterate over hosts within an address range.
  95. If an explicit range specifier is missing, the parameter is
  96. interpreted as a specific individual address.
  97. """
  98. try:
  99. return [netaddr.IPAddress(addresses)]
  100. except ValueError:
  101. net = netaddr.IPNetwork(addresses)
  102. if net.size < 4:
  103. reason = _("/%s should be specified as single address(es) "
  104. "not in cidr format") % net.prefixlen
  105. raise exception.InvalidInput(reason=reason)
  106. elif net.size >= 1000000:
  107. # NOTE(dripton): If we generate a million IPs and put them in
  108. # the database, the system will slow to a crawl and/or run
  109. # out of memory and crash. This is clearly a misconfiguration.
  110. reason = _("Too many IP addresses will be generated. Please "
  111. "increase /%s to reduce the number generated."
  112. ) % net.prefixlen
  113. raise exception.InvalidInput(reason=reason)
  114. else:
  115. return net.iter_hosts()
  116. @args('--ip_range', metavar='<range>', help='IP range')
  117. @args('--pool', metavar='<pool>', help='Optional pool')
  118. @args('--interface', metavar='<interface>', help='Optional interface')
  119. def create(self, ip_range, pool=None, interface=None):
  120. """Creates floating IPs for zone by range."""
  121. admin_context = context.get_admin_context()
  122. if not pool:
  123. pool = CONF.default_floating_pool
  124. if not interface:
  125. interface = CONF.public_interface
  126. ips = [{'address': str(address), 'pool': pool, 'interface': interface}
  127. for address in self.address_to_hosts(ip_range)]
  128. try:
  129. db.floating_ip_bulk_create(admin_context, ips, want_result=False)
  130. except exception.FloatingIpExists as exc:
  131. # NOTE(simplylizz): Maybe logging would be better here
  132. # instead of printing, but logging isn't used here and I
  133. # don't know why.
  134. print('error: %s' % exc)
  135. return 1
  136. @args('--ip_range', metavar='<range>', help='IP range')
  137. def delete(self, ip_range):
  138. """Deletes floating IPs by range."""
  139. admin_context = context.get_admin_context()
  140. ips = ({'address': str(address)}
  141. for address in self.address_to_hosts(ip_range))
  142. db.floating_ip_bulk_destroy(admin_context, ips)
  143. @args('--host', metavar='<host>', help='Host')
  144. def list(self, host=None):
  145. """Lists all floating IPs (optionally by host).
  146. Note: if host is given, only active floating IPs are returned
  147. """
  148. ctxt = context.get_admin_context()
  149. try:
  150. if host is None:
  151. floating_ips = db.floating_ip_get_all(ctxt)
  152. else:
  153. floating_ips = db.floating_ip_get_all_by_host(ctxt, host)
  154. except exception.NoFloatingIpsDefined:
  155. print(_("No floating IP addresses have been defined."))
  156. return
  157. for floating_ip in floating_ips:
  158. instance_uuid = None
  159. if floating_ip['fixed_ip_id']:
  160. fixed_ip = db.fixed_ip_get(ctxt, floating_ip['fixed_ip_id'])
  161. instance_uuid = fixed_ip['instance_uuid']
  162. print("%s\t%s\t%s\t%s\t%s" % (floating_ip['project_id'],
  163. floating_ip['address'],
  164. instance_uuid,
  165. floating_ip['pool'],
  166. floating_ip['interface']))
  167. @decorator.decorator
  168. def validate_network_plugin(f, *args, **kwargs):
  169. """Decorator to validate the network plugin."""
  170. if utils.is_neutron():
  171. print(_("ERROR: Network commands are not supported when using the "
  172. "Neutron API. Use python-neutronclient instead."))
  173. return 2
  174. return f(*args, **kwargs)
  175. class NetworkCommands(object):
  176. """Class for managing networks."""
  177. # TODO(stephenfin): Remove these when we remove cells v1
  178. description = ('DEPRECATED: Network commands are deprecated since '
  179. 'nova-network is deprecated in favor of Neutron. The '
  180. 'network commands will be removed in an upcoming release.')
  181. @validate_network_plugin
  182. @args('--label', metavar='<label>', help='Label for network (ex: public)')
  183. @args('--fixed_range_v4', dest='cidr', metavar='<x.x.x.x/yy>',
  184. help='IPv4 subnet (ex: 10.0.0.0/8)')
  185. @args('--num_networks', metavar='<number>',
  186. help='Number of networks to create')
  187. @args('--network_size', metavar='<number>',
  188. help='Number of IPs per network')
  189. @args('--vlan', metavar='<vlan id>', help='vlan id')
  190. @args('--vlan_start', dest='vlan_start', metavar='<vlan start id>',
  191. help='vlan start id')
  192. @args('--vpn', dest='vpn_start', help='vpn start')
  193. @args('--fixed_range_v6', dest='cidr_v6',
  194. help='IPv6 subnet (ex: fe80::/64')
  195. @args('--gateway', help='gateway')
  196. @args('--gateway_v6', help='ipv6 gateway')
  197. @args('--bridge', metavar='<bridge>',
  198. help='VIFs on this network are connected to this bridge')
  199. @args('--bridge_interface', metavar='<bridge interface>',
  200. help='the bridge is connected to this interface')
  201. @args('--multi_host', metavar="<'T'|'F'>",
  202. help='Multi host')
  203. @args('--dns1', metavar="<DNS Address>", help='First DNS')
  204. @args('--dns2', metavar="<DNS Address>", help='Second DNS')
  205. @args('--uuid', metavar="<network uuid>", help='Network UUID')
  206. @args('--fixed_cidr', metavar='<x.x.x.x/yy>',
  207. help='IPv4 subnet for fixed IPs (ex: 10.20.0.0/16)')
  208. @args('--project_id', metavar="<project id>",
  209. help='Project id')
  210. @args('--priority', metavar="<number>", help='Network interface priority')
  211. def create(self, label=None, cidr=None, num_networks=None,
  212. network_size=None, multi_host=None, vlan=None,
  213. vlan_start=None, vpn_start=None, cidr_v6=None, gateway=None,
  214. gateway_v6=None, bridge=None, bridge_interface=None,
  215. dns1=None, dns2=None, project_id=None, priority=None,
  216. uuid=None, fixed_cidr=None):
  217. """Creates fixed IPs for host by range."""
  218. # NOTE(gmann): These checks are moved here as API layer does all these
  219. # validation through JSON schema.
  220. if not label:
  221. raise exception.NetworkNotCreated(req="label")
  222. if len(label) > 255:
  223. raise exception.LabelTooLong()
  224. if not (cidr or cidr_v6):
  225. raise exception.NetworkNotCreated(req="cidr or cidr_v6")
  226. kwargs = {k: v for k, v in locals().items()
  227. if v and k != "self"}
  228. if multi_host is not None:
  229. kwargs['multi_host'] = multi_host == 'T'
  230. net_manager = importutils.import_object(CONF.network_manager)
  231. net_manager.create_networks(context.get_admin_context(), **kwargs)
  232. @validate_network_plugin
  233. def list(self):
  234. """List all created networks."""
  235. _fmt = "%-5s\t%-18s\t%-15s\t%-15s\t%-15s\t%-15s\t%-15s\t%-15s\t%-15s"
  236. print(_fmt % (_('id'),
  237. _('IPv4'),
  238. _('IPv6'),
  239. _('start address'),
  240. _('DNS1'),
  241. _('DNS2'),
  242. _('VlanID'),
  243. _('project'),
  244. _("uuid")))
  245. try:
  246. # Since network_get_all can throw exception.NoNetworksFound
  247. # for this command to show a nice result, this exception
  248. # should be caught and handled as such.
  249. networks = db.network_get_all(context.get_admin_context())
  250. except exception.NoNetworksFound:
  251. print(_('No networks found'))
  252. else:
  253. for network in networks:
  254. print(_fmt % (network.id,
  255. network.cidr,
  256. network.cidr_v6,
  257. network.dhcp_start,
  258. network.dns1,
  259. network.dns2,
  260. network.vlan,
  261. network.project_id,
  262. network.uuid))
  263. @validate_network_plugin
  264. @args('--fixed_range', metavar='<x.x.x.x/yy>', help='Network to delete')
  265. @args('--uuid', metavar='<uuid>', help='UUID of network to delete')
  266. def delete(self, fixed_range=None, uuid=None):
  267. """Deletes a network."""
  268. if fixed_range is None and uuid is None:
  269. raise Exception(_("Please specify either fixed_range or uuid"))
  270. net_manager = importutils.import_object(CONF.network_manager)
  271. # delete the network
  272. net_manager.delete_network(context.get_admin_context(),
  273. fixed_range, uuid)
  274. @validate_network_plugin
  275. @args('--fixed_range', metavar='<x.x.x.x/yy>', help='Network to modify')
  276. @args('--project', metavar='<project name>',
  277. help='Project name to associate')
  278. @args('--host', metavar='<host>', help='Host to associate')
  279. @args('--disassociate-project', action="store_true", dest='dis_project',
  280. default=False, help='Disassociate Network from Project')
  281. @args('--disassociate-host', action="store_true", dest='dis_host',
  282. default=False, help='Disassociate Host from Project')
  283. def modify(self, fixed_range, project=None, host=None,
  284. dis_project=None, dis_host=None):
  285. """Associate/Disassociate Network with Project and/or Host
  286. arguments: network project host
  287. leave any field blank to ignore it
  288. """
  289. admin_context = context.get_admin_context()
  290. network = db.network_get_by_cidr(admin_context, fixed_range)
  291. net = {}
  292. # User can choose the following actions each for project and host.
  293. # 1) Associate (set not None value given by project/host parameter)
  294. # 2) Disassociate (set None by disassociate parameter)
  295. # 3) Keep unchanged (project/host key is not added to 'net')
  296. if dis_project:
  297. net['project_id'] = None
  298. if dis_host:
  299. net['host'] = None
  300. # The --disassociate-X are boolean options, but if they user
  301. # mistakenly provides a value, it will be used as a positional argument
  302. # and be erroneously interpreted as some other parameter (e.g.
  303. # a project instead of host value). The safest thing to do is error-out
  304. # with a message indicating that there is probably a problem with
  305. # how the disassociate modifications are being used.
  306. if dis_project or dis_host:
  307. if project or host:
  308. error_msg = "ERROR: Unexpected arguments provided. Please " \
  309. "use separate commands."
  310. print(error_msg)
  311. return 1
  312. db.network_update(admin_context, network['id'], net)
  313. return
  314. if project:
  315. net['project_id'] = project
  316. if host:
  317. net['host'] = host
  318. db.network_update(admin_context, network['id'], net)
  319. class DbCommands(object):
  320. """Class for managing the main database."""
  321. # NOTE(danms): These functions are called with a DB context and a
  322. # count, which is the maximum batch size requested by the
  323. # user. They must be idempotent. At most $count records should be
  324. # migrated. The function must return a tuple of (found, done). The
  325. # found value indicates how many unmigrated/candidate records existed in
  326. # the database prior to the migration (either total, or up to the
  327. # $count limit provided), and a nonzero found value may tell the user
  328. # that there is still work to do. The done value indicates whether
  329. # or not any records were actually migrated by the function. Thus
  330. # if both (found, done) are nonzero, work was done and some work
  331. # remains. If found is nonzero and done is zero, some records are
  332. # not migratable (or don't need migrating), but all migrations that can
  333. # complete have finished.
  334. online_migrations = (
  335. # Added in Ocata
  336. # NOTE(mriedem): This online migration is going to be backported to
  337. # Newton also since it's an upgrade issue when upgrading from Mitaka.
  338. build_request_obj.delete_build_requests_with_no_instance_uuid,
  339. # Added in Pike
  340. db.service_uuids_online_data_migration,
  341. # Added in Pike
  342. quotas_obj.migrate_quota_limits_to_api_db,
  343. # Added in Pike
  344. quotas_obj.migrate_quota_classes_to_api_db,
  345. # Added in Queens
  346. sa_db.migration_migrate_to_uuid,
  347. # Added in Queens
  348. block_device_obj.BlockDeviceMapping.populate_uuids,
  349. # Added in Rocky
  350. # NOTE(tssurya): This online migration is going to be backported to
  351. # Queens and Pike since instance.avz of instances before Pike
  352. # need to be populated if it was not specified during boot time.
  353. instance_obj.populate_missing_availability_zones,
  354. # Added in Rocky
  355. instance_mapping_obj.populate_queued_for_delete,
  356. # Added in Stein
  357. compute_node_obj.migrate_empty_ratio,
  358. # Added in Stein
  359. virtual_interface_obj.fill_virtual_interface_list,
  360. # Added in Stein
  361. instance_mapping_obj.populate_user_id,
  362. )
  363. def __init__(self):
  364. pass
  365. @staticmethod
  366. def _print_dict(dct, dict_property="Property", dict_value='Value'):
  367. """Print a `dict` as a table of two columns.
  368. :param dct: `dict` to print
  369. :param dict_property: name of the first column
  370. :param wrap: wrapping for the second column
  371. :param dict_value: header label for the value (second) column
  372. """
  373. pt = prettytable.PrettyTable([dict_property, dict_value])
  374. pt.align = 'l'
  375. for k, v in sorted(dct.items()):
  376. # convert dict to str to check length
  377. if isinstance(v, dict):
  378. v = six.text_type(v)
  379. # if value has a newline, add in multiple rows
  380. # e.g. fault with stacktrace
  381. if v and isinstance(v, six.string_types) and r'\n' in v:
  382. lines = v.strip().split(r'\n')
  383. col1 = k
  384. for line in lines:
  385. pt.add_row([col1, line])
  386. col1 = ''
  387. else:
  388. pt.add_row([k, v])
  389. if six.PY2:
  390. print(encodeutils.safe_encode(pt.get_string()))
  391. else:
  392. print(encodeutils.safe_encode(pt.get_string()).decode())
  393. @args('--local_cell', action='store_true',
  394. help='Only sync db in the local cell: do not attempt to fan-out '
  395. 'to all cells')
  396. @args('version', metavar='VERSION', nargs='?', help='Database version')
  397. def sync(self, version=None, local_cell=False):
  398. """Sync the database up to the most recent version."""
  399. if not local_cell:
  400. ctxt = context.RequestContext()
  401. # NOTE(mdoff): Multiple cells not yet implemented. Currently
  402. # fanout only looks for cell0.
  403. try:
  404. cell_mapping = objects.CellMapping.get_by_uuid(ctxt,
  405. objects.CellMapping.CELL0_UUID)
  406. with context.target_cell(ctxt, cell_mapping) as cctxt:
  407. migration.db_sync(version, context=cctxt)
  408. except exception.CellMappingNotFound:
  409. print(_('WARNING: cell0 mapping not found - not'
  410. ' syncing cell0.'))
  411. except Exception as e:
  412. print(_("""ERROR: Could not access cell0.
  413. Has the nova_api database been created?
  414. Has the nova_cell0 database been created?
  415. Has "nova-manage api_db sync" been run?
  416. Has "nova-manage cell_v2 map_cell0" been run?
  417. Is [api_database]/connection set in nova.conf?
  418. Is the cell0 database connection URL correct?
  419. Error: %s""") % six.text_type(e))
  420. return migration.db_sync(version)
  421. def version(self):
  422. """Print the current database version."""
  423. print(migration.db_version())
  424. @args('--max_rows', type=int, metavar='<number>', dest='max_rows',
  425. help='Maximum number of deleted rows to archive. Defaults to 1000. '
  426. 'Note that this number does not include the corresponding '
  427. 'rows, if any, that are removed from the API database for '
  428. 'deleted instances.')
  429. @args('--before', metavar='<date>',
  430. help=('Archive rows that have been deleted before this date. '
  431. 'Accepts date strings in the default format output by the '
  432. '``date`` command, as well as ``YYYY-MM-DD [HH:mm:ss]``.'))
  433. @args('--verbose', action='store_true', dest='verbose', default=False,
  434. help='Print how many rows were archived per table.')
  435. @args('--until-complete', action='store_true', dest='until_complete',
  436. default=False,
  437. help=('Run continuously until all deleted rows are archived. Use '
  438. 'max_rows as a batch size for each iteration.'))
  439. @args('--purge', action='store_true', dest='purge', default=False,
  440. help='Purge all data from shadow tables after archive completes')
  441. def archive_deleted_rows(self, max_rows=1000, verbose=False,
  442. until_complete=False, purge=False,
  443. before=None):
  444. """Move deleted rows from production tables to shadow tables.
  445. Returns 0 if nothing was archived, 1 if some number of rows were
  446. archived, 2 if max_rows is invalid, 3 if no connection could be
  447. established to the API DB, 4 if before date is invalid. If automating,
  448. this should be run continuously while the result
  449. is 1, stopping at 0.
  450. """
  451. max_rows = int(max_rows)
  452. if max_rows < 0:
  453. print(_("Must supply a positive value for max_rows"))
  454. return 2
  455. if max_rows > db.MAX_INT:
  456. print(_('max rows must be <= %(max_value)d') %
  457. {'max_value': db.MAX_INT})
  458. return 2
  459. ctxt = context.get_admin_context()
  460. try:
  461. # NOTE(tssurya): This check has been added to validate if the API
  462. # DB is reachable or not as this is essential for purging the
  463. # related API database records of the deleted instances.
  464. objects.CellMappingList.get_all(ctxt)
  465. except db_exc.CantStartEngineError:
  466. print(_('Failed to connect to API DB so aborting this archival '
  467. 'attempt. Please check your config file to make sure that '
  468. 'CONF.api_database.connection is set and run this '
  469. 'command again.'))
  470. return 3
  471. if before:
  472. try:
  473. before_date = dateutil_parser.parse(before, fuzzy=True)
  474. except ValueError as e:
  475. print(_('Invalid value for --before: %s') % e)
  476. return 4
  477. else:
  478. before_date = None
  479. table_to_rows_archived = {}
  480. deleted_instance_uuids = []
  481. if until_complete and verbose:
  482. sys.stdout.write(_('Archiving') + '..') # noqa
  483. while True:
  484. try:
  485. run, deleted_instance_uuids = db.archive_deleted_rows(
  486. max_rows, before=before_date)
  487. except KeyboardInterrupt:
  488. run = {}
  489. if until_complete and verbose:
  490. print('.' + _('stopped')) # noqa
  491. break
  492. for k, v in run.items():
  493. table_to_rows_archived.setdefault(k, 0)
  494. table_to_rows_archived[k] += v
  495. if deleted_instance_uuids:
  496. table_to_rows_archived.setdefault('instance_mappings', 0)
  497. table_to_rows_archived.setdefault('request_specs', 0)
  498. table_to_rows_archived.setdefault('instance_group_member', 0)
  499. deleted_mappings = objects.InstanceMappingList.destroy_bulk(
  500. ctxt, deleted_instance_uuids)
  501. table_to_rows_archived['instance_mappings'] += deleted_mappings
  502. deleted_specs = objects.RequestSpec.destroy_bulk(
  503. ctxt, deleted_instance_uuids)
  504. table_to_rows_archived['request_specs'] += deleted_specs
  505. deleted_group_members = (
  506. objects.InstanceGroup.destroy_members_bulk(
  507. ctxt, deleted_instance_uuids))
  508. table_to_rows_archived['instance_group_member'] += (
  509. deleted_group_members)
  510. if not until_complete:
  511. break
  512. elif not run:
  513. if verbose:
  514. print('.' + _('complete')) # noqa
  515. break
  516. if verbose:
  517. sys.stdout.write('.')
  518. if verbose:
  519. if table_to_rows_archived:
  520. self._print_dict(table_to_rows_archived, _('Table'),
  521. dict_value=_('Number of Rows Archived'))
  522. else:
  523. print(_('Nothing was archived.'))
  524. if table_to_rows_archived and purge:
  525. if verbose:
  526. print(_('Rows were archived, running purge...'))
  527. self.purge(purge_all=True, verbose=verbose)
  528. # NOTE(danms): Return nonzero if we archived something
  529. return int(bool(table_to_rows_archived))
  530. @args('--before', metavar='<before>', dest='before',
  531. help='If specified, purge rows from shadow tables that are older '
  532. 'than this. Accepts date strings in the default format output '
  533. 'by the ``date`` command, as well as ``YYYY-MM-DD '
  534. '[HH:mm:ss]``.')
  535. @args('--all', dest='purge_all', action='store_true',
  536. help='Purge all rows in the shadow tables')
  537. @args('--verbose', dest='verbose', action='store_true', default=False,
  538. help='Print information about purged records')
  539. @args('--all-cells', dest='all_cells', action='store_true', default=False,
  540. help='Run against all cell databases')
  541. def purge(self, before=None, purge_all=False, verbose=False,
  542. all_cells=False):
  543. if before is None and purge_all is False:
  544. print(_('Either --before or --all is required'))
  545. return 1
  546. if before:
  547. try:
  548. before_date = dateutil_parser.parse(before, fuzzy=True)
  549. except ValueError as e:
  550. print(_('Invalid value for --before: %s') % e)
  551. return 2
  552. else:
  553. before_date = None
  554. def status(msg):
  555. if verbose:
  556. print('%s: %s' % (identity, msg))
  557. deleted = 0
  558. admin_ctxt = context.get_admin_context()
  559. if all_cells:
  560. try:
  561. cells = objects.CellMappingList.get_all(admin_ctxt)
  562. except db_exc.DBError:
  563. print(_('Unable to get cell list from API DB. '
  564. 'Is it configured?'))
  565. return 4
  566. for cell in cells:
  567. identity = _('Cell %s') % cell.identity
  568. with context.target_cell(admin_ctxt, cell) as cctxt:
  569. deleted += sa_db.purge_shadow_tables(cctxt,
  570. before_date,
  571. status_fn=status)
  572. else:
  573. identity = _('DB')
  574. deleted = sa_db.purge_shadow_tables(admin_ctxt,
  575. before_date, status_fn=status)
  576. if deleted:
  577. return 0
  578. else:
  579. return 3
  580. @args('--delete', action='store_true', dest='delete',
  581. help='If specified, automatically delete any records found where '
  582. 'instance_uuid is NULL.')
  583. def null_instance_uuid_scan(self, delete=False):
  584. """Lists and optionally deletes database records where
  585. instance_uuid is NULL.
  586. """
  587. hits = migration.db_null_instance_uuid_scan(delete)
  588. records_found = False
  589. for table_name, records in hits.items():
  590. # Don't print anything for 0 hits
  591. if records:
  592. records_found = True
  593. if delete:
  594. print(_("Deleted %(records)d records "
  595. "from table '%(table_name)s'.") %
  596. {'records': records, 'table_name': table_name})
  597. else:
  598. print(_("There are %(records)d records in the "
  599. "'%(table_name)s' table where the uuid or "
  600. "instance_uuid column is NULL. Run this "
  601. "command again with the --delete option after you "
  602. "have backed up any necessary data.") %
  603. {'records': records, 'table_name': table_name})
  604. # check to see if we didn't find anything
  605. if not records_found:
  606. print(_('There were no records found where '
  607. 'instance_uuid was NULL.'))
  608. def _run_migration(self, ctxt, max_count):
  609. ran = 0
  610. exceptions = False
  611. migrations = {}
  612. for migration_meth in self.online_migrations:
  613. count = max_count - ran
  614. try:
  615. found, done = migration_meth(ctxt, count)
  616. except Exception:
  617. msg = (_("Error attempting to run %(method)s") % dict(
  618. method=migration_meth))
  619. print(msg)
  620. LOG.exception(msg)
  621. exceptions = True
  622. found = done = 0
  623. name = migration_meth.__name__
  624. if found:
  625. print(_('%(total)i rows matched query %(meth)s, %(done)i '
  626. 'migrated') % {'total': found,
  627. 'meth': name,
  628. 'done': done})
  629. # This is the per-migration method result for this batch, and
  630. # _run_migration will either continue on to the next migration,
  631. # or stop if up to this point we've processed max_count of
  632. # records across all migration methods.
  633. migrations[name] = found, done
  634. if max_count is not None:
  635. ran += done
  636. if ran >= max_count:
  637. break
  638. return migrations, exceptions
  639. @args('--max-count', metavar='<number>', dest='max_count',
  640. help='Maximum number of objects to consider')
  641. def online_data_migrations(self, max_count=None):
  642. ctxt = context.get_admin_context()
  643. if max_count is not None:
  644. try:
  645. max_count = int(max_count)
  646. except ValueError:
  647. max_count = -1
  648. unlimited = False
  649. if max_count < 1:
  650. print(_('Must supply a positive value for max_number'))
  651. return 127
  652. else:
  653. unlimited = True
  654. max_count = 50
  655. print(_('Running batches of %i until complete') % max_count)
  656. ran = None
  657. migration_info = {}
  658. exceptions = False
  659. while ran is None or ran != 0:
  660. migrations, exceptions = self._run_migration(ctxt, max_count)
  661. ran = 0
  662. # For each batch of migration method results, build the cumulative
  663. # set of results.
  664. for name in migrations:
  665. migration_info.setdefault(name, (0, 0))
  666. migration_info[name] = (
  667. migration_info[name][0] + migrations[name][0],
  668. migration_info[name][1] + migrations[name][1],
  669. )
  670. ran += migrations[name][1]
  671. if not unlimited:
  672. break
  673. t = prettytable.PrettyTable([_('Migration'),
  674. _('Total Needed'), # Really: Total Found
  675. _('Completed')])
  676. for name in sorted(migration_info.keys()):
  677. info = migration_info[name]
  678. t.add_row([name, info[0], info[1]])
  679. print(t)
  680. # NOTE(imacdonn): In the "unlimited" case, the loop above will only
  681. # terminate when all possible migrations have been effected. If we're
  682. # still getting exceptions, there's a problem that requires
  683. # intervention. In the max-count case, exceptions are only considered
  684. # fatal if no work was done by any other migrations ("not ran"),
  685. # because otherwise work may still remain to be done, and that work
  686. # may resolve dependencies for the failing migrations.
  687. if exceptions and (unlimited or not ran):
  688. print(_("Some migrations failed unexpectedly. Check log for "
  689. "details."))
  690. return 2
  691. # TODO(mriedem): Potentially add another return code for
  692. # "there are more migrations, but not completable right now"
  693. return ran and 1 or 0
  694. @args('--resource_class', metavar='<class>', required=True,
  695. help='Ironic node class to set on instances')
  696. @args('--host', metavar='<host>', required=False,
  697. help='Compute service name to migrate nodes on')
  698. @args('--node', metavar='<node>', required=False,
  699. help='Ironic node UUID to migrate (all on the host if omitted)')
  700. @args('--all', action='store_true', default=False, dest='all_hosts',
  701. help='Run migrations for all ironic hosts and nodes')
  702. @args('--verbose', action='store_true', default=False,
  703. help='Print information about migrations being performed')
  704. def ironic_flavor_migration(self, resource_class, host=None, node=None,
  705. all_hosts=False, verbose=False):
  706. """Migrate flavor information for ironic instances.
  707. This will manually push the instance flavor migration required
  708. for ironic-hosted instances in Pike. The best way to accomplish
  709. this migration is to run your ironic computes normally in Pike.
  710. However, if you need to push the migration manually, then use
  711. this.
  712. This is idempotent, but not trivial to start/stop/resume. It is
  713. recommended that you do this with care and not from a script
  714. assuming it is trivial.
  715. Running with --all may generate a large amount of DB traffic
  716. all at once. Running at least one host at a time is recommended
  717. for batching.
  718. Return values:
  719. 0: All work is completed (or none is needed)
  720. 1: Specified host and/or node is not found, or no ironic nodes present
  721. 2: Internal accounting error shows more than one instance per node
  722. 3: Invalid combination of required arguments
  723. """
  724. if not resource_class:
  725. # Note that if --resource_class is not specified on the command
  726. # line it will actually result in a return code of 2, but we
  727. # leave 3 here for testing purposes.
  728. print(_('A resource_class is required for all modes of operation'))
  729. return 3
  730. ctx = context.get_admin_context()
  731. if all_hosts:
  732. if host or node:
  733. print(_('--all with --host and/or --node does not make sense'))
  734. return 3
  735. cns = objects.ComputeNodeList.get_by_hypervisor_type(ctx, 'ironic')
  736. elif host and node:
  737. try:
  738. cn = objects.ComputeNode.get_by_host_and_nodename(ctx, host,
  739. node)
  740. cns = [cn]
  741. except exception.ComputeHostNotFound:
  742. cns = []
  743. elif host:
  744. try:
  745. cns = objects.ComputeNodeList.get_all_by_host(ctx, host)
  746. except exception.ComputeHostNotFound:
  747. cns = []
  748. else:
  749. print(_('Either --all, --host, or --host and --node are required'))
  750. return 3
  751. if len(cns) == 0:
  752. print(_('No ironic compute nodes found that match criteria'))
  753. return 1
  754. # Check that we at least got one ironic compute and we can pretty
  755. # safely assume the rest are
  756. if cns[0].hypervisor_type != 'ironic':
  757. print(_('Compute node(s) specified is not of type ironic'))
  758. return 1
  759. for cn in cns:
  760. # NOTE(danms): The instance.node is the
  761. # ComputeNode.hypervisor_hostname, which in the case of ironic is
  762. # the node uuid. Since only one instance can be on a node in
  763. # ironic, do another sanity check here to make sure we look legit.
  764. inst = objects.InstanceList.get_by_filters(
  765. ctx, {'node': cn.hypervisor_hostname,
  766. 'deleted': False})
  767. if len(inst) > 1:
  768. print(_('Ironic node %s has multiple instances? '
  769. 'Something is wrong.') % cn.hypervisor_hostname)
  770. return 2
  771. elif len(inst) == 1:
  772. result = ironic.IronicDriver._pike_flavor_migration_for_node(
  773. ctx, resource_class, inst[0].uuid)
  774. if result and verbose:
  775. print(_('Migrated instance %(uuid)s on node %(node)s') % {
  776. 'uuid': inst[0].uuid,
  777. 'node': cn.hypervisor_hostname})
  778. return 0
  779. class ApiDbCommands(object):
  780. """Class for managing the api database."""
  781. def __init__(self):
  782. pass
  783. @args('version', metavar='VERSION', nargs='?', help='Database version')
  784. def sync(self, version=None):
  785. """Sync the database up to the most recent version."""
  786. return migration.db_sync(version, database='api')
  787. def version(self):
  788. """Print the current database version."""
  789. print(migration.db_version(database='api'))
  790. class CellV2Commands(object):
  791. """Commands for managing cells v2."""
  792. def _validate_transport_url(self, transport_url):
  793. transport_url = transport_url or CONF.transport_url
  794. if not transport_url:
  795. print('Must specify --transport-url if [DEFAULT]/transport_url '
  796. 'is not set in the configuration file.')
  797. return None
  798. try:
  799. messaging.TransportURL.parse(conf=CONF,
  800. url=objects.CellMapping.format_mq_url(
  801. transport_url))
  802. except (messaging.InvalidTransportURL, ValueError) as e:
  803. print(_('Invalid transport URL: %s') % six.text_type(e))
  804. return None
  805. return transport_url
  806. def _non_unique_transport_url_database_connection_checker(self, ctxt,
  807. cell_mapping, transport_url, database_connection):
  808. for cell in objects.CellMappingList.get_all(ctxt):
  809. if cell_mapping and cell.uuid == cell_mapping.uuid:
  810. # If we're looking for a specific cell, then don't check
  811. # that one for same-ness to allow idempotent updates
  812. continue
  813. if (cell.database_connection == database_connection or
  814. cell.transport_url == transport_url):
  815. print(_('The specified transport_url and/or '
  816. 'database_connection combination already exists '
  817. 'for another cell with uuid %s.') % cell.uuid)
  818. return True
  819. return False
  820. @args('--transport-url', metavar='<transport_url>', dest='transport_url',
  821. help='The transport url for the cell message queue')
  822. def simple_cell_setup(self, transport_url=None):
  823. """Simple cellsv2 setup.
  824. This simplified command is for use by existing non-cells users to
  825. configure the default environment. Returns 0 if setup is completed (or
  826. has already been done) and 1 if no hosts are reporting (and this cannot
  827. be mapped).
  828. """
  829. transport_url = self._validate_transport_url(transport_url)
  830. if not transport_url:
  831. return 1
  832. ctxt = context.RequestContext()
  833. try:
  834. cell0_mapping = self._map_cell0()
  835. except db_exc.DBDuplicateEntry:
  836. print(_('Cell0 is already setup'))
  837. cell0_mapping = objects.CellMapping.get_by_uuid(
  838. ctxt, objects.CellMapping.CELL0_UUID)
  839. # Run migrations so cell0 is usable
  840. with context.target_cell(ctxt, cell0_mapping) as cctxt:
  841. try:
  842. migration.db_sync(None, context=cctxt)
  843. except db_exc.DBError as ex:
  844. print(_('Unable to sync cell0 schema: %s') % ex)
  845. cell_uuid = self._map_cell_and_hosts(transport_url)
  846. if cell_uuid is None:
  847. # There are no compute hosts which means no cell_mapping was
  848. # created. This should also mean that there are no instances.
  849. return 1
  850. self.map_instances(cell_uuid)
  851. return 0
  852. @args('--database_connection',
  853. metavar='<database_connection>',
  854. help='The database connection url for cell0. '
  855. 'This is optional. If not provided, a standard database '
  856. 'connection will be used based on the main database connection '
  857. 'from the Nova configuration.'
  858. )
  859. def map_cell0(self, database_connection=None):
  860. """Create a cell mapping for cell0.
  861. cell0 is used for instances that have not been scheduled to any cell.
  862. This generally applies to instances that have encountered an error
  863. before they have been scheduled.
  864. This command creates a cell mapping for this special cell which
  865. requires a database to store the instance data.
  866. Returns 0 if cell0 created successfully or already setup.
  867. """
  868. try:
  869. self._map_cell0(database_connection=database_connection)
  870. except db_exc.DBDuplicateEntry:
  871. print(_('Cell0 is already setup'))
  872. return 0
  873. def _map_cell0(self, database_connection=None):
  874. """Faciliate creation of a cell mapping for cell0.
  875. See map_cell0 for more.
  876. """
  877. def cell0_default_connection():
  878. # If no database connection is provided one is generated
  879. # based on the database connection url.
  880. # The cell0 database will use the same database scheme and
  881. # netloc as the main database, with a related path.
  882. # NOTE(sbauza): The URL has to be RFC1738 compliant in order to
  883. # be usable by sqlalchemy.
  884. connection = CONF.database.connection
  885. # sqlalchemy has a nice utility for parsing database connection
  886. # URLs so we use that here to get the db name so we don't have to
  887. # worry about parsing and splitting a URL which could have special
  888. # characters in the password, which makes parsing a nightmare.
  889. url = sqla_url.make_url(connection)
  890. url.database = url.database + '_cell0'
  891. return urlparse.unquote(str(url))
  892. dbc = database_connection or cell0_default_connection()
  893. ctxt = context.RequestContext()
  894. # A transport url of 'none://' is provided for cell0. RPC should not
  895. # be used to access cell0 objects. Cells transport switching will
  896. # ignore any 'none' transport type.
  897. cell_mapping = objects.CellMapping(
  898. ctxt, uuid=objects.CellMapping.CELL0_UUID, name="cell0",
  899. transport_url="none:///",
  900. database_connection=dbc)
  901. cell_mapping.create()
  902. return cell_mapping
  903. def _get_and_map_instances(self, ctxt, cell_mapping, limit, marker):
  904. filters = {}
  905. with context.target_cell(ctxt, cell_mapping) as cctxt:
  906. instances = objects.InstanceList.get_by_filters(
  907. cctxt.elevated(read_deleted='yes'), filters,
  908. sort_key='created_at', sort_dir='asc', limit=limit,
  909. marker=marker)
  910. for instance in instances:
  911. try:
  912. mapping = objects.InstanceMapping(ctxt)
  913. mapping.instance_uuid = instance.uuid
  914. mapping.cell_mapping = cell_mapping
  915. mapping.project_id = instance.project_id
  916. mapping.user_id = instance.user_id
  917. mapping.create()
  918. except db_exc.DBDuplicateEntry:
  919. continue
  920. if len(instances) == 0 or len(instances) < limit:
  921. # We've hit the end of the instances table
  922. marker = None
  923. else:
  924. marker = instances[-1].uuid
  925. return marker
  926. @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
  927. required=True,
  928. help='Unmigrated instances will be mapped to the cell with the '
  929. 'uuid provided.')
  930. @args('--max-count', metavar='<max_count>', dest='max_count',
  931. help='Maximum number of instances to map. If not set, all instances '
  932. 'in the cell will be mapped in batches of 50. If you have a '
  933. 'large number of instances, consider specifying a custom value '
  934. 'and run the command until it exits with 0.')
  935. @args('--reset', action='store_true', dest='reset_marker',
  936. help='The command will start from the beginning as opposed to the '
  937. 'default behavior of starting from where the last run '
  938. 'finished')
  939. def map_instances(self, cell_uuid, max_count=None, reset_marker=None):
  940. """Map instances into the provided cell.
  941. Instances in the nova database of the provided cell (nova database
  942. info is obtained from the nova-api database) will be queried from
  943. oldest to newest and if unmapped, will be mapped to the provided cell.
  944. A max-count can be set on the number of instance to map in a single
  945. run. Repeated runs of the command will start from where the last run
  946. finished so it is not necessary to increase max-count to finish. A
  947. reset option can be passed which will reset the marker, thus making the
  948. command start from the beginning as opposed to the default behavior of
  949. starting from where the last run finished. An exit code of 0 indicates
  950. that all instances have been mapped.
  951. """
  952. # NOTE(stephenfin): The support for batching in this command relies on
  953. # a bit of a hack. We initially process N instance-cell mappings, where
  954. # N is the value of '--max-count' if provided else 50. To ensure we
  955. # can continue from N on the next iteration, we store a instance-cell
  956. # mapping object with a special name and the UUID of the last
  957. # instance-cell mapping processed (N - 1) in munged form. On the next
  958. # iteration, we search for the special name and unmunge the UUID to
  959. # pick up where we left off. This is done until all mappings are
  960. # processed. The munging is necessary as there's a unique constraint on
  961. # the UUID field and we need something reversable. For more
  962. # information, see commit 9038738d0.
  963. if max_count is not None:
  964. try:
  965. max_count = int(max_count)
  966. except ValueError:
  967. max_count = -1
  968. map_all = False
  969. if max_count < 1:
  970. print(_('Must supply a positive value for max-count'))
  971. return 127
  972. else:
  973. map_all = True
  974. max_count = 50
  975. ctxt = context.RequestContext()
  976. marker_project_id = 'INSTANCE_MIGRATION_MARKER'
  977. # Validate the cell exists, this will raise if not
  978. cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
  979. # Check for a marker from a previous run
  980. marker_mapping = objects.InstanceMappingList.get_by_project_id(ctxt,
  981. marker_project_id)
  982. if len(marker_mapping) == 0:
  983. marker = None
  984. else:
  985. # There should be only one here
  986. marker = marker_mapping[0].instance_uuid.replace(' ', '-')
  987. if reset_marker:
  988. marker = None
  989. marker_mapping[0].destroy()
  990. next_marker = True
  991. while next_marker is not None:
  992. next_marker = self._get_and_map_instances(ctxt, cell_mapping,
  993. max_count, marker)
  994. marker = next_marker
  995. if not map_all:
  996. break
  997. if next_marker:
  998. # Don't judge me. There's already an InstanceMapping with this UUID
  999. # so the marker needs to be non destructively modified.
  1000. next_marker = next_marker.replace('-', ' ')
  1001. # This is just the marker record, so set user_id to the special
  1002. # marker name as well.
  1003. objects.InstanceMapping(ctxt, instance_uuid=next_marker,
  1004. project_id=marker_project_id,
  1005. user_id=marker_project_id).create()
  1006. return 1
  1007. return 0
  1008. def _map_cell_and_hosts(self, transport_url, name=None, verbose=False):
  1009. ctxt = context.RequestContext()
  1010. cell_mapping_uuid = cell_mapping = None
  1011. # First, try to detect if a CellMapping has already been created
  1012. compute_nodes = objects.ComputeNodeList.get_all(ctxt)
  1013. if not compute_nodes:
  1014. print(_('No hosts found to map to cell, exiting.'))
  1015. return None
  1016. missing_nodes = set()
  1017. for compute_node in compute_nodes:
  1018. try:
  1019. host_mapping = objects.HostMapping.get_by_host(
  1020. ctxt, compute_node.host)
  1021. except exception.HostMappingNotFound:
  1022. missing_nodes.add(compute_node.host)
  1023. else:
  1024. if verbose:
  1025. print(_(
  1026. 'Host %(host)s is already mapped to cell %(uuid)s'
  1027. ) % {'host': host_mapping.host,
  1028. 'uuid': host_mapping.cell_mapping.uuid})
  1029. # Re-using the existing UUID in case there is already a mapping
  1030. # NOTE(sbauza): There could be possibly multiple CellMappings
  1031. # if the operator provides another configuration file and moves
  1032. # the hosts to another cell v2, but that's not really something
  1033. # we should support.
  1034. cell_mapping_uuid = host_mapping.cell_mapping.uuid
  1035. if not missing_nodes:
  1036. print(_('All hosts are already mapped to cell(s).'))
  1037. return cell_mapping_uuid
  1038. # Create the cell mapping in the API database
  1039. if cell_mapping_uuid is not None:
  1040. cell_mapping = objects.CellMapping.get_by_uuid(
  1041. ctxt, cell_mapping_uuid)
  1042. if cell_mapping is None:
  1043. cell_mapping_uuid = uuidutils.generate_uuid()
  1044. cell_mapping = objects.CellMapping(
  1045. ctxt, uuid=cell_mapping_uuid, name=name,
  1046. transport_url=transport_url,
  1047. database_connection=CONF.database.connection)
  1048. cell_mapping.create()
  1049. # Pull the hosts from the cell database and create the host mappings
  1050. for compute_host in missing_nodes:
  1051. host_mapping = objects.HostMapping(
  1052. ctxt, host=compute_host, cell_mapping=cell_mapping)
  1053. host_mapping.create()
  1054. if verbose:
  1055. print(cell_mapping_uuid)
  1056. return cell_mapping_uuid
  1057. @args('--transport-url', metavar='<transport_url>', dest='transport_url',
  1058. help='The transport url for the cell message queue')
  1059. @args('--name', metavar='<cell_name>', help='The name of the cell')
  1060. @args('--verbose', action='store_true',
  1061. help='Output the cell mapping uuid for any newly mapped hosts.')
  1062. def map_cell_and_hosts(self, transport_url=None, name=None, verbose=False):
  1063. """EXPERIMENTAL. Create a cell mapping and host mappings for a cell.
  1064. Users not dividing their cloud into multiple cells will be a single
  1065. cell v2 deployment and should specify:
  1066. nova-manage cell_v2 map_cell_and_hosts --config-file <nova.conf>
  1067. Users running multiple cells can add a cell v2 by specifying:
  1068. nova-manage cell_v2 map_cell_and_hosts --config-file <cell nova.conf>
  1069. """
  1070. transport_url = self._validate_transport_url(transport_url)
  1071. if not transport_url:
  1072. return 1
  1073. self._map_cell_and_hosts(transport_url, name, verbose)
  1074. # online_data_migrations established a pattern of 0 meaning everything
  1075. # is done, 1 means run again to do more work. This command doesn't do
  1076. # partial work so 0 is appropriate.
  1077. return 0
  1078. @args('--uuid', metavar='<instance_uuid>', dest='uuid', required=True,
  1079. help=_('The instance UUID to verify'))
  1080. @args('--quiet', action='store_true', dest='quiet',
  1081. help=_('Do not print anything'))
  1082. def verify_instance(self, uuid, quiet=False):
  1083. """Verify instance mapping to a cell.
  1084. This command is useful to determine if the cellsv2 environment is
  1085. properly setup, specifically in terms of the cell, host, and instance
  1086. mapping records required.
  1087. This prints one of three strings (and exits with a code) indicating
  1088. whether the instance is successfully mapped to a cell (0), is unmapped
  1089. due to an incomplete upgrade (1), unmapped due to normally transient
  1090. state (2), it is a deleted instance which has instance mapping (3),
  1091. or it is an archived instance which still has an instance mapping (4).
  1092. """
  1093. def say(string):
  1094. if not quiet:
  1095. print(string)
  1096. ctxt = context.get_admin_context()
  1097. try:
  1098. mapping = objects.InstanceMapping.get_by_instance_uuid(
  1099. ctxt, uuid)
  1100. except exception.InstanceMappingNotFound:
  1101. say('Instance %s is not mapped to a cell '
  1102. '(upgrade is incomplete) or instance '
  1103. 'does not exist' % uuid)
  1104. return 1
  1105. if mapping.cell_mapping is None:
  1106. say('Instance %s is not mapped to a cell' % uuid)
  1107. return 2
  1108. else:
  1109. with context.target_cell(ctxt, mapping.cell_mapping) as cctxt:
  1110. try:
  1111. instance = objects.Instance.get_by_uuid(cctxt, uuid)
  1112. except exception.InstanceNotFound:
  1113. try:
  1114. el_ctx = cctxt.elevated(read_deleted='yes')
  1115. instance = objects.Instance.get_by_uuid(el_ctx, uuid)
  1116. # instance is deleted
  1117. if instance:
  1118. say('The instance with uuid %s has been deleted.'
  1119. % uuid)
  1120. say('Execute '
  1121. '`nova-manage db archive_deleted_rows` '
  1122. 'command to archive this deleted '
  1123. 'instance and remove its instance_mapping.')
  1124. return 3
  1125. except exception.InstanceNotFound:
  1126. # instance is archived
  1127. say('The instance with uuid %s has been archived.'
  1128. % uuid)
  1129. say('However its instance_mapping remains.')
  1130. return 4
  1131. # instance is alive and mapped to a cell
  1132. say('Instance %s is in cell: %s (%s)' % (
  1133. uuid,
  1134. mapping.cell_mapping.name,
  1135. mapping.cell_mapping.uuid))
  1136. return 0
  1137. @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
  1138. help='If provided only this cell will be searched for new hosts to '
  1139. 'map.')
  1140. @args('--verbose', action='store_true',
  1141. help=_('Provide detailed output when discovering hosts.'))
  1142. @args('--strict', action='store_true',
  1143. help=_('Considered successful (exit code 0) only when an unmapped '
  1144. 'host is discovered. Any other outcome will be considered a '
  1145. 'failure (non-zero exit code).'))
  1146. @args('--by-service', action='store_true', default=False,
  1147. dest='by_service',
  1148. help=_('Discover hosts by service instead of compute node'))
  1149. def discover_hosts(self, cell_uuid=None, verbose=False, strict=False,
  1150. by_service=False):
  1151. """Searches cells, or a single cell, and maps found hosts.
  1152. When a new host is added to a deployment it will add a service entry
  1153. to the db it's configured to use. This command will check the db for
  1154. each cell, or a single one if passed in, and map any hosts which are
  1155. not currently mapped. If a host is already mapped nothing will be done.
  1156. This command should be run once after all compute hosts have been
  1157. deployed and should not be run in parallel. When run in parallel,
  1158. the commands will collide with each other trying to map the same hosts
  1159. in the database at the same time.
  1160. """
  1161. def status_fn(msg):
  1162. if verbose:
  1163. print(msg)
  1164. ctxt = context.RequestContext()
  1165. try:
  1166. hosts = host_mapping_obj.discover_hosts(ctxt, cell_uuid, status_fn,
  1167. by_service)
  1168. except exception.HostMappingExists as exp:
  1169. print(_('ERROR: Duplicate host mapping was encountered. This '
  1170. 'command should be run once after all compute hosts have '
  1171. 'been deployed and should not be run in parallel. When '
  1172. 'run in parallel, the commands will collide with each '
  1173. 'other trying to map the same hosts in the database at '
  1174. 'the same time. Error: %s') % exp)
  1175. return 2
  1176. # discover_hosts will return an empty list if no hosts are discovered
  1177. if strict:
  1178. return int(not hosts)
  1179. @action_description(
  1180. _("Add a new cell to nova API database. "
  1181. "DB and MQ urls can be provided directly "
  1182. "or can be taken from config. The result is cell uuid."))
  1183. @args('--name', metavar='<cell_name>', help=_('The name of the cell'))
  1184. @args('--database_connection', metavar='<database_connection>',
  1185. dest='database_connection',
  1186. help=_('The database url for the cell database'))
  1187. @args('--transport-url', metavar='<transport_url>', dest='transport_url',
  1188. help=_('The transport url for the cell message queue'))
  1189. @args('--verbose', action='store_true',
  1190. help=_('Output the uuid of the created cell'))
  1191. @args('--disabled', action='store_true',
  1192. help=_('To create a pre-disabled cell.'))
  1193. def create_cell(self, name=None, database_connection=None,
  1194. transport_url=None, verbose=False, disabled=False):
  1195. ctxt = context.get_context()
  1196. transport_url = self._validate_transport_url(transport_url)
  1197. if not transport_url:
  1198. return 1
  1199. database_connection = database_connection or CONF.database.connection
  1200. if not database_connection:
  1201. print(_('Must specify --database_connection '
  1202. 'if [database]/connection is not set '
  1203. 'in the configuration file.'))
  1204. return 1
  1205. if (self._non_unique_transport_url_database_connection_checker(ctxt,
  1206. None, transport_url, database_connection)):
  1207. return 2
  1208. cell_mapping_uuid = uuidutils.generate_uuid()
  1209. cell_mapping = objects.CellMapping(
  1210. ctxt,
  1211. uuid=cell_mapping_uuid, name=name,
  1212. transport_url=transport_url,
  1213. database_connection=database_connection,
  1214. disabled=disabled)
  1215. cell_mapping.create()
  1216. if verbose:
  1217. print(cell_mapping_uuid)
  1218. return 0
  1219. @args('--verbose', action='store_true',
  1220. help=_('Show sensitive details, such as passwords'))
  1221. def list_cells(self, verbose=False):
  1222. """Lists the v2 cells in the deployment.
  1223. By default the cell name, uuid, disabled state, masked transport
  1224. URL and database connection details are shown. Use the --verbose
  1225. option to see transport URL and database connection with their
  1226. sensitive details.
  1227. """
  1228. cell_mappings = objects.CellMappingList.get_all(
  1229. context.get_admin_context())
  1230. field_names = [_('Name'), _('UUID'), _('Transport URL'),
  1231. _('Database Connection'), _('Disabled')]
  1232. t = prettytable.PrettyTable(field_names)
  1233. for cell in sorted(cell_mappings,
  1234. # CellMapping.name is optional
  1235. key=lambda _cell: _cell.name or ''):
  1236. fields = [cell.name or '', cell.uuid]
  1237. if verbose:
  1238. fields.extend([cell.transport_url, cell.database_connection])
  1239. else:
  1240. fields.extend([
  1241. mask_passwd_in_url(cell.transport_url),
  1242. mask_passwd_in_url(cell.database_connection)])
  1243. fields.extend([cell.disabled])
  1244. t.add_row(fields)
  1245. print(t)
  1246. return 0
  1247. @args('--force', action='store_true', default=False,
  1248. help=_('Delete hosts and instance_mappings that belong '
  1249. 'to the cell as well.'))
  1250. @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
  1251. required=True, help=_('The uuid of the cell to delete.'))
  1252. def delete_cell(self, cell_uuid, force=False):
  1253. """Delete an empty cell by the given uuid.
  1254. This command will return a non-zero exit code in the following cases.
  1255. * The cell is not found by uuid.
  1256. * It has hosts and force is False.
  1257. * It has instance mappings and force is False.
  1258. If force is True and the cell has hosts and/or instance_mappings, they
  1259. are deleted as well (as long as there are no living instances).
  1260. Returns 0 in the following cases.
  1261. * The empty cell is found and deleted successfully.
  1262. * The cell has hosts and force is True then the cell, hosts and
  1263. instance_mappings are deleted successfully; if there are no
  1264. living instances.
  1265. """
  1266. ctxt = context.get_admin_context()
  1267. # Find the CellMapping given the uuid.
  1268. try:
  1269. cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
  1270. except exception.CellMappingNotFound:
  1271. print(_('Cell with uuid %s was not found.') % cell_uuid)
  1272. return 1
  1273. # Check to see if there are any HostMappings for this cell.
  1274. host_mappings = objects.HostMappingList.get_by_cell_id(
  1275. ctxt, cell_mapping.id)
  1276. nodes = []
  1277. if host_mappings:
  1278. if not force:
  1279. print(_('There are existing hosts mapped to cell with uuid '
  1280. '%s.') % cell_uuid)
  1281. return 2
  1282. # We query for the compute nodes in the cell,
  1283. # so that they can be unmapped.
  1284. with context.target_cell(ctxt, cell_mapping) as cctxt:
  1285. nodes = objects.ComputeNodeList.get_all(cctxt)
  1286. # Check to see if there are any InstanceMappings for this cell.
  1287. instance_mappings = objects.InstanceMappingList.get_by_cell_id(
  1288. ctxt, cell_mapping.id)
  1289. if instance_mappings:
  1290. with context.target_cell(ctxt, cell_mapping) as cctxt:
  1291. instances = objects.InstanceList.get_all(cctxt)
  1292. if instances:
  1293. # There are instances in the cell.
  1294. print(_('There are existing instances mapped to cell with '
  1295. 'uuid %s.') % cell_uuid)
  1296. return 3
  1297. else:
  1298. if not force:
  1299. # There are no instances in the cell but the records remain
  1300. # in the 'instance_mappings' table.
  1301. print(_("There are instance mappings to cell with uuid "
  1302. "%s, but all instances have been deleted "
  1303. "in the cell.") % cell_uuid)
  1304. print(_("So execute 'nova-manage db archive_deleted_rows' "
  1305. "to delete the instance mappings."))
  1306. return 4
  1307. # Delete instance_mappings of the deleted instances
  1308. for instance_mapping in instance_mappings:
  1309. instance_mapping.destroy()
  1310. # Unmap the compute nodes so that they can be discovered
  1311. # again in future, if needed.
  1312. for node in nodes:
  1313. node.mapped = 0
  1314. node.save()
  1315. # Delete hosts mapped to the cell.
  1316. for host_mapping in host_mappings:
  1317. host_mapping.destroy()
  1318. # There are no hosts or instances mapped to the cell so delete it.
  1319. cell_mapping.destroy()
  1320. return 0
  1321. @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
  1322. required=True, help=_('The uuid of the cell to update.'))
  1323. @args('--name', metavar='<cell_name>', dest='name',
  1324. help=_('Set the cell name.'))
  1325. @args('--transport-url', metavar='<transport_url>', dest='transport_url',
  1326. help=_('Set the cell transport_url. NOTE that running nodes '
  1327. 'will not see the change until restart!'))
  1328. @args('--database_connection', metavar='<database_connection>',
  1329. dest='db_connection',
  1330. help=_('Set the cell database_connection. NOTE that running nodes '
  1331. 'will not see the change until restart!'))
  1332. @args('--disable', action='store_true', dest='disable',
  1333. help=_('Disables the cell. Note that the scheduling will be blocked '
  1334. 'to this cell until its enabled and followed by a SIGHUP of '
  1335. 'nova-scheduler service.'))
  1336. @args('--enable', action='store_true', dest='enable',
  1337. help=_('Enables the cell. Note that this makes a disabled cell '
  1338. 'available for scheduling after a SIGHUP of the '
  1339. 'nova-scheduler service'))
  1340. def update_cell(self, cell_uuid, name=None, transport_url=None,
  1341. db_connection=None, disable=False, enable=False):
  1342. """Updates the properties of a cell by the given uuid.
  1343. If the cell is not found by uuid, this command will return an exit
  1344. code of 1. If the provided transport_url or/and database_connection
  1345. is/are same as another cell, this command will return an exit code
  1346. of 3. If the properties cannot be set, this will return 2. If an
  1347. attempt is made to disable and enable a cell at the same time, this
  1348. command will exit with a return code of 4. If an attempt is made to
  1349. disable or enable cell0 this command will exit with a return code of 5.
  1350. Otherwise, the exit code will be 0.
  1351. NOTE: Updating the transport_url or database_connection fields on
  1352. a running system will NOT result in all nodes immediately using the
  1353. new values. Use caution when changing these values.
  1354. NOTE (tssurya): The scheduler will not notice that a cell has been
  1355. enabled/disabled until it is restarted or sent the SIGHUP signal.
  1356. """
  1357. ctxt = context.get_admin_context()
  1358. try:
  1359. cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
  1360. except exception.CellMappingNotFound:
  1361. print(_('Cell with uuid %s was not found.') % cell_uuid)
  1362. return 1
  1363. if name:
  1364. cell_mapping.name = name
  1365. transport_url = transport_url or CONF.transport_url
  1366. db_connection = db_connection or CONF.database.connection
  1367. if (self._non_unique_transport_url_database_connection_checker(ctxt,
  1368. cell_mapping, transport_url, db_connection)):
  1369. # We use the return code 3 before 2 to avoid changing the
  1370. # semantic meanings of return codes.
  1371. return 3
  1372. if transport_url:
  1373. cell_mapping.transport_url = transport_url
  1374. if db_connection:
  1375. cell_mapping.database_connection = db_connection
  1376. if disable and enable:
  1377. print(_('Cell cannot be disabled and enabled at the same time.'))
  1378. return 4
  1379. if disable or enable:
  1380. if cell_mapping.is_cell0():
  1381. print(_('Cell0 cannot be disabled.'))
  1382. return 5
  1383. elif disable and not cell_mapping.disabled:
  1384. cell_mapping.disabled = True
  1385. elif enable and cell_mapping.disabled:
  1386. cell_mapping.disabled = False
  1387. elif disable and cell_mapping.disabled:
  1388. print(_('Cell %s is already disabled') % cell_uuid)
  1389. elif enable and not cell_mapping.disabled:
  1390. print(_('Cell %s is already enabled') % cell_uuid)
  1391. try:
  1392. cell_mapping.save()
  1393. except Exception as e:
  1394. print(_('Unable to update CellMapping: %s') % e)
  1395. return 2
  1396. return 0
  1397. @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
  1398. help=_('The uuid of the cell.'))
  1399. def list_hosts(self, cell_uuid=None):
  1400. """Lists the hosts in one or all v2 cells."""
  1401. ctxt = context.get_admin_context()
  1402. if cell_uuid:
  1403. # Find the CellMapping given the uuid.
  1404. try:
  1405. cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
  1406. except exception.CellMappingNotFound:
  1407. print(_('Cell with uuid %s was not found.') % cell_uuid)
  1408. return 1
  1409. host_mappings = objects.HostMappingList.get_by_cell_id(
  1410. ctxt, cell_mapping.id)
  1411. else:
  1412. host_mappings = objects.HostMappingList.get_all(ctxt)
  1413. field_names = [_('Cell Name'), _('Cell UUID'), _('Hostname')]
  1414. t = prettytable.PrettyTable(field_names)
  1415. for host in sorted(host_mappings, key=lambda _host: _host.host):
  1416. fields = [host.cell_mapping.name, host.cell_mapping.uuid,
  1417. host.host]
  1418. t.add_row(fields)
  1419. print(t)
  1420. return 0
  1421. @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
  1422. required=True, help=_('The uuid of the cell.'))
  1423. @args('--host', metavar='<host>', dest='host',
  1424. required=True, help=_('The host to delete.'))
  1425. def delete_host(self, cell_uuid, host):
  1426. """Delete a host in a cell (host mappings) by the given host name
  1427. This command will return a non-zero exit code in the following cases.
  1428. * The cell is not found by uuid.
  1429. * The host is not found by host name.
  1430. * The host is not in the cell.
  1431. * The host has instances.
  1432. Returns 0 if the host is deleted successfully.
  1433. """
  1434. ctxt = context.get_admin_context()
  1435. # Find the CellMapping given the uuid.
  1436. try:
  1437. cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
  1438. except exception.CellMappingNotFound:
  1439. print(_('Cell with uuid %s was not found.') % cell_uuid)
  1440. return 1
  1441. try:
  1442. host_mapping = objects.HostMapping.get_by_host(ctxt, host)
  1443. except exception.HostMappingNotFound:
  1444. print(_('The host %s was not found.') % host)
  1445. return 2
  1446. if host_mapping.cell_mapping.uuid != cell_mapping.uuid:
  1447. print(_('The host %(host)s was not found '
  1448. 'in the cell %(cell_uuid)s.') % {'host': host,
  1449. 'cell_uuid': cell_uuid})
  1450. return 3
  1451. with context.target_cell(ctxt, cell_mapping) as cctxt:
  1452. instances = objects.InstanceList.get_by_host(cctxt, host)
  1453. try:
  1454. nodes = objects.ComputeNodeList.get_all_by_host(cctxt, host)
  1455. except exception.ComputeHostNotFound:
  1456. nodes = []
  1457. if instances:
  1458. print(_('There are instances on the host %s.') % host)
  1459. return 4
  1460. for node in nodes:
  1461. node.mapped = 0
  1462. node.save()
  1463. host_mapping.destroy()
  1464. return 0
  1465. class PlacementCommands(object):
  1466. """Commands for managing placement resources."""
  1467. @staticmethod
  1468. def _get_compute_node_uuid(ctxt, instance, node_cache):
  1469. """Find the ComputeNode.uuid for the given Instance
  1470. :param ctxt: cell-targeted nova.context.RequestContext
  1471. :param instance: the instance to lookup a compute node
  1472. :param node_cache: dict of Instance.node keys to ComputeNode.uuid
  1473. values; this cache is updated if a new node is processed.
  1474. :returns: ComputeNode.uuid for the given instance
  1475. :raises: nova.exception.ComputeHostNotFound
  1476. """
  1477. if instance.node in node_cache:
  1478. return node_cache[instance.node]
  1479. compute_node = objects.ComputeNode.get_by_host_and_nodename(
  1480. ctxt, instance.host, instance.node)
  1481. node_uuid = compute_node.uuid
  1482. node_cache[instance.node] = node_uuid
  1483. return node_uuid
  1484. def _heal_missing_alloc(self, ctxt, instance, node_cache):
  1485. node_uuid = self._get_compute_node_uuid(
  1486. ctxt, instance, node_cache)
  1487. # Now get the resource allocations for the instance based
  1488. # on its embedded flavor.
  1489. resources = scheduler_utils.resources_from_flavor(
  1490. instance, instance.flavor)
  1491. payload = {
  1492. 'allocations': {
  1493. node_uuid: {'resources': resources},
  1494. },
  1495. 'project_id': instance.project_id,
  1496. 'user_id': instance.user_id,
  1497. 'consumer_generation': None
  1498. }
  1499. return payload
  1500. def _heal_missing_project_and_user_id(self, allocations, instance):
  1501. allocations['project_id'] = instance.project_id
  1502. allocations['user_id'] = instance.user_id
  1503. return allocations
  1504. def _heal_allocations_for_instance(self, ctxt, instance, node_cache,
  1505. output, placement, dry_run):
  1506. """Checks the given instance to see if it needs allocation healing
  1507. :param ctxt: cell-targeted nova.context.RequestContext
  1508. :param instance: the instance to check for allocation healing
  1509. :param node_cache: dict of Instance.node keys to ComputeNode.uuid
  1510. values; this cache is updated if a new node is processed.
  1511. :param outout: function that takes a single message for verbose output
  1512. :param placement: nova.scheduler.client.report.SchedulerReportClient
  1513. to communicate with the Placement service API.
  1514. :param dry_run: Process instances and print output but do not commit
  1515. any changes.
  1516. :return: True if allocations were created or updated for the instance,
  1517. None if nothing needed to be done
  1518. :raises: nova.exception.ComputeHostNotFound if a compute node for a
  1519. given instance cannot be found
  1520. :raises: AllocationCreateFailed if unable to create allocations for
  1521. a given instance against a given compute node resource provider
  1522. :raises: AllocationUpdateFailed if unable to update allocations for
  1523. a given instance with consumer project/user information
  1524. """
  1525. if instance.task_state is not None:
  1526. output(_('Instance %(instance)s is undergoing a task '
  1527. 'state transition: %(task_state)s') %
  1528. {'instance': instance.uuid,
  1529. 'task_state': instance.task_state})
  1530. return
  1531. if instance.node is None:
  1532. output(_('Instance %s is not on a host.') % instance.uuid)
  1533. return
  1534. try:
  1535. allocations = placement.get_allocs_for_consumer(
  1536. ctxt, instance.uuid)
  1537. except (ks_exc.ClientException,
  1538. exception.ConsumerAllocationRetrievalFailed) as e:
  1539. raise exception.AllocationUpdateFailed(
  1540. consumer_uuid=instance.uuid,
  1541. error=_("Allocation retrieval failed: %s") % e)
  1542. need_healing = False
  1543. # Placement response can have an empty {'allocations': {}} in it if
  1544. # there are no allocations for the instance
  1545. if not allocations.get('allocations'):
  1546. # This instance doesn't have allocations
  1547. need_healing = 'Create'
  1548. allocations = self._heal_missing_alloc(ctxt, instance, node_cache)
  1549. if (allocations.get('project_id') != instance.project_id or
  1550. allocations.get('user_id') != instance.user_id):
  1551. # We have an instance with allocations but not the correct
  1552. # project_id/user_id, so we want to update the allocations
  1553. # and re-put them. We don't use put_allocations here
  1554. # because we don't want to mess up shared or nested
  1555. # provider allocations.
  1556. need_healing = 'Update'
  1557. allocations = self._heal_missing_project_and_user_id(
  1558. allocations, instance)
  1559. if need_healing:
  1560. if dry_run:
  1561. output(_('[dry-run] %(operation)s allocations for instance '
  1562. '%(instance)s: %(allocations)s') %
  1563. {'operation': need_healing,
  1564. 'instance': instance.uuid,
  1565. 'allocations': allocations})
  1566. else:
  1567. resp = placement.put_allocations(ctxt, instance.uuid,
  1568. allocations)
  1569. if resp:
  1570. output(_('Successfully %(operation)sd allocations for '
  1571. 'instance %(instance)s.') %
  1572. {'operation': need_healing.lower(),
  1573. 'instance': instance.uuid})
  1574. return True
  1575. else:
  1576. raise exception.AllocationUpdateFailed(
  1577. consumer_uuid=instance.uuid, error='')
  1578. else:
  1579. output(_('Instance %s already has allocations with '
  1580. 'matching consumer project/user.') % instance.uuid)
  1581. return
  1582. def _heal_instances_in_cell(self, ctxt, max_count, unlimited, output,
  1583. placement, dry_run, instance_uuid):
  1584. """Checks for instances to heal in a given cell.
  1585. :param ctxt: cell-targeted nova.context.RequestContext
  1586. :param max_count: batch size (limit per instance query)
  1587. :param unlimited: True if all instances in the cell should be
  1588. processed, else False to just process $max_count instances
  1589. :param outout: function that takes a single message for verbose output
  1590. :param placement: nova.scheduler.client.report.SchedulerReportClient
  1591. to communicate with the Placement service API.
  1592. :param dry_run: Process instances and print output but do not commit
  1593. any changes.
  1594. :param instance_uuid: UUID of a specific instance to process.
  1595. :return: Number of instances that had allocations created.
  1596. :raises: nova.exception.ComputeHostNotFound if a compute node for a
  1597. given instance cannot be found
  1598. :raises: AllocationCreateFailed if unable to create allocations for
  1599. a given instance against a given compute node resource provider
  1600. :raises: AllocationUpdateFailed if unable to update allocations for
  1601. a given instance with consumer project/user information
  1602. """
  1603. # Keep a cache of instance.node to compute node resource provider UUID.
  1604. # This will save some queries for non-ironic instances to the
  1605. # compute_nodes table.
  1606. node_cache = {}
  1607. # Track the total number of instances that have allocations created
  1608. # for them in this cell. We return when num_processed equals max_count
  1609. # and unlimited=True or we exhaust the number of instances to process
  1610. # in this cell.
  1611. num_processed = 0
  1612. # Get all instances from this cell which have a host and are not
  1613. # undergoing a task state transition. Go from oldest to newest.
  1614. # NOTE(mriedem): Unfortunately we don't have a marker to use
  1615. # between runs where the user is specifying --max-count.
  1616. # TODO(mriedem): Store a marker in system_metadata so we can
  1617. # automatically pick up where we left off without the user having
  1618. # to pass it in (if unlimited is False).
  1619. filters = {'deleted': False}
  1620. if instance_uuid:
  1621. filters['uuid'] = instance_uuid
  1622. instances = objects.InstanceList.get_by_filters(
  1623. ctxt, filters=filters, sort_key='created_at', sort_dir='asc',
  1624. limit=max_count, expected_attrs=['flavor'])
  1625. while instances:
  1626. output(_('Found %s candidate instances.') % len(instances))
  1627. # For each instance in this list, we need to see if it has
  1628. # allocations in placement and if so, assume it's correct and
  1629. # continue.
  1630. for instance in instances:
  1631. if self._heal_allocations_for_instance(
  1632. ctxt, instance, node_cache, output, placement,
  1633. dry_run):
  1634. num_processed += 1
  1635. # Make sure we don't go over the max count. Note that we
  1636. # don't include instances that already have allocations in the
  1637. # max_count number, only the number of instances that have
  1638. # successfully created allocations.
  1639. # If a specific instance was requested we return here as well.
  1640. if (not unlimited and num_processed == max_count) or instance_uuid:
  1641. return num_processed
  1642. # Use a marker to get the next page of instances in this cell.
  1643. # Note that InstanceList doesn't support slice notation.
  1644. marker = instances[len(instances) - 1].uuid
  1645. instances = objects.InstanceList.get_by_filters(
  1646. ctxt, filters=filters, sort_key='created_at', sort_dir='asc',
  1647. limit=max_count, marker=marker, expected_attrs=['flavor'])
  1648. return num_processed
  1649. @action_description(
  1650. _("Iterates over non-cell0 cells looking for instances which do "
  1651. "not have allocations in the Placement service, or have incomplete "
  1652. "consumer project_id/user_id values in existing allocations, and "
  1653. "which are not undergoing a task state transition. For each "
  1654. "instance found, allocations are created (or updated) against the "
  1655. "compute node resource provider for that instance based on the "
  1656. "flavor associated with the instance. This command requires that "
  1657. "the [api_database]/connection and [placement] configuration "
  1658. "options are set."))
  1659. @args('--max-count', metavar='<max_count>', dest='max_count',
  1660. help='Maximum number of instances to process. If not specified, all '
  1661. 'instances in each cell will be mapped in batches of 50. '
  1662. 'If you have a large number of instances, consider specifying '
  1663. 'a custom value and run the command until it exits with '
  1664. '0 or 4.')
  1665. @args('--verbose', action='store_true', dest='verbose', default=False,
  1666. help='Provide verbose output during execution.')
  1667. @args('--dry-run', action='store_true', dest='dry_run', default=False,
  1668. help='Runs the command and prints output but does not commit any '
  1669. 'changes. The return code should be 4.')
  1670. @args('--instance', metavar='<instance_uuid>', dest='instance_uuid',
  1671. help='UUID of a specific instance to process. If specified '
  1672. '--max-count has no effect.')
  1673. def heal_allocations(self, max_count=None, verbose=False, dry_run=False,
  1674. instance_uuid=None):
  1675. """Heals instance allocations in the Placement service
  1676. Return codes:
  1677. * 0: Command completed successfully and allocations were created.
  1678. * 1: --max-count was reached and there are more instances to process.
  1679. * 2: Unable to find a compute node record for a given instance.
  1680. * 3: Unable to create (or update) allocations for an instance against
  1681. its compute node resource provider.
  1682. * 4: Command completed successfully but no allocations were created.
  1683. * 127: Invalid input.
  1684. """
  1685. # NOTE(mriedem): Thoughts on ways to expand this:
  1686. # - allow passing a specific cell to heal
  1687. # - allow filtering on enabled/disabled cells
  1688. # - add a force option to force allocations for instances which have
  1689. # task_state is not None (would get complicated during a migration);
  1690. # for example, this could cleanup ironic instances that have
  1691. # allocations on VCPU/MEMORY_MB/DISK_GB but are now using a custom
  1692. # resource class
  1693. # - add an option to overwrite allocations for instances which already
  1694. # have allocations (but the operator thinks might be wrong?); this
  1695. # would probably only be safe with a specific instance.
  1696. # - deal with nested resource providers?
  1697. output = lambda msg: None
  1698. if verbose:
  1699. output = lambda msg: print(msg)
  1700. # TODO(mriedem): Rather than --max-count being both a total and batch
  1701. # count, should we have separate options to be specific, i.e. --total
  1702. # and --batch-size? Then --batch-size defaults to 50 and --total
  1703. # defaults to None to mean unlimited.
  1704. if instance_uuid:
  1705. max_count = 1
  1706. unlimited = False
  1707. elif max_count is not None:
  1708. try:
  1709. max_count = int(max_count)
  1710. except ValueError:
  1711. max_count = -1
  1712. unlimited = False
  1713. if max_count < 1:
  1714. print(_('Must supply a positive integer for --max-count.'))
  1715. return 127
  1716. else:
  1717. max_count = 50
  1718. unlimited = True
  1719. output(_('Running batches of %i until complete') % max_count)
  1720. ctxt = context.get_admin_context()
  1721. # If we are going to process a specific instance, just get the cell
  1722. # it is in up front.
  1723. if instance_uuid:
  1724. try:
  1725. im = objects.InstanceMapping.get_by_instance_uuid(
  1726. ctxt, instance_uuid)
  1727. cells = objects.CellMappingList(objects=[im.cell_mapping])
  1728. except exception.InstanceMappingNotFound:
  1729. print('Unable to find cell for instance %s, is it mapped? Try '
  1730. 'running "nova-manage cell_v2 verify_instance" or '
  1731. '"nova-manage cell_v2 map_instances".' %
  1732. instance_uuid)
  1733. return 127
  1734. else:
  1735. cells = objects.CellMappingList.get_all(ctxt)
  1736. if not cells:
  1737. output(_('No cells to process.'))
  1738. return 4
  1739. placement = report.SchedulerReportClient()
  1740. num_processed = 0
  1741. # TODO(mriedem): Use context.scatter_gather_skip_cell0.
  1742. for cell in cells:
  1743. # Skip cell0 since that is where instances go that do not get
  1744. # scheduled and hence would not have allocations against a host.
  1745. if cell.uuid == objects.CellMapping.CELL0_UUID:
  1746. continue
  1747. output(_('Looking for instances in cell: %s') % cell.identity)
  1748. limit_per_cell = max_count
  1749. if not unlimited:
  1750. # Adjust the limit for the next cell. For example, if the user
  1751. # only wants to process a total of 100 instances and we did
  1752. # 75 in cell1, then we only need 25 more from cell2 and so on.
  1753. limit_per_cell = max_count - num_processed
  1754. with context.target_cell(ctxt, cell) as cctxt:
  1755. try:
  1756. num_processed += self._heal_instances_in_cell(
  1757. cctxt, limit_per_cell, unlimited, output, placement,
  1758. dry_run, instance_uuid)
  1759. except exception.ComputeHostNotFound as e:
  1760. print(e.format_message())
  1761. return 2
  1762. except (exception.AllocationCreateFailed,
  1763. exception.AllocationUpdateFailed) as e:
  1764. print(e.format_message())
  1765. return 3
  1766. # Make sure we don't go over the max count. Note that we
  1767. # don't include instances that already have allocations in the
  1768. # max_count number, only the number of instances that have
  1769. # successfully created allocations.
  1770. # If a specific instance was provided then we'll just exit
  1771. # the loop and process it below (either return 4 or 0).
  1772. if num_processed == max_count and not instance_uuid:
  1773. output(_('Max count reached. Processed %s instances.')
  1774. % num_processed)
  1775. return 1
  1776. output(_('Processed %s instances.') % num_processed)
  1777. if not num_processed:
  1778. return 4
  1779. return 0
  1780. @staticmethod
  1781. def _get_rp_uuid_for_host(ctxt, host):
  1782. """Finds the resource provider (compute node) UUID for the given host.
  1783. :param ctxt: cell-targeted nova RequestContext
  1784. :param host: name of the compute host
  1785. :returns: The UUID of the resource provider (compute node) for the host
  1786. :raises: nova.exception.HostMappingNotFound if no host_mappings record
  1787. is found for the host; indicates
  1788. "nova-manage cell_v2 discover_hosts" needs to be run on the cell.
  1789. :raises: nova.exception.ComputeHostNotFound if no compute_nodes record
  1790. is found in the cell database for the host; indicates the
  1791. nova-compute service on that host might need to be restarted.
  1792. :raises: nova.exception.TooManyComputesForHost if there are more than
  1793. one compute_nodes records in the cell database for the host which
  1794. is only possible (under normal circumstances) for ironic hosts but
  1795. ironic hosts are not currently supported with host aggregates so
  1796. if more than one compute node is found for the host, it is
  1797. considered an error which the operator will need to resolve
  1798. manually.
  1799. """
  1800. # Get the host mapping to determine which cell it's in.
  1801. hm = objects.HostMapping.get_by_host(ctxt, host)
  1802. # Now get the compute node record for the host from the cell.
  1803. with context.target_cell(ctxt, hm.cell_mapping) as cctxt:
  1804. # There should really only be one, since only ironic
  1805. # hosts can have multiple nodes, and you can't have
  1806. # ironic hosts in aggregates for that reason. If we
  1807. # find more than one, it's an error.
  1808. nodes = objects.ComputeNodeList.get_all_by_host(
  1809. cctxt, host)
  1810. if len(nodes) > 1:
  1811. # This shouldn't happen, so we need to bail since we
  1812. # won't know which node to use.
  1813. raise exception.TooManyComputesForHost(
  1814. num_computes=len(nodes), host=host)
  1815. return nodes[0].uuid
  1816. @action_description(
  1817. _("Mirrors compute host aggregates to resource provider aggregates "
  1818. "in the Placement service. Requires the [api_database] and "
  1819. "[placement] sections of the nova configuration file to be "
  1820. "populated."))
  1821. @args('--verbose', action='store_true', dest='verbose', default=False,
  1822. help='Provide verbose output during execution.')
  1823. # TODO(mriedem): Add an option for the 'remove aggregate' behavior.
  1824. # We know that we want to mirror hosts aggregate membership to
  1825. # placement, but regarding removal, what if the operator or some external
  1826. # tool added the resource provider to an aggregate but there is no matching
  1827. # host aggregate, e.g. ironic nodes or shared storage provider
  1828. # relationships?
  1829. # TODO(mriedem): Probably want an option to pass a specific host instead of
  1830. # doing all of them.
  1831. def sync_aggregates(self, verbose=False):
  1832. """Synchronizes nova host aggregates with resource provider aggregates
  1833. Adds nodes to missing provider aggregates in Placement.
  1834. NOTE: Depending on the size of your deployment and the number of
  1835. compute hosts in aggregates, this command could cause a non-negligible
  1836. amount of traffic to the placement service and therefore is
  1837. recommended to be run during maintenance windows.
  1838. Return codes:
  1839. * 0: Successful run
  1840. * 1: A host was found with more than one matching compute node record
  1841. * 2: An unexpected error occurred while working with the placement API
  1842. * 3: Failed updating provider aggregates in placement
  1843. * 4: Host mappings not found for one or more host aggregate members
  1844. * 5: Compute node records not found for one or more hosts
  1845. * 6: Resource provider not found by uuid for a given host
  1846. """
  1847. # Start by getting all host aggregates.
  1848. ctxt = context.get_admin_context()
  1849. aggregate_api = compute_api.AggregateAPI()
  1850. placement = aggregate_api.placement_client
  1851. aggregates = aggregate_api.get_aggregate_list(ctxt)
  1852. # Now we're going to loop over the existing compute hosts in aggregates
  1853. # and check to see if their corresponding resource provider, found via
  1854. # the host's compute node uuid, are in the same aggregate. If not, we
  1855. # add the resource provider to the aggregate in Placement.
  1856. output = lambda msg: None
  1857. if verbose:
  1858. output = lambda msg: print(msg)
  1859. output(_('Filling in missing placement aggregates'))
  1860. # Since hosts can be in more than one aggregate, keep track of the host
  1861. # to its corresponding resource provider uuid to avoid redundant
  1862. # lookups.
  1863. host_to_rp_uuid = {}
  1864. unmapped_hosts = set() # keep track of any missing host mappings
  1865. computes_not_found = set() # keep track of missing nodes
  1866. providers_not_found = {} # map of hostname to missing provider uuid
  1867. for aggregate in aggregates:
  1868. output(_('Processing aggregate: %s') % aggregate.name)
  1869. for host in aggregate.hosts:
  1870. output(_('Processing host: %s') % host)
  1871. rp_uuid = host_to_rp_uuid.get(host)
  1872. if not rp_uuid:
  1873. try:
  1874. rp_uuid = self._get_rp_uuid_for_host(ctxt, host)
  1875. host_to_rp_uuid[host] = rp_uuid
  1876. except exception.HostMappingNotFound:
  1877. # Don't fail on this now, we can dump it at the end.
  1878. unmapped_hosts.add(host)
  1879. continue
  1880. except exception.ComputeHostNotFound:
  1881. # Don't fail on this now, we can dump it at the end.
  1882. computes_not_found.add(host)
  1883. continue
  1884. except exception.TooManyComputesForHost as e:
  1885. # TODO(mriedem): Should we treat this like the other
  1886. # errors and not fail immediately but dump at the end?
  1887. print(e.format_message())
  1888. return 1
  1889. # We've got our compute node record, so now we can ensure that
  1890. # the matching resource provider, found via compute node uuid,
  1891. # is in the same aggregate in placement, found via aggregate
  1892. # uuid.
  1893. try:
  1894. placement.aggregate_add_host(ctxt, aggregate.uuid,
  1895. rp_uuid=rp_uuid)
  1896. output(_('Successfully added host (%(host)s) and '
  1897. 'provider (%(provider)s) to aggregate '
  1898. '(%(aggregate)s).') %
  1899. {'host': host, 'provider': rp_uuid,
  1900. 'aggregate': aggregate.uuid})
  1901. except exception.ResourceProviderNotFound:
  1902. # The resource provider wasn't found. Store this for later.
  1903. providers_not_found[host] = rp_uuid
  1904. except exception.ResourceProviderAggregateRetrievalFailed as e:
  1905. print(e.message)
  1906. return 2
  1907. except exception.NovaException as e:
  1908. # The exception message is too generic in this case
  1909. print(_('Failed updating provider aggregates for '
  1910. 'host (%(host)s), provider (%(provider)s) '
  1911. 'and aggregate (%(aggregate)s). Error: '
  1912. '%(error)s') %
  1913. {'host': host, 'provider': rp_uuid,
  1914. 'aggregate': aggregate.uuid,
  1915. 'error': e.message})
  1916. return 3
  1917. # Now do our error handling. Note that there is no real priority on
  1918. # the error code we return. We want to dump all of the issues we hit
  1919. # so the operator can fix them before re-running the command, but
  1920. # whether we return 4 or 5 or 6 doesn't matter.
  1921. return_code = 0
  1922. if unmapped_hosts:
  1923. print(_('The following hosts were found in nova host aggregates '
  1924. 'but no host mappings were found in the nova API DB. Run '
  1925. '"nova-manage cell_v2 discover_hosts" and then retry. '
  1926. 'Missing: %s') % ','.join(unmapped_hosts))
  1927. return_code = 4
  1928. if computes_not_found:
  1929. print(_('Unable to find matching compute_nodes record entries in '
  1930. 'the cell database for the following hosts; does the '
  1931. 'nova-compute service on each host need to be restarted? '
  1932. 'Missing: %s') % ','.join(computes_not_found))
  1933. return_code = 5
  1934. if providers_not_found:
  1935. print(_('Unable to find matching resource provider record in '
  1936. 'placement with uuid for the following hosts: %s. Try '
  1937. 'restarting the nova-compute service on each host and '
  1938. 'then retry.') %
  1939. ','.join('(%s=%s)' % (host, providers_not_found[host])
  1940. for host in sorted(providers_not_found.keys())))
  1941. return_code = 6
  1942. return return_code
  1943. CATEGORIES = {
  1944. 'api_db': ApiDbCommands,
  1945. 'cell_v2': CellV2Commands,
  1946. 'db': DbCommands,
  1947. 'floating': FloatingIpCommands,
  1948. 'network': NetworkCommands,
  1949. 'placement': PlacementCommands
  1950. }
  1951. add_command_parsers = functools.partial(cmd_common.add_command_parsers,
  1952. categories=CATEGORIES)
  1953. category_opt = cfg.SubCommandOpt('category',
  1954. title='Command categories',
  1955. help='Available categories',
  1956. handler=add_command_parsers)
  1957. post_mortem_opt = cfg.BoolOpt('post-mortem',
  1958. default=False,
  1959. help='Allow post-mortem debugging')
  1960. def main():
  1961. """Parse options and call the appropriate class/method."""
  1962. CONF.register_cli_opts([category_opt, post_mortem_opt])
  1963. config.parse_args(sys.argv)
  1964. logging.set_defaults(
  1965. default_log_levels=logging.get_default_log_levels() +
  1966. _EXTRA_DEFAULT_LOG_LEVELS)
  1967. logging.setup(CONF, "nova")
  1968. objects.register_all()
  1969. if CONF.category.name == "version":
  1970. print(version.version_string_with_package())
  1971. return 0
  1972. if CONF.category.name == "bash-completion":
  1973. cmd_common.print_bash_completion(CATEGORIES)
  1974. return 0
  1975. try:
  1976. fn, fn_args, fn_kwargs = cmd_common.get_action_fn()
  1977. ret = fn(*fn_args, **fn_kwargs)
  1978. rpc.cleanup()
  1979. return ret
  1980. except Exception:
  1981. if CONF.post_mortem:
  1982. import pdb
  1983. pdb.post_mortem()
  1984. else:
  1985. print(_("An error has occurred:\n%s") % traceback.format_exc())
  1986. return 1