慎重!慎重!照這個(gè)方法刪除cell后,會(huì)導(dǎo)致已存在的虛擬機(jī)無(wú)法獲取詳情!
此文后續(xù) http://www.itdecent.cn/p/48e1451cf7dc
自從為了配置高可用集群,給OpenStack裝了雙控制節(jié)點(diǎn)后,就遇到了一個(gè)奇怪的現(xiàn)象:

所有計(jì)算節(jié)點(diǎn)都出現(xiàn)了兩次!
當(dāng)時(shí)想排查錯(cuò)誤,但一來(lái)沒(méi)頭緒,二來(lái)任務(wù)緊急,淺淺嘗試一下就放棄排查了。好在也不影響使用,就這么用了下去,直到有一天!
直到有一天,我運(yùn)行了如下命令:
nova-manage cell_v2 discover_hosts --verbose
看到如下輸出時(shí),突然產(chǎn)生疑惑:

為什么出現(xiàn)了兩次cell?
好像隱隱預(yù)感到了什么!
連忙輸入如下命令查看一下:
nova-manage cell_v2 list_cells
見(jiàn)下圖:

果然,有一個(gè)兄弟跟 “正統(tǒng)”的cell1 長(zhǎng)得很像!
它應(yīng)該就是罪魁禍?zhǔn)琢耍?/p>
那么這個(gè)Name是None的兄弟,是怎么混進(jìn)來(lái)的?
我一看就明白了,原來(lái)它曾經(jīng)也是一個(gè)正統(tǒng)的cell1,畢竟它的數(shù)據(jù)庫(kù)配置中還@了曾經(jīng)的controller,可是改朝換代后,原來(lái)的單控制節(jié)點(diǎn) controller 由虛擬IP對(duì)應(yīng)的hostname controllerv 代替。在配置第二個(gè)控制節(jié)點(diǎn)時(shí),我又創(chuàng)了一個(gè)cell1,導(dǎo)致原來(lái)的cell1變成了無(wú)名無(wú)姓的None。
既然你沒(méi)有用了,就把你刪掉吧,于是我:
nova-manage cell_v2 delete_cell --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad
可是:

意思是你根深蒂固,我除不了你了唄?
不行,我又:
nova-manage cell_v2 delete_cell --force --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad
結(jié)果還是:

好了,原來(lái)這個(gè)force是假的 https://bugs.launchpad.net/nova/+bug/1721179
不行,我要把你連根拔起:
nova-manage cell_v2 delete_host --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad --host compute1
結(jié)果:

好了好了,惹不起!只能祭出終極武器了,改一下源碼。
來(lái)到 /usr/lib/python2.7/dist-packages/nova/cmd/manage.py
找到 delete_cell 方法:
@args('--force', action='store_true', default=False,
help=_('Delete hosts that belong to the cell as well.'))
@args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
required=True, help=_('The uuid of the cell to delete.'))
def delete_cell(self, cell_uuid, force=False):
"""Delete an empty cell by the given uuid.
This command will return a non-zero exit code in the following cases.
* The cell is not found by uuid.
* It has hosts and force is False.
* It has instance mappings.
If force is True and the cell has host, hosts are deleted as well.
Returns 0 in the following cases.
* The empty cell is found and deleted successfully.
* The cell has hosts and force is True and the cell and the hosts are
deleted successfully.
"""
ctxt = context.get_admin_context()
# Find the CellMapping given the uuid.
try:
cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
except exception.CellMappingNotFound:
print(_('Cell with uuid %s was not found.') % cell_uuid)
return 1
# Check to see if there are any HostMappings for this cell.
host_mappings = objects.HostMappingList.get_by_cell_id(
ctxt, cell_mapping.id)
nodes = []
if host_mappings:
if not force:
print(_('There are existing hosts mapped to cell with uuid '
'%s.') % cell_uuid)
return 2
# We query for the compute nodes in the cell,
# so that they can be unmapped.
with context.target_cell(ctxt, cell_mapping) as cctxt:
nodes = objects.ComputeNodeList.get_all(cctxt)
# Check to see if there are any InstanceMappings for this cell.
instance_mappings = objects.InstanceMappingList.get_by_cell_id(
ctxt, cell_mapping.id)
if instance_mappings:
print(_('There are existing instances mapped to cell with '
'uuid %s.') % cell_uuid)
return 3
# Unmap the compute nodes so that they can be discovered
# again in future, if needed.
for node in nodes:
node.mapped = 0
node.save()
# Delete hosts mapped to the cell.
for host_mapping in host_mappings:
host_mapping.destroy()
# There are no hosts or instances mapped to the cell so delete it.
cell_mapping.destroy()
return 0
把 return 2 和 return 3 的部分注釋掉!
直奔主題,即最后一句cell_mapping.destroy() !
好了,再刪一下:
root@controller:~# nova-manage cell_v2 delete_cell --force --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad
An error has occurred:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nova/cmd/manage.py", line 1868, in main
ret = fn(*fn_args, **fn_kwargs)
File "/usr/lib/python2.7/dist-packages/nova/cmd/manage.py", line 1713, in delete_cell
cell_mapping.destroy()
File "/usr/lib/python2.7/dist-packages/oslo_versionedobjects/base.py", line 226, in wrapper
return fn(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/nova/objects/cell_mapping.py", line 114, in destroy
self._destroy_in_db(self._context, self.uuid)
File "/usr/lib/python2.7/dist-packages/oslo_db/sqlalchemy/enginefacade.py", line 979, in wrapper
return fn(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/nova/objects/cell_mapping.py", line 108, in _destroy_in_db
uuid=uuid).delete()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 3212, in delete
delete_op.exec_()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 1179, in exec_
self._do_exec()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 1363, in _do_exec
mapper=self.mapper)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1107, in execute
bind, close_with_result=True).execute(clause, params or {})
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 945, in execute
return meth(self, multiparams, params)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1398, in _handle_dbapi_exception
util.raise_from_cause(newraise, exc_info)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "/usr/lib/python2.7/dist-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/usr/lib/python2.7/dist-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 856, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 1057, in _read_query_result
result.read()
File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 1340, in read
first_packet = self.connection._read_packet()
File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 1014, in _read_packet
packet.check_error()
File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/lib/python2.7/dist-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
DBReferenceError: (pymysql.err.IntegrityError) (1451, u'Cannot delete or update a parent row: a foreign key constraint fails (`nova_api`.`instance_mappings`, CONSTRAINT `instance_mappings_ibfk_1` FOREIGN KEY (`cell_id`) REFERENCES `cell_mappings` (`id`))') [SQL: u'DELETE FROM cell_mappings WHERE cell_mappings.uuid = %(uuid_1)s'] [parameters: {u'uuid_1': u'88d1334b-8794-4481-9207-aa12dae132ad'}]
雖然出錯(cuò)了!但看到最后一段,我知道故事已經(jīng)要走到結(jié)局了。
破局的關(guān)鍵就在于這張圖:

簡(jiǎn)單的說(shuō),就是我想在 nova_api 數(shù)據(jù)庫(kù)的 cell_mappings 表中刪除 uuid 為 88d1334b-8794-4481-9207-aa12dae132ad 的這位仁兄。
但這位仁兄在cell_mappings 表中的 id 和 instance_mappings 表中的 cell_id 有“裙帶關(guān)系” !刪不掉!
哦原來(lái)是上頭有人,那就簡(jiǎn)單了!
mysql -uroot -p
use nova_api;
找一下這位仁兄:
select * from cell_mappings where cell_mappings.uuid = '88d1334b-8794-4481-9207-aa12dae132ad';

找到了,id 是 2 !
然后,刪!
delete from instance_mappings where cell_id = '2';

再刪!
delete from cell_mappings where cell_mappings.uuid = '88d1334b-8794-4481-9207-aa12dae132ad';

好了,趕緊看一下:
nova-manage cell_v2 list_cells

同步下數(shù)據(jù)庫(kù),重啟下nova服務(wù):
nova-manage api_db sync
service nova-api restart
service nova-consoleauth restart
service nova-scheduler restart
service nova-conductor restart
service nova-novncproxy restart
又回到最初的起點(diǎn):
openstack compute service list

干凈了!
雖然好像并沒(méi)什么用,但也是時(shí)候?qū)W習(xí)一下nova中關(guān)于cell的知識(shí)了……