blob: 169024ffb2ea40f748bc771bc875aa3aacdbdd3c [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
Scott Bakerc6ed1c42019-01-29 16:57:10 -080019import os
20from xossynchronizer.model_policies.policy import Policy
Scott Bakeredbb2322018-05-08 11:46:25 -070021
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
Scott Baker8b20bb12018-05-18 10:51:15 -070052 template_fn = os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "index.html.j2")
53 template = jinja2.Template(open(template_fn).read())
Scott Bakeredbb2322018-05-08 11:46:25 -070054
55 return template.render(fields)
56
57 def handle_update(self, service_instance):
58 if not service_instance.compute_instance:
59 # TODO: Break dependency
Scott Bakerc6ed1c42019-01-29 16:57:10 -080060 compute_service = self.model_accessor.KubernetesService.objects.first()
61 compute_service_instance_class = self.model_accessor.Service.objects.get(
62 id=compute_service.id
63 ).get_service_instance_class()
Scott Bakeredbb2322018-05-08 11:46:25 -070064
65 exampleservice = service_instance.owner.leaf_model
66
67 # TODO: What if there is the wrong number of slices?
68 slice = exampleservice.slices.first()
69
70 # TODO: What if there is no default image?
71 image = slice.default_image
72
73 name="simpleexampleserviceinstance-%s" % service_instance.id
74 compute_service_instance = compute_service_instance_class(slice=slice, owner=compute_service, image=image, name=name, no_sync=True)
75 compute_service_instance.save()
76
77 # Create a configmap and attach it to the compute instance
78 data = {"index.html": self.render_index(service_instance)}
Scott Bakerc6ed1c42019-01-29 16:57:10 -080079 cfmap = self.model_accessor.KubernetesConfigMap(name="simpleexampleserviceinstance-map-%s" % service_instance.id,
Scott Bakeredbb2322018-05-08 11:46:25 -070080 trust_domain=slice.trust_domain,
81 data=json.dumps(data))
82 cfmap.save()
Scott Bakerc6ed1c42019-01-29 16:57:10 -080083 cfmap_mnt = self.model_accessor.KubernetesConfigVolumeMount(config=cfmap,
Scott Bakeredbb2322018-05-08 11:46:25 -070084 service_instance=compute_service_instance,
85 mount_path="/usr/local/apache2/htdocs")
86 cfmap_mnt.save()
87
88 # Create a secret and attach it to the compute instance
89 data = {"service_secret.txt": base64.b64encode(str(exampleservice.service_secret)),
90 "tenant_secret.txt": base64.b64encode(str(service_instance.tenant_secret))}
Scott Bakerc6ed1c42019-01-29 16:57:10 -080091 secret = self.model_accessor.KubernetesSecret(name="simpleexampleserviceinstance-secret-%s" % service_instance.id,
Scott Bakeredbb2322018-05-08 11:46:25 -070092 trust_domain=slice.trust_domain,
93 data=json.dumps(data))
94 secret.save()
Scott Bakerc6ed1c42019-01-29 16:57:10 -080095 secret_mnt = self.model_accessor.KubernetesSecretVolumeMount(
96 secret=secret,
97 service_instance=compute_service_instance,
98 mount_path="/usr/local/apache2/secrets")
Scott Bakeredbb2322018-05-08 11:46:25 -070099 secret_mnt.save()
100
101 compute_service_instance.no_sync = False
102 compute_service_instance.save(update_fields=["no_sync"])
103
104 service_instance.compute_instance = compute_service_instance
105 service_instance.save(update_fields=["compute_instance"])
106 else:
107 compute_instance = service_instance.compute_instance
108 mnt = compute_instance.leaf_model.kubernetes_config_volume_mounts.first()
109 config = mnt.config
Scott Baker8b20bb12018-05-18 10:51:15 -0700110 new_data = json.dumps({"index.html": self.render_index(service_instance)})
Scott Bakeredbb2322018-05-08 11:46:25 -0700111 if (new_data != config.data):
Scott Baker8b20bb12018-05-18 10:51:15 -0700112 config.data = new_data
Scott Bakeredbb2322018-05-08 11:46:25 -0700113 config.save(always_update_timestamp=True)
114 # Force the Kubernetes syncstep
115 compute_instance.save(always_update_timestamp=True)
116
117 def handle_delete(self, service_instance):
118 log.info("handle_delete")
119 if service_instance.compute_instance:
120 log.info("has a compute_instance")
121 service_instance.compute_instance.delete()
122 service_instance.compute_instance = None
123 # TODO: I'm not sure we can save things that are being deleted...
124 service_instance.save(update_fields=["compute_instance"])