2 from django.core.files import File
3 from celery import task, current_task
4 from djcelery.models import TaskState
5 from xmlrpclib import Fault
7 from forge.models import *
9 import forge.script.Auth as Auth
12 # Import class used to create the environment
13 from forge.script.request import TransformRawXml
14 from forge.script.openvswitch import TransformXml
17 from django.conf import settings
20 def taskInstallEnvironment(env):
23 #TODO Change directory location
25 os.chdir('./ict_education/script/')
27 xml = TransformXml(confFile = curDir+env.confFile.url, linkFile = curDir+env.linkFile.url, keyFile = curDir+env.course.mainKey.url)
33 except Exception, why:
40 def taskDeleteCourse(course):
41 environments = course.environment_set.all()
42 #TODO Change directory location
44 os.chdir('./ict_education/script/')
45 for env in environments:
47 xml = TransformXml(confFile = curDir+env.confFile.url, linkFile = curDir+env.linkFile.url, keyFile = curDir+env.course.mainKey.url)
49 except Exception, why:
52 rawXml = TransformRawXml(sliceName=course.sliceName)
55 print "I got an xmlrpc Fault"
61 def taskRenewSlice(course):
62 rawXml = TransformRawXml(sliceName=course.sliceName)
66 def taskCreateCourse(form):
68 newCourse.lab = Lab.objects.get(id=form.cleaned_data['lab'].id)
69 newCourse.ready = False
70 newCourse.sliceName = form.cleaned_data['sliceName']
72 #TODO Change directory location
74 os.chdir('./ict_education/script/')
75 if form.cleaned_data['keyLocation'] == '':
78 keyLocation = form.cleaned_data['keyLocation']
79 if form.cleaned_data['url'] == '':
80 sliceUrl = 'http://onelab.eu'
82 sliceUrl = form.cleaned_data['url']
83 if form.cleaned_data['description'] == '':
84 sliceDescription = 'Slice used for educational purpose'
86 sliceDescription = form.cleaned_data['url']
88 rawXml = TransformRawXml(confFile = settings.MEDIA_ROOT+newCourse.lab.configurationFile.name, linkFile = settings.MEDIA_ROOT+newCourse.lab.linkFile.name, subnet = form.cleaned_data['subnet'], sliceName = form.cleaned_data['sliceName'], nbEnv = form.cleaned_data['nbEnv'], sliceUrl = sliceUrl, sliceDescription = sliceDescription, mainKeyPriv = keyLocation)
90 newCourse.mainKey.save(rawXml.mainKeyPriv.split('/')[-1], File(open(rawXml.mainKeyPriv)))
93 for environment in rawXml.envList:
94 newEnv = Environment()
96 newEnv.course = newCourse
97 newEnv.sshKey.save(rawXml.envKeyPriv[i].split('/')[-1], File(open(rawXml.envKeyPriv[i])))
98 newEnv.confFile.save(rawXml.envConfFile[i].split('/')[-1], File(open(rawXml.envConfFile[i])))
99 newEnv.linkFile.save(rawXml.envLinkFile[i].split('/')[-1], File(open(rawXml.envLinkFile[i])))
102 for plSlice in environment:
104 newSlice.sliceName = plSlice.slice_name
105 newSlice.environment = newEnv
107 for host in plSlice.hosts:
110 for env in rawXml.nodeList:
112 if node['hostname'] == host.url:
115 if nodeSelected != '':
117 newHost.hostname = host.url
118 newHost.pleSlice = newSlice
119 newHost.latitude = nodeSelected['latitude']
120 newHost.longitude = nodeSelected['longitude']
122 for serviceName, port in host.services.services:
123 newService = Service()
124 newService.serviceName = serviceName
125 newService.servicePort = str(port)
126 newService.host = newHost
128 for device in host.devices:
129 newInterface = Interface()
130 newInterface.ip = device.ip
131 newInterface.name = device.id_dev
132 newInterface.host = newHost
137 newCourse.ready = True
139 except Exception, why:
140 print "An error occured deleting the environment"
142 rawXml = TransformRawXml(sliceName=newCourse.sliceName)
144 raise Exception('Global Fault')