-
Notifications
You must be signed in to change notification settings - Fork 0
/
vv.py
executable file
·167 lines (150 loc) · 5.19 KB
/
vv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python
import argparse
import os.path
import sys
import yaml
import vivarium.vivarium as vivarium
def _config_parser(subparsers, defaults):
config_parser = subparsers.add_parser(
'config',
help='Manage configuration information.')
config_parser.set_defaults(func=_config, **defaults)
return subparsers
def _config(args):
for name, value in args._get_kwargs():
if name == 'config': continue
if not hasattr(value, '__call__'):
print('{0}: {1}'.format(name, value))
def _add_config_option(parser):
parser.add_argument(
'--config',
action='store_true',
default=False,
help='Emit application configuration information and exit.')
def _show_parsed_config(args):
if args.config:
_config(args)
return True
return False
def _seed_parser(subparsers, defaults):
seed_parser = subparsers.add_parser(
'seed',
help="""
Given a hostname in the form 'my.example.com', gather all roles,
environments, packages, templates, and files to generate the seed
for the spawn.""")
seed_parser.add_argument(
'host',
action='store',
help='Host to generate the seed.')
seed_parser.add_argument(
'spawn',
action='store',
help='Spawn and source to use. Can be yaml file or a directory.')
seed_parser.add_argument(
'--source', '-s',
action='store',
default=None,
help='The source spawn to use if different from the spawn.')
seed_parser.add_argument(
'--stdout',
action='store_true',
default=False,
help='Emit configuration to stdout.')
_add_config_option(seed_parser)
seed_parser.set_defaults(func=_seed, **defaults)
return subparsers
def _seed(args):
_show_parsed_config(args) or vivarium.seed(args)
def _copy_parser(subparsers, defaults):
copy_parser = subparsers.add_parser(
'copy', help='Copy a complete humus. NOT YET IMPLEMENTED.')
copy_parser.add_argument(
'source',
action='store',
help='Source configuration to use. Can be yaml file or a directory.')
copy_parser.add_argument(
'destination',
action='store',
help="""
Destination for the copy. If the destination is a directory then the
configuration will use the file system back-end. If the destination is
a file or ends in .yaml, the yaml back-end will be used.""")
_add_config_option(copy_parser)
copy_parser.set_defaults(func=_copy, **defaults)
return subparsers
def _copy(args):
_show_parsed_config(args) or vivarium.copy(args)
def _plant_parser(subparsers, defaults):
plant_parser = subparsers.add_parser(
'plant', help='Plant a seed on the local host.')
plant_parser.add_argument(
'-r', '--root-dir',
action='store',
default='/',
help='Target directory for the plant operation.')
plant_parser.add_argument(
'-s', '--stage-dir',
action='store',
default='/tmp',
help='Directory for plant temporary files.')
# plant_parser.add_argument(
# '--dry-run',
# action='store_true',
# default=False,
# help='Destination directory for configuration. Default is "/".')
plant_parser.add_argument(
'host',
action='store',
help='Host to generate the seed.')
plant_parser.add_argument(
'spawn',
action='store',
help='Spawn to use. Can be yaml file or a directory.')
_add_config_option(plant_parser)
plant_parser.set_defaults(func=_plant, **defaults)
return subparsers
def _plant(args):
_show_parsed_config(args) or vivarium.plant(args)
def _initial_parser(defaults = {}):
description = """
Vivarium is a tool for managing small to medium distributed system
configuration. Vivarium is designed to be backed by zookeeper to store
data though you can also use yaml or just a normal file system."""
parser = argparse.ArgumentParser(description=description, epilog=None)
if len(defaults) > 0:
parser.set_defaults(**defaults)
parser.add_argument(
'-v', '--version',
action='version',
version='%(prog)s 0.1')
subparsers = parser.add_subparsers(
title='Commands',
description="""
To get usage for a particular command:
%(prog)s {command} --help""",
help='commands')
subparsers = _config_parser(subparsers, defaults.get('config', {}))
subparsers = _seed_parser(subparsers, defaults.get('seed', {}))
subparsers = _copy_parser(subparsers, defaults.get('copy', {}))
subparsers = _plant_parser(subparsers, defaults.get('plant', {}))
return parser
def _parse_arguments():
parser = _initial_parser()
configname = os.path.expanduser('~/.vivarium.yaml')
parser.add_argument(
'-c', '--config-file',
action='store',
default=configname,
help='Configuration file.')
args = parser.parse_args()
if os.path.exists(args.config_file):
defaults = yaml.load(open(args.config_file).read())
parser = _initial_parser(defaults)
args = parser.parse_args()
return args
def main():
args = _parse_arguments()
args.func(args)
if __name__ == '__main__':
main()