-
Notifications
You must be signed in to change notification settings - Fork 6
/
vmcatcher_cache
executable file
·225 lines (206 loc) · 8.75 KB
/
vmcatcher_cache
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
#!/usr/bin/env python
import sys
if sys.version_info < (2, 4):
print "Your python interpreter is too old. Please consider upgrading."
sys.exit(1)
if sys.version_info < (2, 5):
import site
import os.path
from distutils.sysconfig import get_python_lib
found = False
module_dir = get_python_lib()
for name in os.listdir(module_dir):
lowername = name.lower()
if lowername[0:10] == 'sqlalchemy' and 'egg' in lowername:
sqlalchemy_dir = os.path.join(module_dir, name)
if os.path.isdir(sqlalchemy_dir):
site.addsitedir(sqlalchemy_dir)
found = True
break
if not found:
print "Could not find SQLAlchemy installed."
sys.exit(1)
import os.path
import logging
import optparse
from vmcatcher.__version__ import version
import vmcatcher
import urllib2
import urllib
import hashlib
import datetime
import os, statvfs
import shutil
import commands
import uuid
try:
import simplejson as json
except:
import json
import urlparse
import subprocess
import time
from vmcatcher.vmcatcher_cache.manged_directory import BaseDir, DownloadDir, CacheDir, ExpireDir
from vmcatcher.launch import EventObj
from vmcatcher.vmcatcher_cache.controler import CacheMan
def main():
"""Runs program and handles command line options"""
p = optparse.OptionParser(version = "%prog " + version)
p.add_option('-d', '--database', action ='store', help='Database conection string')
p.add_option('-x', '--execute', action ='store',help='Event application to launch.', metavar='EVENT')
p.add_option('-C', '--cache-dir', action ='store',help='Set the cache directory.',metavar='DIR_CACHE')
p.add_option('-p', '--partial-dir', action ='store',help='Set the cache download directory.',metavar='DIR_PARTIAL')
p.add_option('-e', '--expired-dir', action ='store',help='Set the cache expired directory.',metavar='DIR_EXPIRE')
p.add_option('-D', '--download', action ='store_true',help='Download subscribed images to cache directory.')
p.add_option('-s', '--sha512', action ='store_true',help='Check cache directory images Sha512.')
p.add_option('-E', '--expire', action ='store_true',help='Remove expired images from cache directory.')
p.add_option('-v', '--verbose', action ='count',help='Change global log level, increasing log output.')
p.add_option('-q', '--quiet', action ='count',help='Change global log level, decreasing log output.')
p.add_option('--log-config', action ='store',help='Logfile configuration file, (overrides command line).', metavar='LOG_CONFIG')
p.add_option('--log-sql-info', action ='store_true',help='Echo all SQL commands.', metavar='LOGFILE')
options, arguments = p.parse_args()
dir_cache = None
dir_partial = None
dir_expired = None
actions = set()
databaseConnectionString = None
eventExecutionString = None
logFile = None
debugSqlEcho = False
if 'VMCATCHER_RDBMS' in os.environ:
databaseConnectionString = os.environ['VMCATCHER_RDBMS']
if 'VMCATCHER_LOG_CONF' in os.environ:
logFile = os.environ['VMCATCHER_LOG_CONF']
if 'VMCATCHER_CACHE_DIR_CACHE' in os.environ:
dir_cache = os.environ['VMCATCHER_CACHE_DIR_CACHE']
if 'VMCATCHER_CACHE_DIR_DOWNLOAD' in os.environ:
dir_partial = os.environ['VMCATCHER_CACHE_DIR_DOWNLOAD']
if 'VMCATCHER_CACHE_DIR_EXPIRE' in os.environ:
dir_expired = os.environ['VMCATCHER_CACHE_DIR_EXPIRE']
if 'VMCATCHER_CACHE_EVENT' in os.environ:
eventExecutionString = os.environ['VMCATCHER_CACHE_EVENT']
if 'VMCATCHER_CACHE_ACTION_DOWNLOAD' in os.environ:
if os.environ['VMCATCHER_CACHE_ACTION_DOWNLOAD'] == "1":
actions.add("download")
if 'VMCATCHER_CACHE_ACTION_CHECK' in os.environ:
if os.environ['VMCATCHER_CACHE_ACTION_CHECK'] == "1":
actions.add("sha512")
if 'VMCATCHER_CACHE_ACTION_EXPIRE' in os.environ:
if os.environ['VMCATCHER_CACHE_ACTION_EXPIRE'] == "1":
actions.add("expire")
# Set up log file
LoggingLevel = logging.WARNING
LoggingLevelCounter = 2
if options.verbose:
LoggingLevelCounter = LoggingLevelCounter - options.verbose
if options.verbose == 1:
LoggingLevel = logging.INFO
if options.verbose == 2:
LoggingLevel = logging.DEBUG
if options.quiet:
LoggingLevelCounter = LoggingLevelCounter + options.quiet
if LoggingLevelCounter <= 0:
LoggingLevel = logging.DEBUG
if LoggingLevelCounter == 1:
LoggingLevel = logging.INFO
if LoggingLevelCounter == 2:
LoggingLevel = logging.WARNING
if LoggingLevelCounter == 3:
LoggingLevel = logging.ERROR
if LoggingLevelCounter == 4:
LoggingLevel = logging.FATAL
if LoggingLevelCounter >= 5:
LoggingLevel = logging.CRITICAL
if options.log_config:
logFile = options.log_config
if logFile != None:
if os.path.isfile(logFile):
logging.config.fileConfig(logFile)
else:
logging.basicConfig(level=LoggingLevel)
log = logging.getLogger("main")
log.error("Logfile configuration file '%s' was not found." % (options.log_config))
sys.exit(1)
else:
logging.basicConfig(level=LoggingLevel)
log = logging.getLogger("main")
# Now logging is set up process other options
if options.log_sql_info:
debugSqlEcho = True
if options.cache_dir:
dir_cache = options.cache_dir
if options.partial_dir:
dir_partial = options.partial_dir
if options.expired_dir:
dir_expired = options.expired_dir
if options.expire:
actions.add("expire")
if options.sha512:
actions.add("sha512")
if options.download:
actions.add("download")
if options.database:
databaseConnectionString = options.database
if options.execute:
eventExecutionString = options.execute
# 1 So we have some command line validation
if databaseConnectionString == None:
databaseConnectionString = 'sqlite:///vmcatcher.db'
log.info("Defaulting DB connection to '%s'" % (databaseConnectionString))
if len(actions) == 0:
actions.add("download")
actions.add("expire")
log.info("Defaulting actions as 'expire', and 'download'.")
if ("download" in actions) and (not "expire" in actions):
log.info("Defaulting action 'expire', as 'download' is requested.")
actions.add("expire")
if dir_cache == None:
dir_cache = "cache"
log.info("Defaulting cache-dir to '%s'." % (dir_cache))
if dir_partial == None:
dir_partial = os.path.join(dir_cache,"partial")
log.info("Defaulting partial-dir to '%s'." % (dir_partial))
if dir_expired == None:
dir_expired = os.path.join(dir_cache,"expired")
log.info("Defaulting expired-dir to '%s'." % (dir_expired))
directories_good = True
if not os.path.isdir(dir_cache):
log.error("Cache directory '%s' does not exist." % (dir_cache))
directories_good = False
if not os.path.isdir(dir_partial):
log.error("Download directory '%s' does not exist." % (dir_partial))
directories_good = False
if not os.path.isdir(dir_expired):
log.error("Expired directory '%s' does not exist." % (dir_expired))
directories_good = False
if not directories_good:
sys.exit(1)
ThisCacheManager = CacheMan(databaseConnectionString,debugSqlEcho,dir_cache, dir_partial, dir_expired)
EventInstance = None
EventInstance = EventObj(eventExecutionString)
EventInstance.env['VMCATCHER_CACHE_DIR_CACHE'] = dir_cache
EventInstance.env['VMCATCHER_CACHE_DIR_DOWNLOAD'] = dir_partial
EventInstance.env['VMCATCHER_CACHE_DIR_EXPIRE'] = dir_expired
EventInstance.env['VMCATCHER_RDBMS'] = databaseConnectionString
ThisCacheManager.callbackEventAvailablePrefix = EventInstance.eventAvailablePrefix
ThisCacheManager.callbackEventAvailablePostfix = EventInstance.eventAvailablePostfix
ThisCacheManager.callbackEventExpirePrefix = EventInstance.eventExpirePrefix
ThisCacheManager.callbackEventExpirePostfix = EventInstance.eventExpirePosfix
ThisCacheManager.load()
EventInstance.eventProcessPrefix({})
if "expire" in actions:
if not ThisCacheManager.expire():
log.error("Failed to expire old images")
sys.exit(1)
if "sha512" in actions:
if not ThisCacheManager.checkSumCache():
log.error("Failed to checksum old images")
sys.exit(1)
if "download" in actions:
if not ThisCacheManager.download():
log.error("Failed to download new images")
sys.exit(1)
ThisCacheManager.save()
EventInstance.eventProcessPostfix({})
if __name__ == "__main__":
main()