Skip to content

Commit

Permalink
engine 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
qiren007 committed Apr 9, 2014
1 parent 01b0395 commit 2d75189
Show file tree
Hide file tree
Showing 19 changed files with 597 additions and 180 deletions.
Binary file modified __pycache__/router.cpython-33.pyc
Binary file not shown.
Binary file added __pycache__/settings.cpython-33.pyc
Binary file not shown.
Binary file modified __pycache__/utils.cpython-33.pyc
Binary file not shown.
Binary file modified __pycache__/worker.cpython-33.pyc
Binary file not shown.
Binary file added engine/__init__.pyc
Binary file not shown.
Binary file modified engine/__pycache__/views.cpython-33.pyc
Binary file not shown.
Binary file added engine/settings.pyc
Binary file not shown.
Binary file added engine/urls.pyc
Binary file not shown.
37 changes: 26 additions & 11 deletions engine/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import log as logging
import worker
import copy
import settings

logger = logging.getLogger('engine')

Expand All @@ -20,6 +21,8 @@
def query_op(request):
if request.method == 'POST':
req = simplejson.loads(request.body.decode())
logger.debug(request.META.get('REMOTE_ADDR'))
logger.debug(req)
try:
resp = router.tmpl_forward(req['template_id'])
logger.info('query template %s' % req['template_id'])
Expand All @@ -37,19 +40,31 @@ def query_op(request):
@csrf_exempt
def install(request):
if request.method == 'POST':
data = simplejson.loads(request.body.decode())

req = copy.deepcopy(data)
remote_ip = request.META.get('REMOTE_ADDR')
try:
res = router.tmpl_store(data)
data = simplejson.loads(request.body.decode())
except Exception as ex:
logger.error(ex)
else:
if res:
logger.info('store request successfully')
work = worker.Worker(req['param']['template_id'], req['param']['template_type'],
req['param']['template_name'], req['param']['template_is_public'])
work.start()
return HttpResponse(simplejson.dumps({'result': 'success'}))
return HttpResponse(simplejson.dumps({'result': 'fail'}))
data['remote_ip'] = 'http://%s:%s' % (remote_ip, settings.SERVER_PORT)
req = copy.deepcopy(data)
if router.tmpl_store(data):
logger.info('store request successfully')
req['param']['template_url'] = 'http://%s:%s%s' % (remote_ip,
settings.SERVER_PORT,
req['param']['template_url'])
job = worker.MakeTemplate(req['param']['template_id'],
req['param']['template_type'],
req['param']['template_name'],
req['remote_ip'],
src=req['param']['template_source'],
# checksum=req['param']['template_checksum'],
checksum='e32fc383b3a787f5c5b39f5455a539d8',
is_public=req['param']['template_is_public'],
fs=req['param']['template_fs'],
remote_image_path=req['param']['template_url'],
worker=worker.engine_thr_pool)
worker.engine_thr_pool.add_job('make_tmpl', job.do_job)
return HttpResponse(simplejson.dumps({'result': 'success'}))
return HttpResponse(simplejson.dumps({'result': 'fail'}))

Binary file added engine/views.pyc
Binary file not shown.
Binary file added engine/wsgi.pyc
Binary file not shown.
File renamed without changes.
3 changes: 3 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env python
import os
import sys
import worker

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "engine.settings")

from django.core.management import execute_from_command_line

worker.engine_thr_pool.init_pool()
execute_from_command_line(sys.argv)
# worker.engine_thr_pool.close_pool()
3 changes: 3 additions & 0 deletions requirements
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Python3.3.2
Django 1.5.1
PyYaml
29 changes: 13 additions & 16 deletions router.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import threading
import settings
from multiprocessing import Queue
from functools import reduce

logger = logging.getLogger('engine')

Expand All @@ -26,27 +27,27 @@ class Router:
store the messages from vinzor server and forward it to right image
"""
ROUTE_TABLE = {}
TEMPLATE_INFO_LIST = ['template_id', 'packages', 'template_name', 'template_is_public',
'template_type', 'template_url', 'template_fs', 'template_checksum',
'template_source']
VINZOR_HEADER = ['name', 'device_id', 'pdu', 'request_id', 'code', 'param', 'remote_ip']
ENGINE_RECV_PACKAGES_KEY = ['name', 'display_name', 'version', 'os_type', 'os_architecture',
'checksum', 'install_cmd', 'priority', 'path']
lock = threading.Lock()

def __init__(self):
pass

def _check(self, data):
for i in settings.VINZOR_HEADER:
if i not in data:
return False
if 'template_id' in data['param'] and 'packages' in data['param'] and \
'template_name' in data['param'] and 'template_is_public' in data['param'] and \
'template_type' in data['param']:
for i in settings.ENGINE_RECV_PACKAGES_KEY:
for j in data['param']['packages']:
if i not in j:
return False
if not set(self.VINZOR_HEADER) ^ set(data.keys()) and \
not set(self.TEMPLATE_INFO_LIST) ^ set(data['param'].keys()):
for i in data['param']['packages']:
if set(self.ENGINE_RECV_PACKAGES_KEY) ^ set(i.keys()):
return False
return True
return False

def tmpl_forward(self, template_id):
logger.debug(template_id)
logger.debug(self.ROUTE_TABLE)
if template_id in self.ROUTE_TABLE:
resp = self.ROUTE_TABLE[template_id].get()
Expand All @@ -59,14 +60,10 @@ def tmpl_forward(self, template_id):
return None

def tmpl_store(self, data):
print(data)
logger.debug(data)
if self._check(data):
ss = data['param']['template_id']
self.lock.acquire()
del data['param']['template_id']
del data['param']['template_name']
del data['param']['template_type']
del data['param']['template_is_public']
data['param'] = data['param']['packages']
if ss not in self.ROUTE_TABLE:
self.ROUTE_TABLE[ss] = Queue()
Expand Down
Binary file added router.pyc
Binary file not shown.
33 changes: 23 additions & 10 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,37 @@
LOG_PATH = os.path.join(ENGINE_HOME, 'log')

LOG_FILE_NAME = os.path.join(LOG_PATH, 'engine.log')
DOWNLOAD_FILE_NAME = os.path.join(LOG_PATH, 'download.log')

LOCAL_IMAGE_WAREHOUSE = os.path.join(ENGINE_HOME, 'local_image_warehouse')
LOCAL_IMAGE_WAREHOUSE = os.path.join(ENGINE_HOME, 'local_image_repository')

TEMP = os.path.join(ENGINE_HOME, 'temp')

OS_FAMILY = ['windows 7', 'ubuntu']

VINZOR_HEADER = ['name', 'device_id', 'pdu', 'request_id', 'code', 'param']
ENGINE_RECV_PACKAGES_KEY = ['name', 'display_name', 'version', 'os_type', 'os_architecture',
'checksum', 'install_cmd', 'priority', 'path']
OS_WINDOWS_7 = 'windows 7'
OS_UBUNTU = 'ubuntu'
OS_FAMILY = {'nt':[OS_WINDOWS_7], 'posix':[OS_UBUNTU]}

# change this url to your server
SERVER_URL = '192.168.0.132:8000'
SERVER_PORT = 80

SERVER_URL_PATH = '/api/agent/make_template/'

AGENT_VM_ID_KEY='VM_ID_FOR_IMAGE'
AGENT_DIR_NAME = 'agent'
AGENT_SETTINGS = 'settings.py'
AGENT_DB_NAME = 'db'
AGENT_INFO_FROM_ENGINE_FILENAME = 'data.db'

TIMEOUT = 3600

MAX_TRY_DOWNLOAD_IMAGE_TIME = 3

MAKE_TMPL_THR_NUM = 3

UPLOAD_TMPL_THR_NUM = 5
# the input queue size in engine pool
MAX_INPUT_QUEUE_SIZE = 100

IS_PUBLIC = True
CONTAINER_FORMAT = 'ovf'
DISK_FORMAT='qcow2'
Expand All @@ -40,7 +53,7 @@
OS_USERNAME = 'admin'
OS_PASSWORD = 'sysuadmin'
OS_TENANT_NAME = 'demo'
OS_URL = '192.168.0.195'
OS_URL = '192.168.0.215'
OS_AUTH_PORT = 5000
OS_AUTH_URL = 'http://192.168.0.195:5000/v2.0'
OS_IMAGE_URL = 'http://192.168.0.195:9292/'
OS_AUTH_URL = 'http://192.168.0.215:5000/v2.0'
OS_IMAGE_URL = 'http://192.168.0.215:9292/'
Loading

0 comments on commit 2d75189

Please sign in to comment.