boyoung | f42f2bf | 2017-10-16 19:06:30 +0900 | [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 | |
| 17 | import os |
| 18 | import base64 |
| 19 | from synchronizers.swarm.swarmsyncstep import SwarmSyncStep |
| 20 | from synchronizers.new_base.syncstep import * |
| 21 | from synchronizers.new_base.ansible_helper import * |
| 22 | from synchronizers.new_base.modelaccessor import * |
| 23 | from xos.logger import observer_logger as logger |
| 24 | import synchronizers.swarm.swarmlog as slog |
| 25 | |
| 26 | |
| 27 | class SyncControllerImages(SwarmSyncStep): |
| 28 | provides = [ControllerImages] |
| 29 | observes = ControllerImages |
| 30 | requested_interval = 0 |
| 31 | playbook='sync_controller_images.yaml' |
| 32 | |
| 33 | def fetch_pending(self, deleted): |
| 34 | return super(SyncControllerImages, self).fetch_pending(deleted) |
| 35 | |
| 36 | def map_sync_inputs(self, controller_image): |
| 37 | swarm_manager_url = controller_image.controller.auth_url |
| 38 | (swarm_manager_address, docker_registry_port) = swarm_manager_url.split(':') |
| 39 | slog.debug("swarm_manager_address: %s docker_registry_port: %s" |
| 40 | % (swarm_manager_address, docker_registry_port)) |
| 41 | |
| 42 | input_fields = { |
| 43 | 'swarm_manager_address' : swarm_manager_address, |
| 44 | 'docker_registry_port' : docker_registry_port, |
| 45 | 'image_file_path' : controller_image.image.path, |
| 46 | 'image_dir' : os.path.dirname(controller_image.image.path), |
| 47 | 'image_name' : controller_image.image.name, |
| 48 | 'image_tag' : controller_image.image.tag, |
| 49 | 'delete' : False, |
| 50 | 'ansible_tag' : '%s@%s' % ( |
| 51 | controller_image.image.name, |
| 52 | controller_image.controller.name) # name of ansible playbook |
| 53 | } |
| 54 | slog.debug("input_fields: %s" % str(input_fields)) |
| 55 | |
| 56 | return input_fields |
| 57 | |
| 58 | def map_sync_outputs(self, controller_image, res): |
| 59 | slog.debug("Ansible playbook result: %s" % str(res)) |
| 60 | slog.debug("Ansible playbook result[4]['image']['Id']: %s" % str(res[4]['image']['Id'])) |
| 61 | controller_image.glance_image_id = str(res[4]['image']['Id']) |
| 62 | if len(controller_image.glance_image_id) > 2: |
| 63 | controller_image.backend_status = '1 - OK' |
| 64 | else: |
| 65 | controller_image.backend_status = '2 - Ansible playbook failure' |
| 66 | controller_image.save() |
| 67 | |
| 68 | def map_delete_inputs(self, controller_image): |
| 69 | try: |
| 70 | controller_register = json.loads(instance.node.site_deployment.controller.backend_register) |
| 71 | slog.debug("controller_register: %s" % controller_register) |
| 72 | |
| 73 | if (controller_register.get('disabled', False)): |
| 74 | slog.info('Controller %s is disabled' % instance.node.site_deployment.controller.name) |
| 75 | raise InnocuousException('Controller %s is disabled' % instance.node.site_deployment.controller.name) |
| 76 | |
| 77 | swarm_manager_url = controller_image.controller.auth_url |
| 78 | (swarm_manager_address, docker_registry_port) = swarm_manager_url.split(':') |
| 79 | slog.info("swarm_manager_address: %s docker_registry_port: %s" % |
| 80 | (swarm_manager_address, docker_registry_port)) |
| 81 | |
| 82 | input_fields = { |
| 83 | 'swarm_manager_address' : swarm_manager_address, |
| 84 | 'docker_registry_port' : docker_registry_port, |
| 85 | 'image_file_path' : controller_image.image.path, |
| 86 | 'image_dir' : os.path.dirname(controller_image.image.path), |
| 87 | 'image_name' : controller_image.image.name, |
| 88 | 'image_tag' : controller_image.image.tag, |
| 89 | 'delete' : True, |
| 90 | 'ansible_tag' : '%s@%s' % ( |
| 91 | controller_image.image.name, |
| 92 | controller_image.controller.name) # name of ansible playbook |
| 93 | } |
| 94 | slog.debug("input_fields: %s" % str(input_fields)) |
| 95 | return input_fields |
| 96 | except Exception as ex: |
| 97 | slog.error("Exception: %s %s %s" % (type(ex), str(ex), ex.args)) |
| 98 | slog.error("%s" % str(traceback.format_exc())) |
| 99 | |