Exemplo n.º 1
0
 def add_relations_after_scaling(self, config, unit):
     logger.info("adding relations after scaling %s" % self.service_instance.name)
     for ext_service in self.service_instance.relation:
         logger.debug(
             "solving dependencies between si %s and external service %s"
             % (self.service_instance.name, ext_service.name)
         )
         if ext_service.name == "dns" and self.is_dnsaas is True:
             ext_unit = Unit(hostname=None, state="INITIALISING")
             ext_si = ServiceInstance(
                 name="dns", service_type="dns", state="INITIALISING", image="test", flavor="test", size={}
             )
             ext_unit.ips["mgmt"] = os.environ["DNSAAS_IP"]
             self.dns_configurator.configure_dns_entry(self.service_instance.service_type)(
                 unit.ips["mgmt"], unit.hostname
             )
             self.service_instance.adapter_instance.add_dependency(config, ext_unit, ext_si)
         else:
             service_list = self.db.get_by_name(ServiceInstance, ext_service.name)
             if len(service_list) == 1:
                 ext_si = service_list[0]
                 for ext_unit in ext_si.units:
                     ext_unit_config = {}
                     ext_unit_config["hostname"] = ext_unit.hostname
                     ext_unit_config["ips"] = ext_unit.ips
                     ext_unit_config["zabbix_ip"] = os.environ["ZABBIX_IP"]
                     ext_unit_config["floating_ips"] = ext_unit.floating_ips
                     ext_unit_config["hostname"] = ext_unit.hostname
                     logging.info(
                         "sending request add_dependency to the adapter %s with config %s and ext_unit %s"
                         % (self.service_instance.service_type, config, ext_unit)
                     )
                     self.service_instance.adapter_instance.add_dependency(config, ext_unit, ext_si)
                     ext_si.adapter_instance = FactoryServiceAdapter.get_agent(ext_si.service_type, ext_si.adapter)
                     ext_si.adapter_instance.add_dependency(ext_unit_config, unit, self.service_instance)
Exemplo n.º 2
0
    def configure_topology(self):
        for si in self.topology.service_instances:
            si.adapter_instance = FactoryServiceAdapter.get_agent(si.service_type, si.adapter)
            for unit in si.units:
                try:
                    config = {}
                    config["hostname"] = unit.hostname
                    config["ips"] = unit.ips
                    config["zabbix_ip"] = os.environ["ZABBIX_IP"]
                    config["floating_ips"] = unit.floating_ips
                    config["hostname"] = unit.hostname
                except:
                    logging.error("there was an issue getting the config for the vnf")

                try:
                    logging.info("sending requests to the adapter %s with config" % config)
                    si.adapter_instance.preinit(config)
                    si.adapter_instance.install(config)
                except Exception, e:
                    logging.error("error while configuring vnf %s" % e)

                # add relations
                for ext_service in si.relation:
                    logger.info(
                        "solving dependencies between si %s and external service %s" % (si.name, ext_service.name)
                    )
                    if ext_service.name == "dns" and self.is_dnsaas is True:
                        ext_unit = Unit(hostname=None, state="INITIALISING")
                        ext_si = ServiceInstance(
                            name="dns", service_type="dns", state="INITIALISING", image="test", flavor="test", size={}
                        )
                        ext_unit.ips["mgmt"] = os.environ["DNSAAS_IP"]
                        self.dns_configurator.configure_dns_entry(si.service_type)(unit.ips["mgmt"], unit.hostname)
                        si.adapter_instance.add_dependency(config, ext_unit, ext_si)
                    else:
                        service_list = self.db.get_by_name(ServiceInstance, ext_service.name)
                        if len(service_list) == 1:
                            ext_si = service_list[0]
                            for ext_unit in ext_si.units:
                                logging.info(
                                    "sending request add_dependency to the adapter %s with config %s and ext_unit %s"
                                    % (si.service_type, config, ext_unit)
                                )
                                si.adapter_instance.add_dependency(config, ext_unit, ext_si)
                try:
                    si.adapter_instance.pre_start(config)
                    si.adapter_instance.start(config)
                except Exception, e:
                    logging.error("error while configuring vnf %s" % e)
Exemplo n.º 3
0
 def start_policy_checker_si(self):
     LOG.debug("Start active_policy check for policy %s on service instance %s" % (
         self.policy.name, self.service_instance.name))
     while not self.is_stopped:
         LOG.debug("Locking policy checking from %s" % self.policy.name)
         self.lock.acquire()
         LOG.debug("Locked policy checking from %s" % self.policy.name)
         action = self.policy.action
         if action.scaling_adjustment > 0:
             if (len(self.service_instance.units) + action.scaling_adjustment) > self.service_instance.size.get(
                     'max'):
                 LOG.warning(
                     'Check upscaling - Maximum number of unit exceeded for service instance: %s' % self.service_instance.name)
                 LOG.debug("Release Policy lock by %s" % self.policy.name)
                 self.lock.release()
                 time.sleep(self.policy.period)
                 continue
         if action.scaling_adjustment < 0:
             if (len(self.service_instance.units) + action.scaling_adjustment) < self.service_instance.size.get(
                     'min'):
                 LOG.warning(
                     'Check downscaling - Minimum number of unit exceeded for service instance: %s' % self.service_instance.name)
                 LOG.debug("Release Policy lock by %s" % self.policy.name)
                 self.lock.release()
                 time.sleep(self.policy.period)
                 continue
         if self.service_instance.state != 'UPDATING' and self.check_alarm_si():
             LOG.debug('Execute action: %s' % repr(self.policy.action))
             if action.adjustment_type == 'ChangeInCapacity':
                 self.service_instance.state = 'UPDATING'
                 self.topology.state = 'UPDATING'
                 if action.scaling_adjustment > 0:
                     if (len(
                             self.service_instance.units) + action.scaling_adjustment) <= self.service_instance.size.get(
                             'max'):
                         for i in range(action.scaling_adjustment):
                             _hostname = '%s-%s' % (
                                 self.service_instance.name, str(len(self.service_instance.units) + 1))
                             _state = 'DEFINED'
                             new_unit = Unit(hostname=_hostname, state=_state)
                             new_unit.service_instance_id = self.service_instance.id
                             self.service_instance.units.append(new_unit)
                             self.db.persist(new_unit)
                     else:
                         LOG.warning(
                             'Maximum number of unit exceeded for service instance: %s' % self.service_instance.name)
                 else:
                     if (len(
                             self.service_instance.units) + action.scaling_adjustment) >= self.service_instance.size.get(
                             'min'):
                         for i in range(-action.scaling_adjustment):
                             removed_unit = self.remove_unit(self.topology, self.service_instance)
                             self.db.remove(removed_unit)
                     else:
                         LOG.warning(
                             'Minimum number of unit exceeded for service instance: %s' % self.service_instance.name)
                 topology = self.db.update(self.topology)
                 template = self.template_manager.get_template(self.topology)
                 # LOG.debug("Send update to heat template with: \n%s" % template)
                 try:
                     self.heat_client.update(stack_id=self.topology.ext_id, template=template)
                     self.wait_until_final_state()
                     if not self.topology.state == 'DEPLOYED':
                         LOG.error(
                             "ERROR: Something went wrong. Seems to be an error. Topology state -> %s" % self.topology.state)
                         self.lock.release()
                         return
                 except:
                     self.is_stopped = True
                     self.lock.release()
             LOG.info('Sleeping (cooldown) for %s seconds' % self.policy.action.cooldown)
             time.sleep(self.policy.action.cooldown)
         LOG.debug("Release Policy lock from %s" % self.policy.name)
         self.lock.release()
         LOG.info('Sleeping (evaluation period) for %s seconds' % self.policy.period)
         time.sleep(self.policy.period)
Exemplo n.º 4
0
 def start_policy_checker_si(self):
     LOG.debug(
         "Start active_policy check for policy %s on service instance %s" %
         (self.policy.name, self.service_instance.name))
     while not self.is_stopped:
         LOG.debug("Locking policy checking from %s" % self.policy.name)
         self.lock.acquire()
         LOG.debug("Locked policy checking from %s" % self.policy.name)
         action = self.policy.action
         if action.scaling_adjustment > 0:
             if (len(self.service_instance.units) +
                     action.scaling_adjustment
                 ) > self.service_instance.size.get('max'):
                 LOG.warning(
                     'Check upscaling - Maximum number of unit exceeded for service instance: %s'
                     % self.service_instance.name)
                 LOG.debug("Release Policy lock by %s" % self.policy.name)
                 self.lock.release()
                 time.sleep(self.policy.period)
                 continue
         if action.scaling_adjustment < 0:
             if (len(self.service_instance.units) +
                     action.scaling_adjustment
                 ) < self.service_instance.size.get('min'):
                 LOG.warning(
                     'Check downscaling - Minimum number of unit exceeded for service instance: %s'
                     % self.service_instance.name)
                 LOG.debug("Release Policy lock by %s" % self.policy.name)
                 self.lock.release()
                 time.sleep(self.policy.period)
                 continue
         if self.service_instance.state != 'UPDATING' and self.check_alarm_si(
         ):
             LOG.debug('Execute action: %s' % repr(self.policy.action))
             if action.adjustment_type == 'ChangeInCapacity':
                 self.service_instance.state = 'UPDATING'
                 self.topology.state = 'UPDATING'
                 if action.scaling_adjustment > 0:
                     if (len(self.service_instance.units) +
                             action.scaling_adjustment
                         ) <= self.service_instance.size.get('max'):
                         for i in range(action.scaling_adjustment):
                             _hostname = '%s-%s' % (
                                 self.service_instance.name,
                                 str(len(self.service_instance.units) + 1))
                             _state = 'DEFINED'
                             new_unit = Unit(hostname=_hostname,
                                             state=_state)
                             new_unit.service_instance_id = self.service_instance.id
                             self.service_instance.units.append(new_unit)
                             self.db.persist(new_unit)
                     else:
                         LOG.warning(
                             'Maximum number of unit exceeded for service instance: %s'
                             % self.service_instance.name)
                 else:
                     if (len(self.service_instance.units) +
                             action.scaling_adjustment
                         ) >= self.service_instance.size.get('min'):
                         for i in range(-action.scaling_adjustment):
                             removed_unit = self.remove_unit(
                                 self.topology, self.service_instance)
                             self.db.remove(removed_unit)
                     else:
                         LOG.warning(
                             'Minimum number of unit exceeded for service instance: %s'
                             % self.service_instance.name)
                 topology = self.db.update(self.topology)
                 template = self.template_manager.get_template(
                     self.topology)
                 # LOG.debug("Send update to heat template with: \n%s" % template)
                 try:
                     self.heat_client.update(stack_id=self.topology.ext_id,
                                             template=template)
                     self.wait_until_final_state()
                     if not self.topology.state == 'DEPLOYED':
                         LOG.error(
                             "ERROR: Something went wrong. Seems to be an error. Topology state -> %s"
                             % self.topology.state)
                         self.lock.release()
                         return
                 except:
                     self.is_stopped = True
                     self.lock.release()
             LOG.info('Sleeping (cooldown) for %s seconds' %
                      self.policy.action.cooldown)
             time.sleep(self.policy.action.cooldown)
         LOG.debug("Release Policy lock from %s" % self.policy.name)
         self.lock.release()
         LOG.info('Sleeping (evaluation period) for %s seconds' %
                  self.policy.period)
         time.sleep(self.policy.period)
Exemplo n.º 5
0
 def active_policy_unit(self):
     LOG.debug("Start active_policy check")
     while not self.is_stopped:
         LOG.debug("Locking policy checking by %s" % self.policy.name)
         self.lock.acquire()
         for unit in self.service_instance.units:
             action = self.policy.action
             if action.scaling_adjustment > 0:
                 if (len(self.service_instance.units) +
                         action.scaling_adjustment
                     ) > self.service_instance.size.get('max'):
                     LOG.warning(
                         'Check upscaling - Maximum number of unit exceeded for service instance: %s'
                         % self.service_instance.name)
                     break
             if action.scaling_adjustment < 0:
                 if (len(self.service_instance.units) +
                         action.scaling_adjustment
                     ) < self.service_instance.size.get('min'):
                     LOG.warning(
                         'Check downscaling - Minimum number of unit exceeded for service instance: %s'
                         % self.service_instance.name)
                     break
             if self.service_instance.state != 'UPDATING' and self.check_alarm_unit(
                     unit, self.monitor):
                 LOG.debug('Execute action: %s' % repr(self.policy.action))
                 if action.adjustment_type == 'ChangeInCapacity':
                     self.service_instance.state = 'UPDATING'
                     self.topology.state = 'UPDATING'
                     if action.scaling_adjustment > 0:
                         if (len(self.service_instance.units) +
                                 action.scaling_adjustment
                             ) <= self.service_instance.size.get('max'):
                             for i in range(action.scaling_adjustment):
                                 _hostname = '%s-%s' % (
                                     self.service_instance.name,
                                     str(
                                         len(self.service_instance.units) +
                                         1))
                                 _state = 'Initialised'
                                 new_unit = Unit(hostname=_hostname,
                                                 state=_state)
                                 self.service_instance.units.append(
                                     new_unit)
                         else:
                             LOG.warning(
                                 'Maximum number of unit exceeded for service instance: %s'
                                 % self.service_instance.name)
                     else:
                         if (len(self.service_instance.units) +
                                 action.scaling_adjustment
                             ) >= self.service_instance.size.get('min'):
                             for i in range(-action.scaling_adjustment):
                                 self.remove_unit(self.topology,
                                                  self.service_instance)
                         else:
                             LOG.warning(
                                 'Minimum number of unit exceeded for service instance: %s'
                                 % self.service_instance.name)
                     try:
                         self.db.update(self.topology)
                     except Exception, msg:
                         LOG.error(msg)
                         self.topology.state = 'ERROR'
                         self.topology.ext_id = None
                     template = self.template_manager.get_template(
                         self.topology)
                     # LOG.debug("Send update to heat template with: \n%s" % template)
                     self.heat_client.update(stack_id=self.topology.ext_id,
                                             template=template)
                 LOG.info('Sleeping (cooldown) for %s seconds' %
                          self.policy.action.cooldown)
                 time.sleep(self.policy.action.cooldown)
         LOG.debug("Release Policy lock by %s" % self.policy.name)
         self.lock.release()
         LOG.info('Sleeping (evaluation period) for %s seconds' %
                  self.policy.period)
         time.sleep(self.policy.period)
Exemplo n.º 6
0
    def create(self, config):
        ###Topology arguments
        top_name = config.get('name')
        top_state = 'DEFINED'
        top_service_instances = []
        LOG.debug("Parse Topology \"%s\"" % top_name)
        ###Parse all service instances described in config file
        sis_config = config.get('service_instances')
        for si_config in sis_config:
            si_args = {}
            si_args['state'] = 'DEFINED'
            service = None
            if si_config.get('service_type'):
                service_type = si_config.get('service_type')
                LOG.debug('Fetching service of service_type \"%s\"' %
                          service_type)
                service_list = self.db.get_by_service_type(
                    Service, service_type)
                if len(service_list) == 1:
                    service = service_list[0]
                    LOG.debug('Service \"%s\" is available.' % service)
                    si_args['service_type'] = service.service_type
                    si_args['image'] = service.image
                    si_args['flavor'] = service.flavor
                    si_args['configuration'] = service.configuration
                    si_args['key'] = service.key
                    si_args['size'] = service.size
                    si_args['adapter'] = service.adapter
                    si_args['requirements'] = []
                    for requirement in service.requirements:
                        func = Requirement.__init__
                        needed_parameters = func.func_code.co_varnames[
                            1:func.func_code.co_argcount]
                        args = {}
                        for param in needed_parameters:
                            args[param] = requirement.__dict__.get(param)
                        new_requirement = Requirement(**args)
                        si_args['requirements'].append(new_requirement)
                    si_args['networks'] = []
                    for network in service.networks:
                        func = Network_Instance.__init__
                        needed_parameters = func.func_code.co_varnames[
                            1:func.func_code.co_argcount]
                        args = {}
                        for param in needed_parameters:
                            args[param] = network.__dict__.get(param)
                        new_network = Network_Instance(**args)
                        si_args['networks'].append(new_network)
                    si_args['user_data'] = []
                    for command in service.user_data:
                        func = Command.__init__
                        needed_parameters = func.func_code.co_varnames[
                            1:func.func_code.co_argcount]
                        args = {}
                        for param in needed_parameters:
                            args[param] = command.__dict__.get(param)
                        new_command = Command(**args)
                        si_args['user_data'].append(new_command)
                else:
                    raise NotFoundException(
                        'service_type:\"%s\" is not available.' % service_type)
            else:
                raise NotDefinedException("service_type is not defined.")
            for si_item in si_config:
                if si_item == "name":
                    si_args['name'] = si_config.get(si_item)
                    LOG.debug("Parsing service instance \"%s\"" %
                              si_args['name'])
                elif si_item == "service_type":
                    si_args['service_type'] = si_config.get(si_item)
                elif si_item == "adapter":
                    si_args['adapter'] = si_config.get(si_item)
                elif si_item == "image":
                    image_name = si_config.get(si_item)
                    image_list = self.db.get_by_name(Image, image_name)
                    if len(image_list) == 1:
                        image = image_list[0]
                        si_args['image'] = image
                    else:
                        raise NotFoundException(
                            'image:\"%s\" is not available.' % image_name)
                elif si_item == "flavor":
                    flavor_name = si_config.get(si_item)
                    flavor_list = self.db.get_by_name(Flavor, flavor_name)
                    if len(flavor_list) == 1:
                        flavor = flavor_list[0]
                        si_args['flavor'] = flavor
                    else:
                        raise NotFoundException(
                            'flavor:\"%s\" is not available.' % flavor_name)
                elif si_item == "key":
                    key_name = si_config.get(si_item)
                    key_list = self.db.get_by_name(Key, key_name)
                    if len(key_list) == 1:
                        key = key_list[0]
                        si_args['key'] = key
                    else:
                        raise NotFoundException(
                            'key:\"%s\" is not available.' % key_name)
                elif si_item == "size":
                    si_args['size'].update(si_config.get(si_item))
                elif si_item == "networks":
                    networks = []
                    _networks = si_config.get(si_item)
                    LOG.debug("Fetch SecurityGroups for networks %s." %
                              _networks)
                    for _net_inst in _networks:
                        secgroups = []
                        for _secgroup_name in _net_inst.get('security_groups'):
                            lst = self.db.get_by_name(SecurityGroup,
                                                      _secgroup_name)
                            if len(lst) > 0:
                                _secgroup = lst[0]
                            else:
                                raise NotFoundException(
                                    'SecurityGroup:\"%s\" is not available.' %
                                    _secgroup_name)
                            secgroups.append(_secgroup)
                        _net_inst['security_groups'] = secgroups
                        networks.append(Network_Instance(**_net_inst))
                    si_args['networks'] = networks
                elif si_item == "configuration":
                    for key in si_config.get(si_item).keys():
                        if key in service.configuration.keys():
                            si_args['configuration'][key] = si_config.get(
                                si_item).get(key)
                elif si_item == "policies":
                    policies = []
                    _policies = si_config.get(si_item)
                    for _policy in _policies:
                        _new_policy_args = {}
                        for _po_item in _policy:
                            if _po_item == "name":
                                _new_policy_args.update(
                                    {'name': _policy.get(_po_item)})
                            elif _po_item == "period":
                                _new_policy_args.update(
                                    {'period': _policy.get(_po_item)})
                            elif _po_item == "alarm":
                                _new_alarm_args = _policy.get(_po_item)
                                _new_alarm = Alarm(**_new_alarm_args)
                                _new_policy_args.update({'alarm': _new_alarm})
                            elif _po_item == "action":
                                _new_action_args = _policy.get(_po_item)
                                _new_action = Action(**_new_action_args)
                                _new_policy_args.update(
                                    {'action': _new_action})
                        try:
                            _new_policy = Policy(**_new_policy_args)
                        except TypeError:
                            raise InvalidInputException()
                        policies.append(_new_policy)
                    si_args['policies'] = policies
                elif si_item == "requirements":
                    requirements = []
                    for _req in si_config.get(si_item):
                        _req_args = _req
                        requirement = Requirement(**_req_args)
                        requirements.append(requirement)
                    si_args['requirements'] = requirements
                elif si_item == "user_data":
                    user_data = []
                    for _user_data_item in si_config.get(si_item):
                        command = Command(_user_data_item)
                        user_data.append(command)
                    si_args['user_data'] = user_data
                elif si_item == "location":
                    location = []
                    for _loc in si_config.get(si_item):
                        LOG.debug("found item location %s" % _loc)
                        loc = Location(_loc)
                        location.append(loc)
                    si_args['location'] = location
                elif si_item == "relation":
                    relation = []
                    for _rel in si_config.get(si_item):
                        LOG.debug("found item relation %s" % _rel)
                        rel = Relation(_rel)
                        relation.append(rel)
                    si_args['relation'] = relation
                else:
                    raise InvalidInputException(
                        "parameter \"%s\" is not provided by Services." %
                        si_config.get(si_item))

            ###Initialise Units
            units = []
            unit_number = si_args.get('size').get('def') or 1
            for i in range(1, unit_number + 1):
                if i == 1:
                    _hostname = '%s' % si_args.get('name')
                else:
                    _hostname = '%s-%s' % (si_args.get('name'), i)
                _new_unit = Unit(hostname=_hostname, state='DEFINED')
                units.append(_new_unit)
            si_args['units'] = units
            ###Initialise Service Instance
            new_service_instance = ServiceInstance(**si_args)
            ###Add the new service instance to the topology
            top_service_instances.append(new_service_instance)

        ###Initialise Topology
        ext_name = '' + top_name + '_' + str(random.randint(1000, 9999))
        topology = Topology(name=top_name,
                            state=top_state,
                            service_instances=top_service_instances,
                            ext_name=ext_name)
        LOG.debug(topology)
        return topology
Exemplo n.º 7
0
 def update(self, new_topology, old_topology):
     conf = sys_util().get_sys_conf()
     db = FactoryAgent().get_agent(conf['database_manager'])
     updated_topology = old_topology
     updated_topology.name = new_topology.name
     #check for additional service instances and add them to the list of new instances
     appended_service_instances = []
     for new_service_instance in new_topology.service_instances:
         is_found = False
         for updated_service_instance in updated_topology.service_instances:
             if new_service_instance.name == updated_service_instance.name:
                 is_found = True
                 break
         if not is_found:
             appended_service_instances.append(new_service_instance)
     #check for removed service instances and add it to the list of removed instances
     removed_service_instances = []
     for updated_service_instance in updated_topology.service_instances:
         is_found = False
         for new_service_instance in new_topology.service_instances:
             if new_service_instance.name == updated_service_instance.name:
                 is_found = True
                 break
         if not is_found:
             removed_service_instances.append(updated_service_instance)
     #remove removed service instances
     for removed_service_instance in removed_service_instances:
         updated_topology.service_instances.remove(removed_service_instance)
         LOG.debug('Removed ServiceInstance \"%s\" from Topology \"%s\".' %
                   (removed_service_instance.name, updated_topology.name))
     #append additional service instances
     for appended_service_instance in appended_service_instances:
         appended_service_instance.topology_id = updated_topology.id
         updated_topology.service_instances.append(
             appended_service_instance)
         if appended_service_instance.policies is not None:
             for policy in appended_service_instance.policies:
                 db.persist(policy)
         db.persist(appended_service_instance)
         LOG.debug('Appended ServiceInstance \"%s\" to Topology \"%s\".' %
                   (appended_service_instance.name, updated_topology.name))
     #Update all values for each service instance
     for updated_service_instance in updated_topology.service_instances:
         for new_service_instance in new_topology.service_instances:
             if updated_service_instance.name == new_service_instance.name:
                 updated_service_instance.size = new_service_instance.size
                 updated_service_instance.configuration = new_service_instance.configuration
                 updated_service_instance.policies = new_service_instance.policies
                 #updated_service_instance.service_type = new_service_instance.service_type
                 if new_service_instance.service_type and updated_service_instance.service_type != new_service_instance.service_type:
                     LOG.warning(
                         "Cannot update service_type for %s->%s. Not Implemented."
                         % (updated_topology.name,
                            updated_service_instance.name))
                 if new_service_instance.adapter and updated_service_instance.adapter != new_service_instance.adapter:
                     LOG.warning(
                         "Cannot update adapter for %s->%s. Not Implemented."
                         % (updated_topology.name,
                            updated_service_instance.name))
                 #updated_service_instance.flavor = new_service_instance.flavor
                 if new_service_instance.flavor and updated_service_instance.flavor.name != new_service_instance.flavor.name:
                     LOG.warning(
                         "Cannot update flavor for %s->%s. Not Implemented."
                         % (updated_topology.name,
                            updated_service_instance.name))
                 #updated_service_instance.image = new_service_instance.image
                 if new_service_instance.image and updated_service_instance.image.name != new_service_instance.image.name:
                     LOG.warning(
                         "Cannot update image for %s->%s. Not Implemented."
                         % (updated_topology.name,
                            updated_service_instance.name))
                 #updated_service_instance.networks = new_service_instance.networks
                 if new_service_instance.networks is not None:
                     LOG.warning(
                         "Cannot update networks for %s->%s. Not Implemented."
                         % (updated_topology.name,
                            updated_service_instance.name))
                 #updated_service_instance.requirements = new_service_instance.requirements
                 if new_service_instance.requirements is not None:
                     LOG.warning(
                         "Cannot update networks for %s->%s. Not Implemented."
                         % (updated_topology.name,
                            updated_service_instance.name))
                 #updated_service_instance.user_data = new_service_instance.user_data
                 if new_service_instance.user_data is not None:
                     LOG.warning(
                         "Cannot update user_data for %s->%s. Not Implemented."
                         % (updated_topology.name,
                            updated_service_instance.name))
                 if new_service_instance.key and updated_service_instance.key.name != new_service_instance.key.name:
                     LOG.warning(
                         "Cannot update key for %s->%s without replacement."
                         % (updated_topology.name,
                            updated_service_instance.name))
     #Add or remove units according to minimal or maximal size
     for updated_service_instance in updated_topology.service_instances:
         if updated_service_instance not in appended_service_instances:
             if len(updated_service_instance.units
                    ) < updated_service_instance.size.get('min'):
                 for i in range(
                         updated_service_instance.size.get('min') -
                         len(updated_service_instance.units)):
                     _hostname = '%s-%s' % (
                         updated_service_instance.name,
                         str(len(updated_service_instance.units) + 1))
                     _state = 'DEFINED'
                     new_unit = Unit(hostname=_hostname, state=_state)
                     new_unit.service_instance_id = updated_service_instance.id
                     updated_service_instance.units.append(new_unit)
                     db.persist(new_unit)
             if len(updated_service_instance.units
                    ) > updated_service_instance.size.get('max'):
                 for i in range(
                         len(updated_service_instance.units) -
                         updated_service_instance.size.get('max')):
                     removed_unit = updated_service_instance.units.pop(
                         len(updated_service_instance.units) - 1)
                     db.remove(removed_unit)
     return updated_topology
 def update(self, new_topology, old_topology):
     conf = sys_util().get_sys_conf()
     db = FactoryAgent().get_agent(conf['database_manager'])
     updated_topology = old_topology
     updated_topology.name = new_topology.name
     #check for additional service instances and add them to the list of new instances
     appended_service_instances = []
     for new_service_instance in new_topology.service_instances:
         is_found = False
         for updated_service_instance in updated_topology.service_instances:
             if new_service_instance.name == updated_service_instance.name:
                 is_found = True
                 break
         if not is_found:
             appended_service_instances.append(new_service_instance)
     #check for removed service instances and add it to the list of removed instances
     removed_service_instances = []
     for updated_service_instance in updated_topology.service_instances:
         is_found = False
         for new_service_instance in new_topology.service_instances:
             if new_service_instance.name == updated_service_instance.name:
                 is_found = True
                 break
         if not is_found:
             removed_service_instances.append(updated_service_instance)
     #remove removed service instances
     for removed_service_instance in removed_service_instances:
         updated_topology.service_instances.remove(removed_service_instance)
         logger.debug('Removed ServiceInstance \"%s\" from Topology \"%s\".' % (removed_service_instance.name, updated_topology.name))
     #append additional service instances
     for appended_service_instance in appended_service_instances:
         appended_service_instance.topology_id = updated_topology.id
         updated_topology.service_instances.append(appended_service_instance)
         if appended_service_instance.policies is not None:
             for policy in appended_service_instance.policies:
                 db.persist(policy)
         db.persist(appended_service_instance)
         logger.debug('Appended ServiceInstance \"%s\" to Topology \"%s\".' % (appended_service_instance.name, updated_topology.name))
     #Update all values for each service instance
     for updated_service_instance in updated_topology.service_instances:
         for new_service_instance in new_topology.service_instances:
             if updated_service_instance.name == new_service_instance.name:
                 updated_service_instance.size = new_service_instance.size
                 updated_service_instance.configuration = new_service_instance.configuration
                 updated_service_instance.policies = new_service_instance.policies
                 #updated_service_instance.service_type = new_service_instance.service_type
                 if new_service_instance.service_type and updated_service_instance.service_type != new_service_instance.service_type:
                     logger.warning("Cannot update service_type for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                 if new_service_instance.adapter and updated_service_instance.adapter != new_service_instance.adapter:
                     logger.warning("Cannot update adapter for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                 #updated_service_instance.flavor = new_service_instance.flavor
                 if new_service_instance.flavor and updated_service_instance.flavor.name != new_service_instance.flavor.name:
                     logger.warning("Cannot update flavor for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                 #updated_service_instance.image = new_service_instance.image
                 if new_service_instance.image and updated_service_instance.image.name != new_service_instance.image.name:
                     logger.warning("Cannot update image for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                 #updated_service_instance.networks = new_service_instance.networks
                 if new_service_instance.networks is not None:
                     logger.warning("Cannot update networks for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                 #updated_service_instance.requirements = new_service_instance.requirements
                 if new_service_instance.requirements is not None:
                     logger.warning("Cannot update networks for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                 #updated_service_instance.user_data = new_service_instance.user_data
                 if new_service_instance.user_data is not None:
                     logger.warning("Cannot update user_data for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                 if new_service_instance.key and updated_service_instance.key.name != new_service_instance.key.name:
                     logger.warning("Cannot update key for %s->%s without replacement." % (updated_topology.name, updated_service_instance.name))
     #Add or remove units according to minimal or maximal size
     for updated_service_instance in updated_topology.service_instances:
         if updated_service_instance not in appended_service_instances:
             if len(updated_service_instance.units) < updated_service_instance.size.get('min'):
                 for i in range(updated_service_instance.size.get('min') - len(updated_service_instance.units)):
                     _hostname = '%s-%s' % (
                         updated_service_instance.name, str(len(updated_service_instance.units) + 1))
                     _state = 'DEFINED'
                     new_unit = Unit(hostname=_hostname, state=_state)
                     new_unit.service_instance_id = updated_service_instance.id
                     updated_service_instance.units.append(new_unit)
                     db.persist(new_unit)
             if len(updated_service_instance.units) > updated_service_instance.size.get('max'):
                 for i in range(len(updated_service_instance.units) - updated_service_instance.size.get('max')):
                     removed_unit = updated_service_instance.units.pop(len(updated_service_instance.units) - 1)
                     db.remove(removed_unit)
     return updated_topology
Exemplo n.º 9
0
    def start_policy_checker_si(self):
        logger.debug(
            "Start active_policy check for policy %s on service instance %s"
            % (self.policy.name, self.service_instance.name)
        )
        while not self.is_stopped:
            logger.debug("Locking policy checking from %s" % self.policy.name)
            self.lock.acquire()
            logger.debug("Locked policy checking from %s" % self.policy.name)
            action = self.policy.action
            if action.scaling_adjustment > 0:
                if (len(self.service_instance.units) + action.scaling_adjustment) > self.service_instance.size.get(
                    "max"
                ):
                    logger.warning(
                        "Check upscaling - Maximum number of unit exceeded for service instance: %s"
                        % self.service_instance.name
                    )
                    logger.debug("Release Policy lock by %s" % self.policy.name)
                    self.lock.release()
                    time.sleep(self.policy.period)
                    continue
            if action.scaling_adjustment < 0:
                if (len(self.service_instance.units) + action.scaling_adjustment) < self.service_instance.size.get(
                    "min"
                ):
                    logger.warning(
                        "Check downscaling - Minimum number of unit exceeded for service instance: %s"
                        % self.service_instance.name
                    )
                    logger.debug("Release Policy lock by %s" % self.policy.name)
                    self.lock.release()
                    time.sleep(self.policy.period)
                    continue
            if self.service_instance.state != "UPDATING" and self.check_alarm_si():
                logger.debug("Execute action: %s" % repr(self.policy.action))
                if action.adjustment_type == "ChangeInCapacity":
                    self.service_instance.state = "UPDATING"
                    self.topology.state = "UPDATING"
                    if action.scaling_adjustment > 0:
                        logger.info("executing scaling out action ")
                        info = {
                            "so_id": "idnotusefulhere",
                            "sm_name": "imsaas",
                            "so_phase": "update",
                            "phase_event": "start",
                            "response_time": 0,
                            "tenant": "mcntub",
                        }
                        self.start_time = time.time()
                        info_json = json.dumps(info)
                        glogger.debug(info_json)
                        if (
                            len(self.service_instance.units) + action.scaling_adjustment
                        ) <= self.service_instance.size.get("max"):
                            for i in range(action.scaling_adjustment):
                                _hostname = "%s-%s" % (
                                    self.service_instance.name,
                                    str(len(self.service_instance.units) + 1),
                                )
                                _state = "DEFINED"
                                new_unit = Unit(hostname=_hostname, state=_state)
                                new_unit.service_instance_id = self.service_instance.id
                                self.service_instance.units.append(new_unit)
                                self.db.persist(new_unit)
                        else:
                            logger.warning(
                                "Maximum number of unit exceeded for service instance: %s" % self.service_instance.name
                            )
                    else:
                        logger.info("executing scaling in action ")
                        if (
                            len(self.service_instance.units) + action.scaling_adjustment
                        ) >= self.service_instance.size.get("min"):
                            for i in range(-action.scaling_adjustment):
                                removed_unit = self.remove_unit(self.topology, self.service_instance)
                                self.db.remove(removed_unit)
                        else:
                            logger.warning(
                                "Minimum number of unit exceeded for service instance: %s" % self.service_instance.name
                            )
                    topology = self.db.update(self.topology)
                    template = self.template_manager.get_template(self.topology)
                    # logger.debug("Send update to heat template with: \n%s" % template)
                    if action.scaling_adjustment <= 0:
                        logger.info("provisioning the unit after scaling in operation")
                        self.configure_after_scaling(removed_unit)
                    try:
                        logger.info("updating the heat template including new units")
                        self.heat_client.update(stack_id=self.topology.ext_id, template=template)
                        self.wait_until_final_state()
                        logger.info("wait until final state function executed")
                        if self.topology.state not in ["DEPLOYED", "UPDATED"]:
                            logger.error(
                                "ERROR: Something went wrong. Seems to be an error. Topology state -> %s"
                                % self.topology.state
                            )
                            self.lock.release()
                            return
                    except:
                        self.is_stopped = True
                        self.lock.release()
                    logger.info("entering provisioning phase")
                    if action.scaling_adjustment > 0:
                        logger.info("provisioning new unit after scaling out operation")
                        # adding relations between newly added unit and existing units from dependent services
                        self.configure_new_unit(new_unit)

                logger.info("Sleeping (cooldown) for %s seconds" % self.policy.action.cooldown)
                time.sleep(self.policy.action.cooldown)
            logger.debug("Release Policy lock from %s" % self.policy.name)
            self.lock.release()
            logger.info("Sleeping (evaluation period) for %s seconds" % self.policy.period)
            time.sleep(self.policy.period)