Updated Branches: refs/heads/trunk f07332938 -> 98243a918
AMBARI-3559. Resource Manager. Test Service() on suse, centos. Refactor/Remove unnecessary code (Andrew Onischuk via dlysnichenko) Project: http://git-wip-us.apache.org/repos/asf/incubator-ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ambari/commit/98243a91 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/98243a91 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/98243a91 Branch: refs/heads/trunk Commit: 98243a9186220eb994c62fd4bf06a781ae798eb5 Parents: f073329 Author: Lisnichenko Dmitro <[email protected]> Authored: Tue Oct 22 18:58:39 2013 +0300 Committer: Lisnichenko Dmitro <[email protected]> Committed: Tue Oct 22 18:58:39 2013 +0300 ---------------------------------------------------------------------- .../resource_management/providers/__init__.py | 6 +- .../resource_management/providers/service.py | 79 ++++++++++++++++++ .../providers/service/__init__.py | 86 -------------------- .../providers/service/redhat.py | 8 -- .../resource_management/resources/service.py | 15 +--- 5 files changed, 84 insertions(+), 110 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/98243a91/ambari-agent/src/main/python/resource_management/providers/__init__.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/providers/__init__.py b/ambari-agent/src/main/python/resource_management/providers/__init__.py index 220649b..21dd953 100644 --- a/ambari-agent/src/main/python/resource_management/providers/__init__.py +++ b/ambari-agent/src/main/python/resource_management/providers/__init__.py @@ -21,23 +21,18 @@ class Provider(object): PROVIDERS = dict( redhat=dict( - Service="resource_management.providers.service.redhat.RedhatServiceProvider", Package="resource_management.providers.package.yumrpm.YumProvider", ), centos=dict( - Service="resource_management.providers.service.redhat.RedhatServiceProvider", Package="resource_management.providers.package.yumrpm.YumProvider", ), suse=dict( - Service="resource_management.providers.service.suse.SuseServiceProvider", Package="resource_management.providers.package.zypper.ZypperProvider", ), fedora=dict( - Service="resource_management.providers.service.redhat.RedhatServiceProvider", Package="resource_management.providers.package.yumrpm.YumProvider", ), amazon=dict( - Service="resource_management.providers.service.redhat.RedhatServiceProvider", Package="resource_management.providers.package.yumrpm.YumProvider", ), default=dict( @@ -49,6 +44,7 @@ PROVIDERS = dict( Mount="resource_management.providers.mount.MountProvider", User="resource_management.providers.accounts.UserProvider", Group="resource_management.providers.accounts.GroupProvider", + Service="resource_management.providers.service.ServiceProvider", ), ) http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/98243a91/ambari-agent/src/main/python/resource_management/providers/service.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/providers/service.py b/ambari-agent/src/main/python/resource_management/providers/service.py new file mode 100644 index 0000000..beb1c24 --- /dev/null +++ b/ambari-agent/src/main/python/resource_management/providers/service.py @@ -0,0 +1,79 @@ +import os + +from resource_management import shell +from resource_management.base import Fail +from resource_management.providers import Provider + + +class ServiceProvider(Provider): + def action_start(self): + if not self.status(): + self._exec_cmd("start", 0) + self.resource.updated() + + def action_stop(self): + if self.status(): + self._exec_cmd("stop", 0) + self.resource.updated() + + def action_restart(self): + if not self.status(): + self._exec_cmd("start", 0) + self.resource.updated() + else: + self._exec_cmd("restart", 0) + self.resource.updated() + + def action_reload(self): + if not self.status(): + self._exec_cmd("start", 0) + self.resource.updated() + else: + self._exec_cmd("reload", 0) + self.resource.updated() + + def status(self): + return self._exec_cmd("status") == 0 + + def _exec_cmd(self, command, expect=None): + if command != "status": + self.log.info("%s command '%s'" % (self.resource, command)) + + custom_cmd = getattr(self.resource, "%s_command" % command, None) + if custom_cmd: + self.log.debug("%s executing '%s'" % (self.resource, custom_cmd)) + if hasattr(custom_cmd, "__call__"): + if custom_cmd(): + ret = 0 + else: + ret = 1 + else: + ret,out = shell.call(custom_cmd) + else: + ret = self._init_cmd(command) + + if expect is not None and expect != ret: + raise Fail("%r command %s for service %s failed with return code: %d. %s" % ( + self, command, self.resource.service_name, ret, out)) + return ret + + def _init_cmd(self, command): + if self._upstart: + if command == "status": + ret,out = shell.call(["/sbin/" + command, self.resource.service_name]) + _proc, state = out.strip().split(' ', 1) + ret = 0 if state != "stop/waiting" else 1 + else: + ret,out = shell.call(["/sbin/" + command, self.resource.service_name]) + else: + ret,out = shell.call(["/etc/init.d/%s" % self.resource.service_name, command]) + return ret + + @property + def _upstart(self): + try: + return self.__upstart + except AttributeError: + self.__upstart = os.path.exists("/sbin/start") \ + and os.path.exists("/etc/init/%s.conf" % self.resource.service_name) + return self.__upstart http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/98243a91/ambari-agent/src/main/python/resource_management/providers/service/__init__.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/providers/service/__init__.py b/ambari-agent/src/main/python/resource_management/providers/service/__init__.py deleted file mode 100644 index 42fc56d..0000000 --- a/ambari-agent/src/main/python/resource_management/providers/service/__init__.py +++ /dev/null @@ -1,86 +0,0 @@ -import os -import subprocess - -from resource_management.base import Fail -from resource_management.providers import Provider - - -class ServiceProvider(Provider): - def action_start(self): - if not self.status(): - self._exec_cmd("start", 0) - self.resource.updated() - - def action_stop(self): - if self.status(): - self._exec_cmd("stop", 0) - self.resource.updated() - - def action_restart(self): - if not self.status(): - self._exec_cmd("start", 0) - self.resource.updated() - else: - self._exec_cmd("restart", 0) - self.resource.updated() - - def action_reload(self): - if not self.status(): - self._exec_cmd("start", 0) - self.resource.updated() - else: - self._exec_cmd("reload", 0) - self.resource.updated() - - def status(self): - return self._exec_cmd("status") == 0 - - def _exec_cmd(self, command, expect=None): - if command != "status": - self.log.info("%s command '%s'" % (self.resource, command)) - - custom_cmd = getattr(self.resource, "%s_command" % command, None) - if custom_cmd: - self.log.debug("%s executing '%s'" % (self.resource, custom_cmd)) - if hasattr(custom_cmd, "__call__"): - if custom_cmd(): - ret = 0 - else: - ret = 1 - else: - ret = subprocess.call(custom_cmd, shell=True, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - else: - ret = self._init_cmd(command) - - if expect is not None and expect != ret: - raise Fail("%r command %s for service %s failed" % ( - self, command, self.resource.service_name)) - return ret - - def _init_cmd(self, command): - if self._upstart: - if command == "status": - proc = subprocess.Popen( - ["/sbin/" + command, self.resource.service_name], - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - out = proc.communicate()[0] - _proc, state = out.strip().split(' ', 1) - ret = 0 if state != "stop/waiting" else 1 - else: - ret = subprocess.call(["/sbin/" + command, self.resource.service_name], - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - else: - ret = subprocess.call( - ["/etc/init.d/%s" % self.resource.service_name, command], - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - return ret - - @property - def _upstart(self): - try: - return self.__upstart - except AttributeError: - self.__upstart = os.path.exists("/sbin/start") \ - and os.path.exists("/etc/init/%s.conf" % self.resource.service_name) - return self.__upstart http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/98243a91/ambari-agent/src/main/python/resource_management/providers/service/redhat.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/providers/service/redhat.py b/ambari-agent/src/main/python/resource_management/providers/service/redhat.py deleted file mode 100644 index ba006a8..0000000 --- a/ambari-agent/src/main/python/resource_management/providers/service/redhat.py +++ /dev/null @@ -1,8 +0,0 @@ -__all__ = ["RedhatServiceProvider"] - -from resource_management.providers.service import ServiceProvider - - -class RedhatServiceProvider(ServiceProvider): - def enable_runlevel(self, runlevel): - pass http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/98243a91/ambari-agent/src/main/python/resource_management/resources/service.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/resources/service.py b/ambari-agent/src/main/python/resource_management/resources/service.py index 5f4a50c..7fbcd89 100644 --- a/ambari-agent/src/main/python/resource_management/resources/service.py +++ b/ambari-agent/src/main/python/resource_management/resources/service.py @@ -1,23 +1,16 @@ __all__ = ["Service"] -from resource_management.base import Resource, ResourceArgument, BooleanArgument +from resource_management.base import Resource, ResourceArgument, BooleanArgument, ForcedListArgument class Service(Resource): + action = ForcedListArgument(default="start") service_name = ResourceArgument(default=lambda obj: obj.name) - enabled = ResourceArgument() - running = ResourceArgument() - pattern = ResourceArgument() + #enabled = ResourceArgument() # Maybe add support to put in/out autostart. start_command = ResourceArgument() stop_command = ResourceArgument() restart_command = ResourceArgument() - reload_command = ResourceArgument() + reload_command = ResourceArgument() # reload the config file without interrupting pending operations status_command = ResourceArgument() - supports_restart = BooleanArgument( - default=lambda obj: bool(obj.restart_command)) - supports_reload = BooleanArgument( - default=lambda obj: bool(obj.reload_command)) - supports_status = BooleanArgument( - default=lambda obj: bool(obj.status_command)) actions = ["nothing", "start", "stop", "restart", "reload"]
