Browse Source

split main app into export and query subcommands

Signed-off-by: Doug Hellmann <doug@doughellmann.com>
Doug Hellmann 2 years ago
parent
commit
6b086aeeb7
3 changed files with 140 additions and 49 deletions
  1. 38
    49
      aerostat/app.py
  2. 82
    0
      aerostat/export.py
  3. 20
    0
      aerostat/query.py

+ 38
- 49
aerostat/app.py View File

@@ -26,6 +26,8 @@ import shade
26 26
 import yaml
27 27
 
28 28
 from aerostat import download
29
+from aerostat import export
30
+from aerostat import query
29 31
 from aerostat import resolver
30 32
 from aerostat import resources
31 33
 
@@ -34,64 +36,51 @@ def main():
34 36
     parser = argparse.ArgumentParser()
35 37
     config = os_client_config.OpenStackConfig()
36 38
 
39
+    verbose_group = parser.add_mutually_exclusive_group()
40
+    verbose_group.add_argument(
41
+        '--verbose', '-v',
42
+        action='count',
43
+        dest='verbose_level',
44
+        default=1,
45
+        help='Increase verbosity of output. Can be repeated.',
46
+    )
47
+    verbose_group.add_argument(
48
+        '-q', '--quiet',
49
+        action='store_const',
50
+        dest='verbose_level',
51
+        const=0,
52
+        help='Suppress output except warnings and errors.',
53
+    )
54
+
37 55
     config.register_argparse_arguments(parser, sys.argv, None)
38
-    parser.add_argument(
56
+    subparsers = parser.add_subparsers(title='commands')
57
+
58
+    do_export = subparsers.add_parser(
59
+        'export',
60
+        help='export data',
61
+    )
62
+    do_export.add_argument(
39 63
         'resource_file',
40 64
         help='the name of the file listing resources to be exported',
41 65
     )
42
-    parser.add_argument(
66
+    do_export.add_argument(
43 67
         'output_path',
44 68
         default='.',
45 69
         nargs='?',
46 70
         help='the name of a directory to use for output file(s)',
47 71
     )
72
+    do_export.set_defaults(func=export.export_data)
48 73
 
49
-    args, remaining = parser.parse_known_args(sys.argv[1:])
50
-    output_path = args.output_path
51
-
52
-    cloud_config = config.get_one_cloud(options=(args, remaining))
53
-    cloud = shade.OpenStackCloud(cloud_config=cloud_config)
54
-    downloader = download.Downloader(output_path, cloud)
55
-    res = resolver.Resolver(cloud, downloader)
56
-    tasks = []
57
-
58
-    # Export independent resources. The resolver handles dependencies
59
-    # automatically.
60
-    to_export = resources.load(args.resource_file)
61
-
62
-    for image_info in to_export.images:
63
-        image = cloud.get_image(image_info.name)
64
-        tasks.extend(res.image(image))
65
-
66
-    for volume_info in to_export.volumes:
67
-        volume = cloud.get_volume(volume_info.name)
68
-        tasks.extend(res.volume(volume, save_state=volume_info.save_state))
69
-
70
-    for server_info in to_export.servers:
71
-        server = cloud.get_server(server_info.name)
72
-        tasks.extend(res.server(server, save_state=server_info.save_state))
73
-
74
-    playbook = [
75
-        # The default playbook is configured to run instructions
76
-        # locally to talk to the cloud API.
77
-        {'hosts': 'localhost',
78
-         'connection': 'local',
79
-         'tasks': tasks,
80
-         },
81
-    ]
82
-    playbook_filename = os.path.join(output_path, 'playbook.yml')
83
-    with open(playbook_filename, 'w', encoding='utf-8') as fd:
84
-        yaml.dump(playbook, fd, default_flow_style=False, explicit_start=True)
85
-    print('wrote playbook to {}'.format(playbook_filename))
86
-
87
-    downloader.start()
74
+    do_query = subparsers.add_parser(
75
+        'query',
76
+        help='query to build an export list',
77
+    )
78
+    do_query.add_argument(
79
+        'resource_file',
80
+        help='the name of the file listing resources to be updated',
81
+    )
82
+    do_query.set_defaults(func=query.query_data)
88 83
 
89
-    # print('downloading volume snapshot')
90
-    # snapshot = cloud.get_volume_snapshot('testvol1-sn1')
91
-    # pprint.pprint(snapshot)
92
-    # # with download.ProgressBarDownloader('testvol1-sn1.dat', snapshot.size) as out:
93
-    # #     cloud.download_image('dev1-sn1', output_file=out)
84
+    args = parser.parse_args(sys.argv[1:])
94 85
 
95
-    # for volume in dev1.volumes:
96
-    #     vol = cloud.get_volume(volume.id)
97
-    #     pprint.pprint(vol)
86
+    return args.func(config, args)

+ 82
- 0
aerostat/export.py View File

@@ -0,0 +1,82 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+# Copyright 2010-2011 OpenStack Foundation
4
+# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
5
+#
6
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
+# not use this file except in compliance with the License. You may obtain
8
+# a copy of the License at
9
+#
10
+#      http://www.apache.org/licenses/LICENSE-2.0
11
+#
12
+# Unless required by applicable law or agreed to in writing, software
13
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
+# License for the specific language governing permissions and limitations
16
+# under the License.
17
+
18
+import argparse
19
+import os.path
20
+import pprint
21
+import sys
22
+
23
+import os_client_config
24
+import progressbar
25
+import shade
26
+import yaml
27
+
28
+from aerostat import download
29
+from aerostat import export
30
+from aerostat import resolver
31
+from aerostat import resources
32
+
33
+
34
+def export_data(config, args):
35
+    output_path = args.output_path
36
+
37
+    cloud_config = config.get_one_cloud(options=(args, []))
38
+    cloud = shade.OpenStackCloud(cloud_config=cloud_config)
39
+    downloader = download.Downloader(output_path, cloud)
40
+    res = resolver.Resolver(cloud, downloader)
41
+    tasks = []
42
+
43
+    # Export independent resources. The resolver handles dependencies
44
+    # automatically.
45
+    to_export = resources.load(args.resource_file)
46
+
47
+    for image_info in to_export.images:
48
+        image = cloud.get_image(image_info.name)
49
+        tasks.extend(res.image(image))
50
+
51
+    for volume_info in to_export.volumes:
52
+        volume = cloud.get_volume(volume_info.name)
53
+        tasks.extend(res.volume(volume, save_state=volume_info.save_state))
54
+
55
+    for server_info in to_export.servers:
56
+        server = cloud.get_server(server_info.name)
57
+        tasks.extend(res.server(server, save_state=server_info.save_state))
58
+
59
+    playbook = [
60
+        # The default playbook is configured to run instructions
61
+        # locally to talk to the cloud API.
62
+        {'hosts': 'localhost',
63
+         'connection': 'local',
64
+         'tasks': tasks,
65
+         },
66
+    ]
67
+    playbook_filename = os.path.join(output_path, 'playbook.yml')
68
+    with open(playbook_filename, 'w', encoding='utf-8') as fd:
69
+        yaml.dump(playbook, fd, default_flow_style=False, explicit_start=True)
70
+    print('wrote playbook to {}'.format(playbook_filename))
71
+
72
+    downloader.start()
73
+
74
+    # print('downloading volume snapshot')
75
+    # snapshot = cloud.get_volume_snapshot('testvol1-sn1')
76
+    # pprint.pprint(snapshot)
77
+    # # with download.ProgressBarDownloader('testvol1-sn1.dat', snapshot.size) as out:
78
+    # #     cloud.download_image('dev1-sn1', output_file=out)
79
+
80
+    # for volume in dev1.volumes:
81
+    #     vol = cloud.get_volume(volume.id)
82
+    #     pprint.pprint(vol)

+ 20
- 0
aerostat/query.py View File

@@ -0,0 +1,20 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+# Copyright 2010-2011 OpenStack Foundation
4
+# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
5
+#
6
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
+# not use this file except in compliance with the License. You may obtain
8
+# a copy of the License at
9
+#
10
+#      http://www.apache.org/licenses/LICENSE-2.0
11
+#
12
+# Unless required by applicable law or agreed to in writing, software
13
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
+# License for the specific language governing permissions and limitations
16
+# under the License.
17
+
18
+
19
+def query_data(config, args):
20
+    raise NotImplementedError('query not implemented')

Loading…
Cancel
Save