Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4901bc32cf
Fetching contributors…

Cannot retrieve contributors at this time

93 lines (79 sloc) 2.789 kb
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 Sina Corporation
# All Rights Reserved.
# Author: YuWei Peng <>
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sys
import time
import ConfigParser
import json
from collections import OrderedDict
from sqlalchemy import *
#from sqlalchemy.orm import sessionmaker
from import *
class NovaTools():
def __init__(self, app): = app
self.cfg ="DEFAULT")
self.db = create_engine(self.cfg["sql_connection"])
self.db.echo = False
metadata = MetaData(self.db)
self.instances = Table('instances', metadata, autoload=True)
def get_uuid_by_ip(self, ip):
return None
def get_uuid_by_id(self, id):
ret = self.get_instances(id=id)
if ret is None:
return None
return ret[1]
def get_uuid_by_novaid(self, instance):
ret = self.get_id(instance)
if ret is None:
return None
ret = self.get_instances(id=ret)
if ret is None:
return None
return ret[1]
def get_id(self, instance):
if len(instance) < 17:
return None
ret = None
ret = int(instance[9:17], 16)
ret = None
return ret
def get_instances(self, uuid=None, id=None):
"return format: (id, uuid, display_name) or None"
if not uuid is None:
stmt = == uuid)
elif not id is None:
stmt = == id)
return None
rs = stmt.execute()
if not rs is None:
row = rs.first()
if row is None:
return None
return (row[4], row[34], row[26])
return None
if __name__ == '__main__':
app = App(conf="/etc/nova/nova.conf", name='kanyun')
tool = NovaTools(app)
rs = tool.get_instances(uuid='76d6f296-68b8-4a21-9a20-9f46ff48d6ef')
print rs
print tool.get_id("instance-00000138")
Jump to Line
Something went wrong with that request. Please try again.