incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmitriu...@apache.org
Subject [02/15] git commit: AMBARI-3653. File: refactor, handle special exceptional situations, support replace attribute, support absolute path for source files, default don't backup (Andrew Onischuk via dlysnichenko)
Date Fri, 01 Nov 2013 15:28:13 GMT
AMBARI-3653. File: refactor, handle special exceptional situations, support replace attribute,
support absolute path for source files, default don't backup (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/99ac88e1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/99ac88e1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/99ac88e1

Branch: refs/heads/trunk
Commit: 99ac88e1c60a54163df693160539e4d4e62f6d4e
Parents: 85bdf2b
Author: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Authored: Fri Nov 1 17:06:41 2013 +0200
Committer: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Committed: Fri Nov 1 17:06:41 2013 +0200

----------------------------------------------------------------------
 .../resource_management/providers/system.py     | 32 ++++++++++++++------
 .../resource_management/resources/system.py     |  4 ++-
 .../main/python/resource_management/source.py   | 20 +++++++++---
 3 files changed, 42 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/99ac88e1/ambari-agent/src/main/python/resource_management/providers/system.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/resource_management/providers/system.py b/ambari-agent/src/main/python/resource_management/providers/system.py
index b1ce3b9..d366942 100644
--- a/ambari-agent/src/main/python/resource_management/providers/system.py
+++ b/ambari-agent/src/main/python/resource_management/providers/system.py
@@ -13,7 +13,10 @@ def _coerce_uid(user):
   try:
     uid = int(user)
   except ValueError:
-    uid = pwd.getpwnam(user).pw_uid
+    try:
+      uid = pwd.getpwnam(user).pw_uid
+    except KeyError:
+      raise Fail("User %s doesn't exist." % user)
   return uid
 
 
@@ -21,7 +24,10 @@ def _coerce_gid(group):
   try:
     gid = int(group)
   except ValueError:
-    gid = grp.getgrnam(group).gr_gid
+    try:
+      gid = grp.getgrnam(group).gr_gid
+    except KeyError:
+      raise Fail("Group %s doesn't exist." % group)
   return gid
 
 
@@ -59,19 +65,28 @@ def _ensure_metadata(path, user, group, mode=None, log=None):
 class FileProvider(Provider):
   def action_create(self):
     path = self.resource.path
+    
+    if os.path.isdir(path):
+      raise Fail("Applying %s failed, directory with name %s exists" % (self.resource, path))
+    
+    dirname = os.path.dirname(path)
+    if not os.path.isdir(dirname):
+      raise Fail("Applying %s failed, parent directory %s doesn't exist" % (self.resource,
dirname))
+    
     write = False
     content = self._get_content()
     if not os.path.exists(path):
       write = True
       reason = "it doesn't exist"
-    else:
+    elif self.resource.replace:
       if content is not None:
         with open(path, "rb") as fp:
           old_content = fp.read()
         if content != old_content:
           write = True
           reason = "contents don't match"
-          self.resource.env.backup_file(path)
+          if self.resource.backup:
+            self.resource.env.backup_file(path)
 
     if write:
       self.log.info("Writing %s because %s" % (self.resource, reason))
@@ -87,16 +102,15 @@ class FileProvider(Provider):
 
   def action_delete(self):
     path = self.resource.path
+    
+    if os.path.isdir(path):
+      raise Fail("Applying %s failed, %s is directory not file!" % (self.resource, path))
+    
     if os.path.exists(path):
       self.log.info("Deleting %s" % self.resource)
       os.unlink(path)
       self.resource.updated()
 
-  def action_touch(self):
-    path = self.resource.path
-    with open(path, "a"):
-      pass
-
   def _get_content(self):
     content = self.resource.content
     if content is None:

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/99ac88e1/ambari-agent/src/main/python/resource_management/resources/system.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/resource_management/resources/system.py b/ambari-agent/src/main/python/resource_management/resources/system.py
index 96da5e4..7765102 100644
--- a/ambari-agent/src/main/python/resource_management/resources/system.py
+++ b/ambari-agent/src/main/python/resource_management/resources/system.py
@@ -11,8 +11,10 @@ class File(Resource):
   owner = ResourceArgument()
   group = ResourceArgument()
   content = ResourceArgument()
+  # whether to replace files with different content
+  replace = ResourceArgument(default=True)
 
-  actions = Resource.actions + ["create", "delete", "touch"]
+  actions = Resource.actions + ["create", "delete"]
 
 
 class Directory(Resource):

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/99ac88e1/ambari-agent/src/main/python/resource_management/source.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/resource_management/source.py b/ambari-agent/src/main/python/resource_management/source.py
index ea3a25f..925bd65 100644
--- a/ambari-agent/src/main/python/resource_management/source.py
+++ b/ambari-agent/src/main/python/resource_management/source.py
@@ -27,8 +27,14 @@ class StaticFile(Source):
     self.env = env or environment.Environment.get_instance()
 
   def get_content(self):
-    basedir = self.env.config.basedir
-    path = os.path.join(basedir, "files", self.name)
+    # absolute path
+    if self.name.startswith(os.path.sep):
+      path = self.name
+    # relative path
+    else:
+      basedir = self.env.config.basedir
+      path = os.path.join(basedir, "files", self.name)
+      
     with open(path, "rb") as fp:
       return fp.read()
 
@@ -45,8 +51,14 @@ else:
       self.env = env or environment.Environment.get_instance()
 
     def get_source(self, environment, template_name):
-      basedir = self.env.config.basedir
-      path = os.path.join(basedir, "templates", template_name)
+      # absolute path
+      if template_name.startswith(os.path.sep):
+        path = template_name
+      # relative path
+      else:
+        basedir = self.env.config.basedir
+        path = os.path.join(basedir, "templates", template_name)
+      
       if not os.path.exists(path):
         raise TemplateNotFound("%s at %s" % (template_name, path))
       mtime = os.path.getmtime(path)


Mime
View raw message