<s> """<STR_LIT>""" <EOL> import json <EOL> import logging <EOL> import tempfile <EOL> import datetime <EOL> import traceback <EOL> from atomic_reactor . build import InsideBuilder <EOL> from atomic_reactor . plugin import PostBuildPluginsRunner , PreBuildPluginsRunner , InputPluginsRunner , PrePublishPluginsRunner , ExitPluginsRunner , PluginFailedException , AutoRebuildCanceledException <EOL> from atomic_reactor . source import get_source_instance_for <EOL> from atomic_reactor . util import ImageName <EOL> logger = logging . getLogger ( __name__ ) <EOL> class BuildResults ( object ) : <EOL> build_logs = None <EOL> dockerfile = None <EOL> built_img_inspect = None <EOL> built_img_info = None <EOL> base_img_inspect = None <EOL> base_img_info = None <EOL> base_plugins_output = None <EOL> built_img_plugins_output = None <EOL> container_id = None <EOL> return_code = None <EOL> class BuildResultsEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , BuildResults ) : <EOL> return { <EOL> '<STR_LIT>' : obj . build_logs , <EOL> '<STR_LIT>' : obj . built_img_inspect , <EOL> '<STR_LIT>' : obj . built_img_info , <EOL> '<STR_LIT>' : obj . base_img_info , <EOL> '<STR_LIT>' : obj . base_plugins_output , <EOL> '<STR_LIT>' : obj . built_img_plugins_output , <EOL> } <EOL> return json . JSONEncoder . default ( self , obj ) <EOL> class BuildResultsJSONDecoder ( json . JSONDecoder ) : <EOL> def decode ( self , obj ) : <EOL> d = super ( BuildResultsJSONDecoder , self ) . decode ( obj ) <EOL> results = BuildResults ( ) <EOL> results . built_img_inspect = d . get ( '<STR_LIT>' , None ) <EOL> results . built_img_info = d . get ( '<STR_LIT>' , None ) <EOL> results . base_img_info = d . get ( '<STR_LIT>' , None ) <EOL> results . base_plugins_output = d . get ( '<STR_LIT>' , None ) <EOL> results . built_img_plugins_output = d . get ( '<STR_LIT>' , None ) <EOL> return results <EOL> class TagConf ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _primary_images = [ ] <EOL> self . _unique_images = [ ] <EOL> @ property <EOL> def primary_images ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _primary_images <EOL> @ property <EOL> def images ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _primary_images + self . _unique_images <EOL> @ property <EOL> def unique_images ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _unique_images <EOL> def add_primary_image ( self , image ) : <EOL> """<STR_LIT>""" <EOL> self . _primary_images . append ( ImageName . parse ( image ) ) <EOL> def add_unique_image ( self , image ) : <EOL> """<STR_LIT>""" <EOL> self . _unique_images . append ( ImageName . parse ( image ) ) <EOL> def add_primary_images ( self , images ) : <EOL> """<STR_LIT>""" <EOL> for image in images : <EOL> self . add_primary_image ( image ) <EOL> class Registry ( object ) : <EOL> def __init__ ( self , uri , insecure = False ) : <EOL> """<STR_LIT>""" <EOL> self . uri = uri <EOL> self . insecure = insecure <EOL> class PulpRegistry ( Registry ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , crane_uri , insecure = False ) : <EOL> """<STR_LIT>""" <EOL> super ( PulpRegistry , self ) . __init__ ( crane_uri , insecure = insecure ) <EOL> self . name = name <EOL> class DockerRegistry ( Registry ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , uri , insecure = False ) : <EOL> """<STR_LIT>""" <EOL> super ( DockerRegistry , self ) . __init__ ( uri , insecure = insecure ) <EOL> self . digests = { } <EOL> class PushConf ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _registries = { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : { } , <EOL> } <EOL> def add_docker_registry ( self , registry_uri , insecure = False ) : <EOL> if registry_uri is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> r = DockerRegistry ( registry_uri , insecure = insecure ) <EOL> self . _registries [ "<STR_LIT>" ] . append ( r ) <EOL> return r <EOL> def add_docker_registries ( self , registry_uris , insecure = False ) : <EOL> for registry_uri in registry_uris : <EOL> self . add_docker_registry ( registry_uri , insecure = insecure ) <EOL> def add_pulp_registry ( self , name , crane_uri ) : <EOL> if crane_uri is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> r = PulpRegistry ( name , crane_uri ) <EOL> self . _registries [ "<STR_LIT>" ] [ name ] = r <EOL> return r <EOL> @ property <EOL> def has_some_docker_registry ( self ) : <EOL> return len ( self . docker_registries ) > <NUM_LIT:0> <EOL> @ property <EOL> def docker_registries ( self ) : <EOL> return self . _registries [ "<STR_LIT>" ] <EOL> @ property <EOL> def pulp_registries ( self ) : <EOL> return [ registry for registry in self . _registries [ "<STR_LIT>" ] . values ( ) ] <EOL> @ property <EOL> def all_registries ( self ) : <EOL> return self . docker_registries + self . pulp_registries <EOL> class DockerBuildWorkflow ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source , image , prebuild_plugins = None , prepublish_plugins = None , <EOL> postbuild_plugins = None , exit_plugins = None , plugin_files = None , <EOL> openshift_build_selflink = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . source = get_source_instance_for ( source , tmpdir = tempfile . mkdtemp ( ) ) <EOL> self . image = image <EOL> self . prebuild_plugins_conf = prebuild_plugins <EOL> self . prepublish_plugins_conf = prepublish_plugins <EOL> self . postbuild_plugins_conf = postbuild_plugins <EOL> self . exit_plugins_conf = exit_plugins <EOL> self . prebuild_results = { } <EOL> self . postbuild_results = { } <EOL> self . prepub_results = { } <EOL> self . exit_results = { } <EOL> self . plugins_timestamps = { } <EOL> self . plugins_durations = { } <EOL> self . plugins_errors = { } <EOL> self . autorebuild_canceled = False <EOL> self . build_failed = False <EOL> self . plugin_failed = False <EOL> self . plugin_files = plugin_files <EOL> self . kwargs = kwargs <EOL> self . builder = None <EOL> self . build_logs = [ ] <EOL> self . built_image_inspect = None <EOL> self . _base_image_inspect = None <EOL> self . pulled_base_images = set ( ) <EOL> self . exported_image_sequence = [ ] <EOL> self . tag_conf = TagConf ( ) <EOL> self . push_conf = PushConf ( ) <EOL> self . files = { } <EOL> self . openshift_build_selflink = openshift_build_selflink <EOL> if kwargs : <EOL> logger . warning ( "<STR_LIT>" , kwargs ) <EOL> @ property <EOL> def build_process_failed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . build_failed or self . plugin_failed <EOL> @ property <EOL> def base_image_inspect ( self ) : <EOL> if self . _base_image_inspect is None : <EOL> self . _base_image_inspect = self . builder . tasker . inspect_image ( self . builder . base_image ) <EOL> return self . _base_image_inspect <EOL> def build_docker_image ( self ) : <EOL> """<STR_LIT>""" <EOL> self . builder = InsideBuilder ( self . source , self . image ) <EOL> try : <EOL> logger . info ( "<STR_LIT>" ) <EOL> prebuild_runner = PreBuildPluginsRunner ( self . builder . tasker , self , self . prebuild_plugins_conf , <EOL> plugin_files = self . plugin_files ) <EOL> try : <EOL> prebuild_runner . run ( ) <EOL> except PluginFailedException as ex : <EOL> logger . error ( "<STR_LIT>" , ex ) <EOL> raise <EOL> except AutoRebuildCanceledException as ex : <EOL> logger . info ( str ( ex ) ) <EOL> self . autorebuild_canceled = True <EOL> raise <EOL> start_time = datetime . datetime . now ( ) <EOL> self . plugins_timestamps [ '<STR_LIT>' ] = start_time . isoformat ( ) <EOL> build_result = self . builder . build ( ) <EOL> try : <EOL> finish_time = datetime . datetime . now ( ) <EOL> duration = finish_time - start_time <EOL> seconds = duration . total_seconds ( ) <EOL> logger . debug ( "<STR_LIT>" , seconds ) <EOL> self . plugins_durations [ '<STR_LIT>' ] = seconds <EOL> except Exception : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> self . build_logs = build_result . logs <EOL> self . build_failed = build_result . is_failed ( ) <EOL> if build_result . is_failed ( ) : <EOL> self . plugins_errors [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return build_result <EOL> self . built_image_inspect = self . builder . inspect_built_image ( ) <EOL> prepublish_runner = PrePublishPluginsRunner ( self . builder . tasker , self , self . prepublish_plugins_conf , <EOL> plugin_files = self . plugin_files ) <EOL> try : <EOL> prepublish_runner . run ( ) <EOL> except PluginFailedException as ex : <EOL> logger . error ( "<STR_LIT>" , ex ) <EOL> raise <EOL> postbuild_runner = PostBuildPluginsRunner ( self . builder . tasker , self , self . postbuild_plugins_conf , <EOL> plugin_files = self . plugin_files ) <EOL> try : <EOL> postbuild_runner . run ( ) <EOL> except PluginFailedException as ex : <EOL> logger . error ( "<STR_LIT>" , ex ) <EOL> raise <EOL> return build_result <EOL> finally : <EOL> exit_runner = ExitPluginsRunner ( self . builder . tasker , self , <EOL> self . exit_plugins_conf , <EOL> plugin_files = self . plugin_files ) <EOL> try : <EOL> exit_runner . run ( keep_going = True ) <EOL> except PluginFailedException as ex : <EOL> logger . error ( "<STR_LIT>" , ex ) <EOL> raise <EOL> finally : <EOL> self . source . remove_tmpdir ( ) <EOL> def build_inside ( input_method , input_args = None , substitutions = None ) : <EOL> """<STR_LIT>""" <EOL> def process_keyvals ( keyvals ) : <EOL> """<STR_LIT>""" <EOL> keyvals = keyvals or [ ] <EOL> processed_keyvals = { } <EOL> for arg in keyvals : <EOL> key , value = arg . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> processed_keyvals [ key ] = value <EOL> return processed_keyvals <EOL> if not input_method : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> else : <EOL> logger . debug ( "<STR_LIT>" , input_method ) <EOL> cleaned_input_args = process_keyvals ( input_args ) <EOL> cleaned_subs = process_keyvals ( substitutions ) <EOL> cleaned_input_args [ '<STR_LIT>' ] = cleaned_subs <EOL> input_runner = InputPluginsRunner ( [ { '<STR_LIT:name>' : input_method , <EOL> '<STR_LIT:args>' : cleaned_input_args } ] ) <EOL> build_json = input_runner . run ( ) [ input_method ] <EOL> logger . debug ( "<STR_LIT>" , build_json ) <EOL> if not build_json : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> dbw = DockerBuildWorkflow ( ** build_json ) <EOL> build_result = dbw . build_docker_image ( ) <EOL> if not build_result or build_result . is_failed ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> else : <EOL> logger . info ( "<STR_LIT>" ) </s>
