Commit bc48619b authored by Antoine Catton's avatar Antoine Catton

Use archived rootfs

parent 3282889c
...@@ -28,26 +28,60 @@ ...@@ -28,26 +28,60 @@
import os import os
import urllib import urllib
import hashlib import hashlib
import tempfile
import shutil
import subprocess
from slapos.recipe.librecipe import GenericBaseRecipe from slapos.recipe.librecipe import GenericBaseRecipe
BUFFER_SIZE = 1024 BUFFER_SIZE = 1024
def service(args): def service(args):
environ = os.environ.copy()
environ.update(PATH=args['path'])
if not os.path.exists(args['confirm']): if not os.path.exists(args['confirm']):
urllib.urlretrieve(args['url'], args['output']) tmpdir = tempfile.mkdtemp()
try:
if args['md5'] is not None: # XXX: Hardcoded path
# XXX: Find a better way to do a md5sum tmpoutput = os.path.join(tmpdir, 'downloaded')
md5sum = hashlib.md5() urllib.urlretrieve(args['url'], tmpoutput)
with open(args['output'], 'r') as output:
file_buffer = output.read(BUFFER_SIZE) if args['md5'] is not None:
while len(file_buffer) > 0: # XXX: we need to find a better way to do a md5sum
md5sum.update(file_buffer) md5sum = hashlib.md5()
with open(args['output'], 'r') as output:
file_buffer = output.read(BUFFER_SIZE) file_buffer = output.read(BUFFER_SIZE)
while len(file_buffer) > 0:
md5sum.update(file_buffer)
file_buffer = output.read(BUFFER_SIZE)
if args['md5'] != md5sum.hexdigest():
return 127 # Not-null return code
if not args['archive']:
os.rename(tmpoutput, args['output'])
else:
# XXX: hardcoding path
extract_dir = os.path.join(tmpdir, 'extract')
os.mkdir(extract_dir)
subprocess.check_call(
['tar', '-x', '-f', tmpoutput,
'-C', extract_dir,
],
env=environ,
)
archive_content = os.listdir(extract_dir)
if len(archive_content) == 1 and \
os.path.isfile(os.path.join(extract_dir,
archive_content[0])):
os.rename(os.path.join(extract_dir,
archive_content[0]),
args['output'])
else:
return 127 # Not-null return code
if args['md5'] != md5sum.hexdigest(): finally:
return 127 # Not-null return code shutil.rmtree(tmpdir)
# Just a touch on args['confirm'] file # Just a touch on args['confirm'] file
open(args['confirm'], 'w').close() open(args['confirm'], 'w').close()
...@@ -65,16 +99,20 @@ class Recipe(GenericBaseRecipe): ...@@ -65,16 +99,20 @@ class Recipe(GenericBaseRecipe):
if len(md5sum) == 0: if len(md5sum) == 0:
md5sum = None md5sum = None
keywords = {
'url': self.options['url'],
'md5': md5sum,
'output': self.options['downloaded-file'],
'confirm': self.options['downloaded-file-complete'],
'archive': self.optionIsTrue('archive', False),
}
if keywords['archive']:
keywords['path'] = self.options['path']
path_list.append( path_list.append(
self.createPythonScript( self.createPythonScript(
self.options['binary'], self.options['binary'],
'slapos.recipe.downloader.service', 'slapos.recipe.downloader.service',
{ keywords,
'url': self.options['url'],
'md5': md5sum,
'output': self.options['downloaded-file'],
'confirm': self.options['downloaded-file-complete']
}
) )
) )
......
...@@ -53,7 +53,8 @@ md5sum = $${slap-parameter:rootfs-md5sum} ...@@ -53,7 +53,8 @@ md5sum = $${slap-parameter:rootfs-md5sum}
downloaded-file = $${rootdirectory:srv}/rootfs.img downloaded-file = $${rootdirectory:srv}/rootfs.img
downloaded-file-complete = $${:downloaded-file}.complete downloaded-file-complete = $${:downloaded-file}.complete
binary = $${basedirectory:services}/rootfsdownload binary = $${basedirectory:services}/rootfsdownload
wget-binary = ${wget:location}/bin/wget path = ${tar:location}/bin/:${gzip:location}/bin/:${bzip2:location}/bin/:${xz-utils:location}/bin/
archive = true
[lxc-conf] [lxc-conf]
recipe = slapos.cookbook:template recipe = slapos.cookbook:template
......
...@@ -5,10 +5,10 @@ extends = ...@@ -5,10 +5,10 @@ extends =
../../component/lxc/buildout.cfg ../../component/lxc/buildout.cfg
../../component/lxml-python/buildout.cfg ../../component/lxml-python/buildout.cfg
../../component/curl/buildout.cfg ../../component/curl/buildout.cfg
../../component/wget/buildout.cfg
../../component/gzip/buildout.cfg ../../component/gzip/buildout.cfg
../../component/bzip2/buildout.cfg ../../component/bzip2/buildout.cfg
../../component/xz-utils/buildout.cfg ../../component/xz-utils/buildout.cfg
../../component/tar/buildout.cfg
../../component/shellinabox/buildout.cfg ../../component/shellinabox/buildout.cfg
../../component/pwgen/buildout.cfg ../../component/pwgen/buildout.cfg
...@@ -33,7 +33,7 @@ mode = 0644 ...@@ -33,7 +33,7 @@ mode = 0644
[template-lxc] [template-lxc]
recipe = slapos.recipe.template recipe = slapos.recipe.template
url = ${:_profile_base_location_}/instance-lxc.cfg url = ${:_profile_base_location_}/instance-lxc.cfg
md5sum = 2d0975e08aa402a9aec325cfc03229d2 md5sum = 93791cb9eb71236c94273167d9b7df33
output = ${buildout:directory}/template-lxc.cfg output = ${buildout:directory}/template-lxc.cfg
mode = 0644 mode = 0644
......
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