Browse Source

use logging for multiple levels of verbosity

Signed-off-by: Doug Hellmann <doug@doughellmann.com>
Doug Hellmann 2 years ago
parent
commit
69a15ce38e
4 changed files with 42 additions and 8 deletions
  1. 16
    1
      aerostat/app.py
  2. 17
    6
      aerostat/download.py
  3. 4
    1
      aerostat/export.py
  4. 5
    0
      aerostat/resources.py

+ 16
- 1
aerostat/app.py View File

@@ -74,5 +74,20 @@ def main():
74 74
 
75 75
     args = parser.parse_args(sys.argv[1:])
76 76
 
77
-    return args.func(config, args)
77
+    cloud_config = config.get_one_cloud(options=(args, []))
78
+    cloud = shade.OpenStackCloud(cloud_config=cloud_config)
79
+
80
+    root_logger = logging.getLogger('')
81
+    root_logger.setLevel(logging.DEBUG)
82
+    console = logging.StreamHandler(sys.stderr)
83
+    console_level = {
84
+        0: logging.WARNING,
85
+        1: logging.INFO,
86
+        2: logging.DEBUG,
87
+    }.get(args.verbose_level, logging.DEBUG)
88
+    console.setLevel(console_level)
89
+    formatter = logging.Formatter('[%(asctime)s] %(message)s')
90
+    console.setFormatter(formatter)
91
+    root_logger.addHandler(console)
92
+
78 93
     return args.func(cloud, config, args)

+ 17
- 6
aerostat/download.py View File

@@ -12,10 +12,13 @@
12 12
 # License for the specific language governing permissions and limitations
13 13
 # under the License.
14 14
 
15
+import logging
15 16
 import os.path
16 17
 
17 18
 import progressbar
18 19
 
20
+LOG = logging.getLogger(__name__)
21
+
19 22
 
20 23
 class ProgressBarDownloader:
21 24
 
@@ -61,6 +64,7 @@ class Downloader:
61 64
         self._tasks = []
62 65
 
63 66
     def _add(self, resource_type, resource, output_path):
67
+        LOG.info('scheduling download of %s %s', resource_type, resource.name)
64 68
         self._tasks.append((resource_type, resource, output_path))
65 69
 
66 70
     def add_image(self, image):
@@ -70,20 +74,27 @@ class Downloader:
70 74
         return base
71 75
 
72 76
     def add_volume(self, volume):
73
-        print('DO NOT KNOW HOW TO SAVE VOLUME STATE YET', volume.name)
77
+        LOG.error('DO NOT KNOW HOW TO SAVE VOLUME STATE YET %s', volume.name)
74 78
 
75 79
     def start(self):
76 80
         # FIXME(dhellmann): start downloads in a separate thread or process
77 81
         for resource_type, resource, output_path in self._tasks:
78 82
             if os.path.exists(output_path):
79
-                print(
80
-                    'output file {} already exists, skipping download'.format(
81
-                        output_path))
83
+                LOG.info(
84
+                    'output file %s already exists, skipping download',
85
+                    output_path,
86
+                )
82 87
                 continue
83 88
             if resource_type == 'image':
84
-                print('downloading image {} to {}'.format(
89
+                LOG.info(
90
+                    'downloading image %s to %s',
85 91
                     resource.name,
86 92
                     output_path,
87
-                ))
93
+                )
88 94
                 with ProgressBarDownloader(output_path, resource.size) as out:
89 95
                     self.cloud.download_image(resource.name, output_file=out)
96
+                LOG.info(
97
+                    'downloaded image %s to %s',
98
+                    resource.name,
99
+                    output_path,
100
+                )

+ 4
- 1
aerostat/export.py View File

@@ -12,6 +12,7 @@
12 12
 # License for the specific language governing permissions and limitations
13 13
 # under the License.
14 14
 
15
+import logging
15 16
 import os.path
16 17
 
17 18
 import yaml
@@ -20,6 +21,8 @@ from aerostat import download
20 21
 from aerostat import resolver
21 22
 from aerostat import resources
22 23
 
24
+LOG = logging.getLogger(__name__)
25
+
23 26
 
24 27
 def export_data(cloud, config, args):
25 28
     output_path = args.output_path
@@ -55,6 +58,6 @@ def export_data(cloud, config, args):
55 58
     playbook_filename = os.path.join(output_path, 'playbook.yml')
56 59
     with open(playbook_filename, 'w', encoding='utf-8') as fd:
57 60
         yaml.dump(playbook, fd, default_flow_style=False, explicit_start=True)
58
-    print('wrote playbook to {}'.format(playbook_filename))
61
+    LOG.info('wrote playbook to %s', playbook_filename)
59 62
 
60 63
     downloader.start()

+ 5
- 0
aerostat/resources.py View File

@@ -12,11 +12,16 @@
12 12
 # License for the specific language governing permissions and limitations
13 13
 # under the License.
14 14
 
15
+import logging
16
+
15 17
 import munch
16 18
 
19
+LOG = logging.getLogger(__name__)
20
+
17 21
 
18 22
 def load(filename):
19 23
     "Read the file and return the parsed data in a consistent format."
24
+    LOG.info('loading resource list from %s', filename)
20 25
 
21 26
     # Ensure the return value has a basic set of keys representing the
22 27
     # types of resources we expect to find.

Loading…
Cancel
Save