dummy.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. Ambari Agent
  16. """
  17. __all__ = ["Dummy"]
  18. # Python Imports
  19. import os
  20. import re
  21. # Local Imports
  22. from resource_management.libraries.script.script import Script
  23. from resource_management.core.resources.system import Directory, File, Execute
  24. from ambari_commons.constants import AMBARI_SUDO_BINARY
  25. from resource_management.core.exceptions import ComponentIsNotRunning
  26. from resource_management.core.logger import Logger
  27. from resource_management.libraries.functions.default import default
  28. from resource_management.libraries.functions import get_kinit_path
  29. from resource_management.libraries.functions import conf_select
  30. from resource_management.libraries.functions import stack_select
  31. from resource_management.libraries.functions import StackFeature
  32. class Dummy(Script):
  33. """
  34. Dummy component to be used for performance testing since doesn't actually run a service.
  35. Reports status command based on the existence of a pid file.
  36. """
  37. # Whether or not configs have been loaded already in the prepare() method.
  38. loaded = False
  39. def prepare(self):
  40. # During restart commands which executes stop + start, avoid loading multiple times.
  41. if self.loaded:
  42. return
  43. self.loaded = True
  44. self.config = Script.get_config()
  45. # Cannot rely on system hostname since will run multiple Ambari agents on the same host.
  46. self.host_name = self.config["hostname"]
  47. # Should still define self.component_name which is needed for status commands.
  48. if "role" in self.config:
  49. self.component_name = self.config["role"]
  50. self.pid_file = "/var/run/%s/%s.pid" % (self.host_name, self.component_name)
  51. self.user = "root"
  52. self.user_group = "root"
  53. self.sudo = AMBARI_SUDO_BINARY
  54. print "Host: %s" % self.host_name
  55. print "Component: %s" % self.component_name
  56. print "Pid File: %s" % self.pid_file
  57. def install(self, env):
  58. print "Install"
  59. self.prepare()
  60. component_name = self.get_component_name()
  61. repo_info = str(default("/hostLevelParams/repo_info", "1.1.1.1-1"))
  62. matches = re.findall(r"([\d\.]+\-\d+)", repo_info)
  63. version = matches[0] if matches and len(matches) > 0 else "1.1.1.1-1"
  64. from resource_management.libraries.functions import stack_tools
  65. (stack_selector_name, stack_selector_path, stack_selector_package) = stack_tools.get_stack_tool(stack_tools.STACK_SELECTOR_NAME)
  66. command = 'ambari-python-wrap {0} install {1}'.format(stack_selector_path, version)
  67. Execute(command)
  68. if component_name:
  69. conf_select.select("PERF", component_name, version)
  70. stack_select.select(component_name, version)
  71. def configure(self, env):
  72. print "Configure"
  73. self.prepare()
  74. def start(self, env, upgrade_type=None):
  75. print "Start"
  76. self.prepare()
  77. if self.config['configurations']['cluster-env']['security_enabled'] :
  78. print "Executing kinit... "
  79. kinit_path_local = get_kinit_path(default('/configurations/kerberos-env/executable_search_paths', None))
  80. principal_replaced = self.config['configurations'][self.principal_conf_name][self.principal_name].replace("_HOST", self.host_name)
  81. keytab_path_replaced = self.config['configurations'][self.keytab_conf_name][self.keytab_name].replace("_HOST", self.host_name)
  82. Execute("%s -kt %s %s" % (kinit_path_local, keytab_path_replaced, principal_replaced),
  83. user="root")
  84. if not os.path.isfile(self.pid_file):
  85. print "Creating pid file: %s" % self.pid_file
  86. Directory(os.path.dirname(self.pid_file),
  87. owner=self.user,
  88. group=self.user_group,
  89. mode=0755,
  90. create_parents=True
  91. )
  92. File(self.pid_file,
  93. owner=self.user,
  94. content=""
  95. )
  96. def stop(self, env, upgrade_type=None):
  97. print "Stop"
  98. self.prepare()
  99. if os.path.isfile(self.pid_file):
  100. print "Deleting pid file: %s" % self.pid_file
  101. Execute("%s rm -rf %s" % (self.sudo, self.pid_file))
  102. def status(self, env):
  103. print "Status"
  104. self.prepare()
  105. if not os.path.isfile(self.pid_file):
  106. raise ComponentIsNotRunning()
  107. def get_component_name(self):
  108. """
  109. To be overridden by subclasses.
  110. Returns a string with the component name used in selecting the version.
  111. """
  112. pass