
    }f.F                     0   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlmZmZmZmZmZmZ ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZ dZ ejB                   G d dejD                               Z#ejB                   G d dejD                               Z$ejB                   G d dejD                               Z% e&e%jN                  e%jP                  e%jR                  e%jT                  g      Z+ G d de      Z,dZ-dee.   de/de.fdZ0d.dZ1de#de$dee.e.f   fdZ2de,fdZ3de4fdZ5de/fd Z6dee%e.f   fd!Z7de/fd"Z8de/de/fd#Z9de/fd$Z:de#fd%Z;de.fd&Z<d' Z=d( Z>deeef   fd)Z?	 d/d*ee   de/de,fd+Z@d, ZAeBd-k(  r eA        yy)0zGDefine 'status' utility and handler as part of cloud-init command line.    N)deepcopy)gmtimesleepstrftime)AnyDictList
NamedTupleOptionalTuple)safeyamlsubp)read_cfg_paths)uses_systemd)Paths)get_cmdline	load_jsonload_text_filez/etc/cloud/cloud-init.disabledc                        e Zd ZdZdZdZdZdZy)RunningStatusz=Enum representing user-visible cloud-init application status.znot startedrunningdonedisabledN)__name__
__module____qualname____doc__NOT_STARTEDRUNNINGDONEDISABLED     6/usr/lib/python3/dist-packages/cloudinit/cmd/status.pyr   r      s    GKGDHr#   r   c                       e Zd ZdZdZdZdZy)ConditionStatusz;Enum representing user-visible cloud-init condition status.errordegradedhealthyN)r   r   r   r   ERRORDEGRADEDPEACHYr"   r#   r$   r&   r&   %   s    EEHFr#   r&   c                   0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)EnabledStatusz<Enum representing user-visible cloud-init boot status codes.zdisabled-by-generatorzdisabled-by-kernel-command-linezdisabled-by-marker-filez disabled-by-environment-variablezenabled-by-generatorzenabled-by-kernel-command-linezenabled-by-sysvinitunknownN)r   r   r   r   DISABLED_BY_GENERATORDISABLED_BY_KERNEL_CMDLINEDISABLED_BY_MARKER_FILEDISABLED_BY_ENV_VARIABLEENABLED_BY_GENERATORENABLED_BY_KERNEL_CMDLINEENABLED_BY_SYSVINITUNKNOWNr"   r#   r$   r.   r.   .   s4    F3!B7A1 @/Gr#   r.   c                       e Zd ZU eed<   eed<   eed<   eed<   ee   ed<   e	eee   f   ed<   eed<   e
e   ed<   e	ee	f   ed	<   y
)StatusDetailsrunning_statuscondition_statusboot_status_codedescriptionerrorsrecoverable_errorslast_update
datasourcev1N)r   r   r   r   __annotations__r&   r.   strr	   r   r   r"   r#   r$   r9   r9   F   sU    !!%%##IS$s)^,,S$Yr#   r9   zextended_status: {extended_status}
boot_status_code: {boot_code}
{last_update}detail: {description}
errors:{errors}
recoverable_errors:{recoverable_errors}systemctl_argswaitreturnc                    	 	 t        j                   dg|       j                  j                         S # t         j                  $ r |s t	        d       Y nw xY wY)z-Query systemd with retries and return output.	systemctl      ?)r   stdoutstripProcessExecutionErrorr   )rE   rF   s     r$   query_systemctlrN   Z   sY     	99k;N;<CCIIKK)) 	$K	 s   /3 !AAc                     | st        j                  dd      } | j                  dt        g ddd       | j                  d	d
ddd       | j                  ddddd       | S )a%  Build or extend an arg parser for status utility.

    @param parser: Optional existing ArgumentParser instance representing the
        status subcommand which will be extended to support the args of
        this utility.

    @returns: ArgumentParser with proper argument configuration.
    statuszReport run status of cloud init)progr=   z--format)jsontabularyamlrS   z5Specify output format for cloud-id (default: tabular))typechoicesdefaulthelpz-lz--long
store_trueFzJReport long format of statuses including run stage name and error messages)actionrW   rX   z-wz--waitz'Block waiting on cloud-init to complete)argparseArgumentParseradd_argumentrD   parsers    r$   
get_parserr`   i   s     (('H
 +D     	 6   Mr#   r   	conditionc                 J   |t         j                  k(  rd|j                   d| j                   fS |t         j                  k(  rI| t        j
                  t        j                  fv r'| j                  |j                   d| j                   fS | j                  | j                  fS )zTranslate running and condition status to human readable strings.

    Returns (status, extended_status).
    Much of this is for backwards compatibility
    r'   z -  )r&   r*   valuer+   r   r    r   )r   ra   s     r$   translate_statusre      s     O)))9??+3w}}o>>>	o..	.7? 4 }} 17==/BBB=='--''r#   detailsc                    t        |j                  |j                        \  }}|j                  |j                  j
                  |||j                  |j                  |j                  |j                  d|j                  }| j                  dk(  r| j                  rdnd}|d   }t        | d|        | j                  r|j                  d      r
d|d    d}nd}|d	   rd
d
j!                  |d	         z   nd}|d   rmddj!                  |d   j#                         D 	
cg c]:  \  }	}
|	 dd
j!                  |
D cg c]  }|j%                  dd       c}      z   < c}}
}	      z   nd}t        t&        j                  |d   ||d   |d   |||             yy| j                  dk(  r#t        t)        j*                  |ddd             y| j                  dk(  rt        t-        j*                  |             yyc c}w c c}}
}	w )zPrint status out to the CLI.)rA   r<   rP   extended_statusdetailr>   r?   r@   rS   
 rP   zstatus: r@   zlast_update: r>   z
	- z []r?   z:
	- rc   z {}rh   r<   ri   )rh   prefix	boot_coder=   r@   r>   r?   rR      T),z: )indent	sort_keys
separatorsrT   N)re   r:   r;   rA   r<   rd   r=   r>   r?   r@   rB   formatrF   printlonggetjoinitemsreplaceTABULAR_LONG_TMPLrR   dumpsr   )argsrf   rP   rh   details_dictrl   stater@   errors_outputkvirecoverable_errors_outputs                r$   print_statusr      s9   . 8 8FO ((#44::*%%..%88**
$ **
$L {{i X&()99. -l=.I-J"M   ) 8==h)?@@    45 )) %11E$F$L$L$N  !Aq #W"--q(I!4)=(IJK  & !(($01B$C!*+=> ,X 6 +('@ ) 
- B 
	JJQ$;	

 
	hnn\*+ 
1 )Js   %H HHHc                 x   t               }t        ||j                        }|j                  r|j                  t        j
                  t        j                  fv r|j                  dk(  r=t        j                  j                  d       t        j                  j                          t        ||j                        }t        d       |j                  t        j
                  t        j                  fv rt        ||       |j                  t        j                   k(  ry|j                  t        j"                  k(  ryy)z4Handle calls to 'cloud-init status' as a subcommand.rS   .rJ      rn   r   )r   get_status_detailsrF   r:   r   r   r   rs   sysrK   writeflushr   r   r;   r&   r*   r+   )namer|   pathsrf   s       r$   handle_status_argsr      s     E 		2Gyy$$%%!!)
 
 {{i'

  %

  "(		:G$K $$%%!!)
 
 w ?#8#88		!	!_%=%=	=r#   c                 d    	 t        dg|       }d|v S # t        j                  $ r d}Y d|v S w xY w)z?Return whether cloud-init is disabled via environment variable.zshow-environmentrF   rk   cloud-init=disabled)rN   r   rM   )rF   envs     r$   _disabled_via_environmentr     sK    12> !C'' %%  C''s    //c                    t               j                         }t               st        j                  }d}||fS d|v rt        j
                  }d}||fS t        j                  j                  |       r%t        j                  }dj                  |       }||fS d|v rt        j                  }d}||fS dt        j                  j                  dd      v st               r"t        |	      rt        j                  }d
}||fS t        j                  j                  t        j                  j!                  |j"                  d            rt        j$                  }d}||fS t        j                  j                  t        j                  j!                  |j"                  d            rt        j&                  }d}||fS t        j(                  }d}||fS )aX  Report whether cloud-init current boot status

    @param disable_file: The path to the cloud-init disable file.
    @param paths: An initialized cloudinit.helpers.Paths object.
    @param wait: If user has indicated to wait for cloud-init to complete.
    @returns: A tuple containing (code, reason) about cloud-init's status and
    why.
    zCloud-init enabled on sysvinitzcloud-init=enabledz<Cloud-init enabled by kernel command line cloud-init=enabledzCloud-init disabled by {0}r   z;Cloud-init disabled by kernel parameter cloud-init=disabledKERNEL_CMDLINErk   r   zNCloud-init disabled by environment variable KERNEL_CMDLINE=cloud-init=disabledr   z+Cloud-init disabled by cloud-init-generatorenabledz2Cloud-init enabled by systemd cloud-init-generatorz'Systemd generator may not have run yet.)r   splitr   r.   r6   r5   ospathexistsr2   rs   r1   environrv   r   r3   rw   run_dirr0   r4   r7   )disable_filer   rF   cmdline_partsbootstatus_codereasons         r$   get_bootstatusr     s     M'')M>';;16 V$$5 
	.'AAO0 V$$/ 
	%'??-44\B* V$$) 
-	/'BBN$ V$$# 
"**..1A2"F	F4$?'@@1 	 V$$ 
U]]J?	@'==> V$$ 
U]]I>	?'<<E V$$ (//:V$$r#   c                  H    t        t                     j                  t        vS N)r   r   r<   DISABLED_BOOT_CODESr"   r#   r$   is_cloud_init_enabledr   5  s     >+,=="	#r#   c                 0   dD ]  }	 t        dd|g|       }t        |j                         D cg c]/  }|j                  d	      D cg c]  }|j                          c}1 c}}      }|d
   j                  d      s
|d
   dk(  s y|d   dk(  r|d   dk(  r|d   dk(  r
|d   dk(  r y y|d   dk(  s|d   dk(  r y y y# t        j                  $ r3}t        d|j                   t
        j                         Y d}~ yd}~ww xY wc c}w c c}}w )z2Return if systemd units report a cloud-init error.)zcloud-final.servicezcloud-config.servicezcloud-init.servicezcloud-init-local.serviceshowz5--property=ActiveState,UnitFileState,SubState,MainPIDr   z^Failed to get status from systemd. Cloud-init status may be inaccurate. Error from systemctl: )fileNF=UnitFileStater   staticTActiveStateactiveSubStateexitedr   MainPID0failed)rN   r   rM   rt   stderrr   dict
splitlinesr   rL   
startswith)rF   servicerK   erxstatess          r$   systemd_failedr   <  sK    2	$K
 F" 8>8I8I8KL1.Aaggi.L
 ?#..y9o&(2 M"h.j!X-
#y0VI5F#5M   =!X-
1Cx1O e2j M )) 	))*
4 ZZ	 	 /Ls.   CDDDD
(DD
Dc                     t         j                  j                  |       xr  t         j                  j                  |       S )z%Return True if cloud-init is running.)r   r   r   )status_fileresult_files     r$   
is_runningr   v  s*    77>>+&Jrww~~k/J+JJr#   c                     |t         v rt        j                  S t        | |      rt        j                  S |dkD  rt        j
                  S t        j                  S )z(Return the running status of cloud-init.r   )r   r   r!   r   r   r    r   )r   r   r<   latest_events       r$   get_running_statusr   {  sO     ..%%%	K	-$$$		!!!(((r#   c                     | j                  dd      }|r5|j                  d      \  }}}|j                         j                  dd      }|S )zyGet the datasource from status.json.

    Return a lowercased non-prefixed version. So "DataSourceEc2" becomes "ec2"
    rA   rk   rc   )rv   	partitionlowerry   )	status_v1rA   ds_s       r$   get_datasourcer     sK    
 |R0J'',AqXXZ''b9
r#   c                 b    | j                  d      }|r|S | j                  d      rd| d    S |S )zReturn a description of the current status.

    If we have a datasource, return that. If we're running in a particular
    stage, return that. Otherwise, return the boot_description.
    rA   stagezRunning in stage: )rv   )r   boot_descriptionrA   s      r$   get_descriptionr     s@     |,J	w	#Ig$6#788r#   c                     d}| j                         D ]F  }t        |t              st        ||j	                  d      xs d|j	                  d      xs d      }H |S )z,Return the latest event time from status_v1.r   startfinished)values
isinstancer   maxrv   )r   r   
stage_infos      r$   get_latest_eventr     s`    L&&( 
j$'w',1z*/aL r#   c                 `   g }i }t        | j                               D ]  \  }}t        |t              s|j	                  |j                  dg              |j                  di       }|j                         D ]/  }||vrt        ||         ||<   ||   j	                  ||          1  ||fS )z>Return a list of errors and recoverable_errors from status_v1.r>   r?   )sortedrx   r   r   extendrv   keysr   )r   r>   r?   _keyr   current_recoverable_errorserr_types          r$   
get_errorsr     s    F"9??#45 jj$'MM*..267 *4$b*& 7;;= #553;28<4&x0 'x07728<" %%%r#   r   c                    t         j                  }| xs
 t               } t        j                  j                  | j                  d      }t        j                  j                  | j                  d      }t        t        | |      \  }}i }t        j                  j                  |      r$t        t        |            j                  di       }t        |      }t        ||      }	t        |      }
|
rt!        dt#        |
            nd}t%        |      \  }}|rt         j&                  }n|rt         j(                  }t+        ||||
      }|t,        j.                  k(  rIt1               r?t3        |      r3t,        j4                  }t         j&                  }d}	|j7                  d       |j9                  d	d
       t;        ||||	|||||	      S )a  Return a dict with status, details and errors.

    @param paths: An initialized cloudinit.helpers.paths object.
    @param wait: If user has indicated to wait for cloud-init to complete.

    Values are obtained from parsing paths.run_dir/status.json.
    zstatus.jsonzresult.jsonrB   z%a, %d %b %Y %H:%M:%S %zrk   r   z"Failed due to systemd unit failurezFailed due to systemd unit failure. Ensure all cloud-init services are enabled, and check 'systemctl' or 'journalctl' for more information.rA   N)r&   r,   r   r   r   rw   r   r   CLOUDINIT_DISABLED_FILEr   r   r   rv   r   r   r   r   r   r   r*   r+   r   r   r   r   r   r    appendpopr9   )r   rF   r;   r   r   r<   r   r   rA   r=   r   r@   r>   r?   r:   s                  r$   r   r     s    '--%^%E'',,u}}m<K'',,u}}m<K)7*&& I	ww~~k"n[9:>>tRH		*J!)-=>K#I.L  	+VL-AB  ",I!6F*00	*33'["2LN
 	-///N%&++*00:$	
 MM,%
 
r#   c                  r    t               } t        j                  t        d| j	                                      y)z$Tool to report status of cloud-init.rP   N)r`   r   exitr   
parse_argsr^   s    r$   mainr     s&    \FHH&*;*;*=>?r#   __main__r   )NF)Cr   r[   enumrR   r   r   copyr   timer   r   r   typingr   r   r	   r
   r   r   	cloudinitr   r   cloudinit.cmd.develr   cloudinit.distrosr   cloudinit.helpersr   cloudinit.utilr   r   r   r   uniqueEnumr   r&   r.   	frozensetr0   r1   r2   r3   r   r9   rz   rD   boolrN   r`   re   r   intr   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r#   r$   <module>r      s+   N    	 
  ( ( ? ? $ . * # A A:  DII   dii   
DII 
 
  ++00--..	 	J 	+ I  		%P(('6(
38_((?, ?,Dc 6(t ('%}c7I1J '%Tt 7 7$ 7tKD K
))	 	 
&U4:. &2 16DE?D)-DDN@ zF r#   