]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Writing cache file
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 24 Aug 2018 10:51:36 +0000 (12:51 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 24 Aug 2018 10:51:36 +0000 (12:51 +0200)
lib/webhooks/__init__.py
lib/webhooks/get_forge_modules.py
lib/webhooks/module_info.py

index 16503b7f83646420756d633454bde9085731b8ff..ff793f9e18ecba12a814f525c902b923f5cbcde1 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.9.5'
+__version__ = '0.9.6'
 
 # vim: ts=4 et list
index cc7e927b7fb1a497e948b7b63592c17798496733..2f085950cb11aa9c85fcb7c4c1c27cda3f38b931 100644 (file)
@@ -17,18 +17,23 @@ import pipes
 import json
 import re
 import glob
-import warnings
 import datetime
+import fcntl
+import pwd
+import grp
+
+from operator import attrgetter, itemgetter
 
 # Third party modules
 import six
 import yaml
-import requests
+
+from six import reraise
 
 # Own modules
 from . import __version__
 
-from .common import pp, to_str
+from .common import pp, to_str, to_bytes
 
 from .base_app import BaseHookError, BaseHookApp
 
@@ -161,6 +166,8 @@ class GetForgeModulesApp(BaseHookApp):
         if self.verbose > 2:
             LOG.debug("Found modules:\n{}".format(pp(self.modules)))
 
+        self.write_cache_file()
+
         print()
         dt = datetime.datetime.now().isoformat(' ')
         msg = "[{}]: Finished.".format(dt)
@@ -347,6 +354,61 @@ class GetForgeModulesApp(BaseHookApp):
             raise GetForgeModulesError(
                 "Did not found environments in {!r}.".format(self.puppet_root_env_dir))
 
+    # -------------------------------------------------------------------------
+    def write_cache_file(self):
+
+        output_file = os.path.join(self.data_dir, 'modules-info.yaml')
+        tmp_file = output_file + '.new'
+        fd = None
+
+        LOG.debug("Sorting found modules ...")
+
+        data = []
+        for full_name in self.modules.keys():
+            module_info = self.modules[full_name]
+            data.append(module_info.to_data())
+        data.sort(key=itemgetter('name', 'vendor'))
+        dump = yaml.dump(data, default_flow_style=False)
+
+        LOG.info("Trying to open {!r} exclusive ...".format(tmp_file))
+
+        try:
+            fd = os.open(tmp_file, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644)
+            fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
+        except OSError as e:
+            msg = "Error on creating file {f!r}: {e}".format(f=tmp_file, e=e)
+            if e.errno == errno.EEXIST:
+                LOG.warn(msg)
+                return None
+            else:
+                error_tuple = sys.exc_info()
+                reraise(GetForgeModulesError, msg, error_tuple[2])
+
+        try:
+            os.write(fd, to_bytes('---\n'))
+            os.write(fd, to_bytes(dump))
+        finally:
+            os.close(fd)
+
+        if os.path.exists(output_file) and not os.geteuid():
+            fstat = os.stat(output_file)
+            user = fstat.st_uid
+            try:
+                user = pwd.getpwuid(fstat.st_uid).pw_name
+            except KeyError:
+                user = fstat.st_uid
+            group = fstat.st_gid
+            try:
+                group = grp.getgrgid(fstat.st_gid).gr_name
+            except KeyError:
+                group = fstat.st_gid
+            LOG.debug("Chowning {f!r} to {u}:{g} ...".format(
+                f=tmp_file, u=user, g=group))
+            os.chown(tmp_file, fstat.st_uid, fstat.st_gid)
+
+        LOG.info("Renaming {src!r} => {tgt!r}.".format(src=tmp_file, tgt=output_file))
+        os.rename(tmp_file, output_file)
+
 # =============================================================================
 
 if __name__ == "__main__":
index b414aff72d1be5da6e503a857350f8983ceaef7d..339548a84c0fd005f3e5cf0399d9bd199801a233 100644 (file)
@@ -264,9 +264,9 @@ class ModuleInfo(BaseObject):
         res['forge_source'] = self.forge_source
         res['repo'] = self.repo
 
-        red['local_versions'] = copy.copy(self.local_versions)
-        red['expected_versions'] = copy.copy(self.local_versions)
-        red['forge_releases'] = copy.copy(self.forge_releases)
+        res['local_versions'] = copy.copy(self.local_versions)
+        res['expected_versions'] = copy.copy(self.expected_versions)
+        res['forge_releases'] = copy.copy(self.forge_releases)
 
         return res