Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/landscape/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/landscape/watchdog.pyc


Tc@sldZddlZddlZddlZddlZddlZddlZddlmZm	Z	m
Z
ddlmZm
Z
ddlmZddlmZmZddlmZddlmZdd	lmZmZdd
lmZddlmZmZddlm Z dd
l!m"Z"ddl#m$Z$m%Z%m&Z&ddl'm(Z(ddl)m*Z*m+Z+m,Z,ddl-m.Z.dZ/dZ0dZ1dZ2de3fdYZ4de3fdYZ5de3fdYZ6de7fdYZ8de8fdYZ9de8fd YZ:d!e8fd"YZ;d#efd$YZ<d%e7fd&YZ=d'efd(YZ>d)Z?d*efd+YZ@e$e&d,d-d.d/e&d0d-d.d/e&d1d-d.d/e&d2d-d.d/e&d3d-d.d/e&d4d-d.d/e&d5d-d.d6e&d7d-d.d/e&d8d-d.d/e%d9d-d.d:g
ZAd;ZBejCdd<ZEdS(=sSee L{WatchDog}.

The WatchDog must run as root, because it spawns the Landscape Manager.

The main C{landscape-client} program uses this watchdog.
iN(twarningtinfoterror(t	setrlimitt
RLIMIT_NOFILE(treactor(tDeferredtsucceed(tProcessProtocol(tProcessExitedAlready(tServicetApplication(tstartApplication(tinit_loggingt
Configuration(tgather_results(tlog_failure(t
BootstrapListt
BootstrapFiletBootstrapDirectory(trotate_logs(tRemoteBrokerConnectortRemoteMonitorConnectortRemoteManagerConnector(tLandscapeReactori
iitDaemonErrorcBseZdZRS(s(One of the daemons could not be started.(t__name__t
__module__t__doc__(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR)stTimeoutErrorcBseZdZRS(sSomething took too long.(RRR(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR-stExecutableNotFoundErrorcBseZdZRS(sAn executable was not found.(RRR(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR1stDaemoncBseZdZdZdZdZdZee	ddZ
dZdZdZ
dZd	Zd
ZdZdZd
ZdZdZRS(sqA Landscape daemon which can be started and tracked.

    This class should be subclassed to specify individual daemon.

    @cvar program: The name of the executable program that will start this
        daemon.
    @cvar username: The name of the user to switch to, by default.
    @cvar service: The DBus service name that the program will be expected to
        listen on.
    @cvar max_retries: The maximum number of retries before giving up when
        trying to connect to the watched daemon.
    @cvar factor: The factor by which the delay between subsequent connection
        attempts will increase.

    @param connector: The L{ComponentConnector} of the daemon.
    @param reactor: The reactor used to spawn the process and schedule timed
        calls.
    @param verbose: Optionally, report more information when running this
        program.  Defaults to False.
    t	landscapeig?c	Cs'||_||_tjj|_tj}|dkrtj|j	}|j
}|j}||krx||_n	d|_|tjkr||_n	d|_|j|jd<|j	|jd<|j	|jd<nd|_d|_||_||_d|_d|_d|_t|_dS(NitHOMEtUSERtLOGNAME(t
_connectort_reactortostenvirontcopyt_envtgetuidtpwdtgetpwnamtusernametpw_uidtpw_gidt_uidtNonetgetgidt_gidtpw_dirt_verboset_configt_processt
_last_startedt
_quick_startstTruet_allow_restart(	tselft	connectorRtverbosetconfigtmy_uidtpwd_infot
target_uidt
target_gid((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyt__init__Ps2													cCsitjjtjjtjd}tjj||j}tjj|set	d|fn|S(sFind the fully-qualified path to the executable.

        If the executable can't be found, L{ExecutableNotFoundError} will be
        raised.
        is%s doesn't exist(
R&tpathtdirnametabspathtsystargvtjointprogramtexistsR(R<RFt
executable((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytfind_executableqs
%cCs<d|_tj}|jt|krk|jd7_|jtkrttd|j|j	j
dSn	d|_||_t||_|j}|dg}|j
s|jdn|jr|jd|jgn|jdk	r|j|jn|j	j|j|d|d	|jd
|jd|jdS(sStart this daemon.isCan't keep %s running. Exiting.Nis--ignore-sigints--quiets-ctargstenvtuidtgid(R1R7ttimeR8tRESTART_BURST_DELAYR9tMAXIMUM_CONSECUTIVE_RESTARTSRRKR%tstoptWatchedProcessProtocolRNR5tappendR6textendtoptionstspawnProcessR)R0R3(R<tnowtexeRO((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytstart}s*	
				cCs |jstdS|jjS(sStop this daemon.N(R7RR1tkill(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRVs	
csgfd}jjjjdt}|jfd|j||jd|S(sConnect to the remote daemon over AMP and perform the given command.

        @param name: The name of the command to perform.
        @param args: Arguments list to be passed to the connect method
        @param kwargs: Keywords arguments to pass to the connect method.
        @return: A L{Deferred} resulting in C{True} if the command was
            successful or C{False} otherwise.
        @see: L{RemoteLandscapeComponentCreator.connect}.
        csjjtS(N(R$t
disconnectR:(tignored(R<(s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR`s
tquietcst|S(N(tgetattr(tremote(tname(s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyt<lambda>scSstS(N(tFalse(tx((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRfs(R$tconnecttmax_retriestfactorR:taddCallbackt
addErrback(R<ReROtkwargsR`t	connected((ReR<s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyt_connect_and_calls	
cCs
|jdS(Ntexit(Rp(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytrequest_exitscCs
|jdS(Ntping(Rp(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyt
is_runningscCs |jstdS|jjS(sN
        Return a Deferred which will fire when the process has died.
        N(R7RR1twait(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRus	
cCs |jstdS|jjS(s
        Wait for the process to die for C{GRACEFUL_WAIT_PERIOD}. If it hasn't
        died by that point, send it a SIGTERM. If it doesn't die for
        C{SIGKILL_DELAY},
        N(R7RR1twait_or_die(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRvs	
cCs
t|_dS(sCalled by the watchdog when starting to shut us down.

        It will prevent our L{WatchedProcessProtocol} to restart the process
        when it exits.
        N(RgR;(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytprepare_for_shutdownscCs|jS(s>Return a boolean indicating if the daemon should be restarted.(R;(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyt
allow_restartscCs|jjdS(N(R7R(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRsN(RRRR-RjRkR1RZRRgRDRNR^RVRpRrRtRuRvRwRxR(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR5s$ 								
		tBrokercBseZdZRS(slandscape-broker(RRRK(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRystMonitorcBseZdZRS(slandscape-monitor(RRRK(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRzstManagercBseZdZdZRS(slandscape-managertroot(RRRKR-(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR{sRWcBsqeZdZeZdZdZedZdZdZ	dZ
dZdZd	Z
d
ZRS(su
    A process-watching protocol which sends any of its output to the log file
    and restarts it when it dies.
    cCs(||_d|_d|_d|_dS(N(tdaemonR1t_wait_resultt_delayed_really_killt_delayed_terminate(R<R}((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRDs			cCs|j|jS(N(t
_terminateRu(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR_s
cCs|jdk	r{|r/td|jjfny|jjtjWntk
rYq{Xt	j
t|j}||_
ndS(Ns%s didn't exit. Sending SIGTERM(t	transportR1RR}RKt
signalProcesstsignaltSIGTERMR	Rt	callLatert
SIGKILL_DELAYt_really_killR(R<twarntdelayed((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRs
cCsLy|jjtjWntk
r*nXtd|jjd|_	dS(Ns %s didn't die.  Sending SIGKILL.(
RRRtSIGKILLR	RR}RKR1R(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR
s
cCsA|jdk	r=y|jjtjWq=tk
r9q=XndS(N(RR1RRtSIGUSR1R	(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRs

cCs/|jjdkrtdSt|_|jS(N(RtpidR1RRR~(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRus
cCs(tjt|jdt|_|jS(NR(RRtGRACEFUL_WAIT_PERIODRR:RRu(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRv#s	cCstjj|dS(N(RHtstdouttwrite(R<tdata((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytoutReceived(scCstjj|dS(N(RHtstderrR(R<R((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyterrReceived,scCs|jdk	r|jjn|jdk	rM|jjrM|jjn|jdk	ro|jjdn|jjr|jj	ndS(s"The process has ended; restart it.N(
RR1tcancelRtactiveR~tcallbackR}RxR^(R<treason((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytprocessEnded/s(RRRRgt_killedRDR_RRRRuRvRRR(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRWs									tWatchDogcBskeZdZeed	d	d	d	d	dZdZdZdZ	dZ
dZdZdZ
RS(
sj
    The Landscape WatchDog starts all other landscape daemons and ensures that
    they are working.
    c	st}|dkr'tttg}n|dkrft|krftt||d|d|j}n|dkrt|krtt||d|d|j}n|dkrt|krtt||d|d|j}n|_	|_
|_tdj	j
jg_
_d_t_tjtjfd|dk	r|jdkrdt|jdt|jg}	xj
D]}
|	|
_qWni_dS(NR>R?csjjS(N(tcallFromThreadt_notify_rotate_logs(Rtframe(R<R(s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRf^sis--cloness--start-clones-over(RR1RyRzR{RR?RRtbrokertmonitortmanagertfiltertdaemonsRt	_checkingRgt	_stoppingRRtcloneststrtstart_clones_overRZt_ping_failures(R<RR>R?RRRtenabled_daemonstlandscape_reactorRZR}((R<Rs6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRDBs>				$			cCsbg}x=|jD]2}|j}|j|d|j|qWd}t|j|S(s6Return a list of any daemons that are already running.cSs
||fS(N((Rttd((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRfpscSs%g|D]}|dr|d^qS(Nii((trRh((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytgot_all_resultsss(RRtRlRXR(R<tresultsR}tresultR((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyt
check_runninghs	cCs,x|jD]}|jq
W|jdS(sm
        Start all daemons. The broker will be started first, and no other
        daemons will be started before it is running and responding to DBUS
        messages.

        @return: A deferred which fires when all services have successfully
            started. If a daemon could not be started, the deferred will fail
            with L{DaemonError}.
        N(RR^tstart_monitoring(R<R}((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR^ws
cCs|jjd|j|_dS(s;Start monitoring processes which have already been started.iN(RRt_checkR(R<((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRscs|rjrtdjfjkrDdj<njcd7<jdkrtdjfj}fd}|j||Sn
dj<dS(Ns%s failed to respond to a ping.iiis%s died! Restarting.csjdj<dS(Ni(R^R(Ra(R}R<(s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytstoppeds
(RRRKRRVtaddBoth(R<RtR}tstoppingR((R}R<s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyt_restart_if_not_runnings
cslg}x=jD]2}|j}|jj||j|qWfd}t|j|dS(Ncsjjdj_dS(Ni(RRRR(Ra(R<(s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyt
reschedules(RRtRlRRXRR(R<tall_runningR}RtR((R<s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRscsjdk	r.jjr.jjnt_xjD]}|jqAWfd}jj	}|j
|S(Ncsa|r+gjD]}|j^q}n,tdgjD]}|j^q?}t|S(NsFCouldn't request that broker gracefully shut down; killing forcefully.(RRvRRVR(tbroker_stoppedR}RRh(R<(s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytterminate_processess
%
"(RR1RRR:RRRwRRrRl(R<R}RR((R<s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRrs	cCs)x|jD]}|jq
WtdS(N(RR(R<R}((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRsN(RRRRRgR1RDRR^RRRRrR(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR<s		$						tWatchDogConfigurationcBseZdZdZRS(cCsdtt|j}|jddddd|jddddd	|jd
dddd|S(Ns--daemontactiont
store_truethelpsFork and run in the background.s
--pid-filettypeRsThe file to write the PID to.s--monitor-onlysbDon't enable management features. This is useful if you want to run the client as a non-root user.(tsuperRtmake_parsert
add_option(R<tparser((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRscCs)ttg}|js%|jtn|S(N(RyRztmonitor_onlyRXR{(R<R((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytget_enabled_daemonss	(RRRR(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRs	cCstjrtjdntjtjrBtjdntjdtjdtj}xVtdD]H}ytj||Wqqt	k
r}|j
t
jkrqqqXqqWtj|dS(Nii?s	/dev/nulli(
R&tforkt_exittsetsidtumasktopentO_RDWRtrangetdup2tOSErrorterrnotEBADFtclose(tnulltite((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyt	daemonizes

tWatchDogServicecBs5eZdZdZdZdZdZRS(cCs>||_td|jd|d|j|_d|_dS(NR>R?Ri(R6RR}Rtwatchdogt	exit_code(R<R?((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRDs
	cs3tjtjdjjdjjjjdkrtt	jjdjjdfddl
m}d|_xQt
jjD]:}d	|}tjdjj|djj|qWntd}|jfd
fd}fd}|j||j||S(
Nt	data_pathtlog_diriidii(tMethodCallSenderi,s	-clone-%dcs
jjS(N(RR(t_(R<(s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRfscs_|r>tddjd|Dd_tjdSjtdjjS(Ns4ERROR: The following daemons are already running: %ss, css|]}|jVqdS(N(RK(t.0Rh((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pys	<genexpr>sisWatchdog watching for daemons.(	RRJRRtcrasht
_daemonizeRRR^(trunning_daemons(R<(s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytstart_if_not_runnings	


cs$t|dd_tjdS(NsUnknown error occurred!i(RRRR(tfailure(R<(s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytdies
	(R
tstartServicetbootstrap_listt	bootstrapR6RRRRRtlandscape.lib.ampRttimeoutRRR1RlRm(R<RRtsuffixRRR((R<s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRs(

	
	

cCsa|jjr]t|jjr]t|jjd}|jttj|j	q]ndS(Ntw(
R6R}Rtpid_fileRRRR&tgetpidR(R<tstream((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR&scsVtdtjtjtjtjjj}|jfd|S(NsStopping client...cs
jS(N(t_remove_pid(R(R<(s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRf7s(	RR
tstopServiceRtSIGINTtSIG_IGNRRrR(R<tdone((R<s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR.s

cCs~|jj}|dk	rztj|tjrzt|}|j}|j|t	tj
krztj|qzndS(N(R6RR1R&taccesstW_OKRtreadRRRtunlink(R<RRR((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyR:s!
(RRRDRRRR(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRs
		-		s
$data_pathR R|is$data_path/packages$data_path/package/hash-ids$data_path/package/binariess$data_path/package/upgrade-tools$data_path/messagess$data_path/socketsis$data_path/custom-graph-scriptss$log_dirs$data_path/package/databaseicCsFx?tjjD].}|jds1|dkrtj|=qqWdS(sHUnset dangerous environment variables.

    In particular unset all variables beginning with DEBIAN_ or DEBCONF_,
    to avoid any problems when landscape-client is invoked from its
    postinst script.  Some environment variables may be set which would affect
    *other* maintainer scripts which landscape-client invokes.
    tDEBIAN_tDEBCONF_tLANDSCAPE_ATTACHMENTStMAILN(RR(RR(R&R'tkeyst
startswith(tkey((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pytclean_environmentUscCstt}|j|ytjdj}Wntk
rStjdnXt	j
d|fkr|tjdnt|dtd}t
|}|j||d	krddlm}n|jdt|t|j|jS(
sStart the watchdog.

    This is the topmost function that kicks off the Landscape client.  It
    cleans up the environment, loads the configuration, and starts the
    reactor.

    @param args: Command line arguments, including the program name as the
        first element.
    @param reactor: The reactor to use.  If none is specified, the global
        reactor is used.
    @raise SystemExit: if command line arguments are bad, or when landscape-
        client is not running as 'root' or 'landscape'.
    R s#The 'landscape' user doesn't exist!is:landscape-client can only be run as 'root' or 'landscape'.Rslandscape-clienti(RN(RRtloadR+R,R.tKeyErrorRHRqR&R*R
RRtsetServiceParentR1ttwisted.internetRRRRgtrunR(RORR?t
landscape_uidtapplicationtwatchdog_service((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyRcs$	




(FRR&RRHR+RRStloggingRRRtresourceRRRRttwisted.internet.deferRRttwisted.internet.protocolRttwisted.internet.errorR	ttwisted.application.serviceR
Rttwisted.application.appRtlandscape.deploymentR
Rtlandscape.lib.twisted_utilRtlandscape.lib.logRtlandscape.lib.bootstrapRRRt
landscape.logRtlandscape.broker.ampRRRtlandscape.reactorRRRURTRt	ExceptionRRRtobjectRRyRzR{RWRRRRRRRIR1R(((s6/usr/lib/python2.7/dist-packages/landscape/watchdog.pyt<module>slO	T