use logging for multiple levels of verbosity

Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2017-01-31 17:34:10 -05:00
parent e9f52ca2d7
commit 69a15ce38e
4 changed files with 42 additions and 8 deletions

View File

@ -74,5 +74,20 @@ def main():
args = parser.parse_args(sys.argv[1:])
return args.func(config, args)
cloud_config = config.get_one_cloud(options=(args, []))
cloud = shade.OpenStackCloud(cloud_config=cloud_config)
root_logger = logging.getLogger('')
root_logger.setLevel(logging.DEBUG)
console = logging.StreamHandler(sys.stderr)
console_level = {
0: logging.WARNING,
1: logging.INFO,
2: logging.DEBUG,
}.get(args.verbose_level, logging.DEBUG)
console.setLevel(console_level)
formatter = logging.Formatter('[%(asctime)s] %(message)s')
console.setFormatter(formatter)
root_logger.addHandler(console)
return args.func(cloud, config, args)

View File

@ -12,10 +12,13 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
import os.path
import progressbar
LOG = logging.getLogger(__name__)
class ProgressBarDownloader:
@ -61,6 +64,7 @@ class Downloader:
self._tasks = []
def _add(self, resource_type, resource, output_path):
LOG.info('scheduling download of %s %s', resource_type, resource.name)
self._tasks.append((resource_type, resource, output_path))
def add_image(self, image):
@ -70,20 +74,27 @@ class Downloader:
return base
def add_volume(self, volume):
print('DO NOT KNOW HOW TO SAVE VOLUME STATE YET', volume.name)
LOG.error('DO NOT KNOW HOW TO SAVE VOLUME STATE YET %s', volume.name)
def start(self):
# FIXME(dhellmann): start downloads in a separate thread or process
for resource_type, resource, output_path in self._tasks:
if os.path.exists(output_path):
print(
'output file {} already exists, skipping download'.format(
output_path))
LOG.info(
'output file %s already exists, skipping download',
output_path,
)
continue
if resource_type == 'image':
print('downloading image {} to {}'.format(
LOG.info(
'downloading image %s to %s',
resource.name,
output_path,
))
)
with ProgressBarDownloader(output_path, resource.size) as out:
self.cloud.download_image(resource.name, output_file=out)
LOG.info(
'downloaded image %s to %s',
resource.name,
output_path,
)

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
import os.path
import yaml
@ -20,6 +21,8 @@ from aerostat import download
from aerostat import resolver
from aerostat import resources
LOG = logging.getLogger(__name__)
def export_data(cloud, config, args):
output_path = args.output_path
@ -55,6 +58,6 @@ def export_data(cloud, config, args):
playbook_filename = os.path.join(output_path, 'playbook.yml')
with open(playbook_filename, 'w', encoding='utf-8') as fd:
yaml.dump(playbook, fd, default_flow_style=False, explicit_start=True)
print('wrote playbook to {}'.format(playbook_filename))
LOG.info('wrote playbook to %s', playbook_filename)
downloader.start()

View File

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