merge
[unfold.git] / forge / tasks.py
1 # -*- coding:utf-8 -*-
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
6
7 from forge.models import *
8
9 import forge.script.Auth as Auth
10 import time
11
12 # Import class used to create the environment
13 from forge.script.request import TransformRawXml
14 from forge.script.openvswitch import TransformXml
15
16 # Import the settings
17 from django.conf import settings
18
19 @task()
20 def taskInstallEnvironment(env):
21         env.ready = False
22         env.save()
23         #TODO Change directory location
24         curDir = os.getcwd()
25         os.chdir('./ict_education/script/')
26         try:
27                 xml = TransformXml(confFile = curDir+env.confFile.url, linkFile = curDir+env.linkFile.url, keyFile = curDir+env.course.mainKey.url)
28                 xml.clearConf()
29                 xml.setSliceConf()
30                 xml.setLinks()
31                 xml.setRoutes()
32                 xml.setServices()
33         except Exception, why:
34                 print why
35         os.chdir(curDir)
36         env.ready = True
37         env.save()
38
39 @task()
40 def taskDeleteCourse(course):
41         environments = course.environment_set.all()
42         #TODO Change directory location
43         curDir = os.getcwd()
44         os.chdir('./ict_education/script/')
45         for env in environments:
46                 try:
47                         xml = TransformXml(confFile = curDir+env.confFile.url, linkFile = curDir+env.linkFile.url, keyFile = curDir+env.course.mainKey.url)
48                         xml.clearConf()
49                 except Exception, why:
50                         print why
51         try:
52                 rawXml = TransformRawXml(sliceName=course.sliceName)
53                 rawXml.deleteSlice()
54         except Fault, why:
55                 print "I got an xmlrpc Fault"
56                 print why
57         os.chdir(curDir)
58         course.delete()
59
60 @task()
61 def taskRenewSlice(course):
62         rawXml = TransformRawXml(sliceName=course.sliceName)
63         rawXml.renewSlice()
64
65 @task()
66 def taskCreateCourse(form):
67         newCourse = Course()
68         newCourse.lab = Lab.objects.get(id=form.cleaned_data['lab'].id)
69         newCourse.ready = False
70         newCourse.sliceName = form.cleaned_data['sliceName']
71         newCourse.save()
72         #TODO Change directory location
73         curDir = os.getcwd()
74         os.chdir('./ict_education/script/')
75         if form.cleaned_data['keyLocation'] == '':
76                 keyLocation = None
77         else:
78                 keyLocation = form.cleaned_data['keyLocation']
79         if form.cleaned_data['url'] == '':
80                 sliceUrl = 'http://onelab.eu'
81         else:
82                 sliceUrl = form.cleaned_data['url']
83         if form.cleaned_data['description'] == '':
84                 sliceDescription = 'Slice used for educational purpose'
85         else:
86                 sliceDescription = form.cleaned_data['url']
87         try:
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)
89                 rawXml.setSlice()
90                 newCourse.mainKey.save(rawXml.mainKeyPriv.split('/')[-1], File(open(rawXml.mainKeyPriv)))
91                 newCourse.save()
92                 i = 0
93                 for environment in rawXml.envList:
94                         newEnv = Environment()
95                         newEnv.ready = False
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])))
100                         newEnv.save()
101                         i += 1
102                         for plSlice in environment:
103                                 newSlice = Slice()
104                                 newSlice.sliceName = plSlice.slice_name
105                                 newSlice.environment = newEnv
106                                 newSlice.save()
107                                 for host in plSlice.hosts:
108                                         newHost = Host()
109                                         nodeSelected = ''
110                                         for env in rawXml.nodeList:
111                                                 for node in env:
112                                                         if node['hostname'] == host.url:
113                                                                 nodeselected = node
114                                                                 break
115                                                 if nodeSelected != '':
116                                                         break
117                                         newHost.hostname = host.url
118                                         newHost.pleSlice = newSlice
119                                         newHost.latitude = nodeSelected['latitude']
120                                         newHost.longitude = nodeSelected['longitude']
121                                         newHost.save()
122                                         for serviceName, port in host.services.services:
123                                                 newService = Service()
124                                                 newService.serviceName = serviceName
125                                                 newService.servicePort = str(port)
126                                                 newService.host = newHost
127                                                 newService.save()
128                                         for device in host.devices:
129                                                 newInterface = Interface()
130                                                 newInterface.ip = device.ip
131                                                 newInterface.name = device.id_dev
132                                                 newInterface.host = newHost
133                                                 newInterface.save()
134                         newEnv.ready = True
135                         newEnv.save()
136                 os.chdir(curDir)
137                 newCourse.ready = True
138                 newCourse.save()
139         except Exception, why:
140                 print "An error occured deleting the environment"
141                 print why
142                 rawXml = TransformRawXml(sliceName=newCourse.sliceName)
143                 rawXml.deleteSlice()
144                 raise Exception('Global Fault')
145         return 0