blob: 8cfea04469ffeac924603831b49bf0b4f49d6f1b [file] [log] [blame]
Scott Bakeredbb2322018-05-08 11:46:25 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import base64
17import jinja2
18import json
19from synchronizers.new_base.modelaccessor import *
20from synchronizers.new_base.policy import Policy
21
22from xosconfig import Config
23from multistructlog import create_logger
24
25log = create_logger(Config().get('logging'))
26
27class SimpleExampleServiceInstancePolicy(Policy):
28 model_name = "SimpleExampleServiceInstance"
29
30 def handle_create(self, service_instance):
31 return self.handle_update(service_instance)
32
33 def render_index(self, service_instance):
34 service = service_instance.owner.leaf_model
35
36 fields = {}
37 fields['tenant_message'] = service_instance.tenant_message
38 fields['service_message'] = service.service_message
39
40 if service_instance.foreground_color:
41 fields["foreground_color"] = service_instance.foreground_color.html_code
42
43 if service_instance.background_color:
44 fields["background_color"] = service_instance.background_color.html_code
45
46 images=[]
47 for image in service_instance.embedded_images.all():
48 images.append({"name": image.name,
49 "url": image.url})
50 fields["images"] = images
51
52 template = jinja2.Template(open("model_policies/index.html.j2").read())
53
54 return template.render(fields)
55
56 def handle_update(self, service_instance):
57 if not service_instance.compute_instance:
58 # TODO: Break dependency
59 compute_service = KubernetesService.objects.first()
60 compute_service_instance_class = Service.objects.get(id=compute_service.id).get_service_instance_class()
61
62 exampleservice = service_instance.owner.leaf_model
63
64 # TODO: What if there is the wrong number of slices?
65 slice = exampleservice.slices.first()
66
67 # TODO: What if there is no default image?
68 image = slice.default_image
69
70 name="simpleexampleserviceinstance-%s" % service_instance.id
71 compute_service_instance = compute_service_instance_class(slice=slice, owner=compute_service, image=image, name=name, no_sync=True)
72 compute_service_instance.save()
73
74 # Create a configmap and attach it to the compute instance
75 data = {"index.html": self.render_index(service_instance)}
76 cfmap = KubernetesConfigMap(name="simpleexampleserviceinstance-map-%s" % service_instance.id,
77 trust_domain=slice.trust_domain,
78 data=json.dumps(data))
79 cfmap.save()
80 cfmap_mnt = KubernetesConfigVolumeMount(config=cfmap,
81 service_instance=compute_service_instance,
82 mount_path="/usr/local/apache2/htdocs")
83 cfmap_mnt.save()
84
85 # Create a secret and attach it to the compute instance
86 data = {"service_secret.txt": base64.b64encode(str(exampleservice.service_secret)),
87 "tenant_secret.txt": base64.b64encode(str(service_instance.tenant_secret))}
88 secret = KubernetesSecret(name="simpleexampleserviceinstance-secret-%s" % service_instance.id,
89 trust_domain=slice.trust_domain,
90 data=json.dumps(data))
91 secret.save()
92 secret_mnt = KubernetesSecretVolumeMount(secret=secret, service_instance=compute_service_instance, mount_path="/usr/local/apache2/secrets")
93 secret_mnt.save()
94
95 compute_service_instance.no_sync = False
96 compute_service_instance.save(update_fields=["no_sync"])
97
98 service_instance.compute_instance = compute_service_instance
99 service_instance.save(update_fields=["compute_instance"])
100 else:
101 compute_instance = service_instance.compute_instance
102 mnt = compute_instance.leaf_model.kubernetes_config_volume_mounts.first()
103 config = mnt.config
104 new_data = {"index.html": self.render_index(service_instance)}
105 if (new_data != config.data):
106 config.data = json.dumps(new_data)
107 config.save(always_update_timestamp=True)
108 # Force the Kubernetes syncstep
109 compute_instance.save(always_update_timestamp=True)
110
111 def handle_delete(self, service_instance):
112 log.info("handle_delete")
113 if service_instance.compute_instance:
114 log.info("has a compute_instance")
115 service_instance.compute_instance.delete()
116 service_instance.compute_instance = None
117 # TODO: I'm not sure we can save things that are being deleted...
118 service_instance.save(update_fields=["compute_instance"])