init git project
0 parents
Showing
19 changed files
with
777 additions
and
0 deletions
.gitignore
0 → 100644
| 1 | # Byte-compiled / optimized / DLL files | ||
| 2 | __pycache__/ | ||
| 3 | *.py[cod] | ||
| 4 | |||
| 5 | # C extensions | ||
| 6 | *.so | ||
| 7 | |||
| 8 | # Distribution / packaging | ||
| 9 | .Python | ||
| 10 | env/ | ||
| 11 | bin/ | ||
| 12 | build/ | ||
| 13 | develop-eggs/ | ||
| 14 | dist/ | ||
| 15 | eggs/ | ||
| 16 | lib/ | ||
| 17 | lib64/ | ||
| 18 | parts/ | ||
| 19 | sdist/ | ||
| 20 | var/ | ||
| 21 | *.egg-info/ | ||
| 22 | .installed.cfg | ||
| 23 | *.egg | ||
| 24 | |||
| 25 | # Installer logs | ||
| 26 | pip-log.txt | ||
| 27 | pip-delete-this-directory.txt | ||
| 28 | |||
| 29 | # Unit test / coverage reports | ||
| 30 | htmlcov/ | ||
| 31 | .tox/ | ||
| 32 | .coverage | ||
| 33 | .cache | ||
| 34 | nosetests.xml | ||
| 35 | coverage.xml | ||
| 36 | |||
| 37 | # Translations | ||
| 38 | *.mo | ||
| 39 | |||
| 40 | # Mr Developer | ||
| 41 | .mr.developer.cfg | ||
| 42 | .project | ||
| 43 | .pydevproject | ||
| 44 | |||
| 45 | # Rope | ||
| 46 | .ropeproject | ||
| 47 | |||
| 48 | # Django stuff: | ||
| 49 | *.log | ||
| 50 | *.pot | ||
| 51 | |||
| 52 | # Sphinx documentation | ||
| 53 | docs/_build/ | ||
| 54 | |||
| 55 | .idea/ | ||
| 56 | |||
| 57 | |||
| 58 | .settings/ | ||
| 59 | |||
| 60 | *.iml | ||
| 61 | |||
| 62 | # certificates | ||
| 63 | *.cer | ||
| 64 | *.pem | ||
| 65 | |||
| 66 | *guacamole.properties | ||
| 67 | *config.py | ||
| 68 | *.rb~ | ||
| 69 | *.erb~ | ||
| 70 | *.json~ | ||
| 71 | *.py~ | ||
| 72 | *.txt~ | ||
| 73 | *.log~ | ||
| 74 | *.log.* |
README.md
0 → 100644
requirement.txt
0 → 100644
run.py
0 → 100644
server/__init__.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | from flask import Flask | ||
| 6 | from flask_restful import Api | ||
| 7 | |||
| 8 | |||
| 9 | # initialize flask and flask restful | ||
| 10 | app = Flask(__name__) | ||
| 11 | app.config['SECRET_KEY'] = "myNameIsHuBian" | ||
| 12 | app.debug = True | ||
| 13 | |||
| 14 | api = Api(app) | ||
| 15 | |||
| 16 | from utils import init_factory | ||
| 17 | init_factory() | ||
| 18 | |||
| 19 | from views import init_routes | ||
| 20 | init_routes() |
server/database/__init__.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | |||
| 6 | from sqlalchemy import create_engine | ||
| 7 | from sqlalchemy.orm import scoped_session, sessionmaker | ||
| 8 | from sqlalchemy.ext.declarative import declarative_base | ||
| 9 | from db_adapters import SQLAlchemyAdapter | ||
| 10 | |||
| 11 | |||
| 12 | engine = create_engine('mysql://root:123456@localhost/sunnycloud', | ||
| 13 | convert_unicode=True, | ||
| 14 | pool_size=50, | ||
| 15 | max_overflow=100, | ||
| 16 | echo=False) | ||
| 17 | db_session = scoped_session(sessionmaker(autocommit=False, | ||
| 18 | autoflush=True, | ||
| 19 | bind=engine)) | ||
| 20 | Base = declarative_base() | ||
| 21 | Base.query = db_session.query_property() | ||
| 22 | db_adapter = SQLAlchemyAdapter(db_session) | ||
| 23 | |||
| 24 | from models import * | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
server/database/db_adapters.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | class SQLAlchemyAdapterMetaClass(type): | ||
| 6 | @staticmethod | ||
| 7 | def wrap(func): | ||
| 8 | """Return a wrapped instance method""" | ||
| 9 | |||
| 10 | def auto_commit(self, *args, **kwargs): | ||
| 11 | try: | ||
| 12 | # todo a trick for DB transaction issue | ||
| 13 | # self.commit() | ||
| 14 | return_value = func(self, *args, **kwargs) | ||
| 15 | self.commit() | ||
| 16 | return return_value | ||
| 17 | except: | ||
| 18 | self.rollback() | ||
| 19 | raise | ||
| 20 | |||
| 21 | return auto_commit | ||
| 22 | |||
| 23 | def __new__(cls, name, bases, attrs): | ||
| 24 | """If the method in this list, DON'T wrap it""" | ||
| 25 | no_wrap = ["commit", "merge", "rollback", "remove", "session"] | ||
| 26 | |||
| 27 | def wrap(method): | ||
| 28 | """private methods are not wrapped""" | ||
| 29 | if method not in no_wrap and not method.startswith("__"): | ||
| 30 | attrs[method] = cls.wrap(attrs[method]) | ||
| 31 | |||
| 32 | map(lambda m: wrap(m), attrs.keys()) | ||
| 33 | return super(SQLAlchemyAdapterMetaClass, cls).__new__(cls, name, bases, attrs) | ||
| 34 | |||
| 35 | |||
| 36 | class DBAdapter(object): | ||
| 37 | def __init__(self, db_session): | ||
| 38 | self.db_session = db_session | ||
| 39 | |||
| 40 | |||
| 41 | class SQLAlchemyAdapter(DBAdapter): | ||
| 42 | """Use MetaClass to make this class""" | ||
| 43 | __metaclass__ = SQLAlchemyAdapterMetaClass | ||
| 44 | |||
| 45 | def __init__(self, db_session): | ||
| 46 | super(SQLAlchemyAdapter, self).__init__(db_session) | ||
| 47 | |||
| 48 | # ------------------------------ methods that no need to wrap --- start ------------------------------ | ||
| 49 | |||
| 50 | def commit(self): | ||
| 51 | self.db_session.commit() | ||
| 52 | |||
| 53 | def remove(self): | ||
| 54 | self.db_session.remove() | ||
| 55 | |||
| 56 | def merge(self, obj): | ||
| 57 | self.db_session.merge(obj) | ||
| 58 | |||
| 59 | def rollback(self): | ||
| 60 | self.db_session.rollback() | ||
| 61 | |||
| 62 | def session(self): | ||
| 63 | return self.db_session | ||
| 64 | |||
| 65 | # ------------------------------ methods that no need to wrap --- end------------------------------ | ||
| 66 | |||
| 67 | # ------------------------------ auto wrapped 'public' methods --- start ------------------------------ | ||
| 68 | def get_object(self, ObjectClass, id): | ||
| 69 | """ Retrieve one object specified by the primary key 'pk' """ | ||
| 70 | return ObjectClass.query.get(id) | ||
| 71 | |||
| 72 | def find_all_objects(self, ObjectClass, *criterion): | ||
| 73 | return ObjectClass.query.filter(*criterion).all() | ||
| 74 | |||
| 75 | def find_all_objects_by(self, ObjectClass, **kwargs): | ||
| 76 | return ObjectClass.query.filter_by(**kwargs).all() | ||
| 77 | |||
| 78 | def find_all_objects_order_by(self, ObjectClass, limit=None, *order_by, **kwargs): | ||
| 79 | if limit is not None: | ||
| 80 | return ObjectClass.query.filter_by(**kwargs).order_by(*order_by).limit(limit) | ||
| 81 | else: | ||
| 82 | return ObjectClass.query.filter_by(**kwargs).order_by(*order_by).all() | ||
| 83 | |||
| 84 | def count(self, ObjectClass, *criterion): | ||
| 85 | return ObjectClass.query.filter(*criterion).count() | ||
| 86 | |||
| 87 | def count_by(self, ObjectClass, **kwargs): | ||
| 88 | return ObjectClass.query.filter_by(**kwargs).count() | ||
| 89 | |||
| 90 | def find_first_object(self, ObjectClass, *criterion): | ||
| 91 | return ObjectClass.query.filter(*criterion).first() | ||
| 92 | |||
| 93 | def find_first_object_by(self, ObjectClass, **kwargs): | ||
| 94 | return ObjectClass.query.filter_by(**kwargs).first() | ||
| 95 | |||
| 96 | |||
| 97 | def add_object(self, inst): | ||
| 98 | self.db_session.add(inst) | ||
| 99 | |||
| 100 | def add_object_kwargs(self, ObjectClass, **kwargs): | ||
| 101 | """ Add an object of class 'ObjectClass' with fields and values specified in '**kwargs'. """ | ||
| 102 | object = ObjectClass(**kwargs) | ||
| 103 | self.db_session.add(object) | ||
| 104 | return object | ||
| 105 | |||
| 106 | def update_object(self, object, **kwargs): | ||
| 107 | """ Update object 'object' with the fields and values specified in '**kwargs'. """ | ||
| 108 | for key, value in kwargs.items(): | ||
| 109 | if hasattr(object, key): | ||
| 110 | setattr(object, key, value) | ||
| 111 | else: | ||
| 112 | raise KeyError("Object '%s' has no field '%s'." % (type(object), key)) | ||
| 113 | |||
| 114 | def delete_object(self, instance): | ||
| 115 | """ Delete object 'object'. """ | ||
| 116 | self.db_session.delete(instance) | ||
| 117 | |||
| 118 | def delete_all_objects(self, ObjectClass, *criterion): | ||
| 119 | ObjectClass.query.filter(*criterion).delete(synchronize_session=False) | ||
| 120 | |||
| 121 | def delete_all_objects_by(self, ObjectClass, **kwargs): | ||
| 122 | """ Delete all objects matching the case sensitive filters in 'kwargs'. """ | ||
| 123 | |||
| 124 | # Convert each name/value pair in 'kwargs' into a filter | ||
| 125 | query = ObjectClass.query.filter_by(**kwargs) | ||
| 126 | |||
| 127 | # query filter by in_ do not support none args, use synchronize_session=False instead | ||
| 128 | return query.delete(synchronize_session=False) | ||
| 129 | |||
| 130 | # ------------------------------ auto wrapped 'public' methods --- end ------------------------------ |
server/database/models.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | import json | ||
| 6 | from . import Base, db_adapter | ||
| 7 | from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text, TypeDecorator | ||
| 8 | from sqlalchemy.orm import relation, backref | ||
| 9 | |||
| 10 | |||
| 11 | def relationship(*arg, **kw): | ||
| 12 | ret = relation(*arg, **kw) | ||
| 13 | db_adapter.commit() | ||
| 14 | return ret | ||
| 15 | |||
| 16 | |||
| 17 | def to_dic(inst, cls): | ||
| 18 | # add your coversions for things like datetime's | ||
| 19 | # and what-not that aren't serializable. | ||
| 20 | convert = dict() | ||
| 21 | # convert[TZDateTime] = date_serializer | ||
| 22 | |||
| 23 | d = dict() | ||
| 24 | for c in cls.__table__.columns: | ||
| 25 | v = getattr(inst, c.name) | ||
| 26 | if c.type.__class__ in convert.keys() and v is not None: | ||
| 27 | try: | ||
| 28 | func = convert[c.type.__class__] | ||
| 29 | d[c.name] = func(v) | ||
| 30 | except: | ||
| 31 | d[c.name] = "Error: Failed to covert using ", str(convert[c.type.__class__]) | ||
| 32 | else: | ||
| 33 | d[c.name] = v | ||
| 34 | return d | ||
| 35 | |||
| 36 | |||
| 37 | def to_json(inst, cls): | ||
| 38 | return json.dumps(to_dic(inst, cls)) | ||
| 39 | |||
| 40 | |||
| 41 | class DBBase(Base): | ||
| 42 | """ | ||
| 43 | DB model base class, providing basic functions | ||
| 44 | """ | ||
| 45 | __abstract__ = True | ||
| 46 | |||
| 47 | def __init__(self, **kwargs): | ||
| 48 | super(DBBase, self).__init__(**kwargs) | ||
| 49 | |||
| 50 | def dic(self): | ||
| 51 | return to_dic(self, self.__class__) | ||
| 52 | |||
| 53 | def json(self): | ||
| 54 | return to_json(self, self.__class__) | ||
| 55 | |||
| 56 | def __repr__(self): | ||
| 57 | return '%s: %s' % (self.__class__.__name__, self.json()) | ||
| 58 | |||
| 59 | |||
| 60 | class Host(DBBase): | ||
| 61 | __tablename__ = 'host' | ||
| 62 | |||
| 63 | id = Column(Integer, primary_key=True) | ||
| 64 | hostname = Column(String(50), unique=True, nullable=False) | ||
| 65 | public_ip = Column(String(50), unique=True, nullable=False) | ||
| 66 | private_ip = Column(String(50), unique=True, nullable=False) | ||
| 67 | mem = Column(String(50), nullable=False) | ||
| 68 | cores = Column(Integer, nullable=False) | ||
| 69 | disk = Column(String(50), nullable=False) | ||
| 70 | create_time = Column(DateTime, nullable=False) | ||
| 71 | update_time = Column(DateTime) | ||
| 72 | |||
| 73 | |||
| 74 | class VM(DBBase): | ||
| 75 | __tablename__ = 'vm' | ||
| 76 | |||
| 77 | id = Column(Integer, primary_key=True) | ||
| 78 | vm_name = Column(String(50), unique=True, nullable=False) | ||
| 79 | os_type = Column(String(50), nullable=False) # constants 0:linux 1:windows | ||
| 80 | cores = Column(Integer, nullable=False) | ||
| 81 | mem = Column(String(50), nullable=False) | ||
| 82 | capacity_g = Column(Integer, nullable=False) | ||
| 83 | config = Column(String(50)) | ||
| 84 | user = Column(String(50), nullable=False) | ||
| 85 | create_time = Column(DateTime, nullable=False) | ||
| 86 | update_time = Column(DateTime) | ||
| 87 | |||
| 88 | image_id = Column(Integer, ForeignKey('image.id', ondelete='CASCADE')) | ||
| 89 | image = relationship('Image', backref=backref('images_vms', lazy='dynamic')) | ||
| 90 | |||
| 91 | host_id = Column(Integer, ForeignKey('host.id', ondelete='CASCADE')) | ||
| 92 | host = relationship('Host', backref=backref('host_vms', lazy='dynamic')) | ||
| 93 | |||
| 94 | |||
| 95 | class Network(DBBase): | ||
| 96 | __tablename__ = "network" | ||
| 97 | |||
| 98 | id = Column(Integer, primary_key=True) | ||
| 99 | mac = Column(String(50), unique=True, nullable=False) | ||
| 100 | type = Column(String(50), nullable=False) # constants 0:public 1:private | ||
| 101 | address = Column(String(50), unique=True, nullable=False) | ||
| 102 | device = Column(String(50), nullable=False) | ||
| 103 | create_time = Column(DateTime, nullable=False) | ||
| 104 | update_time = Column(DateTime) | ||
| 105 | |||
| 106 | vm_id = Column(Integer, ForeignKey('vm.id', ondelete='CASCADE')) | ||
| 107 | vm = relationship('VM', backref=backref('networks', lazy='dynamic')) | ||
| 108 | |||
| 109 | |||
| 110 | class Disk(DBBase): | ||
| 111 | __tablename__ = "disk" | ||
| 112 | |||
| 113 | id = Column(Integer, primary_key=True) | ||
| 114 | type = Column(Integer, nullable=True) # constants 0:system 1:mounted | ||
| 115 | capacity_g = Column(Integer, nullable=False) | ||
| 116 | format = Column(Integer, nullable=False) # constants 0:ntfs 1:ext4 | ||
| 117 | path = Column(String(50), unique=True, nullable=False) | ||
| 118 | create_time = Column(DateTime, nullable=False) | ||
| 119 | update_time = Column(DateTime) | ||
| 120 | |||
| 121 | vm_id = Column(Integer, ForeignKey('vm.id', ondelete='CASCADE')) | ||
| 122 | vm = relationship('VM', backref=backref('disks', lazy='dynamic')) | ||
| 123 | |||
| 124 | |||
| 125 | class Image(DBBase): | ||
| 126 | __tablename__ = "image" | ||
| 127 | |||
| 128 | id = Column(Integer, primary_key=True) | ||
| 129 | name = Column(String(50), unique=True, nullable=False) | ||
| 130 | type = Column(Integer, nullable=True) # constants 0:default 1:provide 2:customize | ||
| 131 | path = Column(String(50), unique=True, nullable=False) | ||
| 132 | create_by = Column(String(50)) | ||
| 133 | create_time = Column(DateTime, nullable=False) | ||
| 134 | update_time = Column(DateTime) |
server/host/__init__.py
0 → 100644
server/setup_db.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | from server.database import Base, engine | ||
| 6 | from server.database.models import Host | ||
| 7 | from server.database import db_adapter | ||
| 8 | |||
| 9 | |||
| 10 | def setup_db(): | ||
| 11 | """Initialize db tables | ||
| 12 | |||
| 13 | make sure database and user correctly created in mysql | ||
| 14 | in case upgrade the table structure, the origin table need be dropped firstly | ||
| 15 | """ | ||
| 16 | Base.metadata.create_all(bind=engine) | ||
| 17 | |||
| 18 | # init REQUIRED db data. | ||
| 19 | db_adapter.add_object_kwargs(Host, | ||
| 20 | id='1', | ||
| 21 | hostname='test1', | ||
| 22 | public_ip='10.0.2.15', | ||
| 23 | private_ip='127.0.0.1', | ||
| 24 | mem='32G', | ||
| 25 | cores=16 | ||
| 26 | ) | ||
| 27 | setup_db() |
server/utils/__init__.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | from utils import Utils | ||
| 6 | from log import Log | ||
| 7 | from factory import factory | ||
| 8 | from server.database import db_session | ||
| 9 | from server.database.db_adapters import SQLAlchemyAdapter | ||
| 10 | |||
| 11 | def init_factory(): | ||
| 12 | factory.provide("util", Utils) | ||
| 13 | factory.provide("log", Log) | ||
| 14 | factory.provide("db", SQLAlchemyAdapter, db_session) | ||
| 15 |
server/utils/constants.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | |||
| 6 | class VM: | ||
| 7 | OS_TYPE_LINUX = 0 | ||
| 8 | OS_TYPE_WINDOWS = 1 | ||
| 9 | |||
| 10 | |||
| 11 | class NETWORK: | ||
| 12 | IP_TYPE_PUBLIC = 0 | ||
| 13 | IP_TYPE_PRIVATE = 1 | ||
| 14 | |||
| 15 | |||
| 16 | class DISK: | ||
| 17 | TYPE_SYSTEM = 0 | ||
| 18 | TYPE_MOUNTED = 1 | ||
| 19 | |||
| 20 | FORMAT_NTFS = 0 | ||
| 21 | FORMAT_EXT4 = 1 | ||
| 22 | |||
| 23 | |||
| 24 | class IMAGE: | ||
| 25 | TYPE_DEFAULT = 0 # Router or Monitor | ||
| 26 | TYPE_PROVIDER = 1 # vm images type | ||
| 27 | TYPE_CUSTOMIZE = 2 # created by users |
server/utils/factory.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | |||
| 6 | class SunnycloudFactory: | ||
| 7 | def __init__(self, allow_replace=False): | ||
| 8 | """Create a new factory | ||
| 9 | |||
| 10 | :param allow_replace: whether to replace existing provider with same feature. AssertionException will be raised it's | ||
| 11 | False and more than one providers with same key(feature) are provided | ||
| 12 | """ | ||
| 13 | self.providers = {} | ||
| 14 | self.allow_replace = allow_replace | ||
| 15 | |||
| 16 | def set_allow_replace(self, allow_replace): | ||
| 17 | """Set the value of allow_replace""" | ||
| 18 | self.allow_replace = allow_replace | ||
| 19 | |||
| 20 | def provide(self, feature, provider, *args, **kwargs): | ||
| 21 | """Add a provider to factory | ||
| 22 | |||
| 23 | :type feature: str|unicode | ||
| 24 | :param feature: key to store and get the object into/from the factory | ||
| 25 | |||
| 26 | :type provider: object | callable | ||
| 27 | :param provider: the object to be added. | ||
| 28 | |||
| 29 | :Example: | ||
| 30 | from *** import VMManager | ||
| 31 | factory.provide("vm_manager", VMManager) | ||
| 32 | factory.provide("vm_manager", VMManager, *init_args, **init_kwargs) | ||
| 33 | |||
| 34 | # or: | ||
| 35 | vmm = VMManager | ||
| 36 | factory.provide("user_manager", vmm) | ||
| 37 | |||
| 38 | """ | ||
| 39 | if not self.allow_replace: | ||
| 40 | assert not self.providers.has_key(feature), "Duplicate feature: %r" % feature | ||
| 41 | if callable(provider): | ||
| 42 | def call(): | ||
| 43 | return provider(*args, **kwargs) | ||
| 44 | else: | ||
| 45 | def call(): | ||
| 46 | return provider | ||
| 47 | self.providers[feature] = call | ||
| 48 | |||
| 49 | def __getitem__(self, feature): | ||
| 50 | try: | ||
| 51 | provider = self.providers[feature] | ||
| 52 | except KeyError: | ||
| 53 | raise KeyError, "Unknown feature named %r" % feature | ||
| 54 | return provider() | ||
| 55 | |||
| 56 | |||
| 57 | factory = SunnycloudFactory() | ||
| 58 | |||
| 59 | |||
| 60 | def NoAssertion(obj): | ||
| 61 | return True | ||
| 62 | |||
| 63 | |||
| 64 | class RequiredFeature(object): | ||
| 65 | def __init__(self, feature, assertion=NoAssertion): | ||
| 66 | """Create instance of RequiredFeature. | ||
| 67 | |||
| 68 | Will get the actual target from factory upon the first call. | ||
| 69 | |||
| 70 | :type feature: str|unicode | ||
| 71 | :param feature: the key to get object from factory | ||
| 72 | |||
| 73 | :Example: | ||
| 74 | inst = RequiredFeature("user_manager") | ||
| 75 | inst.some_method() # where user_manager.some_method will be called | ||
| 76 | |||
| 77 | :raise: | ||
| 78 | KeyError if feature doesn't exist in factory. | ||
| 79 | """ | ||
| 80 | self.feature = feature | ||
| 81 | self.assertion = assertion | ||
| 82 | |||
| 83 | def __get__(self, obj, T): | ||
| 84 | return self.result # <-- will request the feature upon first call | ||
| 85 | |||
| 86 | def __getattr__(self, name): | ||
| 87 | self.result = self.request() | ||
| 88 | if name == "result": | ||
| 89 | return self.result | ||
| 90 | else: | ||
| 91 | return getattr(self.result, name) | ||
| 92 | |||
| 93 | def request(self): | ||
| 94 | obj = factory[self.feature] | ||
| 95 | assert self.assertion(obj), \ | ||
| 96 | "The value %r of %r does not match the specified criteria" \ | ||
| 97 | % (obj, self.feature) | ||
| 98 | return obj |
server/utils/log.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | import logging | ||
| 6 | from os.path import realpath, dirname | ||
| 7 | from logging import config, DEBUG, INFO | ||
| 8 | |||
| 9 | class Log(object): | ||
| 10 | """Wrapper of Python logging module for easier usage | ||
| 11 | |||
| 12 | :Example: | ||
| 13 | from hackathon.log import log | ||
| 14 | |||
| 15 | log.info("message of INFO level ") | ||
| 16 | log.error(exception) # where exception is of type Exception or it inheritances | ||
| 17 | |||
| 18 | .. notes:: make sure directory '/var/log/open-hackathon/' exists and accessible | ||
| 19 | """ | ||
| 20 | |||
| 21 | def debug(self, debug): | ||
| 22 | """write message into log file with DEBUG level | ||
| 23 | |||
| 24 | :type debug: str|unicode | ||
| 25 | :param debug: message to write | ||
| 26 | """ | ||
| 27 | if self.logger.isEnabledFor(DEBUG): | ||
| 28 | self.logger.debug(debug) | ||
| 29 | |||
| 30 | def info(self, info): | ||
| 31 | """write message into log file with INFO level | ||
| 32 | |||
| 33 | :type info: str|unicode | ||
| 34 | :param info: message to write | ||
| 35 | """ | ||
| 36 | if self.logger.isEnabledFor(INFO): | ||
| 37 | self.logger.info(info) | ||
| 38 | |||
| 39 | def warn(self, warn): | ||
| 40 | """write message into log file with WARN level | ||
| 41 | |||
| 42 | :type warn: str|unicode | ||
| 43 | :param warn: message to write | ||
| 44 | """ | ||
| 45 | self.logger.warn(warn) | ||
| 46 | |||
| 47 | def error(self, exception): | ||
| 48 | """write exception message and stack trace into log file with ERROR level | ||
| 49 | |||
| 50 | :type exception: Exception | ||
| 51 | :param exception: exception to write | ||
| 52 | """ | ||
| 53 | self.logger.error(str(exception), exc_info=1) | ||
| 54 | |||
| 55 | def critical(self, critical): | ||
| 56 | """write message into log file with FATAL level | ||
| 57 | |||
| 58 | :type critical: str|unicode | ||
| 59 | :param critical: message to write | ||
| 60 | """ | ||
| 61 | self.logger.critical(critical) | ||
| 62 | |||
| 63 | def __init__(self): | ||
| 64 | """initialize the log wrapper through 'logging.conf' file which should be in the same dir of this file""" | ||
| 65 | logging.config.fileConfig("%s/logging.conf" % dirname(realpath(__file__))) | ||
| 66 | self.logger = logging.getLogger("myLogger") | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
server/utils/logging.conf
0 → 100644
| 1 | #define logger, handler,formatter names. | ||
| 2 | [loggers] | ||
| 3 | keys=root,myLogger,sqlalchemy,sqlConPool | ||
| 4 | |||
| 5 | [handlers] | ||
| 6 | keys=consoleHandler,logHandler,sqlLogHandler | ||
| 7 | |||
| 8 | [formatters] | ||
| 9 | keys=myFormatter | ||
| 10 | |||
| 11 | #define default logger | ||
| 12 | [logger_root] | ||
| 13 | level=DEBUG | ||
| 14 | handlers=consoleHandler | ||
| 15 | |||
| 16 | #define system wide logger | ||
| 17 | [logger_myLogger] | ||
| 18 | level=DEBUG | ||
| 19 | handlers=consoleHandler,logHandler | ||
| 20 | qualname=myLogger | ||
| 21 | propagate=0 | ||
| 22 | |||
| 23 | #define sqlalchemy echo logger | ||
| 24 | [logger_sqlalchemy] | ||
| 25 | level=WARNING | ||
| 26 | handlers=consoleHandler,sqlLogHandler | ||
| 27 | qualname=sqlalchemy.engine | ||
| 28 | propagate=0 | ||
| 29 | |||
| 30 | #define sqlalchemy connection pool logger | ||
| 31 | [logger_sqlConPool] | ||
| 32 | level=WARNING | ||
| 33 | handlers=consoleHandler,sqlLogHandler | ||
| 34 | qualname=sqlalchemy.pool | ||
| 35 | propagate=0 | ||
| 36 | |||
| 37 | #define consoleHandler | ||
| 38 | [handler_consoleHandler] | ||
| 39 | class=StreamHandler | ||
| 40 | level=DEBUG | ||
| 41 | formatter=myFormatter | ||
| 42 | args=(sys.stdout,) | ||
| 43 | |||
| 44 | #define logHandler | ||
| 45 | [handler_logHandler] | ||
| 46 | class=logging.handlers.TimedRotatingFileHandler | ||
| 47 | level=DEBUG | ||
| 48 | formatter=myFormatter | ||
| 49 | args=('/var/log/open-hackathon/sunnycloud.log','midnight',1,14) | ||
| 50 | |||
| 51 | #define sqlLogHandler | ||
| 52 | [handler_sqlLogHandler] | ||
| 53 | class=logging.handlers.TimedRotatingFileHandler | ||
| 54 | level=DEBUG | ||
| 55 | formatter=myFormatter | ||
| 56 | args=('/var/log/open-hackathon/sunnycloud.log','midnight',1,14) | ||
| 57 | |||
| 58 | #define formatter | ||
| 59 | [formatter_myFormatter] | ||
| 60 | format=%(asctime)s - %(levelname)s(%(threadName)s) - %(message)s | ||
| 61 | datefmt= | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
server/utils/utils.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | from datetime import datetime | ||
| 6 | |||
| 7 | |||
| 8 | class Utils(): | ||
| 9 | |||
| 10 | def get_now(self): | ||
| 11 | return datetime.now() | ||
| 12 | |||
| 13 | def get_config(self): | ||
| 14 | return None | ||
| 15 | |||
| 16 | def get_safe_config(self): | ||
| 17 | return None | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
server/views/__init__.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | from server import api | ||
| 6 | from server.database import db_adapter | ||
| 7 | from server.database.models import Host | ||
| 8 | from flask_restful import Resource | ||
| 9 | |||
| 10 | |||
| 11 | class TestResource(Resource): | ||
| 12 | def get(self): | ||
| 13 | return "server started" | ||
| 14 | |||
| 15 | |||
| 16 | class HostResource(Resource): | ||
| 17 | def get(self): | ||
| 18 | return db_adapter.find_first_object_by(Host, id=1).dic() | ||
| 19 | |||
| 20 | |||
| 21 | def init_routes(): | ||
| 22 | api.add_resource(TestResource, "/api/test") | ||
| 23 | api.add_resource(HostResource, "/api/host") | ||
| 24 |
server/vm/__init__.py
0 → 100644
server/vm/vm_manager.py
0 → 100644
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | |||
| 3 | __author__ = 'hubian' | ||
| 4 | |||
| 5 | |||
| 6 | class VMManager(): | ||
| 7 | |||
| 8 | def create_vm(self): | ||
| 9 | pass | ||
| 10 | |||
| 11 | def update_vm(self): | ||
| 12 | pass | ||
| 13 | |||
| 14 | def delete_vm(self): | ||
| 15 | pass | ||
| 16 | |||
| 17 | def get_vm_list(self): | ||
| 18 | pass | ||
| 19 | |||
| 20 | # ------------------------helper functions ---------------------- # | ||
| 21 | |||
| 22 | def __generate_vm_xml(self): | ||
| 23 | pass |
-
Please register or sign in to post a comment