Skip to content

Commit

Permalink
bug fixes 😌
Browse files Browse the repository at this point in the history
  • Loading branch information
avinassh committed Jan 30, 2014
1 parent e4a2b90 commit c1cd0b6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
15 changes: 8 additions & 7 deletions CentOSVZAdapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,26 +167,27 @@ def take_snapshot(vm_id):
vm_id = validate_vm_id(vm_id)
pass

def construct_vzctl_args(lab_spec):
def construct_vzctl_args(lab_specz):
""" Returns a tuple of vzctl create arguments and set arguments """

def get_vm_spec():
lab_spec = dict2default(lab_spec)
lab_spec = dict2default(lab_specz)
vm_spec = { "lab_ID" : lab_spec['lab']['description']['id'],
"os" : lab_spec['lab']['runtime_requirements']['platform']['os'],
"os_version" : lab_spec['lab']['runtime_requirements']['platform']['osVersion'],
"ram" : lab_spec['lab']['runtime_requirements']['platform']['memory']['min_required'],
"diskspace" : lab_spec['lab']['runtime_requirements']['platform']['storage']['min_required']
"diskspace" : lab_spec['lab']['runtime_requirements']['platform']['storage']['min_required'],
"swap" : lab_spec['lab']['runtime_requirements']['platform']['memory']['swap']
}
return vm_spec

vm_spec = get_vm_spec()
lab_ID = LAB_ID if vm_spec[lab_ID] == "" else vm_spec[lab_ID]
lab_ID = LAB_ID if vm_spec["lab_ID"] == "" else vm_spec["lab_ID"]
host_name = lab_ID + "." + HOST_NAME
ip_address = find_available_ip()
os_template = find_os_template(vm_spec[os], vm_spec[os_version])
(ram, swap) = VMUtils.get_ram_swap(vm_spec[ram], vm_spec[swap])
(disk_soft, disk_hard) = VMUtils.get_disk_space(vm_spec[diskspace])
os_template = find_os_template(vm_spec["os"], vm_spec["os_version"])
(ram, swap) = VMUtils.get_ram_swap(vm_spec["ram"], vm_spec["swap"])
(disk_soft, disk_hard) = VMUtils.get_disk_space(vm_spec["diskspace"])
vm_create_args = " --ostemplate " + os_template + \
" --ipadd " + ip_address + \
" --diskspace " + disk_soft + ":" + disk_hard + \
Expand Down
3 changes: 1 addition & 2 deletions CentOSVZAdapterServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def get(self):

def post(self):
post_data = dict(urlparse.parse_qsl(self.request.body))
#lab_spec = VMSpec(post_data)
result = CentOSVZAdapter.create_vm(post_data['lab_spec'])
result = CentOSVZAdapter.create_vm(json.loads(post_data['lab_spec']))
self.write(result)


Expand Down
11 changes: 8 additions & 3 deletions Controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ def __init__(self):
def test_lab(self, lab_id, lab_src_url, version=None):
try:
lab_spec = LabManager.get_lab_reqs(lab_id, lab_src_url, version)
vmpoolmgr = VMPoolManager.VMPoolManager()
(ip, port) = vmpoolmgr.create_vm(lab_spec)
print 'created vm ', ip, port
LabManager.test_lab(ip, port, lab_src_url, version)
except Exception, e:
# This should return an error json when Controller is a web service
print e

vmpoolmgr = VMPoolManager.VMPoolManager()
(ip, port) = vmpoolmgr.create_vm(lab_spec)
LabManager.test_lab(ip, port, lab_src_url, version)

if __name__ == '__main__':
c = Controller()
#c.test_lab("asdf", "asdf")
c.test_lab("asdf", "https://github.com/vlead/simo.git")
24 changes: 14 additions & 10 deletions LabManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import shlex
import logging
from exceptions import Exception
from logging.handlers import TimedRotatingFileHandler


import requests

Expand All @@ -30,15 +32,16 @@ def repo_exists(repo_name):
return os.path.isdir(GIT_CLONE_LOC+repo_name)

def clone_repo(repo_name):
clone_cmd = shlex.split("git clone %s" % lab_src_url)
clone_cmd = shlex.split("git clone %s %s%s" % (lab_src_url, GIT_CLONE_LOC, repo_name))
try:
subprocess.check_call(clone_cmd, stdout=LOG_FD, stderr=LOG_FD)
except Exception, e:
OVPL_LOGGER.error("git clone failed: %s %s" % (repo_name, str(e)))
raise e

def pull_repo(repo_name):
pull_cmd = shlex.split("git --git-dir=%s pull" % \
# dirty hack to fix git pull
pull_cmd = shlex.split("git --git-dir=%s/.git pull" % \
(GIT_CLONE_LOC + repo_name))
try:
subprocess.check_call(pull_cmd, stdout=LOG_FD, stderr=LOG_FD)
Expand All @@ -58,11 +61,11 @@ def checkout_version(repo_name):
raise e

def get_lab_spec(repo_name):
repo_path = GIT_CLONE_LOC + repo_name + LAB_SPEC_LOC
if not os.path.exists(repo_path):
spec_path = GIT_CLONE_LOC + repo_name + LAB_SPEC_LOC
if not os.path.exists(spec_path):
raise LabSpecInvalid("Lab spec file not found")
try:
return json.loads(open(repo_path).read())
return json.loads(open(spec_path).read())
except Exception, e:
raise LabSpecInvalid("Lab spec JSON invalid: " + str(e))

Expand All @@ -72,7 +75,6 @@ def get_lab_spec(repo_name):
else:
clone_repo(repo_name)
checkout_version(repo_name)

return get_lab_spec(repo_name)
#vm_spec = json.loads(open("vmspec.json", "r").read())

Expand All @@ -84,7 +86,7 @@ def test_lab(ip, port, lab_src_url, version=None):
# clone the repo in the VM
# get the lab_spec
# run Lab Action Runner
payload = {lab_src_url = lab_src_url, version = version}
payload = {"lab_src_url": lab_src_url, "version": version}
url = '%s:%s%s' % (ip, port, TEST_LAB_API_URI)
response = requests.post(url=url, data=payload)
print response.text
Expand All @@ -93,12 +95,14 @@ def test_lab(ip, port, lab_src_url, version=None):
def setup_logging():
OVPL_LOGGER.setLevel(logging.DEBUG) # make log level a setting
# Add the log message handler to the logger
handler = logging.handlers.TimedRotatingFileHandler(
myhandler = TimedRotatingFileHandler(
LOG_FILENAME, when='midnight', backupCount=5)

formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %I:%M:%S %p')
handler.setFormatter(formatter)
OVPL_LOGGER.addHandler(handler)
myhandler.setFormatter(formatter)
OVPL_LOGGER.addHandler(myhandler)


setup_logging()
6 changes: 5 additions & 1 deletion VMPool.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ def create_vm(self, lab_spec):
# Invoke platform adapter server (POST)
#vm_spec = json.loads(open("vmspec.json", "r").read())
url = "%s:%s%s" % (self.adapter_ip, self.adapter_port, CREATE_PATH)
payload = {'lab_spec': lab_spec}
#print "VMPool::create_vm()", lab_spec
payload = {'lab_spec': json.dumps(lab_spec)}
print
print "VMPool::create_vm()", payload
result = requests.post(url=url, data=payload)
print result.text
if result.status_code == requests.codes.ok:
print result.headers
print result.json()
Expand Down
10 changes: 6 additions & 4 deletions VMPoolManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ def __init__(self):
def add_vm_pool(self, adapter_ip, adapter_port):
self.VMPools.append(VMPool.VMPool(adapter_ip, adapter_port))

def get_available_pool(self, vm_spec):
def get_available_pool(self, lab_spec):
return self.VMPools[0]

def create_vm(self, vm_spec):
vmpool = self.get_available_pool(vm_spec)
return vmpool.create_vm(vm_spec)
def create_vm(self, lab_spec):
#dirty hack
self.add_vm_pool("http://localhost", "8000")
vmpool = self.get_available_pool(lab_spec)
return vmpool.create_vm(lab_spec)

0 comments on commit c1cd0b6

Please sign in to comment.