<s> """<STR_LIT>""" <EOL> import abc <EOL> import logging <EOL> import os <EOL> import shutil <EOL> from glob import glob <EOL> from aminator . config import conf_action <EOL> from aminator . plugins . base import BasePlugin <EOL> from aminator . util import download_file <EOL> from aminator . util . linux import Chroot , monitor_command <EOL> from aminator . util . metrics import fails , lapse <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> log = logging . getLogger ( __name__ ) <EOL> class BaseProvisionerPlugin ( BasePlugin ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = abc . ABCMeta <EOL> _entry_point = '<STR_LIT>' <EOL> @ abc . abstractmethod <EOL> def _provision_package ( self ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractmethod <EOL> def _store_package_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> def _pre_chroot_block ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _post_chroot_block ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def add_plugin_args ( self , * args , ** kwargs ) : <EOL> context = self . _config . context <EOL> prov = self . _parser . add_argument_group ( title = '<STR_LIT>' ) <EOL> prov . add_argument ( "<STR_LIT>" , "<STR_LIT>" , dest = '<STR_LIT>' , help = "<STR_LIT>" , action = conf_action ( config = context . package , action = "<STR_LIT:store_true>" ) ) <EOL> def provision ( self ) : <EOL> context = self . _config . context <EOL> if self . _local_install ( ) : <EOL> log . info ( '<STR_LIT>' . format ( context . package . arg ) ) <EOL> context . package . local_install = True <EOL> if not self . _stage_pkg ( ) : <EOL> log . critical ( '<STR_LIT>' . format ( context . package . arg ) ) <EOL> return False <EOL> else : <EOL> log . info ( '<STR_LIT>' . format ( context . package . arg ) ) <EOL> context . package . local_install = False <EOL> log . debug ( '<STR_LIT>' ) <EOL> self . _pre_chroot_block ( ) <EOL> log . debug ( '<STR_LIT>' . format ( self . _distro . _mountpoint ) ) <EOL> with Chroot ( self . _distro . _mountpoint ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> result = self . _provision_package ( ) <EOL> if context . package . get ( '<STR_LIT>' , False ) : <EOL> os . system ( "<STR_LIT>" ) <EOL> if not result . success : <EOL> log . critical ( '<STR_LIT>' . format ( context . package . arg , result . result ) ) <EOL> return False <EOL> self . _store_package_metadata ( ) <EOL> if context . package . local_install and not context . package . get ( '<STR_LIT>' , False ) : <EOL> os . remove ( context . package . arg ) <EOL> scripts_dir = self . _config . plugins [ self . full_name ] . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> log . debug ( '<STR_LIT>' . format ( scripts_dir ) ) <EOL> if scripts_dir : <EOL> if not self . _run_provision_scripts ( scripts_dir ) : <EOL> return False <EOL> log . debug ( '<STR_LIT>' ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> self . _post_chroot_block ( ) <EOL> log . info ( '<STR_LIT>' ) <EOL> return True <EOL> @ fails ( "<STR_LIT>" ) <EOL> @ lapse ( "<STR_LIT>" ) <EOL> def _run_provision_scripts ( self , scripts_dir ) : <EOL> """<STR_LIT>""" <EOL> script_files = sorted ( glob ( scripts_dir + '<STR_LIT>' ) + glob ( scripts_dir + '<STR_LIT>' ) ) <EOL> if not script_files : <EOL> log . debug ( "<STR_LIT>" . format ( scripts_dir ) ) <EOL> else : <EOL> log . debug ( '<STR_LIT>' . format ( script_files , scripts_dir ) ) <EOL> for script in script_files : <EOL> log . debug ( '<STR_LIT>' . format ( script ) ) <EOL> if os . access ( script , os . X_OK ) : <EOL> result = run_script ( script ) <EOL> else : <EOL> if script . endswith ( '<STR_LIT>' ) : <EOL> result = run_script ( [ '<STR_LIT>' , script ] ) <EOL> else : <EOL> result = run_script ( [ '<STR_LIT>' , script ] ) <EOL> if not result . success : <EOL> log . critical ( "<STR_LIT>" . format ( script , result . result ) ) <EOL> return False <EOL> return True <EOL> def _local_install ( self ) : <EOL> """<STR_LIT>""" <EOL> config = self . _config <EOL> ext = config . plugins [ self . full_name ] . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not ext : <EOL> return False <EOL> ext = '<STR_LIT>' . format ( ext . lstrip ( '<STR_LIT:.>' ) ) <EOL> return config . context . package . arg . endswith ( ext ) <EOL> def _stage_pkg ( self ) : <EOL> """<STR_LIT>""" <EOL> context = self . _config . context <EOL> context . package . file = os . path . basename ( context . package . arg ) <EOL> context . package . full_path = os . path . join ( self . _distro . _mountpoint , context . package . dir . lstrip ( '<STR_LIT:/>' ) , context . package . file ) <EOL> try : <EOL> if any ( protocol in context . package . arg for protocol in [ '<STR_LIT>' , '<STR_LIT>' ] ) : <EOL> self . _download_pkg ( context ) <EOL> else : <EOL> self . _move_pkg ( context ) <EOL> except Exception : <EOL> errstr = '<STR_LIT>' <EOL> log . critical ( errstr ) <EOL> log . debug ( errstr , exc_info = True ) <EOL> return False <EOL> context . package . arg = os . path . join ( context . package . dir , context . package . file ) <EOL> return True <EOL> def _download_pkg ( self , context ) : <EOL> """<STR_LIT>""" <EOL> pkg_url = context . package . arg <EOL> dst_file_path = context . package . full_path <EOL> log . debug ( '<STR_LIT>' . format ( pkg_url , dst_file_path ) ) <EOL> download_file ( pkg_url , dst_file_path , context . package . get ( '<STR_LIT>' , <NUM_LIT:1> ) , verify_https = context . get ( '<STR_LIT>' , False ) ) <EOL> def _move_pkg ( self , context ) : <EOL> src_file = context . package . arg . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> dst_file_path = context . package . full_path <EOL> shutil . move ( src_file , dst_file_path ) <EOL> def __call__ ( self , distro ) : <EOL> self . _distro = distro <EOL> return self <EOL> def run_script ( script ) : <EOL> return monitor_command ( script ) </s>
