diff options
author | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2012-09-07 13:17:13 +0100 |
---|---|---|
committer | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2012-09-07 13:46:35 +0100 |
commit | 1a0e40d854d37f81e9cdaf8bb23e480790614d2a (patch) | |
tree | d1e7beaa8d10c6356f3ca918a98ba0fd7430e4f6 /morph-cache-server | |
parent | 465e830d1d6d2c51425e2418b8e802a95145b6ee (diff) | |
download | morph-1a0e40d854d37f81e9cdaf8bb23e480790614d2a.tar.gz |
Support for fetching artifacts to the cache
Rather than pushing artifacts to the cache, this method allows the caller to
specify a host and artifact which the cache server will then fetch into its
local cache. It takes the following arguments:
host=hostname:port
artifact=artifactname
This is transformed into a fetch to:
http://hostname:port/artifacts?basename=artifactname
Which is then fetched into the cache under the given name.
The return from this is a JSON object of the form:
{
"filename": artifactname,
"size": NBYTES_SIZE_OF_FILE,
"used": NBYTES_DISK_SPACE_USED
}
Diffstat (limited to 'morph-cache-server')
-rwxr-xr-x | morph-cache-server | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/morph-cache-server b/morph-cache-server index b726c1e5..286e56db 100755 --- a/morph-cache-server +++ b/morph-cache-server @@ -20,6 +20,7 @@ import cliapp import logging import os import urllib +import shutil from bottle import Bottle, request, response, run, static_file @@ -94,17 +95,48 @@ class MorphCacheServer(cliapp.Application): fsstinfo = os.statvfs(artifactdir) results["freespace"] = fsstinfo.f_bsize * fsstinfo.f_bavail for fname in filenames: - try: - stinfo = os.stat("%s/%s" % (artifactdir, fname)) - files[fname] = { - "atime": stinfo.st_atime, - "size": stinfo.st_size, - "used": stinfo.st_blocks * 512, - } - except Exception, e: - print(e) + if not fname.startswith(".dl."): + try: + stinfo = os.stat("%s/%s" % (artifactdir, fname)) + files[fname] = { + "atime": stinfo.st_atime, + "size": stinfo.st_size, + "used": stinfo.st_blocks * 512, + } + except Exception, e: + print(e) return results + @writable('/fetch') + def fetch(): + host = self._unescape_parameter(request.query.host) + artifact = self._unescape_parameter(request.query.artifact) + try: + response.set_header('Cache-Control', 'no-cache') + in_fh = urllib.urlopen("http://%s/artifacts?basename=%s" % + (host, urllib.quote(artifact))) + tmpname = "%s/.dl.%s" % ( + self.settings['artifact-dir'], + artifact) + localtmp = open(tmpname, "w") + shutil.copyfileobj(in_fh, localtmp) + localtmp.close() + in_fh.close() + artifilename = "%s/%s" % (self.settings['artifact-dir'], + artifact) + os.rename(tmpname, artifilename) + stinfo = os.stat(artifilename) + ret = {} + ret[artifact] = { + "size": stinfo.st_size, + "used": stinfo.st_blocks * 512 + } + return ret + + except Exception, e: + response.status = 500 + logging.debug('%s' % e) + @app.get('/sha1s') def sha1(): repo = self._unescape_parameter(request.query.repo) |