forked from remotesyssupport/cato
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatedb.py
executable file
·385 lines (360 loc) · 16.9 KB
/
updatedb.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python
# Copyright 2012 Cloud Sidekick
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:# www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This file will evaluate the database of the current Cato installation,
and apply any database changes needed, by version.
SUPPORTED OPERATORS:
droptable - table
createtable - table, columns and keys (basically the entire statement except the
beginning "create table `foo`" bit.
addcolumn - table, column, options
modifycolumn - table, column, options (allows changing column options but not name)
changecolumn - table, column, `newname` options (will allow changing of a column name)
dropcolumn - table, column
droppk - table
addpk - table, options (comma delimited columns making up the PK)
sql - a sql statement
function - function name (will execute a funcion in this file, useful only
when very complicated logic is needed for an update.)
NOTE: BE VERY CAREFUL with 'modifycolumn' - it will execute if a column exists
regardless of whether or not the change is needed.
This could result is truncating data if used without care across versions.
Finally, any very specific schema or data updates that can't be done
as simply as this, should be added to the bottom.
"""
# For each version supported, add a set of statements to the dictionary
versions = [
["1.17", [
["createtable", "api_tokens", """(`user_id` varchar(36) NOT NULL,
`token` varchar(36) NOT NULL,
`created_dt` datetime NOT NULL,
PRIMARY KEY (`user_id`) )"""],
["droptable", "deployment_service_state",
"NO LONGER NEEDED WAS FOR STATE BASED SEQUENCES"],
["dropcolumn", "deployment", "_template_name"],
["dropcolumn", "deployment", "_template_version"]
]
],
["1.18", [
["addcolumn", "deployment", "runstate",
"varchar(16) NULL after `health`"],
["addcolumn", "deployment_sequence",
"options", "TEXT NULL"],
["addcolumn", "application_settings",
"license", "TEXT NULL"],
["changecolumn", "task_step_user_settings",
"button", "button varchar(1024)"],
["sql", "delete from task_step_user_settings"]
]
],
["1.19", [
["addindex", "deployment_log",
"IX_dep_log_1", "`deployment_id` ASC"],
["addindex", "deployment_log", "IX_dep_log_2",
"`deployment_id` ASC, `task_instance` ASC"],
["addindex", "deployment_log", "IX_dep_log_3",
"`deployment_id` ASC, `deployment_service_id` ASC"],
["addindex", "deployment_log", "IX_dep_log_4",
"`deployment_id` ASC, `instance_id` ASC"],
["addindex", "deployment_log", "IX_dep_log_5",
"`deployment_id` ASC, `seq_instance` ASC"],
["addindex", "task_instance",
"IX_task_instance_sched_id", "`schedule_id` ASC"]
]
],
["1.20", [
["createtable", "dep_action_plan", """(
`plan_id` bigint(20) NOT NULL AUTO_INCREMENT,
`schedule_id` varchar(36) DEFAULT NULL,
`type` varchar(16) NOT NULL DEFAULT '',
`original_task_id` varchar(36) NOT NULL DEFAULT '',
`task_version` decimal(18, 3) DEFAULT NULL,
`run_on_dt` datetime NOT NULL,
`instance_id` varchar(36) DEFAULT NULL,
`state` varchar(32) DEFAULT NULL,
`account_id` varchar(36) DEFAULT NULL,
`cloud_id` varchar(36) DEFAULT NULL,
`parameter_xml` mediumtext NOT NULL,
`debug_level` int(11) DEFAULT NULL,
PRIMARY KEY (`plan_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8"""],
["createtable", "dep_action_plan_history", """(
`plan_id` bigint(20) NOT NULL AUTO_INCREMENT,
`schedule_id` varchar(36) DEFAULT NULL,
`type` varchar(16) NOT NULL DEFAULT '',
`task_id` varchar(36) NOT NULL DEFAULT '',
`task_instance` decimal(18, 3) DEFAULT NULL,
`run_on_dt` datetime NOT NULL,
`instance_id` varchar(36) DEFAULT NULL,
`state` varchar(32) DEFAULT NULL,
`account_id` varchar(36) DEFAULT NULL,
`cloud_id` varchar(36) DEFAULT NULL,
`parameter_xml` mediumtext NOT NULL,
`debug_level` int(11) DEFAULT NULL,
PRIMARY KEY (`plan_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"""],
["createtable", "dep_action_schedule", """(
`schedule_id` varchar(36) NOT NULL DEFAULT '',
`type` varchar(16) NOT NULL DEFAULT '',
`original_task_id` varchar(36) NOT NULL DEFAULT '',
`task_version` decimal(18, 3) DEFAULT NULL,
`instance_id` varchar(36) DEFAULT NULL,
`state` varchar(32) DEFAULT NULL,
`account_id` varchar(36) DEFAULT NULL,
`cloud_id` varchar(36) DEFAULT NULL,
`months` varchar(27) DEFAULT NULL,
`days_or_weeks` int(11) DEFAULT NULL,
`days` varchar(84) DEFAULT NULL,
`hours` varchar(62) DEFAULT NULL,
`minutes` varchar(172) DEFAULT NULL,
`parameter_xml` mediumtext NOT NULL,
`debug_level` int(11) DEFAULT NULL,
`label` varchar(64) DEFAULT NULL,
`descr` varchar(512) DEFAULT NULL,
PRIMARY KEY (`schedule_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"""],
["createtable", "dep_monitor_inst", """(
`instance_id` varchar(36) NOT NULL,
`task_instance` bigint(20) DEFAULT NULL,
`status` varchar(32) NOT NULL,
`state` varchar(32) DEFAULT NULL,
`task_id` varchar(36) DEFAULT NULL,
PRIMARY KEY (`instance_id`,`task_instance`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"""],
["createtable", "dash_resource", """(
`id` varchar(36) NOT NULL,
`project` varchar(32) NOT NULL,
`component` varchar(45) NOT NULL,
`name` varchar(45) NOT NULL,
`data` mediumblob,
PRIMARY KEY (`id`),
UNIQUE KEY `proj_comp_name` (`project`,`component`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"""],
["createtable", "deployment_group", """(
`deployment_id` varchar(36) NOT NULL,
`group_name` varchar(45) NOT NULL,
PRIMARY KEY (`deployment_id`,`group_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"""],
["droptable", "dep_service_inst_mon",
"NO LONGER NEEDED with the new Maestro scheduler."],
["droptable", "dep_service_inst_proc",
"Removing a demo feature."],
["droptable", "dep_service_inst_proc_inst",
"Removing a demo feature."],
["addcolumn", "deployment_template",
"groups", "varchar(1024) NULL"],
["addcolumn", "asset_credential",
"private_key", "varchar(4096) NULL"],
["changecolumn", "deployment", "runstate",
"`runstate` VARCHAR(16) NULL DEFAULT 'stopped'"]
]
],
["1.21", [
["addcolumn", "application_settings",
"settings_json", "text NULL"],
["function", "_v121_updates"],
["changecolumn", "asset_credential", "username",
"`username` VARCHAR(128) NULL DEFAULT NULL"],
["changecolumn", "asset_credential", "password",
"`password` VARCHAR(2048) NULL DEFAULT NULL"],
["changecolumn", "dash_resource",
"data", "`data` mediumblob"],
["addcolumn", "dep_service_state_mon",
"parameter_xml", "mediumtext NULL"],
["addcolumn", "dep_service_state_mon",
"debug_level", "int(11) NULL"],
["droptable", "metric_db_waits",
"Not used, was for an early demo."],
["droptable", "user_session",
"Recreating in the next step..."],
["createtable", "user_session", """(
`session_id` bigint NOT NULL,
`user_id` varchar(36) NOT NULL,
`address` varchar(255) NOT NULL,
`login_dt` datetime NOT NULL,
`heartbeat` datetime NOT NULL,
`kick` int(11) NOT NULL,
PRIMARY KEY (`session_id`),
UNIQUE INDEX `IX_user_id_address` (`user_id` ASC, `address` ASC),
KEY `FK_user_session_users` (`user_id`),
CONSTRAINT `FK_user_session_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8"""]
]
],
["1.22", [
["droptable", "poller_settings", "Consolidated"],
["droptable", "scheduler_settings", "Consolidated"],
["droptable", "marshaller_settings", "Consolidated"],
["droptable", "login_security_settings", "Consolidated"],
["droptable", "messenger_settings", "Consolidated"],
["addcolumn", "deployment_action",
"options", "TEXT NULL"],
["createtable", "tag_permissions", """(
`tag_name` varchar(32) NOT NULL,
`permission` varchar(64) NOT NULL,
PRIMARY KEY (`tag_name`, `permission`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8"""]
]
],
["1.23", [
["addcolumn", "deployment_service_inst",
"document_id", "VARCHAR(24) NULL"]
]
],
["1.24", [
["dropcolumn", "asset", "is_connection_system"],
["dropcolumn", "task", "use_connector_system"],
["changecolumn", "user_security_log", "log_msg",
"`log_msg` VARCHAR(1024) DEFAULT ''"]
]
]
]
import os
import sys
base_path = os.path.dirname(os.path.abspath(sys.argv[0]))
lib_path = os.path.join(base_path, "lib")
sys.path.insert(0, lib_path)
from catoconfig import catoconfig
from catodb import catodb
def main(argv):
# now, lets go through each defined version, and apply the changes...
for ver in versions:
print "Validating Version %s..." % (ver[0])
for item in ver[1]:
tblexists = None
colexists = None
pkexists = None
ixexists = None
# these apply to several cases below
if item[0] in ["addcolumn", "dropcolumn", "modifycolumn", "changecolumn"]:
sql = """SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'cato'
AND table_name = %s
AND column_name = %s"""
colexists = db.select_col(sql, (item[1], item[2]))
if item[0] in ["droptable", "createtable"]:
sql = """SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'cato'
AND table_name = %s"""
tblexists = db.select_col(sql, (item[1]))
if item[0] in ["droppk", "addpk"]:
sql = """SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE table_schema = 'cato'
AND table_name = %s
AND constraint_type = 'PRIMARY KEY'"""
pkexists = db.select_col(sql, (item[1]))
if item[0] in ["addindex", "dropindex"]:
sql = """SELECT count(*) FROM information_schema.statistics
where table_name = %s
and index_name = %s"""
ixexists = db.select_col(sql, (item[1], item[2]))
sql = ""
if item[0] == "comment":
continue
if item[0] == "droptable":
if tblexists:
sql = "drop table `%s`" % (item[1])
elif item[0] == "createtable":
if not tblexists:
sql = "create table `%s` %s" % (item[1], item[2])
elif item[0] == "droppk":
if pkexists:
sql = "alter table `%s` drop primary key" % (item[1])
elif item[0] == "addpk":
if not tblexists:
sql = "alter table `%s` add primary key (%s)" % (
item[1], item[2])
elif item[0] == "dropindex":
if ixexists:
sql = "alter table `%s` drop index `%s`" % (
item[1], item[2])
elif item[0] == "addindex":
if not ixexists:
sql = "alter table `%s` add index `%s` (%s)" % (
item[1], item[2], item[3])
elif item[0] == "addcolumn":
if not colexists:
sql = "alter table `%s` add column `%s` %s" % (
item[1], item[2], item[3])
elif item[0] == "modifycolumn":
if colexists:
sql = "alter table `%s` modify column `%s` %s" % (
item[1], item[2], item[3])
elif item[0] == "changecolumn":
if colexists:
sql = "alter table `%s` change column `%s` %s" % (
item[1], item[2], item[3])
elif item[0] == "dropcolumn":
if colexists:
sql = "alter table `%s` drop column `%s`" % (
item[1], item[2])
elif item[0] == "sql":
if len(item) > 1 and item[1]:
db.exec_db(item[1])
elif item[0] == "function":
funcname = item[1]
try:
globals().get(funcname)()
except Exception as ex:
raise Exception(
"An error occured trying to execute [%s]\n %s" % (funcname, ex.__str__()))
else:
print " encountered unknown directive [%s]" % (item[0])
if sql:
print " Executing [%s]" % (sql)
db.exec_db(sql)
print " .... done."
def _v121_updates():
# 1.21 has a new directory name for the Task Engine log.
import os
try:
print(" Renaming 'ce' log directory to 'te'.")
os.rename("/var/cato/log/ce", "/var/cato/log/te")
except:
print("""
WARNING - unable to rename '/var/calo/log/ce' log directory to 'te'.
This is OK if the directory name has already been fixed.
If this system is configured to keep logs in a different location, please change the directory name manually.
""")
pass
if __name__ == "__main__":
args = None
if sys.version_info < (2, 7):
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", "--user", help="Cato root db user.")
parser.add_option("-p", "--password", help="Cato root db password.")
(args, arglist) = parser.parse_args()
else:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--user", help="Cato root db user.")
parser.add_argument("-p", "--password", help="Cato root db password.")
args = parser.parse_args()
if not args:
raise Exception(
"Unable to continue - unable to parse command line arguments.")
UID = args.user
PWD = args.password if args.password else ""
db = catodb.Db()
db.connect_db(
user=UID, password=PWD, server=catoconfig.CONFIG[
"server"], port=catoconfig.CONFIG["port"],
database=catoconfig.CONFIG["database"])
main(sys.argv[1:])
db.close()