示例#1
0
 def _remove_shared_units(self):
     for service in self._get_units():
         version = None
         if ':' in service:
             service, version = service.split(':')
         self._remove_unit(utils.unit_name(service, self._name, self._group,
                                           version))
示例#2
0
    def _deploy_service(self):
        if not self._deploy_shared_units():
            LOGGER.info('Aborting run due to shared unit deployment error')
            return False
        unit_file = path.join(self._config_path, 'units', 'service',
                              self._name)
        unit_name = utils.unit_name(self._name, None, self._group,
                                    self._version)

        if not self._deploy_unit(unit_name, unit_file):
            LOGGER.info('Aborted: service unit deployment error')
            return False

        if not self._skip_consul:
            if not self._check_consul_for_service():
                LOGGER.error('Aborted: service missing on expected nodes')
                return False
            LOGGER.info('Service is being announced with Consul')

        self._shutdown_other_versions()
        if self._file_deployment:
            self._file_deployment.remove_other_archive_versions()

        parts = []
        if self._group:
            parts += [self._group]
        parts += [self._name, self._version]
        LOGGER.info('Deployment of %s and its dependencies successful',
                    ' '.join(parts))
        return True
示例#3
0
    def _check_consul_for_service(self):
        """Return true if the service expected to be running, is reported up
        in Consul by checking for all ip addresses to be present.

        :rtype: bool

        """
        version = 'version:{}'.format(self._version or 'latest')
        group = 'deploy:{}'.format(self._group) if self._group else None
        unit_name = utils.unit_name(self._name, None, self._group,
                                    self._version)
        state = self._fleet.state(True, unit_name)
        expected = set([s.ipaddr for s in state])
        LOGGER.debug('Checking to ensure service is up in consul for %i nodes',
                     len(expected))
        running = self._consul.catalog.service(self._name)
        actual = set()
        for node in running:
            if group and group not in node['ServiceTags']:
                continue
            elif version not in node['ServiceTags']:
                continue
            else:
                actual.add(node['ServiceAddress'])
        LOGGER.debug('Found %i nodes running %s %s',
                     len(actual), self._name, version)
        return (expected & actual) == expected
示例#4
0
    def _add_last_deployed_global(self):
        """Add the last global unit as a deployed unit for dependency injection
        in the first standalone or service unit.

        """
        service = self._config.get('global', [])[-1]
        version = None
        if ':' in service:
            service, version = service.split(':')
        self._deployed_units.append(utils.unit_name(service, 'global',
                                                    self._group, version))
示例#5
0
    def _remove_units(self):
        if self._global:
            self._remove_files()
            self._remove_globals()
            return True

        self._remove_unit(utils.unit_name(self._name, None, self._group,
                                          self._version))
        self._remove_shared_units()
        self._remove_files()
        return True
示例#6
0
 def _deploy_shared_units(self):
     # Ensure the file archive is there
     last_unit = self._deployed_units[-1]
     shared_unit_prefix = path.join(self._config_path, 'units', 'shared')
     for name in self._get_units():
         version = None
         if ':' in name:
             name, version = name.split(':')
         unit_file = path.join(shared_unit_prefix, name)
         unit_name = utils.unit_name(name, self._name,
                                     self._group,
                                     version)
         if not self._deploy_unit(unit_name, unit_file, last_unit):
             LOGGER.error('Aborting, failed to deploy %s', unit_name)
             return False
         last_unit = unit_name
     return True
示例#7
0
    def _deploy_globals(self):
        last_unit = None
        global_unit_prefix = path.join(self._config_path, 'units', 'shared')
        for service in self._config.get('global', []):
            version = None
            if ':' in service:
                service, version = service.split(':')
            unit_file = path.join(global_unit_prefix, service)
            unit_name = utils.unit_name(service, 'global', self._group, version)
            if not self._deploy_unit(unit_name, unit_file, last_unit):
                LOGGER.error('Aborting, failed to deploy %s', service)
                return False
            last_unit = unit_name

        self._shutdown_other_versions()
        if self._file_deployment:
            self._file_deployment.remove_other_archive_versions()

        return True
示例#8
0
    def _remove_files(self):
        if self._file_manifest():
            unit_name = utils.unit_name('file-deploy', self._name, self._group,
                                        self._file_manifest_hash())
            self._remove_unit(unit_name)
            if self._global:
                manifest_file = 'global.yaml'
            else:
                manifest_file = '{0}/{1}.yaml'.format(self._command, self._name)

            file_deployment = files.FileDeployment(unit_name,
                                                   self.env_config,
                                                   self._config_path,
                                                   manifest_file,
                                                   self._name,
                                                   self._environment)
            file_deployment.remove_archive()
            file_deployment.remove_other_archive_versions()
        else:
            LOGGER.debug('No manifest found')
示例#9
0
    def _deploy_files(self):
        if self._file_manifest():
            if self._global:
                manifest = 'global.yaml'
            else:
                manifest = '{0}/{1}.yaml'.format(self._command, self._name)

            unit_name = utils.unit_name('file-deploy', self._name, self._group,
                                        self._file_manifest_hash())

            self._file_deployment = files.FileDeployment(unit_name,
                                                         self.env_config,
                                                         self._config_path,
                                                         manifest,
                                                         self._name,
                                                         self._group,
                                                         self._environment)

            if self._unit_is_active(unit_name):
                self._deployed_units.append(unit_name)
                return True

            if self._file_deployment.build_archive():
                LOGGER.info('Uploading archive file to consul')
                self._file_deployment.upload_archive()

                unit = self._fleet.unit(unit_name)
                unit.read_string(self._file_deployment.unit_file())
                if self._deployed_units:
                    self._maybe_add_last_unit(unit, self._deployed_units[-1])

                LOGGER.info('Deploying archive file service: %s', unit_name)
                unit.submit()
                unit.start()

                if not self._wait_for_unit_to_become_active(unit_name):
                    LOGGER.error('Failed to deploy files')
                    return False

                self._deployed_units.append(unit_name)
        return True
示例#10
0
 def _remove_globals(self):
     for name in self._config.get('global', []):
         version = None
         if ':' in name:
             name, version = name.split(':')
         self._remove_unit(utils.unit_name(name, 'global', None, version))