Skip to content

Commit

Permalink
Merge Devel
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Jan 28, 2016
2 parents ad8a29b + f2f415a commit 2c946cf
Show file tree
Hide file tree
Showing 25 changed files with 923 additions and 189 deletions.
29 changes: 16 additions & 13 deletions IM/InfrastructureManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import InfrastructureInfo
from IM.radl import radl_parse
from IM.radl.radl import Feature
from IM.radl.radl import Feature, RADL
from IM.recipe import Recipe
from IM.db import DataBase

Expand Down Expand Up @@ -259,7 +259,10 @@ def Reconfigure(inf_id, radl_data, auth, vm_list = None):
"""

InfrastructureManager.logger.info("Reconfiguring the inf: " + str(inf_id))
radl = radl_parse.parse_radl(radl_data)
if isinstance(radl_data, RADL):
radl = radl_data
else:
radl = radl_parse.parse_radl(radl_data)
InfrastructureManager.logger.debug(radl)

sel_inf = InfrastructureManager.get_infrastructure(inf_id, auth)
Expand Down Expand Up @@ -356,10 +359,13 @@ def AddResource(inf_id, radl_data, auth, context = True, failed_clouds = []):

InfrastructureManager.logger.info("Adding resources to inf: " + str(inf_id))

radl = radl_parse.parse_radl(radl_data)
radl.check()
if isinstance(radl_data, RADL):
radl = radl_data
else:
radl = radl_parse.parse_radl(radl_data)

InfrastructureManager.logger.debug(radl)
radl.check()

sel_inf = InfrastructureManager.get_infrastructure(inf_id, auth)

Expand Down Expand Up @@ -604,13 +610,7 @@ def GetVMProperty(inf_id, vm_id, property_name, auth):
Return: a str with the property value
"""
radl_data = InfrastructureManager.GetVMInfo(inf_id, vm_id, auth)

try:
radl = radl_parse.parse_radl(radl_data)
except Exception, ex:
InfrastructureManager.logger.exception("Error parsing the RADL: " + radl_data)
raise ex
radl = InfrastructureManager.GetVMInfo(inf_id, vm_id, auth)

res = None
if radl.systems:
Expand Down Expand Up @@ -689,7 +689,10 @@ def AlterVM(inf_id, vm_id, radl_data, auth):
InfrastructureManager.logger.info("VM does not exist or Access Error")
raise Exception("VM does not exist or Access Error")

radl = radl_parse.parse_radl(radl_data)
if isinstance(radl_data, RADL):
radl = radl_data
else:
radl = radl_parse.parse_radl(radl_data)

exception = None
try:
Expand All @@ -706,7 +709,7 @@ def AlterVM(inf_id, vm_id, radl_data, auth):
vm.update_status(auth)
InfrastructureManager.save_data(inf_id)

return str(vm.info)
return vm.info

@staticmethod
def GetInfrastructureRADL(inf_id, auth):
Expand Down
128 changes: 120 additions & 8 deletions IM/REST.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import bottle
import json
from config import Config
from radl.radl_json import parse_radl as parse_radl_json, dump_radl as dump_radl_json
from bottle import HTTPError

AUTH_LINE_SEPARATOR = '\\n'

Expand Down Expand Up @@ -95,6 +97,19 @@ def run(host, port):
def stop():
bottle_server.shutdown()

def get_media_type(header):
"""
Function to get only the header media type
"""
accept = bottle.request.headers.get(header)
if accept:
pos = accept.find(";")
if pos != -1:
accept = accept[:pos]
return accept.strip()
else:
return accept

@app.route('/infrastructures/:id', method='DELETE')
def RESTDestroyInfrastructure(id=None):
try:
Expand All @@ -105,6 +120,7 @@ def RESTDestroyInfrastructure(id=None):

try:
InfrastructureManager.DestroyInfrastructure(id, auth)
bottle.response.content_type = "text/plain"
return ""
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Destroying Inf: " + str(ex))
Expand Down Expand Up @@ -166,6 +182,8 @@ def RESTGetInfrastructureProperty(id=None, prop=None):
else:
bottle.abort(403, "Incorrect infrastructure property")
return str(res)
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Getting Inf. info: " + str(ex))
return False
Expand Down Expand Up @@ -209,11 +227,22 @@ def RESTCreateInfrastructure():
bottle.abort(401, "No authentication data provided")

try:
content_type = get_media_type('Content-Type')
radl_data = bottle.request.body.read()

if content_type:
if content_type == "application/json":
radl_data = parse_radl_json(radl_data)
elif content_type != "text/plain":
bottle.abort(415, "Unsupported Media Type %s" % content_type)
return False

inf_id = InfrastructureManager.CreateInfrastructure(radl_data, auth)

bottle.response.content_type = "text/uri-list"
return "http://" + bottle.request.environ['HTTP_HOST'] + "/infrastructures/" + str(inf_id)
except HTTPError, ex:
raise ex
except UnauthorizedUserException, ex:
bottle.abort(401, "Error Getting Inf. info: " + str(ex))
return False
Expand All @@ -230,9 +259,27 @@ def RESTGetVMInfo(infid=None, vmid=None):
bottle.abort(401, "No authentication data provided")

try:
info = InfrastructureManager.GetVMInfo(infid, vmid, auth)
bottle.response.content_type = "text/plain"
accept = get_media_type('Accept')

radl = InfrastructureManager.GetVMInfo(infid, vmid, auth)

if accept:
if accept == "application/json":
bottle.response.content_type = accept
info = dump_radl_json(radl, enter="", indent="")
elif accept == "text/plain":
info = str(radl)
bottle.response.content_type = accept
else:
bottle.abort(404, "Unsupported Accept Media Type: %s" % accept)
return False
else:
info = str(radl)
bottle.response.content_type = "text/plain"

return info
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Getting VM. info: " + str(ex))
return False
Expand Down Expand Up @@ -262,8 +309,24 @@ def RESTGetVMProperty(infid=None, vmid=None, prop=None):
info = InfrastructureManager.GetVMContMsg(infid, vmid, auth)
else:
info = InfrastructureManager.GetVMProperty(infid, vmid, prop, auth)
bottle.response.content_type = "text/plain"

accept = get_media_type('Accept')
if accept:
if accept == "application/json":
bottle.response.content_type = accept
if isinstance(info, str) or isinstance(info, unicode):
info = '"' + info + '"'
elif accept == "text/plain":
bottle.response.content_type = accept
else:
bottle.abort(404, "Unsupported Accept Media Type: %s" % accept)
return False
else:
bottle.response.content_type = "text/plain"

return str(info)
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Getting VM. property: " + str(ex))
return False
Expand Down Expand Up @@ -298,8 +361,17 @@ def RESTAddResource(id=None):
context = False
else:
bottle.abort(400, "Incorrect value in context parameter")


content_type = get_media_type('Content-Type')
radl_data = bottle.request.body.read()

if content_type:
if content_type == "application/json":
radl_data = parse_radl_json(radl_data)
elif content_type != "text/plain":
bottle.abort(415, "Unsupported Media Type %s" % content_type)
return False

vm_ids = InfrastructureManager.AddResource(id, radl_data, auth, context)

res = ""
Expand All @@ -310,6 +382,8 @@ def RESTAddResource(id=None):

bottle.response.content_type = "text/uri-list"
return res
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Adding resources: " + str(ex))
return False
Expand Down Expand Up @@ -340,7 +414,10 @@ def RESTRemoveResource(infid=None, vmid=None):
bottle.abort(400, "Incorrect value in context parameter")

InfrastructureManager.RemoveResource(infid, vmid, auth, context)
bottle.response.content_type = "text/plain"
return ""
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error Removing resources: " + str(ex))
return False
Expand All @@ -366,10 +443,35 @@ def RESTAlterVM(infid=None, vmid=None):
bottle.abort(401, "No authentication data provided")

try:
content_type = get_media_type('Content-Type')
accept = get_media_type('Accept')
radl_data = bottle.request.body.read()

bottle.response.content_type = "text/plain"
return InfrastructureManager.AlterVM(infid, vmid, radl_data, auth)
if content_type:
if content_type == "application/json":
radl_data = parse_radl_json(radl_data)
elif content_type != "text/plain":
bottle.abort(415, "Unsupported Media Type %s" % content_type)
return False

vm_info = InfrastructureManager.AlterVM(infid, vmid, radl_data, auth)

if accept:
if accept == "application/json":
bottle.response.content_type = accept
res = dump_radl_json(vm_info, enter="", indent="")
elif accept == "text/plain":
res = str(vm_info)
bottle.response.content_type = accept
else:
bottle.abort(404, "Unsupported Accept Media Type: %s" % accept)
return False
else:
bottle.response.content_type = "text/plain"

return res
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error modifying resources: " + str(ex))
return False
Expand Down Expand Up @@ -403,11 +505,21 @@ def RESTReconfigureInfrastructure(id=None):
except:
bottle.abort(400, "Incorrect vm_list format.")

if 'radl' in bottle.request.forms.keys():
radl_data = bottle.request.forms.get('radl')
content_type = get_media_type('Content-Type')
radl_data = bottle.request.body.read()

if radl_data:
if content_type:
if content_type == "application/json":
radl_data = parse_radl_json(radl_data)
elif content_type != "text/plain":
bottle.abort(415, "Unsupported Media Type %s" % content_type)
return False
else:
radl_data = ""
return InfrastructureManager.Reconfigure(id, radl_data, auth, vm_list)
except HTTPError, ex:
raise ex
except DeletedInfrastructureException, ex:
bottle.abort(404, "Error reconfiguring infrastructure: " + str(ex))
return False
Expand Down
4 changes: 2 additions & 2 deletions IM/ServiceRequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Request_GetVMInfo(IMBaseRequest):
def _call_function(self):
self._error_mesage = "Error Getting VM Info."
(inf_id, vm_id, auth_data) = self.arguments
return InfrastructureManager.InfrastructureManager.GetVMInfo(inf_id, vm_id, Authentication(auth_data))
return str(InfrastructureManager.InfrastructureManager.GetVMInfo(inf_id, vm_id, Authentication(auth_data)))

class Request_GetVMProperty(IMBaseRequest):
"""
Expand All @@ -170,7 +170,7 @@ class Request_AlterVM(IMBaseRequest):
def _call_function(self):
self._error_mesage = "Error Changing VM Info."
(inf_id, vm_id, radl, auth_data) = self.arguments
return InfrastructureManager.InfrastructureManager.AlterVM(inf_id, vm_id, radl, Authentication(auth_data))
return str(InfrastructureManager.InfrastructureManager.AlterVM(inf_id, vm_id, radl, Authentication(auth_data)))

class Request_DestroyInfrastructure(IMBaseRequest):
"""
Expand Down
2 changes: 1 addition & 1 deletion IM/VirtualMachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ def get_vm_info(self):
res = RADL()
res.networks = self.info.networks
res.systems = self.info.systems
return str(res)
return res

def get_ssh_ansible_master(self):
ansible_host = None
Expand Down
2 changes: 1 addition & 1 deletion IM/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@


__all__ = ['auth','bottle','CloudManager','config','ConfManager','db','ganglia','HTTPHeaderTransport','ImageManager','InfrastructureInfo','InfrastructureManager','parsetab','radl','recipe','request','REST', 'ServiceRequests','SSH','timedcall','uriparse','VMRC','xmlobject']
__version__ = '1.4.1'
__version__ = '1.4.2'
__author__ = 'Miguel Caballer'

16 changes: 11 additions & 5 deletions IM/ansible/ansible_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from ansible.cli import CLI
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
import ansible.inventory

from ansible_executor_v2 import IMPlaybookExecutor

Expand Down Expand Up @@ -137,10 +137,15 @@ def launch_playbook_v2(self):
options.forks = self.threads

loader = DataLoader()
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=options.inventory)
# Add this to avoid the Ansible bug: no host vars as host is not in inventory
# In version 2.0.1 it must be fixed
ansible.inventory.HOSTS_PATTERNS_CACHE = {}

inventory = ansible.inventory.Inventory(loader=loader, variable_manager=variable_manager, host_list=options.inventory)
variable_manager.set_inventory(inventory)

inventory.subset(self.host)
if self.host:
inventory.subset(self.host)
# let inventory know which playbooks are using so it can know the basedirs
inventory.set_playbook_basedir(os.path.dirname(self.playbook_file))

Expand Down Expand Up @@ -217,8 +222,9 @@ def launch_playbook_v1(self):
inventory = ansible.inventory.Inventory(self.inventory_file)
else:
inventory = ansible.inventory.Inventory(options.inventory)

inventory.subset(self.host)

if self.host:
inventory.subset(self.host)
# let inventory know which playbooks are using so it can know the basedirs
inventory.set_playbook_basedir(os.path.dirname(self.playbook_file))

Expand Down
2 changes: 1 addition & 1 deletion IM/radl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


__all__ = ['radl_lex','radl_parse','radl']
__all__ = ['radl_lex','radl_parse','radl_json','radl']
5 changes: 5 additions & 0 deletions IM/radl/radl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,11 @@ def __init__(self, name, features, line=None):
def __str__(self):
return "ansible %s (%s)" % (self.id, Features.__str__(self))

def getId(self):
"""Return the id of the aspect."""

return self.id

def check(self, radl):
"""Check the features in this network."""

Expand Down
Loading

0 comments on commit 2c946cf

Please sign in to comment.