-
Notifications
You must be signed in to change notification settings - Fork 2
/
cm-manipulate-advanced-config.py
executable file
·200 lines (181 loc) · 6.13 KB
/
cm-manipulate-advanced-config.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/cm/local/apps/python3/bin/python3
#
# Show the current settings, for advanced and global config
# ./cm-manipulate-advanced-config.py -s
# ./cm-manipulate-advanced-config.py -s -g
#
# Add/update two fields to advanced config but do not save (dry-run)
# ./cm-manipulate-advanced-config.py -d Hello=World It=Works
#
# Remove the fields witht the given keys
# ./cm-manipulate-advanced-config.py -r Hello It
#
import os
import sys
from socket import gethostname
from argparse import ArgumentParser, REMAINDER
class Config:
def __init__(self,
name: str,
prefix: str,
filename: str = '/cm/local/apps/cmd/etc/cmd.conf'):
self.name = name
self.prefix = prefix
self.filename = filename
self.data = None
self.pre = None
self.post = None
self.changed = False
self.fields = []
self._read()
self._parse()
@property
def path(self):
return f'{self.prefix}{self.filename}'
def _read(self):
with open(self.path, 'r') as fd:
self.data = fd.read()
def _parse(self):
index = 0
while True:
index = self.data.find(self.name, index)
if index < 0:
self.pre = self.data + '\n' + self.name + ' = {\n'
self.post = '}'
return
newline = self.data.rfind('\n', 0, index)
comment = self.data.rfind('#', 0, index)
if comment < newline:
break
index += len(self.name)
index += len(self.name)
while index < len(self.data):
if self.data[index] == '{':
break
index += 1
index += 1
self.pre = self.data[0:index]
if self.pre[-1] != '\n':
self.pre += '\n'
quote = False
slash = 0
start = 0
while index < len(self.data):
if self.data[index] == '"':
if slash % 2 == 0:
if quote:
self.fields.append(self.data[start:index])
else:
start = index + 1
quote = not quote
slash = 0
elif self.data[index] == '\\':
slash += 1
elif self.data[index] == '}':
if slash % 2 == 0 and not quote:
break
slash = 0
else:
slash = 0
index += 1
self.post = self.data[index:]
def write(self):
with open(self.path, 'w') as fd:
fd.write(str(self))
def __str__(self):
result = self.pre
size = len(self.name) + 5
for field in self.fields:
result += f'{" "*size}"{field}",\n'
size -= 2
result += f'{" "*size}'
result += self.post
return result
def remove(self, fields): # fields: list[str]):
old = self.fields
self.fields = [it for it in self.fields
if not any(it.startswith(jt + '=') for jt in fields)]
self.changed = old != self.fields
def update(self, fields): # fields: list[str]):
for it in fields:
index = it.find('=')
if index > 0:
name = it[0:index+1]
found = False
for index, jt in enumerate(self.fields):
if jt.startswith(name):
if self.fields[index] != it:
self.fields[index] = it
self.changed = True
found = True
break
if not found:
self.fields.append(it)
self.changed = True
else:
print(f'Invalid: {it}')
sys.exit(1)
def main():
parser = ArgumentParser(description='Manage advanced config')
parser.add_argument('-i',
'--image',
dest='image',
default='',
type=str,
help='Image to update, * for all including head node')
parser.add_argument('-r', '--remove',
dest='remove',
action='store_true',
help='Remove')
parser.add_argument('-d', '--dry-run',
dest='dry_run',
action='store_true',
help='Dry run')
parser.add_argument('-s', '--show',
dest='show',
action='store_true',
help='Show fields')
parser.add_argument('-q', '--quiet',
dest='quiet',
action='store_true',
help='Quiet, set exit code to 1 if changed')
parser.add_argument('-g', '--globalconfig',
dest='globalconfig',
action='store_true',
help='Manipulate GlobalConfig')
parser.add_argument('fields', nargs=REMAINDER)
args = parser.parse_args()
if args.image == '*':
image_base = '/cm/images/'
directories = ['']
for name in os.listdir(image_base):
directories.append(image_base + name)
else:
directories = [args.image]
changed = False
for directory in directories:
config = Config(
'GlobalConfig' if args.globalconfig else 'AdvancedConfig',
directory
)
if args.remove:
config.remove(args.fields)
else:
config.update(args.fields)
if args.dry_run:
print(config)
elif args.show:
print(f'=== {config.path} ===')
for field in config.fields:
print(field)
elif config.changed:
config.write()
if args.quiet:
changed = True
else:
print(f'Updated: {config.path}')
elif not args.quiet:
print(f'Keep: {config.path}')
return 1 if args.quiet and changed else 0
if __name__ == '__main__':
sys.exit(main())