<s> import os <EOL> import json <EOL> import subprocess <EOL> import trigger . config as config <EOL> import trigger . drivers as drivers <EOL> import redis <EOL> from datetime import datetime <EOL> from trigger . drivers import SyncDriverError <EOL> from trigger . drivers import LockDriverError <EOL> from trigger . drivers import ServiceDriverError <EOL> from trigger . drivers import ReportDriverError <EOL> LOG = config . LOG <EOL> class SyncDriver ( drivers . SyncDriver ) : <EOL> def __init__ ( self , conf ) : <EOL> self . conf = conf <EOL> self . _deploy_dir = os . path . join ( self . conf . repo . git_dir , <EOL> '<STR_LIT>' ) <EOL> self . _deploy_file = os . path . join ( self . _deploy_dir , '<STR_LIT>' ) <EOL> self . _report_driver = conf . drivers [ '<STR_LIT>' ] <EOL> def get_config ( self ) : <EOL> return { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:default>' : False <EOL> } <EOL> } <EOL> def _write_deploy_file ( self , tag ) : <EOL> timestamp = datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> tag_info = { <EOL> '<STR_LIT>' : tag . name , <EOL> '<STR_LIT>' : timestamp , <EOL> '<STR_LIT:time>' : timestamp , <EOL> '<STR_LIT:user>' : self . conf . config [ '<STR_LIT>' ] , <EOL> } <EOL> try : <EOL> f = open ( self . _deploy_file , '<STR_LIT>' ) <EOL> f . write ( json . dumps ( tag_info ) ) <EOL> f . close ( ) <EOL> except OSError : <EOL> raise SyncDriverError ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> def _update_server_info ( self , tag ) : <EOL> p = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> cwd = self . _deploy_dir , <EOL> stderr = subprocess . PIPE ) <EOL> p . communicate ( ) <EOL> if self . conf . config [ '<STR_LIT>' ] : <EOL> p = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , tag . name ] , <EOL> cwd = self . conf . repo . working_dir , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE ) <EOL> p . communicate ( ) <EOL> p = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> cwd = self . conf . repo . working_dir , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE ) <EOL> p . communicate ( ) <EOL> def _fetch ( self , args ) : <EOL> repo_name = self . conf . config [ '<STR_LIT>' ] <EOL> p = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , repo_name ] , <EOL> stdout = subprocess . PIPE ) <EOL> p . communicate ( ) <EOL> def _checkout ( self , args ) : <EOL> repo_name = self . conf . config [ '<STR_LIT>' ] <EOL> p = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , repo_name + '<STR_LIT:U+002C>' + str ( args . force ) ] , <EOL> stdout = subprocess . PIPE ) <EOL> p . communicate ( ) <EOL> def _ask ( self , stage , args , tag ) : <EOL> self . _report_driver . report_sync ( tag , <EOL> report_type = stage ) <EOL> while True : <EOL> answer = raw_input ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not answer or answer == "<STR_LIT:c>" or answer == "<STR_LIT:C>" : <EOL> self . _report_driver . report_sync ( tag , <EOL> report_type = stage ) <EOL> elif answer == "<STR_LIT:d>" or answer == "<STR_LIT:D>" : <EOL> self . _report_driver . report_sync ( tag , <EOL> report_type = stage , <EOL> detailed = True ) <EOL> elif answer == "<STR_LIT:Y>" or answer == "<STR_LIT:y>" : <EOL> return True <EOL> elif answer == "<STR_LIT:N>" or answer == "<STR_LIT:n>" : <EOL> return False <EOL> elif answer == "<STR_LIT:R>" or answer == "<STR_LIT:r>" : <EOL> if stage == "<STR_LIT>" : <EOL> self . _fetch ( args ) <EOL> if stage == "<STR_LIT>" : <EOL> self . _checkout ( args ) <EOL> def sync ( self , tag , args ) : <EOL> self . _write_deploy_file ( tag ) <EOL> self . _update_server_info ( tag ) <EOL> self . _fetch ( args ) <EOL> if not self . _ask ( '<STR_LIT>' , args , tag . name ) : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise SyncDriverError ( msg , <NUM_LIT:2> ) <EOL> self . _checkout ( args ) <EOL> if not self . _ask ( '<STR_LIT>' , args , tag . name ) : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise SyncDriverError ( msg , <NUM_LIT:3> ) <EOL> def get_deploy_info ( self ) : <EOL> try : <EOL> f = open ( self . _deploy_file , '<STR_LIT:r>' ) <EOL> deploy_info = json . loads ( f . read ( ) ) <EOL> f . close ( ) <EOL> return deploy_info <EOL> except OSError : <EOL> raise SyncDriverError ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> class LockDriver ( drivers . LockDriver ) : <EOL> def __init__ ( self , conf ) : <EOL> self . conf = conf <EOL> self . _deploy_dir = os . path . join ( self . conf . repo . git_dir , <EOL> '<STR_LIT>' ) <EOL> self . _lock_file = os . path . join ( self . _deploy_dir , '<STR_LIT>' ) <EOL> self . _create_deploy_dir ( ) <EOL> def _create_deploy_dir ( self ) : <EOL> if os . path . isdir ( self . _deploy_dir ) : <EOL> return <EOL> try : <EOL> os . mkdir ( self . _deploy_dir ) <EOL> except OSError : <EOL> raise LockDriverError ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> def add_lock ( self , args ) : <EOL> timestamp = datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> lock_info = { <EOL> '<STR_LIT:time>' : timestamp , <EOL> '<STR_LIT:user>' : self . conf . config [ '<STR_LIT>' ] , <EOL> } <EOL> try : <EOL> f = open ( self . _lock_file , '<STR_LIT>' ) <EOL> f . write ( json . dumps ( lock_info ) ) <EOL> f . close ( ) <EOL> except ( OSError , IOError ) : <EOL> raise LockDriverError ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> def remove_lock ( self , args ) : <EOL> try : <EOL> os . remove ( self . _lock_file ) <EOL> except OSError : <EOL> raise LockDriverError ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> def check_lock ( self , args ) : <EOL> try : <EOL> f = open ( self . _lock_file , '<STR_LIT:r>' ) <EOL> lock_info = f . read ( ) <EOL> lock_info = json . loads ( lock_info ) <EOL> f . close ( ) <EOL> return lock_info <EOL> except ( OSError , IOError ) : <EOL> return { } <EOL> except ( KeyError , ValueError ) : <EOL> return { '<STR_LIT:user>' : None , '<STR_LIT:time>' : None } <EOL> class ServiceDriver ( drivers . ServiceDriver ) : <EOL> def __init__ ( self , conf ) : <EOL> self . conf = conf <EOL> def restart ( self , args ) : <EOL> repo_name = self . conf . config [ '<STR_LIT>' ] <EOL> p = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> repo_name + '<STR_LIT:U+002C>' + str ( args . batch ) ] , <EOL> stdout = subprocess . PIPE ) <EOL> out = p . communicate ( ) [ <NUM_LIT:0> ] <EOL> try : <EOL> minion_data = json . loads ( out ) <EOL> except ( ValueError , AttributeError ) : <EOL> msg = '<STR_LIT>' <EOL> raise ServiceDriverError ( msg . format ( out ) , <NUM_LIT:1> ) <EOL> minion_data = minion_data [ '<STR_LIT>' ] <EOL> if isinstance ( minion_data , basestring ) : <EOL> msg = '<STR_LIT>' <EOL> raise ServiceDriverError ( msg . format ( minion_data ) , <NUM_LIT:2> ) <EOL> for i in minion_data : <EOL> try : <EOL> for minion , data in i . items ( ) : <EOL> try : <EOL> LOG . info ( '<STR_LIT>' . format ( minion , data [ '<STR_LIT:status>' ] ) ) <EOL> except KeyError : <EOL> LOG . info ( '<STR_LIT>' . format ( minion ) ) <EOL> except AttributeError : <EOL> LOG . error ( '<STR_LIT>' ) <EOL> LOG . error ( '<STR_LIT:{}>' . format ( i ) ) <EOL> class ReportDriver ( drivers . ReportDriver ) : <EOL> def __init__ ( self , conf ) : <EOL> self . conf = conf <EOL> def _get_redis_serv ( self ) : <EOL> return redis . Redis ( host = '<STR_LIT:localhost>' , port = <NUM_LIT> , db = <NUM_LIT:0> ) <EOL> def _mins_ago ( self , now , timestamp ) : <EOL> if timestamp : <EOL> time = datetime . fromtimestamp ( float ( timestamp ) ) <EOL> delta = now - time <EOL> mins = delta . seconds / <NUM_LIT> <EOL> else : <EOL> mins = None <EOL> return mins <EOL> def _get_minion_data ( self , serv , repo , minion ) : <EOL> data = { } <EOL> now = datetime . now ( ) <EOL> minion_key = '<STR_LIT>' . format ( repo , minion ) <EOL> data [ '<STR_LIT>' ] = serv . hget ( minion_key , '<STR_LIT>' ) <EOL> fetch_checkin_timestamp = serv . hget ( minion_key , <EOL> '<STR_LIT>' ) <EOL> data [ '<STR_LIT>' ] = self . _mins_ago ( now , <EOL> fetch_checkin_timestamp ) <EOL> fetch_timestamp = serv . hget ( minion_key , '<STR_LIT>' ) <EOL> data [ '<STR_LIT>' ] = self . _mins_ago ( now , fetch_timestamp ) <EOL> data [ '<STR_LIT>' ] = serv . hget ( minion_key , '<STR_LIT>' ) <EOL> checkout_checkin_timestamp = serv . hget ( minion_key , <EOL> '<STR_LIT>' ) <EOL> _c_checkin_t = self . _mins_ago ( now , checkout_checkin_timestamp ) <EOL> data [ '<STR_LIT>' ] = _c_checkin_t <EOL> checkout_timestamp = serv . hget ( minion_key , '<STR_LIT>' ) <EOL> data [ '<STR_LIT>' ] = self . _mins_ago ( now , checkout_timestamp ) <EOL> data [ '<STR_LIT>' ] = serv . hget ( minion_key , '<STR_LIT>' ) <EOL> data [ '<STR_LIT>' ] = serv . hget ( minion_key , '<STR_LIT>' ) <EOL> restart_checkin_timestamp = serv . hget ( minion_key , <EOL> '<STR_LIT>' ) <EOL> _r_c_t = self . _mins_ago ( now , restart_checkin_timestamp ) <EOL> data [ '<STR_LIT>' ] = _r_c_t <EOL> data [ '<STR_LIT>' ] = serv . hget ( minion_key , '<STR_LIT>' ) <EOL> restart_timestamp = serv . hget ( minion_key , '<STR_LIT>' ) <EOL> data [ '<STR_LIT>' ] = self . _mins_ago ( now , restart_timestamp ) <EOL> return data <EOL> def report_sync ( self , tag , report_type = '<STR_LIT>' , detailed = False ) : <EOL> serv = self . _get_redis_serv ( ) <EOL> repo_name = self . conf . config [ '<STR_LIT>' ] <EOL> LOG . info ( '<STR_LIT>' . format ( repo_name ) ) <EOL> LOG . info ( '<STR_LIT>' . format ( tag ) ) <EOL> minions = serv . smembers ( '<STR_LIT>' . format ( repo_name ) ) <EOL> _fetch_info = self . _get_fetch_info ( serv , repo_name , minions , tag ) <EOL> _checkout_info = self . _get_checkout_info ( serv , repo_name , minions , tag ) <EOL> min_len = len ( minions ) <EOL> fetch_len = len ( _fetch_info [ '<STR_LIT>' ] ) <EOL> checkout_len = len ( _checkout_info [ '<STR_LIT>' ] ) <EOL> fetch_report = "<STR_LIT>" <EOL> fetch_report = fetch_report . format ( fetch_len , min_len ) <EOL> checkout_report = "<STR_LIT>" <EOL> checkout_report = checkout_report . format ( checkout_len , min_len ) <EOL> if report_type == '<STR_LIT>' : <EOL> msgs = fetch_report <EOL> elif report_type == '<STR_LIT>' : <EOL> msgs = checkout_report <EOL> else : <EOL> msgs = '<STR_LIT>' . format ( fetch_report , checkout_report ) <EOL> LOG . info ( "<STR_LIT>" ) <EOL> LOG . info ( msgs ) <EOL> if detailed : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> LOG . info ( "<STR_LIT>" ) <EOL> LOG . info ( "<STR_LIT>" ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for minion in minions : <EOL> try : <EOL> data = _fetch_info [ '<STR_LIT>' ] [ minion ] <EOL> except KeyError : <EOL> if report_type == '<STR_LIT>' : <EOL> continue <EOL> data = _fetch_info [ '<STR_LIT>' ] [ minion ] <EOL> fetch_msg = msg . format ( '<STR_LIT>' , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] ) <EOL> try : <EOL> data = _checkout_info [ '<STR_LIT>' ] [ minion ] <EOL> except KeyError : <EOL> if report_type == '<STR_LIT>' : <EOL> continue <EOL> data = _checkout_info [ '<STR_LIT>' ] [ minion ] <EOL> checkout_msg = msg . format ( '<STR_LIT>' , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] ) <EOL> if report_type == '<STR_LIT>' : <EOL> msgs = '<STR_LIT>' . format ( fetch_msg ) <EOL> elif report_type == '<STR_LIT>' : <EOL> msgs = '<STR_LIT>' . format ( checkout_msg ) <EOL> else : <EOL> msgs = '<STR_LIT>' . format ( fetch_msg , checkout_msg ) <EOL> msgs = "<STR_LIT>" . format ( minion , msgs ) <EOL> LOG . info ( msgs ) <EOL> def _get_fetch_info ( self , serv , repo_name , minions , tag ) : <EOL> ret = { '<STR_LIT>' : { } , '<STR_LIT>' : { } } <EOL> for minion in minions : <EOL> data = self . _get_minion_data ( serv , repo_name , minion ) <EOL> if data [ '<STR_LIT>' ] == tag : <EOL> ret [ '<STR_LIT>' ] [ minion ] = data <EOL> else : <EOL> ret [ '<STR_LIT>' ] [ minion ] = data <EOL> return ret <EOL> def _get_checkout_info ( self , serv , repo_name , minions , tag ) : <EOL> ret = { '<STR_LIT>' : { } , '<STR_LIT>' : { } } <EOL> for minion in minions : <EOL> data = self . _get_minion_data ( serv , repo_name , minion ) <EOL> if data [ '<STR_LIT>' ] == tag : <EOL> ret [ '<STR_LIT>' ] [ minion ] = data <EOL> else : <EOL> ret [ '<STR_LIT>' ] [ minion ] = data <EOL> return ret </s>
