<s> from __future__ import absolute_import , print_function <EOL> '''<STR_LIT>''' <EOL> import os <EOL> import logging <EOL> import traceback <EOL> import difflib <EOL> import salt . client <EOL> import salt . payload <EOL> import salt . utils <EOL> import salt . output <EOL> import salt . minion <EOL> from salt . utils . odict import OrderedDict <EOL> from mc_states import api <EOL> from mc_states import saltapi <EOL> six = saltapi . six <EOL> log = logging . getLogger ( __name__ ) <EOL> def _noop ( * a , ** kw ) : <EOL> return True <EOL> def master_opts ( * args , ** kwargs ) : <EOL> if not kwargs : <EOL> kwargs = { } <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : __opts__ . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : __opts__ . get ( '<STR_LIT>' , None ) , <EOL> } ) <EOL> return saltapi . _master_opts ( * args , ** kwargs ) <EOL> def cli ( * args , ** kwargs ) : <EOL> return __salt__ [ '<STR_LIT>' ] ( * args , ** kwargs ) <EOL> def _errmsg ( msg ) : <EOL> raise saltapi . MessageError ( msg ) <EOL> def get_ver ( origin ) : <EOL> try : <EOL> with open ( os . path . join ( origin , '<STR_LIT>' ) ) as fic : <EOL> old_ver = int ( fic . read ( ) ) <EOL> except Exception : <EOL> old_ver = <NUM_LIT:0> <EOL> return old_ver <EOL> def test_same_versions ( origin , destination , force = False ) : <EOL> if force : <EOL> return False <EOL> old_ver = get_ver ( origin ) <EOL> dold_ver = get_ver ( destination ) <EOL> return dold_ver == old_ver <EOL> def get__salt__ ( __salt__from_exec = None ) : <EOL> if __salt__from_exec is None : <EOL> return __salt__ <EOL> try : <EOL> for i in [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] : <EOL> __salt__from_exec [ i ] <EOL> except KeyError : <EOL> __salt__from_exec . update ( <EOL> { '<STR_LIT>' : _noop , <EOL> '<STR_LIT>' : ( <EOL> __salt__from_exec [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( <EOL> __salt__from_exec [ '<STR_LIT>' ] ) } ) <EOL> return __salt__from_exec <EOL> def snapshot_container ( cmd_runner , destination ) : <EOL> cmd = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( destination ) <EOL> cret = cmd_runner ( cmd ) <EOL> return cret <EOL> def get_cmd ( _cmd_runner = None ) : <EOL> if _cmd_runner is None : <EOL> def _cmd_runner ( cmd ) : <EOL> return cli ( '<STR_LIT>' , cmd , python_shell = True ) <EOL> return _cmd_runner <EOL> def sync_container ( origin , destination , <EOL> cmd_runner = None , ret = None , <EOL> __salt__from_exec = None , <EOL> snapshot = True , <EOL> force = False ) : <EOL> _s = get__salt__ ( __salt__from_exec ) <EOL> if ret is None : <EOL> ret = saltapi . result ( ) <EOL> cmd_runner = get_cmd ( cmd_runner ) <EOL> fname = '<STR_LIT>' <EOL> _s [ '<STR_LIT>' ] ( <EOL> '<STR_LIT:start>' , fname , origin , destination , force = force ) <EOL> if os . path . exists ( origin ) and os . path . exists ( destination ) : <EOL> if test_same_versions ( origin , destination , force = force ) : <EOL> return ret <EOL> cmd = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( origin , destination ) <EOL> cret = cmd_runner ( cmd ) <EOL> if cret [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' . format ( <EOL> origin , destination ) ) <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> if snapshot : <EOL> cret = snapshot_container ( cmd_runner , destination ) <EOL> if cret [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' . format ( <EOL> destination ) ) <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> _s [ '<STR_LIT>' ] ( '<STR_LIT:end>' , fname , ret = ret ) <EOL> return ret <EOL> def clean_lxc_config ( container , rootfs = None , fstab = None ) : <EOL> if not rootfs : <EOL> rootfs = '<STR_LIT>' . format ( container ) <EOL> if not fstab : <EOL> fstab = '<STR_LIT>' . format ( container ) <EOL> config = os . path . join ( os . path . dirname ( rootfs ) , '<STR_LIT>' ) <EOL> if os . path . exists ( config ) : <EOL> lines = [ ] <EOL> ocontent = [ ] <EOL> with open ( config ) as fic : <EOL> ocontent = fic . readlines ( ) <EOL> for i in ocontent : <EOL> if '<STR_LIT>' in i : <EOL> i = '<STR_LIT>' . format ( container ) <EOL> if '<STR_LIT>' in i : <EOL> i = '<STR_LIT>' . format ( rootfs ) <EOL> if '<STR_LIT>' in i : <EOL> i = '<STR_LIT>' . format ( fstab ) <EOL> if ( <EOL> ( '<STR_LIT>' in i ) or <EOL> ( '<STR_LIT>' in i ) or <EOL> ( '<STR_LIT>' in i ) or <EOL> ( '<STR_LIT>' in i ) <EOL> ) : <EOL> continue <EOL> if i . strip ( ) : <EOL> lines . append ( i ) <EOL> content = '<STR_LIT>' . join ( lines ) <EOL> if ( lines != ocontent ) and content : <EOL> log . info ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( config ) ) <EOL> log . info ( '<STR_LIT>' ) <EOL> for line in difflib . unified_diff ( ocontent , lines ) : <EOL> log . info ( line . strip ( ) ) <EOL> with open ( config , '<STR_LIT:w>' ) as fic : <EOL> fic . write ( content ) <EOL> def sync_image_reference_containers ( imgSettings , ret , _cmd_runner = None , <EOL> force = False , __salt__from_exec = None ) : <EOL> _s = get__salt__ ( __salt__from_exec ) <EOL> fname = '<STR_LIT>' <EOL> _s [ '<STR_LIT>' ] ( '<STR_LIT:start>' , fname ) <EOL> _cmd_runner = get_cmd ( _cmd_runner ) <EOL> for img in imgSettings [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> bref = imgSettings [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ img ] [ '<STR_LIT>' ] <EOL> rootfs = '<STR_LIT>' . format ( img ) <EOL> sync_container ( '<STR_LIT>' . format ( bref ) , <EOL> rootfs , <EOL> _cmd_runner , ret , <EOL> __salt__from_exec = __salt__from_exec , <EOL> force = force ) <EOL> clean_lxc_config ( img ) <EOL> sync_container ( '<STR_LIT>' . format ( bref ) , <EOL> '<STR_LIT>' . format ( img ) , <EOL> _cmd_runner , ret , <EOL> __salt__from_exec = __salt__from_exec , <EOL> force = force ) <EOL> _s [ '<STR_LIT>' ] ( '<STR_LIT:end>' , fname , ret = ret ) <EOL> def sync_images ( only = None , <EOL> force = False , <EOL> output = True , <EOL> force_output = False , <EOL> __salt__from_exec = None ) : <EOL> '''<STR_LIT>''' <EOL> fname = '<STR_LIT>' <EOL> _s = get__salt__ ( __salt__from_exec ) <EOL> if not only : <EOL> only = [ ] <EOL> if isinstance ( only , basestring ) : <EOL> only = only . split ( '<STR_LIT:U+002C>' ) <EOL> ret = saltapi . result ( ) <EOL> ret [ '<STR_LIT>' ] = OrderedDict ( ) <EOL> dest = '<STR_LIT>' <EOL> this_ = saltapi . get_local_target ( ) <EOL> orig = '<STR_LIT>' <EOL> imgSettings = _s [ '<STR_LIT>' ] ( ) <EOL> controllersettings = _s [ '<STR_LIT>' ] ( ) <EOL> rsync_cmd = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> sync_image_reference_containers ( imgSettings , ret , force = force ) <EOL> root = master_opts ( ) [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> lxctargets = [ ] <EOL> for vm , vmdata in controllersettings [ '<STR_LIT>' ] . items ( ) : <EOL> if ( <EOL> ( vmdata [ '<STR_LIT>' ] == '<STR_LIT>' ) <EOL> and ( vmdata [ '<STR_LIT:target>' ] not in lxctargets ) <EOL> ) : <EOL> lxctargets . append ( vmdata [ '<STR_LIT:target>' ] ) <EOL> for target in lxctargets : <EOL> if only and ( target not in only ) : <EOL> continue <EOL> subret = saltapi . result ( ) <EOL> ret [ '<STR_LIT>' ] [ target ] = subret <EOL> try : <EOL> host = cli ( '<STR_LIT>' , '<STR_LIT>' , salt_target = target ) <EOL> if not host : <EOL> host = target <EOL> else : <EOL> host = host [ '<STR_LIT>' ] <EOL> cret = cli ( '<STR_LIT>' , '<STR_LIT:root>' , host , salt_target = target ) <EOL> pubkey = os . path . join ( root , '<STR_LIT>' ) <EOL> with open ( '<STR_LIT>' ) as fic : <EOL> with open ( pubkey , '<STR_LIT:w>' ) as sshkey : <EOL> sshkey . write ( fic . read ( ) ) <EOL> cret = cli ( '<STR_LIT>' , '<STR_LIT:root>' , orig ) <EOL> if not cret [ [ a for a in cret ] [ <NUM_LIT:0> ] ] in [ '<STR_LIT>' ] : <EOL> cret = cli ( '<STR_LIT>' , orig , dest ) <EOL> cret = cli ( '<STR_LIT>' , <EOL> '<STR_LIT:root>' , '<STR_LIT>' . format ( dest ) ) <EOL> if cret not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> _errmsg ( <EOL> '<STR_LIT>' . format ( cret ) ) <EOL> cret = cli ( '<STR_LIT>' , <EOL> '<STR_LIT:root>' , orig , salt_target = target ) <EOL> if not cret [ [ a for a in cret ] [ <NUM_LIT:0> ] ] in [ '<STR_LIT>' ] : <EOL> cret = cli ( '<STR_LIT>' , orig , dest , salt_target = target ) <EOL> cret = cli ( '<STR_LIT>' , <EOL> '<STR_LIT:root>' , '<STR_LIT>' . format ( dest ) , <EOL> salt_target = target ) <EOL> if cret not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> _errmsg ( <EOL> '<STR_LIT>' . format ( cret ) ) <EOL> for img in imgSettings [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> timeout = <NUM_LIT:3> * <NUM_LIT> * <NUM_LIT> <EOL> imgroot = os . path . join ( imgSettings [ '<STR_LIT>' ] [ '<STR_LIT>' ] , img ) <EOL> try : <EOL> if not os . path . exists ( imgroot ) : <EOL> _errmsg ( <EOL> '<STR_LIT>' . format ( img ) ) <EOL> lver = get_ver ( imgroot + '<STR_LIT>' ) <EOL> cmd = ( <EOL> '<STR_LIT>' <EOL> ) . format ( imgroot ) <EOL> cret = cli ( '<STR_LIT>' , cmd , python_shell = True , <EOL> salt_timeout = timeout ) <EOL> if cret [ '<STR_LIT>' ] . strip ( ) > '<STR_LIT:0>' : <EOL> _errmsg ( <EOL> '<STR_LIT>' ) <EOL> o = imgroot + "<STR_LIT>" <EOL> d = imgroot + '<STR_LIT>' <EOL> if not test_same_versions ( o , d , force = force ) : <EOL> cmd = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( imgroot , rsync_cmd ) <EOL> cret = cli ( '<STR_LIT>' , cmd , python_shell = True , <EOL> salt_target = target ) <EOL> if not cret [ '<STR_LIT>' ] : <EOL> subret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' <EOL> ) . format ( target ) <EOL> if cret [ '<STR_LIT>' ] . strip ( ) : <EOL> subret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' . format ( <EOL> cret [ '<STR_LIT>' ] , target ) ) <EOL> do_sync = True <EOL> try : <EOL> cmd = '<STR_LIT>' . format ( imgroot ) <EOL> ver = int ( cli ( '<STR_LIT>' , cmd , python_shell = True , <EOL> salt_timeout = timeout , salt_target = host ) ) <EOL> if lver == ver : <EOL> do_sync = False <EOL> except Exception : <EOL> pass <EOL> good = True <EOL> if do_sync : <EOL> cmd = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) . format ( imgroot , host , rsync_cmd ) <EOL> cret = cli ( '<STR_LIT>' , cmd , python_shell = True , <EOL> salt_timeout = timeout ) <EOL> good = not cret [ '<STR_LIT>' ] <EOL> if good : <EOL> subret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' <EOL> ) . format ( target ) <EOL> if cret [ '<STR_LIT>' ] . strip ( ) : <EOL> subret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' <EOL> ) . format ( cret [ '<STR_LIT>' ] , target ) <EOL> do_sync = True <EOL> try : <EOL> cmd = '<STR_LIT>' . format ( imgroot ) <EOL> dver = int ( cli ( '<STR_LIT>' , cmd , python_shell = True , <EOL> salt_timeout = timeout , <EOL> salt_target = host ) ) <EOL> if dver == lver : <EOL> do_sync = False <EOL> else : <EOL> do_sync = True <EOL> except Exception : <EOL> pass <EOL> good = True <EOL> if do_sync : <EOL> cmd = ( <EOL> '<STR_LIT>' <EOL> ) . format ( imgroot , rsync_cmd ) <EOL> cret = cli ( '<STR_LIT>' , cmd , python_shell = True , <EOL> salt_target = target ) <EOL> good = not cret [ '<STR_LIT>' ] <EOL> if good : <EOL> subret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' <EOL> ) . format ( target ) <EOL> if cret [ '<STR_LIT>' ] . strip ( ) : <EOL> subret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' <EOL> ) . format ( cret [ '<STR_LIT>' ] , target ) <EOL> else : <EOL> _errmsg ( <EOL> '<STR_LIT>' ) <EOL> else : <EOL> _errmsg ( <EOL> '<STR_LIT>' ) <EOL> except saltapi . MessageError , ex : <EOL> subret [ '<STR_LIT>' ] += '<STR_LIT>' . format ( ex ) <EOL> subret [ '<STR_LIT:result>' ] = False <EOL> subret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' <EOL> ) . format ( target , imgroot ) <EOL> except saltapi . MessageError , ex : <EOL> subret [ '<STR_LIT>' ] += '<STR_LIT>' <EOL> subret [ '<STR_LIT:result>' ] = False <EOL> subret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' . format ( <EOL> target , ex ) ) <EOL> except Exception , ex : <EOL> trace = traceback . format_exc ( ) <EOL> subret [ '<STR_LIT>' ] += trace <EOL> subret [ '<STR_LIT:result>' ] = False <EOL> subret [ '<STR_LIT>' ] += ( <EOL> '<STR_LIT>' . format ( <EOL> target , ex ) ) <EOL> for target , subret in ret [ '<STR_LIT>' ] . items ( ) : <EOL> if only and ( target not in only ) : <EOL> continue <EOL> api . msplitstrip ( subret ) <EOL> if not subret [ '<STR_LIT:result>' ] : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> if ret [ '<STR_LIT:result>' ] : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> api . msplitstrip ( ret ) <EOL> if force_output or ( output and not ret [ '<STR_LIT:result>' ] ) : <EOL> salt . output . display_output ( ret , '<STR_LIT>' , __opts__ ) <EOL> _s [ '<STR_LIT>' ] ( '<STR_LIT:end>' , fname , ret = ret ) <EOL> return ret </s>
