Browse Source

AMBARI-4702. Create hash files on agent rpm creation (dlysnichenko)

Lisnichenko Dmitro 11 years ago
parent
commit
54a9df72ae

+ 50 - 11
ambari-agent/pom.xml

@@ -45,6 +45,9 @@
     <ruby.tar>http://public-repo-1.hortonworks.com/HDP-UTILS-1.1.0.15/repos/centos6/ruby-1.8.7-p370.tar.gz</ruby.tar>
     <lib.dir>/usr/lib/ambari-agent/lib</lib.dir>
     <python.ver>python &gt;= 2.6</python.ver>
+    <ambari.server.module>../ambari-server</ambari.server.module>
+    <target.cache.dir>${project.build.directory}/cache/</target.cache.dir>
+    <resource.keeper.script>${ambari.server.module}/src/main/python/ambari_server/resourceFilesKeeper.py</resource.keeper.script>
   </properties>
   <profiles>
     <profile>
@@ -135,6 +138,7 @@
             </goals>
           </execution>
           <execution>
+            <!-- TODO: Looks like section is unused, maybe remove? -->
             <configuration>
               <executable>python2.6</executable>
               <workingDirectory>target/ambari-agent-${project.version}</workingDirectory>
@@ -153,6 +157,24 @@
               <goal>exec</goal>
             </goals>
           </execution>
+          <execution>
+            <configuration>
+              <executable>python2.6</executable>
+              <workingDirectory>${basedir}</workingDirectory>
+              <arguments>
+                <argument>${resource.keeper.script}</argument>
+                <argument>${target.cache.dir}</argument>
+              </arguments>
+              <environmentVariables>
+                <PYTHONPATH>target/ambari-agent-${project.version}:$PYTHONPATH</PYTHONPATH>
+              </environmentVariables>
+            </configuration>
+            <id>generate-hash-files</id>
+            <phase>package</phase>
+            <goals>
+              <goal>exec</goal>
+            </goals>
+          </execution>
         </executions>
       </plugin>
       <plugin>
@@ -336,22 +358,13 @@
               </sources>
             </mapping>
             <mapping>
-              <directory>/var/lib/ambari-agent/cache/stacks</directory>
-              <sources>
-                <source>
-                  <location>../ambari-server/src/main/resources/stacks</location>
-                </source>
-              </sources>
-            </mapping>
-            <mapping>
-              <!-- custom actions root-->
-              <directory>/var/lib/ambari-agent/cache/custom_actions</directory>
+              <directory>/var/lib/ambari-agent/cache</directory>
               <filemode>755</filemode>
               <username>root</username>
               <groupname>root</groupname>
               <sources>
                 <source>
-                  <location>../ambari-server/src/main/resources/custom_actions</location>
+                  <location>${target.cache.dir}</location>
                 </source>
               </sources>
             </mapping>
@@ -391,6 +404,32 @@
           </execution>
         </executions>
       </plugin>
+      <plugin>
+        <artifactId>maven-resources-plugin</artifactId>
+        <version>2.6</version>
+        <executions>
+          <execution>
+            <id>copy-resources</id>
+            <phase>prepare-package</phase>
+            <goals>
+              <goal>copy-resources</goal>
+            </goals>
+            <configuration>
+              <outputDirectory>${target.cache.dir}</outputDirectory>
+              <resources>
+                <resource>
+                  <directory>${ambari.server.module}/src/main/resources</directory>
+                  <includes>
+                    <include>stacks/**/*</include>
+                    <include>custom_actions/**/*</include>
+                  </includes>
+                  <filtering>false</filtering>
+                </resource>
+              </resources>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
       <plugin>
         <groupId>org.apache.rat</groupId>
         <artifactId>apache-rat-plugin</artifactId>

+ 21 - 2
ambari-server/src/main/python/ambari_server/resourceFilesKeeper.py

@@ -53,9 +53,13 @@ class ResourceFilesKeeper():
   # Change that to True to see debug output at stderr
   DEBUG=False
 
-  def __init__(self, resources_dir, verbose=False):
+  def __init__(self, resources_dir, verbose=False, nozip=False):
+    """
+      nozip = create only hash files and skip creating zip archives
+    """
     self.resources_dir = resources_dir
     self.verbose = verbose
+    self.nozip = nozip
 
 
   def perform_housekeeping(self):
@@ -116,7 +120,8 @@ class ResourceFilesKeeper():
     cur_hash = self.count_hash_sum(directory)
     saved_hash = self.read_hash_sum(directory)
     if cur_hash != saved_hash:
-      self.zip_directory(directory)
+      if not self.nozip:
+        self.zip_directory(directory)
       self.write_hash_sum(directory, cur_hash)
 
 
@@ -256,3 +261,17 @@ class ResourceFilesKeeper():
     return None
 
 
+def main(argv=None):
+  """
+  This method is called by maven during rpm creation.
+  Params:
+    1: Path to resources root directory
+  """
+  path = argv[1]
+  resource_files_keeper = ResourceFilesKeeper(path, nozip=True)
+  resource_files_keeper.perform_housekeeping()
+
+
+if __name__ == '__main__':
+  main(sys.argv)
+

+ 16 - 1
ambari-server/src/test/python/TestResourceFilesKeeper.py

@@ -178,6 +178,21 @@ class TestResourceFilesKeeper(TestCase):
     self.assertTrue(zip_directory_mock.called)
     self.assertFalse(write_hash_sum_mock.called)
 
+    read_hash_sum_mock.reset_mock()
+    count_hash_sum_mock.reset_mock()
+    zip_directory_mock.reset_mock()
+    write_hash_sum_mock.reset_mock()
+
+    # Test nozip option
+    read_hash_sum_mock.return_value = None
+    count_hash_sum_mock.return_value = self.YA_HASH
+    resource_files_keeper = ResourceFilesKeeper(self.SOME_PATH, nozip=True)
+    resource_files_keeper.update_directory_archive(self.SOME_PATH)
+    self.assertTrue(read_hash_sum_mock.called)
+    self.assertTrue(count_hash_sum_mock.called)
+    self.assertFalse(zip_directory_mock.called)
+    self.assertTrue(write_hash_sum_mock.called)
+
 
   def test_count_hash_sum(self):
     # Test normal flow
@@ -316,4 +331,4 @@ class TestResourceFilesKeeper(TestCase):
 
 
   def keeper_exc_side_effect(self, *a):
-    raise KeeperException("horrible_keeper_exc")
+    raise KeeperException("horrible_keeper_exc")