<s> import logging <EOL> import zope <EOL> import libxml2 <EOL> import json <EOL> import os <EOL> import os . path <EOL> import struct <EOL> import subprocess <EOL> import datetime <EOL> import random <EOL> import shutil <EOL> import oz . TDL <EOL> import tempfile <EOL> import tarfile <EOL> import threading <EOL> import subprocess <EOL> import hashlib <EOL> from xml . etree . ElementTree import fromstring <EOL> from imgfac . Template import Template <EOL> from imgfac . ApplicationConfiguration import ApplicationConfiguration <EOL> from imgfac . BuildDispatcher import BuildDispatcher <EOL> from imgfac . ImageFactoryException import ImageFactoryException <EOL> from imgfac . CloudDelegate import CloudDelegate <EOL> from imgfac . FactoryUtils import launch_inspect_and_mount , shutdown_and_close , remove_net_persist , create_cloud_info , parameter_cast_to_bool <EOL> class Docker ( object ) : <EOL> zope . interface . implements ( CloudDelegate ) <EOL> compress_commands = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> docker_json_template_0_11_1 = """<STR_LIT>""" <EOL> docker_json_template_1_0_0 = """<STR_LIT>""" <EOL> docker_json_template_1_7_0 = """<STR_LIT>""" <EOL> docker_json_template_1_10_1 = """<STR_LIT>""" <EOL> docker_templates_dict = { "<STR_LIT>" : docker_json_template_0_11_1 , <EOL> "<STR_LIT>" : docker_json_template_1_0_0 , <EOL> "<STR_LIT>" : docker_json_template_1_7_0 , <EOL> "<STR_LIT>" : docker_json_template_1_10_1 } <EOL> def __init__ ( self ) : <EOL> super ( Docker , self ) . __init__ ( ) <EOL> self . app_config = ApplicationConfiguration ( ) . configuration <EOL> self . log = logging . getLogger ( '<STR_LIT>' % ( __name__ , self . __class__ . __name__ ) ) <EOL> def activity ( self , activity ) : <EOL> self . log . debug ( activity ) <EOL> self . active_image . status_detail [ '<STR_LIT>' ] = activity <EOL> def push_image_to_provider ( self , builder , provider , credentials , target , target_image , parameters ) : <EOL> raise ImageFactoryException ( "<STR_LIT>" ) <EOL> def snapshot_image_on_provider ( self , builder , provider , credentials , template , parameters ) : <EOL> raise ImageFactoryException ( "<STR_LIT>" ) <EOL> def _generate_docker_id ( self ) : <EOL> did = "<STR_LIT>" <EOL> for i in range ( <NUM_LIT:8> ) : <EOL> did += "<STR_LIT>" % ( random . randint ( <NUM_LIT:0> , <NUM_LIT:2> ** <NUM_LIT:32> ) ) <EOL> return did <EOL> def _file_sha256 ( self , filename ) : <EOL> f = open ( filename , "<STR_LIT:rb>" ) <EOL> hasher = hashlib . sha256 ( ) <EOL> while True : <EOL> chunk = f . read ( <NUM_LIT:2> ** <NUM_LIT:20> ) <EOL> if not chunk : <EOL> break <EOL> hasher . update ( chunk ) <EOL> f . close ( ) <EOL> return hasher . hexdigest ( ) <EOL> def builder_should_create_target_image ( self , builder , target , image_id , template , parameters ) : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> tdlobj = oz . TDL . TDL ( xmlstring = template . xml , rootpw_required = self . app_config [ "<STR_LIT>" ] ) <EOL> wrap_metadata = parameter_cast_to_bool ( parameters . get ( '<STR_LIT>' , True ) ) <EOL> compress_type = parameters . get ( '<STR_LIT>' , None ) <EOL> if compress_type : <EOL> if compress_type in self . compress_commands . keys ( ) : <EOL> compress_command = self . compress_commands [ compress_type ] <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % ( compress_type ) ) <EOL> else : <EOL> compress_command = None <EOL> guestfs_handle = launch_inspect_and_mount ( builder . base_image . data , readonly = True ) <EOL> storagedir = os . path . dirname ( builder . target_image . data ) <EOL> tempdir = None <EOL> fuse_thread = None <EOL> try : <EOL> tempdir = tempfile . mkdtemp ( dir = storagedir ) <EOL> self . log . debug ( "<STR_LIT>" % ( tempdir ) ) <EOL> guestfs_handle . mount_local ( tempdir ) <EOL> def _run_guestmount ( g ) : <EOL> g . mount_local_run ( ) <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> fuse_thread = threading . Thread ( group = None , target = _run_guestmount , args = ( guestfs_handle , ) ) <EOL> fuse_thread . start ( ) <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> tarcmd = [ '<STR_LIT>' , '<STR_LIT>' , builder . target_image . data , '<STR_LIT>' , tempdir , '<STR_LIT>' , '<STR_LIT>' ] <EOL> tar_excludes = parameters . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> for exclude in tar_excludes : <EOL> tarcmd . append ( '<STR_LIT>' % ( exclude . strip ( ) ) ) <EOL> tarcmd . append ( '<STR_LIT>' ) <EOL> self . log . debug ( "<STR_LIT>" % ( str ( tarcmd ) ) ) <EOL> subprocess . check_call ( tarcmd ) <EOL> if wrap_metadata : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> size = <NUM_LIT:0> <EOL> for root , dirs , files in os . walk ( tempdir ) : <EOL> for name in files : <EOL> fp = os . path . join ( root , name ) <EOL> if os . path . isfile ( fp ) and not os . path . islink ( fp ) : <EOL> size += os . path . getsize ( fp ) <EOL> self . log . debug ( "<STR_LIT>" % ( size ) ) <EOL> except Exception , e : <EOL> self . log . exception ( e ) <EOL> raise <EOL> finally : <EOL> if tempdir : <EOL> try : <EOL> subprocess . check_call ( [ '<STR_LIT>' , '<STR_LIT>' , tempdir ] ) <EOL> os . rmdir ( tempdir ) <EOL> except Exception , e : <EOL> self . log . exception ( e ) <EOL> self . log . error ( "<STR_LIT>" % ( tempdir ) ) <EOL> if fuse_thread : <EOL> fuse_thread . join ( <NUM_LIT> ) <EOL> if fuse_thread . isAlive ( ) : <EOL> self . log . error ( "<STR_LIT>" % ( tempdir ) ) <EOL> if wrap_metadata : <EOL> repository = parameters . get ( '<STR_LIT>' , tdlobj . name ) . lower ( ) <EOL> tag = parameters . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> docker_image_id = parameters . get ( '<STR_LIT>' , self . _generate_docker_id ( ) ) <EOL> cmd = parameters . get ( '<STR_LIT>' , '<STR_LIT:null>' ) <EOL> env = parameters . get ( '<STR_LIT>' , '<STR_LIT:null>' ) <EOL> label = parameters . get ( '<STR_LIT>' , '<STR_LIT:null>' ) <EOL> rdict = { repository : { tag : docker_image_id } } <EOL> dockerversion = parameters . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not dockerversion in self . docker_templates_dict : <EOL> raise Exception ( "<STR_LIT>" % ( dockerversion ) ) <EOL> docker_json_template = self . docker_templates_dict [ dockerversion ] <EOL> arch = tdlobj . arch <EOL> if arch == "<STR_LIT>" : <EOL> arch = "<STR_LIT>" <EOL> elif arch == "<STR_LIT>" : <EOL> arch = "<STR_LIT>" <EOL> tdict = { } <EOL> tdict [ '<STR_LIT>' ] = parameters . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> tdict [ '<STR_LIT>' ] = parameters . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> tdict [ '<STR_LIT>' ] = datetime . datetime . utcnow ( ) . strftime ( '<STR_LIT>' ) <EOL> tdict [ '<STR_LIT>' ] = arch <EOL> tdict [ '<STR_LIT>' ] = docker_image_id <EOL> tdict [ '<STR_LIT>' ] = cmd <EOL> tdict [ '<STR_LIT>' ] = env <EOL> tdict [ '<STR_LIT:label>' ] = label <EOL> tdict [ '<STR_LIT:size>' ] = size <EOL> image_json = docker_json_template . format ( ** tdict ) <EOL> if dockerversion == "<STR_LIT>" : <EOL> shasum = self . _file_sha256 ( builder . target_image . data ) <EOL> image_v2_config = json . loads ( image_json ) <EOL> del image_v2_config [ '<STR_LIT>' ] <EOL> del image_v2_config [ '<STR_LIT:id>' ] <EOL> image_v2_config [ '<STR_LIT>' ] = [ { '<STR_LIT>' : image_v2_config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : image_v2_config [ '<STR_LIT>' ] } ] <EOL> image_v2_config [ '<STR_LIT>' ] = { '<STR_LIT>' : [ "<STR_LIT>" % ( shasum ) ] , <EOL> '<STR_LIT:type>' : '<STR_LIT>' } <EOL> image_v2_config_id = hashlib . sha256 ( json . dumps ( image_v2_config ) ) . hexdigest ( ) <EOL> image_v2_manifest = [ { "<STR_LIT>" : "<STR_LIT>" % ( image_v2_config_id ) , <EOL> "<STR_LIT>" : [ "<STR_LIT>" % ( docker_image_id ) ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" % ( repository , tag ) ] } ] <EOL> storagedir = os . path . dirname ( builder . target_image . data ) <EOL> tempdir = None <EOL> try : <EOL> tempdir = tempfile . mkdtemp ( dir = storagedir ) <EOL> self . log . debug ( "<STR_LIT>" % ( tempdir ) ) <EOL> repositories_path = os . path . join ( tempdir , '<STR_LIT>' ) <EOL> repositories = open ( repositories_path , "<STR_LIT:w>" ) <EOL> json . dump ( rdict , repositories ) <EOL> repositories . close ( ) <EOL> if dockerversion == "<STR_LIT>" : <EOL> config_path = os . path . join ( tempdir , '<STR_LIT>' % ( image_v2_config_id ) ) <EOL> config = open ( config_path , "<STR_LIT:w>" ) <EOL> json . dump ( image_v2_config , config ) <EOL> config . close ( ) <EOL> manifest_path = os . path . join ( tempdir , '<STR_LIT>' ) <EOL> manifest = open ( manifest_path , "<STR_LIT:w>" ) <EOL> json . dump ( image_v2_manifest , manifest ) <EOL> manifest . close ( ) <EOL> imagedir = os . path . join ( tempdir , docker_image_id ) <EOL> os . mkdir ( imagedir ) <EOL> jsonfile_path = os . path . join ( imagedir , '<STR_LIT>' ) <EOL> jsonfile = open ( jsonfile_path , '<STR_LIT:w>' ) <EOL> jsonfile . write ( image_json ) <EOL> jsonfile . close ( ) <EOL> versionfile_path = os . path . join ( imagedir , '<STR_LIT>' ) <EOL> versionfile = open ( versionfile_path , '<STR_LIT:w>' ) <EOL> versionfile . write ( "<STR_LIT:1.0>" ) <EOL> versionfile . close ( ) <EOL> layerfile_path = os . path . join ( imagedir , '<STR_LIT>' ) <EOL> shutil . move ( builder . target_image . data , layerfile_path ) <EOL> outtar = tarfile . TarFile ( name = builder . target_image . data , mode = "<STR_LIT:w>" ) <EOL> outtar . add ( imagedir , arcname = docker_image_id ) <EOL> outtar . add ( repositories_path , arcname = '<STR_LIT>' ) <EOL> if dockerversion == "<STR_LIT>" : <EOL> outtar . add ( config_path , arcname = '<STR_LIT>' % ( image_v2_config_id ) ) <EOL> outtar . add ( manifest_path , arcname = '<STR_LIT>' ) <EOL> outtar . close ( ) <EOL> finally : <EOL> if tempdir : <EOL> try : <EOL> shutil . rmtree ( tempdir ) <EOL> except : <EOL> self . log . warning ( "<STR_LIT>" % ( tempdir ) ) <EOL> if compress_command : <EOL> self . log . debug ( "<STR_LIT>" % ( compress_type ) ) <EOL> rawimage = builder . target_image . data <EOL> compimage = builder . target_image . data + "<STR_LIT>" % ( compress_type ) <EOL> result = subprocess . call ( compress_command % ( rawimage , compimage ) , shell = True ) <EOL> if result : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> os . unlink ( rawimage ) <EOL> os . rename ( compimage , rawimage ) <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> return False <EOL> def builder_will_create_target_image ( self , builder , target , image_id , template , parameters ) : <EOL> raise ImageFactoryException ( "<STR_LIT>" ) <EOL> def builder_did_create_target_image ( self , builder , target , image_id , template , parameters ) : <EOL> raise ImageFactoryException ( "<STR_LIT>" ) </s>
