Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 1 | |
| 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 | |
| 16 | import base64 |
| 17 | import jinja2 |
| 18 | import json |
Scott Baker | c6ed1c4 | 2019-01-29 16:57:10 -0800 | [diff] [blame] | 19 | import os |
| 20 | from xossynchronizer.model_policies.policy import Policy |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 21 | |
| 22 | from xosconfig import Config |
| 23 | from multistructlog import create_logger |
| 24 | |
| 25 | log = create_logger(Config().get('logging')) |
| 26 | |
Scott Baker | 31c3c0a | 2019-04-02 11:22:41 -0700 | [diff] [blame] | 27 | |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 28 | class SimpleExampleServiceInstancePolicy(Policy): |
| 29 | model_name = "SimpleExampleServiceInstance" |
| 30 | |
| 31 | def handle_create(self, service_instance): |
Himanshu Bhandari | 1983697 | 2020-06-01 17:39:13 +0530 | [diff] [blame^] | 32 | self.handle_update(service_instance) |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 33 | |
| 34 | def render_index(self, service_instance): |
| 35 | service = service_instance.owner.leaf_model |
| 36 | |
| 37 | fields = {} |
| 38 | fields['tenant_message'] = service_instance.tenant_message |
| 39 | fields['service_message'] = service.service_message |
| 40 | |
| 41 | if service_instance.foreground_color: |
| 42 | fields["foreground_color"] = service_instance.foreground_color.html_code |
| 43 | |
| 44 | if service_instance.background_color: |
| 45 | fields["background_color"] = service_instance.background_color.html_code |
| 46 | |
Scott Baker | 31c3c0a | 2019-04-02 11:22:41 -0700 | [diff] [blame] | 47 | images = [] |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 48 | for image in service_instance.embedded_images.all(): |
| 49 | images.append({"name": image.name, |
| 50 | "url": image.url}) |
| 51 | fields["images"] = images |
| 52 | |
Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -0700 | [diff] [blame] | 53 | template_fn = os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "index.html.j2") |
| 54 | template = jinja2.Template(open(template_fn).read()) |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 55 | |
| 56 | return template.render(fields) |
| 57 | |
| 58 | def handle_update(self, service_instance): |
| 59 | if not service_instance.compute_instance: |
| 60 | # TODO: Break dependency |
Scott Baker | c6ed1c4 | 2019-01-29 16:57:10 -0800 | [diff] [blame] | 61 | compute_service = self.model_accessor.KubernetesService.objects.first() |
| 62 | compute_service_instance_class = self.model_accessor.Service.objects.get( |
| 63 | id=compute_service.id |
| 64 | ).get_service_instance_class() |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 65 | |
| 66 | exampleservice = service_instance.owner.leaf_model |
| 67 | |
| 68 | # TODO: What if there is the wrong number of slices? |
| 69 | slice = exampleservice.slices.first() |
| 70 | |
| 71 | # TODO: What if there is no default image? |
| 72 | image = slice.default_image |
| 73 | |
Scott Baker | 31c3c0a | 2019-04-02 11:22:41 -0700 | [diff] [blame] | 74 | name = "simpleexampleserviceinstance-%s" % service_instance.id |
| 75 | compute_service_instance = compute_service_instance_class( |
| 76 | slice=slice, owner=compute_service, image=image, name=name, no_sync=True) |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 77 | compute_service_instance.save() |
| 78 | |
| 79 | # Create a configmap and attach it to the compute instance |
| 80 | data = {"index.html": self.render_index(service_instance)} |
Scott Baker | 31c3c0a | 2019-04-02 11:22:41 -0700 | [diff] [blame] | 81 | cfmap = self.model_accessor.KubernetesConfigMap( |
| 82 | name="simpleexampleserviceinstance-map-%s" % |
| 83 | service_instance.id, trust_domain=slice.trust_domain, data=json.dumps(data)) |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 84 | cfmap.save() |
Scott Baker | c6ed1c4 | 2019-01-29 16:57:10 -0800 | [diff] [blame] | 85 | cfmap_mnt = self.model_accessor.KubernetesConfigVolumeMount(config=cfmap, |
Scott Baker | 31c3c0a | 2019-04-02 11:22:41 -0700 | [diff] [blame] | 86 | service_instance=compute_service_instance, |
| 87 | mount_path="/usr/local/apache2/htdocs") |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 88 | cfmap_mnt.save() |
| 89 | |
| 90 | # Create a secret and attach it to the compute instance |
| 91 | data = {"service_secret.txt": base64.b64encode(str(exampleservice.service_secret)), |
| 92 | "tenant_secret.txt": base64.b64encode(str(service_instance.tenant_secret))} |
Scott Baker | 31c3c0a | 2019-04-02 11:22:41 -0700 | [diff] [blame] | 93 | secret = self.model_accessor.KubernetesSecret( |
| 94 | name="simpleexampleserviceinstance-secret-%s" % |
| 95 | service_instance.id, trust_domain=slice.trust_domain, data=json.dumps(data)) |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 96 | secret.save() |
Scott Baker | c6ed1c4 | 2019-01-29 16:57:10 -0800 | [diff] [blame] | 97 | secret_mnt = self.model_accessor.KubernetesSecretVolumeMount( |
| 98 | secret=secret, |
| 99 | service_instance=compute_service_instance, |
| 100 | mount_path="/usr/local/apache2/secrets") |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 101 | secret_mnt.save() |
| 102 | |
| 103 | compute_service_instance.no_sync = False |
| 104 | compute_service_instance.save(update_fields=["no_sync"]) |
| 105 | |
| 106 | service_instance.compute_instance = compute_service_instance |
| 107 | service_instance.save(update_fields=["compute_instance"]) |
| 108 | else: |
| 109 | compute_instance = service_instance.compute_instance |
| 110 | mnt = compute_instance.leaf_model.kubernetes_config_volume_mounts.first() |
| 111 | config = mnt.config |
Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -0700 | [diff] [blame] | 112 | new_data = json.dumps({"index.html": self.render_index(service_instance)}) |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 113 | if (new_data != config.data): |
Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -0700 | [diff] [blame] | 114 | config.data = new_data |
Scott Baker | edbb232 | 2018-05-08 11:46:25 -0700 | [diff] [blame] | 115 | config.save(always_update_timestamp=True) |
| 116 | # Force the Kubernetes syncstep |
| 117 | compute_instance.save(always_update_timestamp=True) |
| 118 | |
| 119 | def handle_delete(self, service_instance): |
| 120 | log.info("handle_delete") |
| 121 | if service_instance.compute_instance: |
| 122 | log.info("has a compute_instance") |
| 123 | service_instance.compute_instance.delete() |
| 124 | service_instance.compute_instance = None |
| 125 | # TODO: I'm not sure we can save things that are being deleted... |
| 126 | service_instance.save(update_fields=["compute_instance"]) |