blob: b5ce94ba51f80c273f03fc1cb30ac1166e98f7ce [file] [log] [blame]
Scott Baker66566722015-07-27 17:42:39 -07001import os
2import pdb
3import sys
4import tempfile
5sys.path.append("/opt/tosca")
6from translator.toscalib.tosca_template import ToscaTemplate
7
Scott Baker66566722015-07-27 17:42:39 -07008from core.models import Slice,Sliver,User,Flavor,Node,Image
Scott Bakerefa6ea42015-07-31 11:48:45 -07009from nodeselect import XOSNodeSelector
Scott Baker1f9d4512015-08-03 09:56:35 -070010from imageselect import XOSImageSelector
Scott Baker66566722015-07-27 17:42:39 -070011
Scott Baker3841b372015-08-03 14:20:31 -070012import resources
13
Scott Baker66566722015-07-27 17:42:39 -070014class XOSTosca(object):
Scott Baker7e472dd2015-07-31 12:30:28 -070015 def __init__(self, tosca_yaml, parent_dir=None):
16 # TOSCA will look for imports using a relative path from where the
17 # template file is located, so we have to put the template file
18 # in a specific place.
19 if not parent_dir:
20 parent_dir = os.getcwd()
21
Scott Baker66566722015-07-27 17:42:39 -070022 try:
Scott Baker7e472dd2015-07-31 12:30:28 -070023 (tmp_handle, tmp_pathname) = tempfile.mkstemp(dir=parent_dir)
Scott Baker66566722015-07-27 17:42:39 -070024 os.write(tmp_handle, tosca_yaml)
25 os.close(tmp_handle)
26
27 self.template = ToscaTemplate(tmp_pathname)
28 finally:
29 os.remove(tmp_pathname)
30
Scott Baker9fdffff2015-08-04 23:52:18 -070031 self.compute_dependencies()
32
33 self.ordered_nodetemplates = []
34 self.ordered_names = self.topsort_dependencies()
35 for name in self.ordered_names:
36 if name in self.nodetemplates_by_name:
37 self.ordered_nodetemplates.append(self.nodetemplates_by_name[name])
38
Scott Baker66566722015-07-27 17:42:39 -070039 #pdb.set_trace()
40
Scott Baker9fdffff2015-08-04 23:52:18 -070041 def compute_dependencies(self):
42 nodetemplates_by_name = {}
Scott Baker66566722015-07-27 17:42:39 -070043 for nodetemplate in self.template.nodetemplates:
Scott Baker9fdffff2015-08-04 23:52:18 -070044 nodetemplates_by_name[nodetemplate.name] = nodetemplate
45
46 self.nodetemplates_by_name = nodetemplates_by_name
47
48 for nodetemplate in self.template.nodetemplates:
49 nodetemplate.dependencies = []
50 nodetemplate.dependencies_names = []
51 for reqs in nodetemplate.requirements:
52 for (k,v) in reqs.items():
53 name = v["node"]
54 if (name in nodetemplates_by_name):
55 nodetemplate.dependencies.append(nodetemplates_by_name[name])
56 nodetemplate.dependencies_names.append(name)
57
58 def topsort_dependencies(self):
59 # stolen from observer
60 g = self.nodetemplates_by_name
61
62 # Get set of all nodes, including those without outgoing edges
63 keys = set(g.keys())
64 values = set({})
65 for v in g.values():
66 values=values | set(v.dependencies_names)
67
68 all_nodes=list(keys|values)
69 steps = all_nodes
70
71 # Final order
72 order = []
73
74 # DFS stack, not using recursion
75 stack = []
76
77 # Unmarked set
78 unmarked = all_nodes
79
80 # visiting = [] - skip, don't expect 1000s of nodes, |E|/|V| is small
81
82 while unmarked:
83 stack.insert(0,unmarked[0]) # push first unmarked
84
85 while (stack):
86 n = stack[0]
87 add = True
88 try:
89 for m in g[n].dependencies_names:
90 if (m in unmarked):
91 add = False
92 stack.insert(0,m)
93 except KeyError:
94 pass
95 if (add):
96 if (n in steps and n not in order):
97 order.append(n)
98 item = stack.pop(0)
99 try:
100 unmarked.remove(item)
101 except ValueError:
102 pass
103
104 noorder = list(set(steps) - set(order))
105 return order + noorder
106
107 def execute(self, user):
108 for nodetemplate in self.ordered_nodetemplates: # self.template.nodetemplates:
Scott Baker66566722015-07-27 17:42:39 -0700109 self.execute_nodetemplate(user, nodetemplate)
110
Scott Baker66566722015-07-27 17:42:39 -0700111 def execute_nodetemplate(self, user, nodetemplate):
Scott Baker3841b372015-08-03 14:20:31 -0700112 if nodetemplate.type in resources.resources:
113 cls = resources.resources[nodetemplate.type]
Scott Baker9fdb39f2015-08-04 16:44:18 -0700114 #print "work on", cls.__name__, nodetemplate.name
Scott Baker3841b372015-08-03 14:20:31 -0700115 obj = cls(user, nodetemplate)
Scott Baker9fdb39f2015-08-04 16:44:18 -0700116 obj.create_or_update()
Scott Baker7e472dd2015-07-31 12:30:28 -0700117
Scott Baker8899be92015-08-04 17:02:29 -0700118 def execute_nodetemplate(self, user, nodetemplate):
119 if nodetemplate.type in resources.resources:
120 cls = resources.resources[nodetemplate.type]
Scott Baker9fdffff2015-08-04 23:52:18 -0700121 print "work on", cls.__name__, nodetemplate.name
Scott Baker8899be92015-08-04 17:02:29 -0700122 obj = cls(user, nodetemplate)
123 obj.create_or_update()
Scott Baker172797e2015-07-31 13:28:38 -0700124
Scott Baker8899be92015-08-04 17:02:29 -0700125 def destroy(self, user):
Scott Baker9fdffff2015-08-04 23:52:18 -0700126 nodetemplates = self.ordered_nodetemplates
Scott Baker8899be92015-08-04 17:02:29 -0700127 models = []
Scott Baker9fdffff2015-08-04 23:52:18 -0700128 for nodetemplate in nodetemplates:
Scott Baker8899be92015-08-04 17:02:29 -0700129 if nodetemplate.type in resources.resources:
130 cls = resources.resources[nodetemplate.type]
131 obj = cls(user, nodetemplate)
132 models = models + list(obj.get_existing_objs())
133 models.reverse()
134 for model in models:
135 print "destroying", model
136 model.delete(purge=True) # XXX change before deploying
Scott Baker66566722015-07-27 17:42:39 -0700137