Exemplo n.º 1
0
    def run(self):
        try:
            self._logger = logging.getLogger(__name__)
            self._dynamite_config = self.configuration or DynamiteConfig(
                etcd_endpoint=self._etcd_endpoint)
            self._logger.info(
                "Initialized DynamiteMETRICS with configuration %s", str(self))
            self._logger.setLevel(logging.INFO)
            self._running = True
            self.etcdctl = self.etcdctl or self._init_etcdctl(
                self._etcd_endpoint)
            self._create_etcd_service_metric_information_instances(
                self._dynamite_config)

            while self._running:
                try:
                    self._get_metrics_from_etcd()
                    time.sleep(1)
                except:
                    self._logger.exception("Unexpected error")
                    pass
                if self._exit_flag.value == 1:
                    self._running = False
        finally:
            self._exit_flag.value = 1
Exemplo n.º 2
0
def test_create_dynamiteConfig_object_with_arg_service_folder_list_argument_containing_incorrect_string_value(
        path_to_tmp_dynamite_config_file):
    arg_config_path = path_to_tmp_dynamite_config_file
    arg_service_folder_list = 'path\\to\\nowhere'

    with pytest.raises(NotADirectoryError):
        dynamite_config = DynamiteConfig(arg_config_path,
                                         arg_service_folder_list)
Exemplo n.º 3
0
    def recover_dynamite(self):
        self._set_dynamite_application_status_etcd(DYNAMITE_APPLICATION_STATUS.RECOVERING)

        self.dynamite_config = DynamiteConfig(etcd_endpoint=self._arg_etcd_endpoint, fleet_endpoint=self._arg_fleet_endpoint)
        self.dynamite_service_handler = DynamiteServiceHandler(dynamite_config=self.dynamite_config,
                                                               etcd_endpoint=self._arg_etcd_endpoint)

        self._set_dynamite_application_status_etcd(DYNAMITE_APPLICATION_STATUS.RUNNING)
Exemplo n.º 4
0
def test_create_dynamiteConfig_object_without_fleet_endpoint_argument(
        path_to_tmp_dynamite_config_file):
    arg_config_path = path_to_tmp_dynamite_config_file

    expected_ip = FLEET_IP
    expected_port = FLEET_PORT
    dynamite_config = DynamiteConfig(arg_config_path=arg_config_path)
    assert dynamite_config.FleetAPIEndpoint.ip == expected_ip
    assert dynamite_config.FleetAPIEndpoint.port == expected_port
Exemplo n.º 5
0
def test_create_dynamiteConfig_object_with_fleet_endpoint_argument(
        path_to_tmp_dynamite_config_file):
    arg_config_path = path_to_tmp_dynamite_config_file

    expected_ip = "127.0.0.1"
    expected_port = 9876
    dynamite_config = DynamiteConfig(arg_config_path=arg_config_path,
                                     fleet_endpoint=expected_ip + ":" +
                                     str(expected_port))
    assert dynamite_config.FleetAPIEndpoint.ip == expected_ip
    assert dynamite_config.FleetAPIEndpoint.port == expected_port
Exemplo n.º 6
0
def test_create_dynamiteConfig_object_with_arg_service_folder_list_argument_containing_correct_string_value(
        path_to_tmp_dynamite_config_file):
    arg_config_path = path_to_tmp_dynamite_config_file
    arg_service_folder_list = 'tests\\TEST_CONFIG_FOLDER\\service-files'

    dynamite_config = DynamiteConfig(arg_config_path, arg_service_folder_list)

    path_list = dynamite_config.ServiceFiles.PathList

    assert len(path_list) == 2
    assert 'tests\\TEST_CONFIG_FOLDER\\service-files' in path_list
    assert absolute_path_of_service_folder in path_list
Exemplo n.º 7
0
def test_create_dynamiteConfig_object_with_arg_service_folder_list_argument_same_value_as_in_config(
        path_to_tmp_dynamite_config_file):
    arg_config_path = path_to_tmp_dynamite_config_file
    arg_service_folder_list = [absolute_path_of_service_folder]

    dynamite_config = DynamiteConfig(arg_config_path, arg_service_folder_list)
    assert dynamite_config

    path_list = dynamite_config.ServiceFiles.PathList

    assert len(path_list) == 1
    assert absolute_path_of_service_folder in path_list
Exemplo n.º 8
0
def test_create_dynamiteConfig_object_with_arg_service_folder_list_argument_with_multiple_duplicate_values(
        path_to_tmp_dynamite_config_file):
    arg_config_path = path_to_tmp_dynamite_config_file
    arg_service_folder_list = [
        absolute_path_of_service_folder,
        'tests\\TEST_CONFIG_FOLDER\\service-files'
    ]

    dynamite_config = DynamiteConfig(arg_config_path, arg_service_folder_list)
    assert dynamite_config

    path_list = dynamite_config.ServiceFiles.PathList

    assert len(path_list) == 2
    assert 'tests\\TEST_CONFIG_FOLDER\\service-files' in path_list
    assert absolute_path_of_service_folder in path_list
Exemplo n.º 9
0
def test_correct_values_in_dynamiteConfig_object_after_creation(
        path_to_tmp_dynamite_config_file):
    arg_config_path = path_to_tmp_dynamite_config_file
    arg_service_folder_list = 'tests\\TEST_CONFIG_FOLDER\\service-files'

    dynamite_config = DynamiteConfig(arg_config_path, arg_service_folder_list)

    service_files = dynamite_config.ServiceFiles
    assert len(service_files.PathList) == 2
    assert 'tests\\TEST_CONFIG_FOLDER\\service-files' in service_files.PathList
    assert absolute_path_of_service_folder in service_files.PathList

    fleet_api_endpoint = dynamite_config.FleetAPIEndpoint
    assert fleet_api_endpoint.ip == "172.17.8.101"
    assert fleet_api_endpoint.port == 49153

    etcd = dynamite_config.ETCD
    assert etcd.application_base_path == '/services'

    service = dynamite_config.Service
    assert service.apache is not None
    assert service.haproxy is not None
    assert service.apache.type == 'webserver'
    assert 'zurmo_application' in service.apache.service_dependency
    assert 'zurmo_config' in service.apache.service_dependency
    assert service.haproxy.service_dependency is None
    assert service.haproxy.scale_up_policy is None

    scaling_policy = dynamite_config.ScalingPolicy
    assert scaling_policy.scale_up is not None
    assert scaling_policy.scale_down is not None
    assert scaling_policy.scale_up.metric == 'response_time'
    assert scaling_policy.scale_up.threshold == 250
    assert scaling_policy.scale_down.service_type == 'apache'
    assert scaling_policy.scale_down.cooldown_period_unit == 'minute'


# if __name__ == '__main__':
#     pass
#
#     print(type(dynamite_config_yaml))
#     print(type(x))
#     print(x['Dynamite'])
#     print(x['Dynamite']['FleetAPIEndpoint'])
#     print(x['Dynamite']['FleetAPIEndpoint']['IP'])
Exemplo n.º 10
0
 def _create_dynamite_config(self, etcd_endpoint):
     self._dynamite_config = DynamiteConfig(etcd_endpoint=etcd_endpoint)
Exemplo n.º 11
0
def test_create_dynamiteConfig_with_incorrect_arg_config_path_argument():
    arg_config_path = "/just/plain/wrong.yaml"

    with pytest.raises(FileNotFoundError):
        dynamite_config = DynamiteConfig(arg_config_path)
Exemplo n.º 12
0
def test_create_dynamiteConfig_object_with_arg_config_path_argument(
        path_to_tmp_dynamite_config_file):
    arg_config_path = path_to_tmp_dynamite_config_file

    dynamite_config = DynamiteConfig(arg_config_path=arg_config_path)
    assert dynamite_config
Exemplo n.º 13
0
                    fleet_service = FleetService.dict_to_instance(value)
                    fleet_service_dict[fleet_service.name] = fleet_service
                else:
                    value = json.loads(service.value)
                    fleet_service_instance = FleetService.FleetServiceInstance.dict_to_instance(value)
                    fleet_service_instance_list.append(fleet_service_instance)

            for fleet_service_instance in fleet_service_instance_list:
                if "@" in fleet_service_instance.name:
                    service_name = fleet_service_instance.name.split("@")[0]
                else:
                    service_name = fleet_service_instance.name.replace(".service", "")

                fleet_service_dict[service_name].fleet_service_instances[fleet_service_instance.name] = fleet_service_instance

            return fleet_service_dict

        else:
            return None

if __name__ == '__main__':

    path_to_config_file = "..\\tests\\TEST_CONFIG_FOLDER\\config.yaml"
    service_folder_list = ['C:\\Users\\brnr\\PycharmProjects\\dynamite\\dynamite\\tests\\TEST_CONFIG_FOLDER\\service-files']

    dynamite_config = DynamiteConfig(path_to_config_file, service_folder_list)

    service_handler = DynamiteServiceHandler(dynamite_config)

    service_handler.add_new_fleet_service_instance("a")
Exemplo n.º 14
0
    def _init_dynamite(self, init_failed=False):
        self._set_dynamite_application_status_etcd(DYNAMITE_APPLICATION_STATUS.INITIALIZING)

        self.dynamite_config = DynamiteConfig(arg_config_path=self._arg_config_path,
                                              arg_service_folder_list=self._arg_service_folder,
                                              fleet_endpoint=self._arg_fleet_endpoint)

        # If the initialization process failed then first delete everything already saved to etcd and all
        # the running fleet services
        if init_failed:
            try:
                self.etcdctl.delete(ETCDCTL.etcd_key_base_path, recursive=True)
            except etcd.EtcdKeyNotFound:
                pass

            fleet_ip = self.dynamite_config.FleetAPIEndpoint.ip
            fleet_port = self.dynamite_config.FleetAPIEndpoint.port

            fleet_units_url = "http://" + fleet_ip + ":" + str(fleet_port) + "/fleet/v1/units"
            response = requests.get(fleet_units_url)

            response = json.loads(response.text)

            if len(response) > 0:
                for unit_detail in response["units"]:
                    fleet_service_name = unit_detail['name']

                    fleet_path_to_service = fleet_units_url + "/" + fleet_service_name
                    requests.delete(fleet_path_to_service)

        self.dynamite_service_handler = DynamiteServiceHandler(dynamite_config=self.dynamite_config)

        # save dynamite yaml config to etcd - this will be loaded if dynamite should crash and restart
        dynamite_config_yaml = self.dynamite_config.dynamite_yaml_config
        dynamite_config_json = json.dumps(dynamite_config_yaml)
        self.etcdctl.write(ETCDCTL.etcd_key_init_application_configuration, dynamite_config_json)

        # save the fleet services (including the currently running instances to etcd -
        #   they will be loaded if dynamite should crash and restart
        for service_name, fleet_service in self.dynamite_service_handler.FleetServiceDict.items():
            etcd_base_key = ETCDCTL.etcd_key_running_services + "/" + service_name

            fleet_service_dict = fleet_service.to_dict()

            # Write the json for each service instance in its own path and delete those entries from the original
            # dict which will be saved in its own path
            #   makes it easier to update single service instances (e.g. updating status of service)
            copy_dict = {}
            if "fleet_service_instances" in fleet_service_dict:
                service_instances = fleet_service_dict['fleet_service_instances']
                copy_dict = service_instances.copy() if service_instances is not None else {}

            if len(copy_dict) != 0:
                for fleet_service_instance_name, fleet_service_instance_dict in copy_dict.items():
                    fleet_service_instance_json = json.dumps(fleet_service_instance_dict)
                    fleet_service_instance_name = fleet_service_instance_dict['name']

                    etcd_instance_key = etcd_base_key + "/" + fleet_service_instance_name
                    self.etcdctl.write(etcd_instance_key, fleet_service_instance_json)

                    del fleet_service_dict['fleet_service_instances'][fleet_service_instance_name]

            etcd_template_key = etcd_base_key + "/" + ETCDCTL.etcd_name_fleet_service_template
            self.etcdctl.write(etcd_template_key, json.dumps(fleet_service_dict))

        self._set_dynamite_application_status_etcd(DYNAMITE_APPLICATION_STATUS.RUNNING)