Skip to content
Snippets Groups Projects
Commit 26aaa96f authored by Seblu's avatar Seblu
Browse files

Improve remote cache. Http transport enabled

parent 1a56a5d0
No related branches found
No related tags found
No related merge requests found
...@@ -45,9 +45,9 @@ try: ...@@ -45,9 +45,9 @@ try:
# load config # load config
config = ConfigFile("isinstall", args.config) config = ConfigFile("isinstall", args.config)
# looks if arguments is a file or image name # looks if arguments is a file or image name
image_name_type = istools.get_path_type(args.image_name) image_name_type = istools.pathtype(args.image_name)
if image_name_type == "file": if image_name_type == "file":
pkg = PackageImage(istools.complete_path(args.image_name)) pkg = PackageImage(istools.abspath(args.image_name))
elif image_name_type == "name": elif image_name_type == "name":
# init repo cache object # init repo cache object
repocache = RepositoryCache(config.cache, verbose=args.verbose) repocache = RepositoryCache(config.cache, verbose=args.verbose)
......
...@@ -32,7 +32,7 @@ class ConfigFile(object): ...@@ -32,7 +32,7 @@ class ConfigFile(object):
'''Load/Reload config file''' '''Load/Reload config file'''
# seting default config # seting default config
self._config = {} self._config = {}
self._repos = {} self._repos = []
# loading config file if exists # loading config file if exists
if self.path is not None: if self.path is not None:
debug("Loading config file: %s" % self.path) debug("Loading config file: %s" % self.path)
...@@ -49,7 +49,7 @@ class ConfigFile(object): ...@@ -49,7 +49,7 @@ class ConfigFile(object):
if "image" not in cp.options(rep): if "image" not in cp.options(rep):
continue continue
# get all options in repo # get all options in repo
self._repos[rep] = RepositoryConfig(rep, **dict(cp.items(rep))) self._repos.append( RepositoryConfig(rep, **dict(cp.items(rep))))
except Exception as e: except Exception as e:
raise raise
raise Exception("Unable load file %s: %s" % (self.path, e)) raise Exception("Unable load file %s: %s" % (self.path, e))
...@@ -89,5 +89,6 @@ class ConfigFile(object): ...@@ -89,5 +89,6 @@ class ConfigFile(object):
@property @property
def repos(self): def repos(self):
'''Get a list of repository available''' '''Get a dict of repository available'''
return self._repos.copy() # deep copy
return list(self._repos)
...@@ -22,7 +22,7 @@ class Database(object): ...@@ -22,7 +22,7 @@ class Database(object):
@classmethod @classmethod
def create(cls, path, verbose=True): def create(cls, path, verbose=True):
arrow("Creating repository database", 1, verbose) arrow("Creating repository database", 1, verbose)
dbpath = os.path.abspath(path) dbpath = istools.abspath(path)
if os.path.exists(dbpath): if os.path.exists(dbpath):
raise Exception("db already exists") raise Exception("db already exists")
try: try:
...@@ -34,7 +34,7 @@ class Database(object): ...@@ -34,7 +34,7 @@ class Database(object):
return cls(path, verbose) return cls(path, verbose)
def __init__(self, path, verbose=True): def __init__(self, path, verbose=True):
self.path = os.path.abspath(path) self.path = istools.abspath(path)
self.verbose = verbose self.verbose = verbose
def add(self, package): def add(self, package):
......
...@@ -11,6 +11,7 @@ import time ...@@ -11,6 +11,7 @@ import time
import shutil import shutil
import pwd import pwd
import grp import grp
import copy
import installsystems import installsystems
import installsystems.tools as istools import installsystems.tools as istools
from installsystems.printer import * from installsystems.printer import *
...@@ -176,61 +177,75 @@ class RepositoryCache(object): ...@@ -176,61 +177,75 @@ class RepositoryCache(object):
'''Local repository cache class''' '''Local repository cache class'''
def __init__(self, cache_path, verbose=True): def __init__(self, cache_path, verbose=True):
self.base_path = os.path.abspath(cache_path)
self.image_path = os.path.join(self.base_path, "image")
self.last_path = os.path.join(self.base_path, "last")
self.db_path = os.path.join(self.base_path, "db")
for path in (self.base_path, self.image_path, self.last_path, self.db_path):
if not os.path.exists(path):
os.mkdir(path)
if not os.access(path, os.W_OK | os.X_OK):
raise Exception("%s is not writable or executable" % path)
self.verbose = verbose self.verbose = verbose
self.repos = dict() self.repos = {}
debug("Repository cache is in %s" % self.base_path) self.path = os.path.abspath(cache_path)
# ensure cache directories are avaiblable
if not os.path.exists(self.path):
os.mkdir(self.path)
if not os.access(self.path, os.W_OK | os.X_OK):
raise Exception("%s is not writable or executable" % path)
debug("Repository cache is in %s" % self.path)
def register(self, iterepo): def register(self, configs):
'''Register a repository to track''' '''Register a list of repository from its config'''
for r in iterepo: for conf in configs:
self.repos[r[0]] = Repository(istools.abspath(r[1]), self.repos[conf.name] = {}
istools.abstpath(r[2]), # keep original repository conf
verbose=self.verbose) self.repos[conf.name]["orig"] = Repository(conf, self.verbose)
# change configuration to make remote repository in cache
cconf = copy.copy(conf)
cconf.image = os.path.join(self.path, conf.name)
cconf.data = "/dev/null"
self.repos[conf.name]["cache"] = Repository(cconf, self.verbose)
# create a local directory
if not os.path.exists(cconf.image):
os.mkdir(cconf.image)
def update(self): def update(self):
'''Update cache info''' '''Update cache info'''
arrow("Updating repositories", 1, self.verbose) arrow("Updating repositories", 1, self.verbose)
for r in self.repos: for r in self.repos:
debug("%s: remote_last: %s, local_last:%s" % (r, # last local
self.repos[r].last(), local_last = self.last(r)
self.last(r))) # copy last file
if self.repos[r].last() > self.last(r): arrow("Copying %s repository last" % r, 2, self.verbose)
# copy last file istools.copy(self.repos[r]["orig"].last_path,
istools.copy(self.repos[r].last_path, os.path.join(self.last_path, r)) self.repos[r]["cache"].last_path,)
# last after update
remote_last = self.last(r)
debug("%s: last: local: %s, remote:%s" % (r, local_last, remote_last))
# Updating db?
remote_db = self.repos[r]["orig"].db.path
local_db = self.repos[r]["cache"].db.path
if remote_last > local_last or not os.path.exists(local_db):
# copy db file # copy db file
istools.copy(self.repos[r].db.path, os.path.join(self.db_path, r)) arrow("Copying %s repository db" % r, 2, self.verbose)
istools.copy(remote_db, local_db)
arrow("%s updated" % r, 2, self.verbose) arrow("%s updated" % r, 2, self.verbose)
def last(self, reponame): def last(self, reponame):
'''Return the last timestamp of a repo''' '''Return the last timestamp of a repo'''
last_path = os.path.join(self.last_path, reponame) last_path = os.path.join(self.path, reponame, "last")
if os.path.exists(last_path): try:
return int(open(last_path, "r").read().rstrip()) return int(open(last_path, "r").read().rstrip())
return 0 except Exception:
return 0
def get_image(self, reponame, imagename, imageversion): def get_image(self, reponame, imagename, imageversion):
'''Obtain a local path in cache for a remote image in repo''' '''Obtain a local path in cache for a remote image in repo'''
arrow("Getting image", 1, self.verbose) arrow("Getting image", 1, self.verbose)
filename = "%s-%s%s" % (imagename, imageversion, Image.image_extension) filename = "%s-%s%s" % (imagename, imageversion, Image.image_extension)
localpath = os.path.join(self.image_path, filename) cachepath = os.path.join(self.repos[reponame]["cache"].config.image, filename)
# return db path if exists # return db path if exists
if os.path.exists(localpath): if os.path.exists(cachepath):
arrow("Found in cache", 2, self.verbose) arrow("Found in cache", 2, self.verbose)
return localpath else:
# get remote image # get remote image
remotepath = os.path.join(self.repos[reponame].image_path, filename) remotepath = os.path.join(self.repos[reponame]["orig"].config.image, filename)
arrow("Copying from repository", 2, self.verbose) arrow("Copying from repository", 2, self.verbose)
istools.copy(remotepath, localpath) istools.copy(remotepath, cachepath)
return localpath return cachepath
def find_image(self, name, version): def find_image(self, name, version):
'''Find an image in repositories''' '''Find an image in repositories'''
...@@ -241,7 +256,7 @@ class RepositoryCache(object): ...@@ -241,7 +256,7 @@ class RepositoryCache(object):
img = None img = None
# search in all repositories # search in all repositories
for repo in self.repos: for repo in self.repos:
tempdb = Database(os.path.join(self.db_path, repo), False) tempdb = Database(self.repos[repo]["cache"].db.path, False)
img = tempdb.find(name, version) img = tempdb.find(name, version)
if img is not None: if img is not None:
# \o/ # \o/
......
...@@ -9,6 +9,7 @@ InstallSystems Generic Tools Library ...@@ -9,6 +9,7 @@ InstallSystems Generic Tools Library
import os import os
import hashlib import hashlib
import shutil import shutil
import urllib2
def md5sum(path): def md5sum(path):
'''Compute md5 of a file''' '''Compute md5 of a file'''
...@@ -16,20 +17,26 @@ def md5sum(path): ...@@ -16,20 +17,26 @@ def md5sum(path):
m.update(open(path, "r").read()) m.update(open(path, "r").read())
return m.hexdigest() return m.hexdigest()
def copy(source, destination, uid=None, gid=None, mode=None): def copy(source, destination, uid=None, gid=None, mode=None, timeout=None):
'''Copy a source to destination. Take care of path type''' '''Copy a source to destination. Take care of path type'''
stype = pathtype(source) stype = pathtype(source)
dtype = pathtype(destination) dtype = pathtype(destination)
# ensure destination is not a directory
if dtype == "file" and os.path.isdir(destination):
destination = os.path.join(destination, os.path.basename(source))
# trivial case
if stype == dtype == "file": if stype == dtype == "file":
shutil.copy(source, destination) shutil.copy(source, destination)
elif stype == "http" and dtype == "file":
f_dest = open(destination, "w")
f_source = urllib2.urlopen(source, timeout=timeout)
f_dest.write(f_source.read())
elif stype == "file" and dtype == "": elif stype == "file" and dtype == "":
raise NotImplementedError raise NotImplementedError
else: else:
raise NotImplementedError raise NotImplementedError
# setting destination file rights # setting destination file rights
if dtype == "file": if dtype == "file":
if os.path.isdir(destination):
destination = os.path.join(destination, os.path.basename(source))
chrights(destination, uid, gid, mode) chrights(destination, uid, gid, mode)
def mkdir(path, uid=None, gid=None, mode=None): def mkdir(path, uid=None, gid=None, mode=None):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment