views.py
import MySQLdb
db = MySQLdb.connect(user='nova', db='nova', passwd='password', host='localhost')
cursor = db.cursor()
sql= 'SELECT host FROM services WHERE id IN (SELECT service_id FROM compute_nodes) '
cursor.execute(sql)
names = [row[0] for row in cursor.fetchall()]
db.close() 到此数据库操作完毕
search_opts['limit'] = 1
for na in names:
search_opts['host'] = na
LOG.error(search_opts)
try:
instances = api.nova.server_list_all(self.request,search_opts, all_tenants=True)
except:
exceptions.handle(self.request,
_('Unable to retrieve instance list.'))
for inst in instances:
LOG.error(inst.host_name)
instances2.append(inst)