Commit 80c85d82 authored by Julien Muchembled's avatar Julien Muchembled

New scripts to download & upload manually from command line

parent ac8463ad
......@@ -42,6 +42,8 @@ setup(
entry_points={
'console_scripts': [
'generate-signature-key = slapos.signature:run',
'networkcache-download = slapos.libnetworkcache:download',
'networkcache-upload = slapos.libnetworkcache:upload',
]
},
zip_safe=True,
......
......@@ -12,10 +12,15 @@
#
##############################################################################
import argparse
import ConfigParser
import hashlib
import httplib
import json
import os
import shutil
import sys
import tarfile
import tempfile
import traceback
import urllib2
......@@ -62,6 +67,10 @@ class NetworkcacheClient(object):
}, signature_certificate_list)
def __new_init(self, config, signature_certificate_list=None):
if not hasattr(config, 'get'):
parser = ConfigParser.SafeConfigParser()
parser.readfp(config)
config = dict(parser.items('networkcache'))
self.config = config
path = config.get('signature-private-key-file')
if path:
......@@ -278,3 +287,46 @@ class DirectoryNotFound(Exception):
class UploadError(Exception):
pass
def _newArgumentParser():
parser = argparse.ArgumentParser()
parser.add_argument('slapos_config', type=argparse.FileType('r'),
help='SlapOS configuration file.')
return parser
def cmd_upload(*args):
parser = _newArgumentParser()
parser.add_argument('--prefix-key', default='',
help="Key will be concatenation of PREFIX_KEY and md5(URL).")
parser.add_argument('--url', required=True,
help="Upload data pointed to by this argument, unless --file is specified."
" If argument is not a local path, contents is first downloaded"
" in a temporary file.")
parser.add_argument('--file',
help="Upload the contents of this file, overriding --url")
args = parser.parse_args(args or sys.argv[1:])
nc = NetworkcacheClient(args.slapos_config)
f = None
try:
if args.file:
f = open(args.file)
elif os.path.isdir(args.url):
f = nc.archive(args.url)
else:
f = urllib2.urlopen(args.url)
urlmd5 = hashlib.md5(args.url).hexdigest()
nc.upload(f, args.prefix_key + urlmd5, urlmd5=urlmd5,
file_name=os.path.basename(args.url))
finally:
f is None or f.close()
def cmd_download(*args):
parser = _newArgumentParser()
parser.add_argument('--prefix-key', default='',
help="Key will be concatenation of PREFIX_KEY and md5(URL).")
parser.add_argument('--url', required=True,
help="URL of data to download.")
args = parser.parse_args(args or sys.argv[1:])
nc = NetworkcacheClient(args.slapos_config)
key = args.prefix_key + hashlib.md5(args.url).hexdigest()
shutil.copyfileobj(nc.download(nc.select(key).next()['sha512']), sys.stdout)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment