1 | #!/usr/bin/python26
|
---|
2 | ###
|
---|
3 | ### backmon.commands.db
|
---|
4 | ###
|
---|
5 |
|
---|
6 | import sys
|
---|
7 | import os
|
---|
8 | import os.path
|
---|
9 | import glob
|
---|
10 | import re
|
---|
11 | import datetime
|
---|
12 | import pytz
|
---|
13 | import dateutil
|
---|
14 | import MySQLdb
|
---|
15 |
|
---|
16 | from optparse import OptionParser
|
---|
17 |
|
---|
18 | from ...lib import *
|
---|
19 | from ....dal.builtins import *
|
---|
20 | from ....datetime import *
|
---|
21 |
|
---|
22 | import subcommands
|
---|
23 |
|
---|
24 | SUBCOMMAND_LIST = [
|
---|
25 | 'test',
|
---|
26 | 'update',
|
---|
27 | ]
|
---|
28 |
|
---|
29 | SUBCOMMAND_HELP = {
|
---|
30 | 'test' : 'test database connectivity',
|
---|
31 | 'update' : 'update database with current data',
|
---|
32 | }
|
---|
33 |
|
---|
34 | ##
|
---|
35 | ## OPTIONS
|
---|
36 | ##
|
---|
37 | usage = """Usage: %prog [options] db [subcommand] [options] [arguments]'
|
---|
38 |
|
---|
39 | available subcommands:
|
---|
40 |
|
---|
41 | """ + '\n'.join([' %-12s %s' % (cmd, SUBCOMMAND_HELP[cmd]) for cmd in SUBCOMMAND_LIST])
|
---|
42 |
|
---|
43 | parser = OptionParser(usage=usage)
|
---|
44 | parser.add_option('-n', '--now', action='store', default='now', help='override now for updates')
|
---|
45 |
|
---|
46 | def run(args, kwargs):
|
---|
47 |
|
---|
48 | #
|
---|
49 | # add kwargs to local namespace
|
---|
50 | #
|
---|
51 | for key in kwargs.keys():
|
---|
52 |
|
---|
53 | if re.compile('^[A-Z][A-Z_]+$').match(key):
|
---|
54 | exec(key + ' = kwargs[\'' + key + '\']')
|
---|
55 |
|
---|
56 | #
|
---|
57 | # connect to database
|
---|
58 | #
|
---|
59 | DEBUG('DATABASE_ENGINE....: %s' % (DATABASE_ENGINE))
|
---|
60 | DEBUG('DATABASE_NAME......: %s' % (DATABASE_NAME))
|
---|
61 | DEBUG('DATABASE_USER......: %s' % (DATABASE_USER))
|
---|
62 | DEBUG('DATABASE_PASSWORD..: %s' % (DATABASE_PASSWORD))
|
---|
63 | DEBUG('DATABASE_HOST......: %s' % (DATABASE_HOST))
|
---|
64 | DEBUG('DATABASE_PORT......: %s' % (DATABASE_PORT))
|
---|
65 |
|
---|
66 | try:
|
---|
67 |
|
---|
68 | CONN = MySQLdb.connect(host=DATABASE_HOST, user=DATABASE_USER, passwd=DATABASE_PASSWORD, db=DATABASE_NAME)
|
---|
69 | DEBUG('Connected to %s database.' % (DATABASE_NAME))
|
---|
70 |
|
---|
71 | except Exception, e:
|
---|
72 |
|
---|
73 | ERROR('Could not connect to %s database! %s' % (DATABASE_NAME, e))
|
---|
74 | sys.exit(1)
|
---|
75 |
|
---|
76 |
|
---|
77 | SUBCOMMAND=None
|
---|
78 | subcommand_pos = 0
|
---|
79 |
|
---|
80 | i = 0
|
---|
81 |
|
---|
82 | for arg in args:
|
---|
83 |
|
---|
84 | i += 1
|
---|
85 |
|
---|
86 | if arg in SUBCOMMAND_LIST:
|
---|
87 | SUBCOMMAND = arg
|
---|
88 | subcommand_pos = i
|
---|
89 | break
|
---|
90 |
|
---|
91 | subcommand_args = args[subcommand_pos:]
|
---|
92 |
|
---|
93 | if SUBCOMMAND:
|
---|
94 |
|
---|
95 | (options, args) = parser.parse_args(args[:subcommand_pos])
|
---|
96 |
|
---|
97 | #
|
---|
98 | # timestamps
|
---|
99 | #
|
---|
100 | TZ = pytz.timezone(one_value(CONN.cursor(), "SELECT value FROM settings WHERE name = 'timezone'"))
|
---|
101 | UTC = pytz.timezone('UTC')
|
---|
102 |
|
---|
103 | BOT = dateutil.parser.parse(one_value(CONN.cursor(), "SELECT value FROM settings WHERE name = 'bot'")).replace(tzinfo=TZ)
|
---|
104 | DEBUG('BOT = %s' % (BOT))
|
---|
105 |
|
---|
106 | BOT_ISO = BOT.strftime('%Y-%m-%d %H:%M:%S')
|
---|
107 | DEBUG('BOT_ISO = %s' % (BOT_ISO))
|
---|
108 |
|
---|
109 | if options.now == 'now':
|
---|
110 | NOW = datetime.datetime.now(TZ)
|
---|
111 | else:
|
---|
112 | NOW = dateutil.parser.parse(options.now).replace(tzinfo=TZ)
|
---|
113 |
|
---|
114 | DEBUG('NOW = %s' % (NOW))
|
---|
115 |
|
---|
116 | UTCNOW = NOW.astimezone(UTC)
|
---|
117 | DEBUG('UTCNOW = %s' % (UTCNOW))
|
---|
118 |
|
---|
119 | UTCNOW_ISO = UTCNOW.strftime('%Y-%m-%d %H:%M:%S')
|
---|
120 | DEBUG('UTCNOW_ISO = %s' % (UTCNOW_ISO))
|
---|
121 |
|
---|
122 | #TODAY = NOW.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
|
---|
123 | TODAY = NOW.replace(hour=0, minute=0, second=0, microsecond=0)
|
---|
124 | DEBUG('TODAY = %s' % (TODAY))
|
---|
125 |
|
---|
126 | TODAY_ISO = TODAY.strftime('%Y-%m-%d %H:%M:%S')
|
---|
127 | DEBUG('TODAY_ISO = %s' % (TODAY_ISO))
|
---|
128 |
|
---|
129 | TOMORROW = TODAY + datetime.timedelta(days=1)
|
---|
130 | DEBUG('TOMORROW = %s' % (TOMORROW))
|
---|
131 |
|
---|
132 | TOMORROW_ISO = TOMORROW.strftime('%Y-%m-%d %H:%M:%S')
|
---|
133 | DEBUG('TOMORROW_ISO = %s' % (TOMORROW_ISO))
|
---|
134 |
|
---|
135 | YESTERDAY = TODAY - datetime.timedelta(days=1)
|
---|
136 | DEBUG('YESTERDAY = %s' % (YESTERDAY))
|
---|
137 |
|
---|
138 | YESTERDAY_ISO = YESTERDAY.strftime('%Y-%m-%d %H:%M:%S')
|
---|
139 | DEBUG('YESTERDAY_ISO = %s' % (YESTERDAY_ISO))
|
---|
140 |
|
---|
141 | CURR_BACKUP_DAY = backup_day(NOW)
|
---|
142 | DEBUG('CURR_BACKUP_DAY = %s' % (CURR_BACKUP_DAY))
|
---|
143 |
|
---|
144 | CURR_BACKUP_DAY_ISO = CURR_BACKUP_DAY.strftime('%Y-%m-%d %H:%M:%S')
|
---|
145 | DEBUG('CURR_BACKUP_DAY_ISO = %s' % (CURR_BACKUP_DAY_ISO))
|
---|
146 |
|
---|
147 | NEXT_BACKUP_DAY = CURR_BACKUP_DAY + datetime.timedelta(days=1)
|
---|
148 | DEBUG('NEXT_BACKUP_DAY = %s' % (NEXT_BACKUP_DAY))
|
---|
149 |
|
---|
150 | NEXT_BACKUP_DAY_ISO = NEXT_BACKUP_DAY.strftime('%Y-%m-%d %H:%M:%S')
|
---|
151 | DEBUG('NEXT_BACKUP_DAY_ISO = %s' % (NEXT_BACKUP_DAY_ISO))
|
---|
152 |
|
---|
153 | PREV_BACKUP_DAY = CURR_BACKUP_DAY - datetime.timedelta(days=1)
|
---|
154 | DEBUG('PREV_BACKUP_DAY = %s' % (PREV_BACKUP_DAY))
|
---|
155 |
|
---|
156 | PREV_BACKUP_DAY_ISO = PREV_BACKUP_DAY.strftime('%Y-%m-%d %H:%M:%S')
|
---|
157 | DEBUG('PREV_BACKUP_DAY_ISO = %s' % (PREV_BACKUP_DAY_ISO))
|
---|
158 |
|
---|
159 | TIMESTAMP = NOW.replace(second=0, microsecond=0)
|
---|
160 | DEBUG('TIMESTAMP = %s' % (TIMESTAMP))
|
---|
161 |
|
---|
162 | TIMESTAMP_ISO = TIMESTAMP.strftime('%Y-%m-%d %H:%M:%S')
|
---|
163 | DEBUG('TIMESTAMP_ISO = %s' % (TIMESTAMP_ISO))
|
---|
164 |
|
---|
165 | UTCTIMESTAMP = UTCNOW.replace(second=0, microsecond=0)
|
---|
166 | DEBUG('UTCTIMESTAMP = %s' % (UTCTIMESTAMP))
|
---|
167 |
|
---|
168 | UTCTIMESTAMP_ISO = UTCTIMESTAMP.strftime('%Y-%m-%d %H:%M:%S')
|
---|
169 | DEBUG('UTCTIMESTAMP_ISO = %s' % (UTCTIMESTAMP_ISO))
|
---|
170 |
|
---|
171 |
|
---|
172 | #
|
---|
173 | # SUBCOMMAND
|
---|
174 | #
|
---|
175 | if( not SUBCOMMAND ):
|
---|
176 | parser.print_help()
|
---|
177 | sys.exit(1)
|
---|
178 |
|
---|
179 | DEBUG('SUBCOMMAND=%s' % (SUBCOMMAND))
|
---|
180 |
|
---|
181 | if hasattr(subcommands, SUBCOMMAND):
|
---|
182 | exec 'subcommands.' + SUBCOMMAND + '.run(subcommand_args, locals())'
|
---|
183 | else:
|
---|
184 | ERROR('Invalid subcommand \'%s\'.' % (SUBCOMMAND))
|
---|
185 |
|
---|
186 |
|
---|