diff --git a/ambari-common/src/main/python/resource_management/libraries/functions/config_helper.py b/ambari-common/src/main/python/resource_management/libraries/functions/config_helper.py new file mode 100644 index 00000000000..2d85d29e474 --- /dev/null +++ b/ambari-common/src/main/python/resource_management/libraries/functions/config_helper.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +""" +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Ambari Agent + +""" + + +def get_mpack_name(config): + return config['hostLevelParams']['stack_name'] + + +def get_mpack_version(config): + return config['hostLevelParams']['stack_version'] + + +def get_mpack_instance_name(config): + return config['serviceGroupName'] + + +def get_module_name(config): + return config['serviceName'] + + +def get_component_type(config): + return config['role'] + + +def get_component_instance_name(config): + return "default" diff --git a/ambari-common/src/main/python/resource_management/libraries/functions/mpack_manager_helper.py b/ambari-common/src/main/python/resource_management/libraries/functions/mpack_manager_helper.py new file mode 100644 index 00000000000..652a9e959fa --- /dev/null +++ b/ambari-common/src/main/python/resource_management/libraries/functions/mpack_manager_helper.py @@ -0,0 +1,91 @@ +""" +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Ambari Agent + +""" + +import os +from instance_manager import create_mpack, set_mpack_instance, get_conf_dir, list_instances + +CONFIG_DIR_KEY_NAME = 'config_dir' +PATH_KEY_NAME = 'path' +COMPONENTS_PLURAL_KEY_NAME = 'components' +COMPONENT_INSTANCES_PLURAL_KEY_NAME = 'component-instances' + + +def get_component_conf_path(mpack_name, instance_name, module_name, components_instance_type, + subgroup_name='default', component_instance_name='default'): + """ + :returns the single string that contains the path to the configuration folder of given component instance + :raises ValueError if the parameters doesn't match the mpack or instances structure + """ + + conf_json = get_conf_dir(mpack_name, instance_name, subgroup_name, module_name, + {components_instance_type: [component_instance_name]}) + + return conf_json[COMPONENTS_PLURAL_KEY_NAME][components_instance_type.lower()][COMPONENT_INSTANCES_PLURAL_KEY_NAME][ + component_instance_name][CONFIG_DIR_KEY_NAME] + + +def get_component_target_path(mpack_name, instance_name, module_name, components_instance_type, + subgroup_name='default', component_instance_name='default'): + """ + :returns the single string that contains the path to the mpack component folder of given component instance + :raises ValueError if the parameters doesn't match the mpack or instances structure + """ + + instances_json = list_instances(mpack_name, instance_name, subgroup_name, module_name, + {components_instance_type: [component_instance_name]}) + + return instances_json[COMPONENTS_PLURAL_KEY_NAME][components_instance_type.lower()][ + COMPONENT_INSTANCES_PLURAL_KEY_NAME][component_instance_name][PATH_KEY_NAME] + + +def get_component_home_path(mpack_name, instance_name, module_name, components_instance_type, + subgroup_name='default', component_instance_name='default'): + """ + :returns the single string that contains the path to the module component folder of given component instance + :raises ValueError if the parameters doesn't match the mpack or instances structure + """ + + component_path = get_component_target_path(mpack_name=mpack_name, instance_name=instance_name, + subgroup_name=subgroup_name, + module_name=module_name, components_instance_type=components_instance_type, + component_instance_name=component_instance_name) + + return os.readlink(component_path) + + +def create_component_instance(mpack_name, mpack_version, instance_name, module_name, components_instance_type, + subgroup_name='default', component_instance_name='default'): + """ + creates the single component instance according to the parameters + :raises ValueError if the parameters doesn't match the mpack or instances structure + """ + create_mpack(mpack_name, mpack_version, instance_name, subgroup_name, module_name, + None, {components_instance_type: [component_instance_name]}) + + +def set_component_instance_version(mpack_name, mpack_version, instance_name, module_name, components_instance_type, + subgroup_name='default', component_instance_name='default'): + """ + changes the version of the single component instance according to the parameters + :raises ValueError if the parameters doesn't match the mpack or instances structure + """ + set_mpack_instance(mpack_name, mpack_version, instance_name, subgroup_name, module_name, + None, {components_instance_type: [component_instance_name]}) diff --git a/ambari-common/src/main/python/resource_management/libraries/script/script.py b/ambari-common/src/main/python/resource_management/libraries/script/script.py index 7813efc582c..7c9c0b13f36 100644 --- a/ambari-common/src/main/python/resource_management/libraries/script/script.py +++ b/ambari-common/src/main/python/resource_management/libraries/script/script.py @@ -61,6 +61,9 @@ from resource_management.libraries.functions.show_logs import show_logs from resource_management.core.providers import get_provider from resource_management.libraries.functions.fcntl_based_process_lock import FcntlBasedProcessLock +from resource_management.libraries.functions.mpack_manager_helper import create_component_instance +from resource_management.libraries.functions.config_helper import get_mpack_name, get_mpack_version, \ + get_mpack_instance_name, get_module_name, get_component_type, get_component_instance_name import ambari_simplejson as json # simplejson is much faster comparing to Python 2.6 json module and has the same functions set. @@ -791,6 +794,19 @@ def load_available_packages(self): Logger.exception("Unable to load available packages") self.available_packages_in_repos = [] + def create_component_instance(self): + config = self.get_config() + mpack_name = get_mpack_name(config) + mpack_version = get_mpack_version(config) + mpack_instance_name = get_mpack_instance_name(config) + module_name = get_module_name(config) + component_type = get_component_type(config) + component_instance_name = get_component_instance_name(config) + + create_component_instance(mpack_name=mpack_name, mpack_version=mpack_version, instance_name=mpack_instance_name, + module_name=module_name, components_instance_type=component_type, + component_instance_name=component_instance_name) + def install_packages(self, env): """