소스 검색

AMBARI-3994. Create repository install functionality in the base script (Andrew Onischuk via dlysnichenko)

Lisnichenko Dmitro 11 년 전
부모
커밋
48e2184fe1

+ 2 - 1
ambari-agent/src/main/python/resource_management/libraries/providers/__init__.py

@@ -35,6 +35,7 @@ PROVIDERS = dict(
     ExecuteHadoop="resource_management.libraries.providers.execute_hadoop.ExecuteHadoopProvider",
     TemplateConfig="resource_management.libraries.providers.template_config.TemplateConfigProvider",
     XmlConfig="resource_management.libraries.providers.xml_config.XmlConfigProvider",
-    MonitorWebserver="resource_management.libraries.providers.monitor_webserver.MonitorWebserverProvider"
+    MonitorWebserver="resource_management.libraries.providers.monitor_webserver.MonitorWebserverProvider",
+    Repository="resource_management.libraries.providers.repository.RepositoryProvider"
   ),
 )

+ 53 - 0
ambari-agent/src/main/python/resource_management/libraries/providers/repository.py

@@ -0,0 +1,53 @@
+#!/usr/bin/env python2.6
+"""
+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
+
+"""
+
+from resource_management import *
+
+class RepositoryProvider(Provider):
+  def action_create(self):
+    with Environment.get_instance_copy() as env:
+      repo_file_name = self.resource.repo_file_name
+      repo_dir = repos_dirs[env.system.platform]
+      
+      File(format("{repo_dir}/{repo_file_name}.repo"),
+        content = InlineTemplate("""[{{repo_id}}]
+name={{repo_file_name}}
+{% if mirror_list %}mirrorlist={{mirror_list}}{% else %}baseurl={{base_url}}{% endif %}
+path=/
+enabled=1
+gpgcheck=0""", repo_id=self.resource.repo_id, repo_file_name=self.resource.repo_file_name, base_url=self.resource.base_url, mirror_list=self.resource.mirror_list)
+      )
+  
+  def action_remove(self):
+    with Environment.get_instance_copy() as env:
+      repo_file_name = self.resource.repo_file_name
+      repo_dir = repos_dirs[env.system.platform]
+        
+      File(format("{repo_dir}/{repo_file_name}.repo"),
+           action = "delete")
+    
+  
+repos_dirs = {
+  'centos': '/etc/yum.repos.d',
+  'redhat': '/etc/yum.repos.d',
+  'suse': '/etc/zypp/repos.d'
+}

+ 2 - 1
ambari-agent/src/main/python/resource_management/libraries/resources/__init__.py

@@ -22,4 +22,5 @@ Ambari Agent
 
 from resource_management.libraries.resources.execute_hadoop import *
 from resource_management.libraries.resources.template_config import *
-from resource_management.libraries.resources.xml_config import *
+from resource_management.libraries.resources.xml_config import *
+from resource_management.libraries.resources.repository import *

+ 34 - 0
ambari-agent/src/main/python/resource_management/libraries/resources/repository.py

@@ -0,0 +1,34 @@
+#!/usr/bin/env python2.6
+"""
+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
+
+"""
+
+_all__ = ["Repository"]
+
+from resource_management.core.base import Resource, ForcedListArgument, ResourceArgument, BooleanArgument
+
+class Repository(Resource):
+  action = ForcedListArgument(default="create")
+  repo_id = ResourceArgument(default=lambda obj: obj.name)
+  base_url = ResourceArgument()
+  mirror_list = ResourceArgument()
+  repo_file_name = ResourceArgument()
+
+  actions = Resource.actions + ["create","remove"]

+ 59 - 0
ambari-agent/src/main/python/resource_management/libraries/script/config_dictionary.py

@@ -0,0 +1,59 @@
+#!/usr/bin/env python2.6
+
+'''
+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.
+'''
+from resource_management.core.exceptions import Fail
+
+class ConfigDictionary(dict):
+  """
+  Immutable config dictionary
+  """
+  
+  def __init__(self, dictionary):
+    """
+    Recursively turn dict to ConfigDictionary
+    """
+    for k, v in dictionary.iteritems():
+      if isinstance(v, dict):
+        dictionary[k] = ConfigDictionary(v)
+        
+    super(ConfigDictionary, self).__init__(dictionary)
+
+  def __setitem__(self, name, value):
+    raise Fail("Configuration dictionary is immutable!")
+
+  def __getitem__(self, name):
+    """
+    Use Python types
+    """
+    value = super(ConfigDictionary, self).__getitem__(name)
+    
+    if value == "true":
+      value = True
+    elif value == "false":
+      value = False
+    else: 
+      try:
+        value = int(value)
+      except (ValueError, TypeError):
+        try:
+          value =  float(value)
+        except (ValueError, TypeError):
+          pass
+    
+    return value

+ 60 - 0
ambari-agent/src/main/python/resource_management/libraries/script/repo_installer.py

@@ -0,0 +1,60 @@
+#!/usr/bin/env python2.6
+
+'''
+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.
+'''
+import json
+from resource_management.libraries.resources.repository import Repository
+
+class RepoInstaller():    
+  @classmethod
+  def install_repos(cls, config):
+    cls._alter_repo("create", config['hostLevelParams']['repo_info'])
+    
+    if 'service_repo_info' in config['hostLevelParams']:
+      cls._alter_repo("create", config['hostLevelParams']['service_repo_info'])
+      
+  @classmethod
+  def remove_repos(cls, config):
+    cls._alter_repo("remove", config['hostLevelParams']['repo_info'])
+    
+    if 'service_repo_info' in config['hostLevelParams']:
+      cls._alter_repo("remove", config['hostLevelParams']['service_repo_info'])
+      
+  @staticmethod
+  def _alter_repo(action, repo_string):
+    """
+    @param action: "delete" or "create"
+    @param repo_string: e.g. "[{\"baseUrl\":\"http://public-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.0.6.0\",\"osType\":\"centos6\",\"repoId\":\"HDP-2.0._\",\"repoName\":\"HDP\",\"defaultBaseUrl\":\"http://public-repo-1.hortonworks.com/HDP/centos6/2.x/updates/2.0.6.0\"}]"
+    """
+    print repo_string
+    repo_dicts = json.loads(repo_string)
+    
+    if not isinstance(repo_dicts, list):
+      repo_dicts = [repo_dicts]
+      
+    for repo in repo_dicts:   
+      if not 'baseUrl' in repo:
+        repo['baseUrl'] = None
+      if not 'mirrorsList' in repo:
+        repo['mirrorsList'] = None
+      
+      Repository(repo['repoId'],
+                 action = action,
+                 base_url = repo['baseUrl'],
+                 mirror_list = repo['mirrorsList'],
+                 repo_file_name = repo['repoName'])

+ 7 - 40
ambari-agent/src/main/python/resource_management/libraries/script/script.py

@@ -27,6 +27,8 @@ import logging
 from resource_management.core.environment import Environment
 from resource_management.core.exceptions import Fail
 from resource_management.core.resources.packaging import Package
+from resource_management.libraries.script.config_dictionary import ConfigDictionary
+from resource_management.libraries.script.repo_installer import RepoInstaller
 
 class Script():
   """
@@ -120,6 +122,8 @@ class Script():
     from this list
     """
     config = self.get_config()
+    RepoInstaller.install_repos(config)
+    
     try:
       package_list_str = config['hostLevelParams']['package_list']
       if isinstance(package_list_str,basestring) and len(package_list_str) > 0:
@@ -129,6 +133,8 @@ class Script():
           Package(name)
     except KeyError:
       pass # No reason to worry
+    
+    RepoInstaller.remove_repos(config)
 
 
 
@@ -138,43 +144,4 @@ class Script():
     """
     print("Error: " + message)
     sys.stderr.write("Error: " + message)
-    sys.exit(1)
-
-class ConfigDictionary(dict):
-  """
-  Immutable config dictionary
-  """
-  
-  def __init__(self, dictionary):
-    """
-    Recursively turn dict to ConfigDictionary
-    """
-    for k, v in dictionary.iteritems():
-      if isinstance(v, dict):
-        dictionary[k] = ConfigDictionary(v)
-        
-    super(ConfigDictionary, self).__init__(dictionary)
-
-  def __setitem__(self, name, value):
-    raise Fail("Configuration dictionary is immutable!")
-
-  def __getitem__(self, name):
-    """
-    Use Python types
-    """
-    value = super(ConfigDictionary, self).__getitem__(name)
-    
-    if value == "true":
-      value = True
-    elif value == "false":
-      value = False
-    else: 
-      try:
-        value = int(value)
-      except (ValueError, TypeError):
-        try:
-          value =  float(value)
-        except (ValueError, TypeError):
-          pass
-    
-    return value
+    sys.exit(1)