dummy.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. # Local Imports
  21. from resource_management.libraries.script.script import Script
  22. from resource_management.core.resources.system import Directory, File, Execute
  23. from ambari_commons.constants import AMBARI_SUDO_BINARY
  24. from resource_management.core.exceptions import ComponentIsNotRunning
  25. class Dummy(Script):
  26. """
  27. Dummy component to be used for performance testing since doesn't actually run a service.
  28. Reports status command based on the existence of a pid file.
  29. """
  30. # Whether or not configs have been loaded already in the prepare() method.
  31. loaded = False
  32. def prepare(self):
  33. # During restart commands which executes stop + start, avoid loading multiple times.
  34. if self.loaded:
  35. return
  36. self.loaded = True
  37. self.config = Script.get_config()
  38. # Cannot rely on system hostname since will run multiple Ambari agents on the same host.
  39. self.host_name = self.config["hostname"]
  40. # Should still define self.component_name which is needed for status commands.
  41. if "role" in self.config:
  42. self.component_name = self.config["role"]
  43. self.pid_file = "/var/run/%s/%s.pid" % (self.host_name, self.component_name)
  44. self.user = "root"
  45. self.user_group = "root"
  46. self.sudo = AMBARI_SUDO_BINARY
  47. print "Host: %s" % self.host_name
  48. print "Component: %s" % self.component_name
  49. print "Pid File: %s" % self.pid_file
  50. def install(self, env):
  51. print "Install"
  52. self.prepare()
  53. def configure(self, env):
  54. print "Configure"
  55. self.prepare()
  56. def start(self, env):
  57. print "Start"
  58. self.prepare()
  59. if not os.path.isfile(self.pid_file):
  60. print "Creating pid file: %s" % self.pid_file
  61. Directory(os.path.dirname(self.pid_file),
  62. owner=self.user,
  63. group=self.user_group,
  64. mode=0755,
  65. create_parents=True
  66. )
  67. File(self.pid_file,
  68. owner=self.user,
  69. content=""
  70. )
  71. def stop(self, env):
  72. print "Stop"
  73. self.prepare()
  74. if os.path.isfile(self.pid_file):
  75. print "Deleting pid file: %s" % self.pid_file
  76. Execute("%s rm -rf %s" % (self.sudo, self.pid_file))
  77. def status(self, env):
  78. print "Status"
  79. self.prepare()
  80. if not os.path.isfile(self.pid_file):
  81. raise ComponentIsNotRunning()