Your IP : 172.28.240.42


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


Tc@sdZddlZddlZddlZddlZddlZddlmZddlm	Z	m
Z
mZmZm
Z
ddlmZddlmZddlmZddlmZdd	lmZmZdd
lmZddlmZddlmZmZm Z e!Z"d
Z#dZ$dZ%de&fdYZ'ddZ)de&fdYZ*de&fdYZ+de&fdYZ,de!fdYZ-dee-fdYZ.defdYZ/defd YZ0dS(!su
Functionality for running arbitrary shell scripts.

@var ALL_USERS: A token indicating all users should be allowed.
iN(tProcessProtocol(tDeferredtfailtinlineCallbackstreturnValuetsucceed(tProcessDone(tVERSION(tUBUNTU_PATH(tbuild_script(tfetch_asynct
HTTPCodeError(tPersist(tencode_if_needed(t
ManagerPlugint	SUCCEEDEDtFAILEDifigihtUnknownUserErrorcBseZRS((t__name__t
__module__(((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR scCsd}d}d}|dk	rt|}ytj|}Wn!tk
r`td|nX|j}|j}|j}t	j
j|sd}qn|||fS(NuUnknown user '%s't/(tNoneR
tpwdtgetpwnamtKeyErrorRtpw_uidtpw_gidtpw_dirtostpathtexists(tusernametuidtgidRtusername_strtinfo((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyt
get_user_info$s
			tProcessTimeLimitReachedErrorcBseZdZdZRS(s
    Raised when a process has been running for too long.

    @ivar data: The data that the process printed before reaching the time
        limit.
    cCs
||_dS(N(tdata(tselfR&((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyt__init__>s(RRt__doc__R((((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR%6stProcessFailedErrorcBseZdZdZRS(sRaised when a process exits with a non-0 exit code.

    @ivar data: The data that the process printed before reaching the time
        limit.
    cCs||_||_dS(N(R&t	exit_code(R'R&R+((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR(Is	(RRR)R((((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR*BstUnknownInterpreterErrorcBs eZdZdZdZRS(sRaised when the interpreter specified to run a script is invalid.

    @ivar interpreter: the interpreter specified for the script.
    cCs#||_tj||jdS(N(tinterpretert	ExceptionR(t_get_message(R'R-((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR(Ts	cCsd|jS(NsUnknown interpreter: '%s'(R-(R'((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR/Xs(RRR)R(R/(((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR,Ns	tScriptRunnerMixincBs5eZdZddZdZdZdZRS(sb
    @param process_factory: The L{IReactorProcess} provider to run the
        process with.
    cCs,|dkrddlm}n||_dS(Ni(treactor(Rttwisted.internetR1tprocess_factory(R'R3((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR(bscCs(|jjj}|tkp'||kS(N(tregistrytconfigtget_allowed_script_userst	ALL_USERS(R'tusert
allowed_users((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pytis_user_allowedgscCsVtj|d|dk	r2tj|||n|jt|||jdS(Ni(RtchmodRtchowntwriteR	tclose(R'tscript_filetfilenametshelltcodeR R!((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pytwrite_script_fileks
cCs|tjkrd}n|tjkr6d}nt|jj|j}|jj	||d|d|d|d||dk	r|j
|n|jS(NR R!Rtenv(RtgetuidRtgetgidtProcessAccumulationProtocolR4R1t
size_limitR3tspawnProcesstschedule_canceltresult_deferred(R'R@R R!RRDt
time_limittpp((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyt_run_scriptws			"N(RRR)RR(R:RCRN(((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR0\s
		tScriptExecutionPlugincBszeZdZdZdZddZdZdZdZ	dZ
edZddddd	Z
d
ZRS(sA plugin which allows execution of arbitrary shell scripts.

    @ivar size_limit: The number of bytes at which to truncate process output.
    i cCs-tt|j||jd|jdS(Nsexecute-script(tsuperROtregistertregister_messaget_handle_execute_script(R'R4((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRQscCsut|ts$|jdd}nidd6|d6|d6|d6}|rY||d<n|jjj||jtS(	Nsutf-8treplacesoperation-resultttypetstatussresult-textsoperation-idsresult-code(t
isinstancetunicodetdecodeR4tbrokertsend_messaget_session_idtTrue(R'RVR&topidtresult_codetmessage((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyt_responds


cCs|d}y|d}|j|s@|jtd|f|S|jdd}|j|d|dd|dd	|d
|d
d|}|j|j||j|j	||SWn2t
k
r}|jt|j||nXdS(Nsoperation-idRu!Scripts cannot be run as user %s.RDR-RBRLs
time-limitR8tattachmentstserver_supplied_env(R:RaRtgetRt
run_scripttaddCallbackt_respond_successt
addErrbackt_respond_failureR.t_format_exception(R'R`R^R8Rctdte((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRSs&



	cCsd|jj|jdfS(Nu%s: %si(t	__class__Rtargs(R'Rl((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRjscCs|jt||S(N(RaR(R'R&R^((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRgscCsd}|jtrt}nL|jtr6t}n4|jtrjt}|jt	t
|j|tS|dk	r|jt	|jj||S|jt	t
||SdS(N(
RtcheckR%tTIMEOUT_RESULTR*tPROCESS_FAILED_RESULTRtFETCH_ATTACHMENTS_FAILED_RESULTRaRtstrtvalueR&(R'tfailureR^RB((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRis		c
csg|jjjjdddd}tj}idtd6dd6|d	6}x|jD]\}}	t|	t	r|	}
t
dVn,td
||	fd|jjj
d|V}
tjj||}t|d
}tj|d|dk	rtj|||n|j|
|jq[Wtj|d|dk	rYtj|||nt|dS(NRiis/attachment/slandscape-client/%ss
User-Agentsapplication/octet-streamsContent-Types
X-Computer-IDs%s%dtcainfotheaderstwbii(R4R5turltrsplitttempfiletmkdtempRtitemsRWRsRRR
tssl_public_keyRRtjointfileR;R<R=R>R(
R'RbR R!tcomputer_idt	root_pathtattachment_dirRwR@t
attachment_idR&t
full_filenamet
attachment((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyt_save_attachmentss0#


c	stjj|jds,tt|St|\tj\}tj	|d}j
|||itd6|pdd6pdd6|rj|ntj
d}	|r3tdtjjjjjd	}
|
jd
}
|
jd}j||}ntd
}fd}
|j|
|jj|	S(s8
        Run a script based on a shell and the code.

        A file will be written with #!<shell> as the first line, as executable,
        and run as the given user.

        XXX: Handle the 'reboot' and 'killall landscape-client' commands
        gracefully.

        @param shell: The interpreter to use.
        @param code: The code to run.
        @param user: The username to run the process as.
        @param time_limit: The number of seconds to allow the process to run
            before killing it and failing the returned Deferred with a
            L{ProcessTimeLimitReachedError}.
        @param attachments: C{dict} of filename/data attached to the script.

        @return: A deferred that will fire with the data printed by the process
            or fail with a L{ProcessTimeLimitReachedError}.
        itwtPATHttUSERtHOMEiR@sbroker.bpickletregistrations	secure-idcs5|dk	r|d<njS(NtLANDSCAPE_ATTACHMENTS(RRN(R(RLR R'R@R!RDR(sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pytprepare_scripts
N(RRRtsplitRR,R$R{tmkstemptfdopenRCRtupdatetumaskRRR4R5t	data_pathtroot_atRdRRRRftaddBotht_cleanup(R'RARBR8RLRbRctfdR?t	old_umasktpersistRRkR((RLR R'R@R!RDRsE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRes.
'!
cCsZytj|WnnXd|krIytj|dWqIqIXntj||S(NR(RtunlinktshutiltrmtreeR(R'tresultR@RDR((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR's
N(RRR)RHRQRRaRSRjRgRiRRReR(((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyROs					:RGcBs;eZdZdZdZdZdZdZRS(sxA ProcessProtocol which accumulates output.

    @ivar size_limit: The number of bytes at which to truncate output.
    cCs=g|_t|_t|_||_||_d|_dS(N(	R&RRKtFalset
_cancelledRHR1Rt_scheduled_cancel(R'R1RH((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR(;s				cCs|jj||j|_dS(N(R1t
call_latert_cancelR(R'RL((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRJCs	cCs@ttjtt|jd}|jj||j| dS(sSome data was received from the child.

        Add it to our buffer, as long as it doesn't go over L{size_limit}
        bytes.
        iN(treducetoperatortaddtmaptlenR&tappendRH(R'RR&tcurrent_size((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pytchildDataReceivedGs!cCs|jj}dj|j}|jr@|jjt|no|jdk	rt|j}d|_|j
j|n|jt
r|jj|n|jjt||dS(sFire back the deferred.

        The deferred will be fired with the string of data received from the
        subprocess, or if the subprocess was cancelled, a
        L{ProcessTimeLimitReachedError} will be fired with data accumulated so
        far.
        RN(RttexitCodeRR&RRKterrbackR%RRR1tcancel_callRoRtcallbackR*(R'treasonR+R&t	scheduled((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pytprocessEndedPs			cCs>xdD]}|jj|qW|jjdt|_dS(s
        Close filedescriptors, kill the process, and indicate that a
        L{ProcessTimeLimitReachedError} should be fired on the deferred.
        iiitKILLN(iii(t	transporttcloseChildFDt
signalProcessR]R(R'ti((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRhs

(RRR)R(RJRRR(((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRG5s					tScriptExecutioncBs,eZdZdZdZedZRS(sK
    Meta-plugin wrapping ScriptExecutionPlugin and CustomGraphPlugin.
    cCs,ddlm}t|_||_dS(Ni(tCustomGraphPlugin(tlandscape.manager.customgraphRROt_script_executiont
_custom_graph(R'R((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyR(}scCs:tt|j||jj||jj|dS(N(RPRRQRR(R'R4((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRQscCs|jj|dS(N(Rtexchange(R'turgent((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRs(RRR)R(RQRR(((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyRxs		(1R)RRR{RRttwisted.internet.protocolRttwisted.internet.deferRRRRRttwisted.internet.errorRt	landscapeRtlandscape.constantsRtlandscape.lib.scriptcontentR	tlandscape.lib.fetchR
Rtlandscape.lib.persistRtlandscape.lib.encodingR
tlandscape.manager.pluginRRRtobjectR7RpRqRrR.RRR$R%R*R,R0RORGR(((sE/usr/lib/python2.7/dist-packages/landscape/manager/scriptexecution.pyt<module>s8(	+C