summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Dague <sean@dague.net>2017-01-17 08:31:49 -0500
committerSean Dague <sean@dague.net>2017-01-17 08:31:49 -0500
commitbe8cb7402141d9bc561f219693f1015eccaab57d (patch)
treed6bad40cb46bcd481624bc09626480162db70400
parent36b1e0ff346ee674d5fde71f8517ed9b4f65eaa5 (diff)
add more cli support
Make it possible to create a blank local.conf
-rw-r--r--devstack/cmd.py66
-rw-r--r--devstack/dsconf.py13
2 files changed, 75 insertions, 4 deletions
diff --git a/devstack/cmd.py b/devstack/cmd.py
index 97149bd..da8a502 100644
--- a/devstack/cmd.py
+++ b/devstack/cmd.py
@@ -34,36 +34,92 @@ def iniuncomment(inifile, args):
34 inifile.uncomment(args.section, args.name) 34 inifile.uncomment(args.section, args.name)
35 35
36 36
37def extract_local(local_conf, args):
38 local_conf.extract_localrc(args.local_rc)
39
40
41def extract_config(local_conf, args):
42 local_conf.extract(args.group, args.conf, args.local_rc)
43
44
45def setlc(local_conf, args):
46 local_conf.set_local(args.name, args.value)
47
48
49def setlc_conf(local_conf, args):
50 local_conf.set(args.group, args.conf, args.section, args.name, args.value)
51
52
37def parse_args(argv): 53def parse_args(argv):
38 parser = argparse.ArgumentParser(prog='dsconf') 54 parser = argparse.ArgumentParser(prog='dsconf')
39 subparsers = parser.add_subparsers(title='commands', 55 subparsers = parser.add_subparsers(title='commands',
40 help='sub-command help') 56 help='sub-command help')
41 57
42 parser_iniset = subparsers.add_parser('iniset', help='iniset help') 58 parser_iniset = subparsers.add_parser('iniset',
59 help='set item in ini file')
43 parser_iniset.set_defaults(func=iniset) 60 parser_iniset.set_defaults(func=iniset)
44 parser_iniset.add_argument('inifile', help='name of file') 61 parser_iniset.add_argument('inifile', help='name of file')
45 parser_iniset.add_argument('section', help='name of section') 62 parser_iniset.add_argument('section', help='name of section')
46 parser_iniset.add_argument('name', help='name') 63 parser_iniset.add_argument('name', help='name')
47 parser_iniset.add_argument('value', help='value') 64 parser_iniset.add_argument('value', help='value')
48 65
49 parser_inicomment = subparsers.add_parser('inicomment', help='inicomment help') 66 parser_inicomment = subparsers.add_parser(
67 'inicomment',
68 help='comment item in ini file')
50 parser_inicomment.set_defaults(func=inicomment) 69 parser_inicomment.set_defaults(func=inicomment)
51 parser_inicomment.add_argument('inifile', help='name of file') 70 parser_inicomment.add_argument('inifile', help='name of file')
52 parser_inicomment.add_argument('section', help='name of section') 71 parser_inicomment.add_argument('section', help='name of section')
53 parser_inicomment.add_argument('name', help='name') 72 parser_inicomment.add_argument('name', help='name')
54 73
55 parser_iniuncomment = subparsers.add_parser('iniuncomment', help='iniuncomment help') 74 parser_iniuncomment = subparsers.add_parser(
75 'iniuncomment',
76 help='uncomment item in ini file')
56 parser_iniuncomment.set_defaults(func=iniuncomment) 77 parser_iniuncomment.set_defaults(func=iniuncomment)
57 parser_iniuncomment.add_argument('inifile', help='name of file') 78 parser_iniuncomment.add_argument('inifile', help='name of file')
58 parser_iniuncomment.add_argument('section', help='name of section') 79 parser_iniuncomment.add_argument('section', help='name of section')
59 parser_iniuncomment.add_argument('name', help='name') 80 parser_iniuncomment.add_argument('name', help='name')
60 81
61 parser_inirm = subparsers.add_parser('inirm', help='inirm help') 82 parser_inirm = subparsers.add_parser(
83 'inirm',
84 help='delete item from ini file')
62 parser_inirm.set_defaults(func=inirm) 85 parser_inirm.set_defaults(func=inirm)
63 parser_inirm.add_argument('inifile', help='name of file') 86 parser_inirm.add_argument('inifile', help='name of file')
64 parser_inirm.add_argument('section', help='name of section') 87 parser_inirm.add_argument('section', help='name of section')
65 parser_inirm.add_argument('name', help='name') 88 parser_inirm.add_argument('name', help='name')
66 89
90 parser_extract_local = subparsers.add_parser(
91 'extract-localrc',
92 help='extract localrc from local.conf')
93 parser_extract_local.set_defaults(func=extract_local)
94 parser_extract_local.add_argument('local_conf')
95 parser_extract_local.add_argument('local_rc')
96
97 parser_extract = subparsers.add_parser(
98 'extract',
99 help='extract and merge config from local.conf')
100 parser_extract.set_defaults(func=extract_config)
101 parser_extract.add_argument('local_conf')
102 parser_extract.add_argument('group')
103 parser_extract.add_argument('conf')
104 parser_extract.add_argument('local_rc')
105
106 parser_setlc = subparsers.add_parser(
107 'setlc', help='set variable in localrc of local.conf')
108 parser_setlc.set_defaults(func=setlc)
109 parser_setlc.add_argument('local_conf')
110 parser_setlc.add_argument('name')
111 parser_setlc.add_argument('value')
112
113 parser_setlc_conf = subparsers.add_parser(
114 'setlc_conf', help='set variable in localrc of local.conf')
115 parser_setlc_conf.set_defaults(func=setlc_conf)
116 parser_setlc_conf.add_argument('local_conf')
117 parser_setlc_conf.add_argument('group')
118 parser_setlc_conf.add_argument('conf')
119 parser_setlc_conf.add_argument('section')
120 parser_setlc_conf.add_argument('name')
121 parser_setlc_conf.add_argument('value')
122
67 return parser.parse_args() 123 return parser.parse_args()
68 124
69 125
@@ -71,5 +127,7 @@ def main(argv=None):
71 args = parse_args(argv or sys.argv) 127 args = parse_args(argv or sys.argv)
72 if hasattr(args, 'inifile'): 128 if hasattr(args, 'inifile'):
73 f = devstack.dsconf.IniFile(args.inifile) 129 f = devstack.dsconf.IniFile(args.inifile)
130 elif hasattr(args, 'local_conf'):
131 f = devstack.dsconf.LocalConf(args.local_conf)
74 args.func(f, args) 132 args.func(f, args)
75 return 133 return
diff --git a/devstack/dsconf.py b/devstack/dsconf.py
index a1f88b9..9e9ff6c 100644
--- a/devstack/dsconf.py
+++ b/devstack/dsconf.py
@@ -208,6 +208,12 @@ class LocalConf(object):
208 func(writer, None) 208 func(writer, None)
209 209
210 def set_local(self, name, value): 210 def set_local(self, name, value):
211 if not os.path.exists(self.fname):
212 with open(self.fname, "w+") as writer:
213 writer.write("[[local|localrc]]\n")
214 writer.write("%s = %s\n" % (name, value))
215 return
216
211 def _do_set(writer, line): 217 def _do_set(writer, line):
212 writer.write("%s = %s\n" % (name, value)) 218 writer.write("%s = %s\n" % (name, value))
213 self._at_insert_point_local(name, _do_set) 219 self._at_insert_point_local(name, _do_set)
@@ -267,6 +273,13 @@ class LocalConf(object):
267 func(writer, None) 273 func(writer, None)
268 274
269 def set(self, group, conf, section, name, value): 275 def set(self, group, conf, section, name, value):
276 if not os.path.exists(self.fname):
277 with open(self.fname, "w+") as writer:
278 writer.write("[[%s|%s]]\n" % (group, conf))
279 writer.write("[%s]\n" % section)
280 writer.write("%s = %s\n" % (name, value))
281 return
282
270 def _do_set(writer, line): 283 def _do_set(writer, line):
271 writer.write("%s = %s\n" % (name, value)) 284 writer.write("%s = %s\n" % (name, value))
272 self._at_insert_point(group, conf, section, name, _do_set) 285 self._at_insert_point(group, conf, section, name, _do_set)