blob: 63db3ef74a54a1c72276a9a36b07acee0ebd23f4 [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
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
60 compute_service = KubernetesService.objects.first()
61 compute_service_instance_class = Service.objects.get(id=compute_service.id).get_service_instance_class()
62
63 exampleservice = service_instance.owner.leaf_model
64
65 # TODO: What if there is the wrong number of slices?
66 slice = exampleservice.slices.first()
67
68 # TODO: What if there is no default image?
69 image = slice.default_image
70
71 name="simpleexampleserviceinstance-%s" % service_instance.id
72 compute_service_instance = compute_service_instance_class(slice=slice, owner=compute_service, image=image, name=name, no_sync=True)
73 compute_service_instance.save()
74
75 # Create a configmap and attach it to the compute instance
76 data = {"index.html": self.render_index(service_instance)}
77 cfmap = KubernetesConfigMap(name="simpleexampleserviceinstance-map-%s" % service_instance.id,
78 trust_domain=slice.trust_domain,
79 data=json.dumps(data))
80 cfmap.save()
81 cfmap_mnt = KubernetesConfigVolumeMount(config=cfmap,
82 service_instance=compute_service_instance,
83 mount_path="/usr/local/apache2/htdocs")
84 cfmap_mnt.save()
85
86 # Create a secret and attach it to the compute instance
87 data = {"service_secret.txt": base64.b64encode(str(exampleservice.service_secret)),
88 "tenant_secret.txt": base64.b64encode(str(service_instance.tenant_secret))}
89 secret = KubernetesSecret(name="simpleexampleserviceinstance-secret-%s" % service_instance.id,
90 trust_domain=slice.trust_domain,
91 data=json.dumps(data))
92 secret.save()
93 secret_mnt = KubernetesSecretVolumeMount(secret=secret, service_instance=compute_service_instance, mount_path="/usr/local/apache2/secrets")
94 secret_mnt.save()
95
96 compute_service_instance.no_sync = False
97 compute_service_instance.save(update_fields=["no_sync"])
98
99 service_instance.compute_instance = compute_service_instance
100 service_instance.save(update_fields=["compute_instance"])
101 else:
102 compute_instance = service_instance.compute_instance
103 mnt = compute_instance.leaf_model.kubernetes_config_volume_mounts.first()
104 config = mnt.config
Scott Baker8b20bb12018-05-18 10:51:15 -0700105 new_data = json.dumps({"index.html": self.render_index(service_instance)})
Scott Bakeredbb2322018-05-08 11:46:25 -0700106 if (new_data != config.data):
Scott Baker8b20bb12018-05-18 10:51:15 -0700107 config.data = new_data
Scott Bakeredbb2322018-05-08 11:46:25 -0700108 config.save(always_update_timestamp=True)
109 # Force the Kubernetes syncstep
110 compute_instance.save(always_update_timestamp=True)
111
112 def handle_delete(self, service_instance):
113 log.info("handle_delete")
114 if service_instance.compute_instance:
115 log.info("has a compute_instance")
116 service_instance.compute_instance.delete()
117 service_instance.compute_instance = None
118 # TODO: I'm not sure we can save things that are being deleted...
119 service_instance.save(update_fields=["compute_instance"])