HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //lib/python3.6/site-packages/awscli/customizations/__pycache__/opsworks.cpython-36.opt-1.pyc
3

�T_mR�@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZddl
mZmZmZddlmZddlmZeje�ZdZejdd�Zd	Zd
Zejdej�Zejd�Z ejd
�Z!dZ"dj#�Z$dd�Z%dd�Z&Gdd�de�Z'dd�Z(dd�Z)dS)�N)�ClientError)�shlex_quote�urlopen�ensure_text_type)�BasicCommand)�!create_client_from_parsed_globalszOpsWorks-Instance�)Zminutesz/AWS/OpsWorks/z7arn:aws:iam::aws:policy/AWSOpsWorksInstanceRegistrationz^(?!-)[a-z0-9-]{1,63}(?<!-)$z
^i-[0-9a-f]+$z^\d+\.\d+\.\d+\.\d+$z@http://169.254.169.254/latest/dynamic/instance-identity/documenta�
set -e
umask 007
AGENT_TMP_DIR=$(mktemp -d /tmp/opsworks-agent-installer.XXXXXXXXXXXXXXXX)
curl --retry 5 -L %(agent_installer_url)s | tar xz -C $AGENT_TMP_DIR
cat >$AGENT_TMP_DIR/opsworks-agent-installer/preconfig <<EOF
%(preconfig)s
EOF
exec sudo /bin/sh -c "OPSWORKS_ASSETS_DOWNLOAD_BUCKET=%(assets_download_bucket)s $AGENT_TMP_DIR/opsworks-agent-installer/boot-registration; rm -rf $AGENT_TMP_DIR"
cCs|jdt�dS)Nzbuilding-command-table.opsworks)�register�inject_commands)Zcli�r�/usr/lib/python3.6/opsworks.py�
initialize=sr
cKst|�|d<dS)Nr	)�OpsWorksRegister)Z
command_table�session�kwargsrrrr
Asr
cseZdZdZejd�j�Zdddd�dddd	gd
d�dd
dd�dddd�dddd�dddd�dddd�dddd�dd d!d"�d#d d$d"�d%dd&d'd(d)�gZ�fd*d+�Z	d,d-�Z
d.d/�Zd0d1�Zd2d3�Z
d4d5�Zd6d7�Zd8d9�Zd:d;�Zd<d=�Zd>d?�ZedEdAdB��ZedCdD��Z�ZS)Frr	z�
        Registers an EC2 instance or machine with AWS OpsWorks.

        Registering a machine using this command will install the AWS OpsWorks
        agent on the target machine and register it with an existing OpsWorks
        stack.
    zstack-idTzZA stack ID. The instance will be registered with the
                         given stack.)�name�required�	help_textzinfrastructure-class�ec2zon-premiseszzSpecifies whether to register an EC2 instance (`ec2`)
                         or an on-premises instance (`on-premises`).)rr�choicesrzoverride-hostname�hostnamezrThe instance hostname. If not provided, the current
                         hostname of the machine will be used.)r�destrzoverride-private-ip�
private_ipaAAn IP address. If you set this parameter, the given IP
                         address will be used as the private IP address within
                         OpsWorks.  Otherwise the private IP address will be
                         determined automatically. Not to be used with EC2
                         instances.zoverride-public-ip�	public_ipa?An IP address. If you set this parameter, the given IP
                         address will be used as the public IP address within
                         OpsWorks.  Otherwise the public IP address will be
                         determined automatically. Not to be used with EC2
                         instances.zoverride-ssh�sshzmIf you set this parameter, the given command will be
                         used to connect to the machine.zssh-username�usernamezXIf provided, this username will be used to connect to
                         the host.zssh-private-key�private_keyzhIf provided, the given private key file will be used
                         to connect to the machine.�local�
store_truez�If given, instead of a remote machine, the local
                         machine will be imported. Cannot be used together
                         with `target`.)r�actionrzuse-instance-profilezRUse the instance profile instead of creating an IAM
                         user.�target�?z
[<target>]z�Either the EC2 instance ID or the hostname of the
                         instance or machine to be registered with OpsWorks.
                         Cannot be used together with `--local`.)rZpositional_arg�nargsZsynopsisrcs>tt|�j|�d|_d|_d|_d|_d|_d|_d|_	dS)N)
�superr�__init__�_stack�
_ec2_instance�_prov_params�_use_address�
_use_hostname�
_name_for_iam�
access_key)�selfr)�	__class__rrr$}szOpsWorksRegister.__init__cCs"|jjd�|_t|jd|�|_dS)N�iam�opsworks)�_session�
create_clientr.rr/)r,�args�parsed_globalsrrr�_create_clients�sz OpsWorksRegister._create_clientscCsL|j||�|j|�|j|�|j|�|j|�|j|�|j|�dS)N)r4�prevalidate_arguments�retrieve_stack�validate_arguments�determine_details�create_iam_entities�setup_target_machine)r,r2r3rrr�	_run_main�s




zOpsWorksRegister._run_maincCs�|jr|jrtd��n|jr.|jr.td��|jrHtj�dkrHtd��|jrb|jsZ|jrbtd��|jdkr�|j	rztd��|j
r�td��|jd	kr�|jr�td
��|jr�t
j|j�s�td|j��dS)
zN
        Validates command line arguments before doing anything else.
        z%One of target or --local is required.z4Arguments target and --local are mutually exclusive.ZLinuxz6Non-Linux instances are not supported by AWS OpsWorks.zYArgument --override-ssh cannot be used together with --ssh-username or --ssh-private-key.rz/--override-private-ip is not supported for EC2.z.--override-public-ip is not supported for EC2.zon-premisesz1--use-instance-profile is only supported for EC2.zxInvalid hostname: '%s'. Hostnames must consist of letters, digits and dashes only and must not start or end with a dash.N)r r�
ValueError�platform�systemrrr�infrastructure_classrr�use_instance_profiler�HOSTNAME_RE�match)r,r2rrrr5�s6


z&OpsWorksRegister.prevalidate_argumentscs~tjd�|jj�jgd�dd|_|jj|jdd�|_�jdkoL�j	�rztjd�|j
jd|jd	d
�}dgi}g�d|jkr�|djd
|jdgd��n�jdd��t
j�j�r̈jg|d<n@tj�j�r�j�fdd���j|_n|djd�jgd���fdd�|jf|�dD�}|�s@td�j��n0t|�dk�rptd�jdjdd�|D��f��|d|_dS)z�
        Retrieves the stack from the API, thereby ensures that it exists.

        Provides `self._stack`, `self._prov_params`, `self._use_address`, and
        `self._ec2_instance`.
        z,Retrieving stack and provisioning parameters)ZStackIdsZStacksr�StackId)rCrz#Retrieving EC2 instance information�Region)Zregion_nameZFilters�VpcIdzvpc-id)�NameZValuescSsd|kS)NrEr)�instancerrr�<lambda>�sz1OpsWorksRegister.retrieve_stack.<locals>.<lambda>ZInstanceIdscs |jd��jkp|jd��jkS)N�PrivateIpAddress�PublicIpAddress)�getr )rG)r2rrrH�sztag:Namecs4g|],}|dD]�t�fdd��D��r��qqS)�	Instancesc3s|]}|��VqdS)Nr)�.0�c)�irr�	<genexpr>�sz=OpsWorksRegister.retrieve_stack.<locals>.<listcomp>.<genexpr>)�all)rM�r)�
conditions)rOr�
<listcomp>�sz3OpsWorksRegister.retrieve_stack.<locals>.<listcomp>ZReservationsz&Did not find any instance matching %s.�z)Found multiple instances matching %s: %s.z, css|]}|dVqdS)Z
InstanceIdNr)rMrOrrrrPsz2OpsWorksRegister.retrieve_stack.<locals>.<genexpr>N)�LOG�debugr/Zdescribe_stacks�stack_idr%Z&describe_stack_provisioning_parametersr'r?rr0r1�append�INSTANCE_ID_RErBr �
IP_ADDRESS_REr(�describe_instancesr<�len�joinr&)r,r2rZ	desc_args�	instancesr)r2rSrr6�sF





zOpsWorksRegister.retrieve_stackcs��jrB|jj|jdd�d}t�fdd�|D��rBtd�j���jdkr��jr�tj	t
tt�j
���d}||jd	kr�td
��dS)zS
        Validates command line arguments using the retrieved information.
        rC)rCrLc3s |]}�jj�|dkVqdS)ZHostnameN)r�lower)rMrG)r2rrrPsz6OpsWorksRegister.validate_arguments.<locals>.<genexpr>z@Invalid hostname: '%s'. Hostnames must be unique within a stack.r�regionrDz1The stack's and the instance's region must match.N)rr/r\r%�anyr<r?r�json�loadsrr�IDENTITY_URL�read)r,r2r_rar)r2rr7	s

z#OpsWorksRegister.validate_argumentscCs�|jsn|jrn`|jdkr\d|jkr0|jd|_qnd|jkrRtjd�|jd|_qntd��n|jdkrn|j|_|jr�|j|_	|j|_
n&|jr�d|_	tj�|_
nd|_	|j|_
dS)a

        Determine details (like the address to connect to and the hostname to
        use) from the given arguments and the retrieved data.

        Provides `self._use_address` (if not provided already),
        `self._use_hostname` and `self._name_for_iam`.
        rrJrIzYInstance does not have a public IP address. Trying to use the private address to connect.z1The instance does not seem to have an IP address.zon-premisesN)
r(rr?r&rV�warnr<r rr)r*�socketZgethostname)r,r2rrrr8 s,





z"OpsWorksRegister.determine_detailscCsR|jrtjd�d|_dStjd�dt|jd�}y |jj|td�tjd|�WnJt	k
r�}z.|j
jdi�jd	�d
kr�tjd|�n�WYdd}~XnXtjd�d
tt|jd�d�tt|j
�d�f}x�td�D]�}||r�d|nd}y|jj|td�WnNt	k
�rX}z0|j
jdi�jd	�d
k�rFtjd|�n�WYdd}~Xq�Xtjd|�Pq�Wtd��tjd�|jj||d�y|jjt|d�Wn~t	k
�r }z`|j
jdi�jd	�dk�rtjdt|�|jjt|j|jdt�|d�tjdt|�n�WYdd}~XnXtjdt|�tjd �|jj|d!�d"|_dS)#zp
        Creates an IAM group, user and corresponding credentials.

        Provides `self.access_key`.
        zSkipping IAM entity creationNz#Creating the IAM group if necessaryzOpsWorks-%srC)�	GroupName�PathzCreated IAM group %s�ErrorZCodeZEntityAlreadyExistszIAM group %s exists, continuingzCreating an IAM userzOpsWorks-%s-%srF��z+%s�)�UserNamerjz/IAM user %s already exists, trying another namezCreated IAM user %sz&Couldn't find an unused IAM user name.z3Adding the user to the group and attaching a policy)riro)Z	PolicyArnroZAccessDeniedzFUnauthorized to attach policy %s to user %s. Trying to put user policyZArn)Z
PolicyNameZPolicyDocumentrozPut policy %s to user %szAttached policy %s to user %szCreating an access key)roZ	AccessKey)r@rVrWr+�
clean_for_iamr%r.Zcreate_group�IAM_PATHrZresponserK�shorten_namer*�rangeZcreate_userr<Zadd_user_to_groupZattach_user_policy�IAM_POLICY_ARNZput_user_policy�IAM_USER_POLICY_NAME�_iam_policy_document�IAM_USER_POLICY_TIMEOUTZcreate_access_key)r,r2Z
group_name�eZ
base_usernameZtry_rrrrr9Gs|




z$OpsWorksRegister.create_iam_entitiescCsht|jd|j|j|��|jddd�}|jrNtjd�tjdd|g�ntjd�|j	||�d	S)
zz
        Setups the target machine by copying over the credentials and starting
        the installation process.
        ZAgentInstallerUrl�
Parameters�assets_download_bucket)Zagent_installer_urlZ	preconfigrzzRunning the installer locallyz/bin/shz-cz6Connecting to the target machine to run the installer.N)
�
REMOTE_SCRIPTr'�
_to_ruby_yaml�_pre_config_documentrrVrW�
subprocess�
check_callr)r,r2�
remote_scriptrrrr:�s

z%OpsWorksRegister.setup_target_machinecCs8tj�dkr�z�tjddd�}|j|�|j�|jr<|j}nBd}|jrT|d|j7}|jrh|d|j7}|d|j	7}|d	7}|d|j
7}tj|d
d�Wdt
j|j
�Xn�|jr�tjt|j��}n@d
dg}|jr�|jd|jg�|jr�|jd|jg�|j|j	�dd|g}|jdjdd�|D���tj|�dS)zA
        Runs a (sh) script on a remote machine via SSH.
        ZWindowsZwtF)�deleteZplinkz -l "%s"z -i "%s"z "%s"z -mT)�shellNrz-ttz-lz-iz/bin/shz-c� css|]}t|�VqdS)N)r)rMZwordrrrrP�sz'OpsWorksRegister.ssh.<locals>.<genexpr>)r=r>�tempfileZNamedTemporaryFile�write�closerrrr(rr~r�os�remove�shlex�split�str�extendrYr^)r,r2r�Zscript_fileZcallZremote_callrrrr�s8

zOpsWorksRegister.sshcCs�tfd|jdi|jd��}|jr@|jd|d<|jd|d<|jrP|j|d<|jr`|j|d	<|jrp|j|d
<|jdk|d<tj	d
|�|S)NrXrCryZAccessKeyIdZ
access_key_idZSecretAccessKeyZsecret_access_keyrrrr�importzUsing pre-config: %r)
�dictr%r'r+r)rrr?rVrW)r,r2�
parametersrrrr}�s



z%OpsWorksRegister._pre_config_documentNcCsNdd|d�}|dk	r8tjj�|}dd|jd�ii|d<|gdd	�}tj|�S)
Nzopsworks:RegisterInstanceZAllow)ZActionZEffectZResourceZDateLessThanzaws:CurrentTimez%Y-%m-%dT%H:%M:%SZZ	Conditionz
2012-10-17)Z	StatementZVersion)�datetimeZutcnowZstrftimerc�dumps)ZarnZtimeoutZ	statementZvalid_untilZpolicy_documentrrrrv�sz%OpsWorksRegister._iam_policy_documentcCsdjdd�t|j��D��S)N�
css$|]\}}d|tj|�fVqdS)z:%s: %sN)rcr�)rM�k�vrrrrPsz1OpsWorksRegister._to_ruby_yaml.<locals>.<genexpr>)r^�sorted�items)r�rrrr|	s
zOpsWorksRegister._to_ruby_yaml)N)�__name__�
__module__�__qualname__�NAME�textwrap�dedent�stripZDESCRIPTIONZ	ARG_TABLEr$r4r;r5r6r7r8r9r:rr}�staticmethodrvr|�
__classcell__rr)r-rrEsR



'L'\(rcCstjdd|�S)z9
    Cleans a name to fit IAM's naming requirements.
    z[^A-Za-z0-9+=,.@_-]+�-)�re�sub)rrrrrpsrpcCsDt|�|kr|St|dd�\}}|d||�d||d�S)z<
    Shortens a name to the given number of characters.
    ��Nz...)r]�divmod)rZ
max_length�qrRrrrrrsrr)*r�rcZloggingr�r=r�r�rhr~r�r�Zbotocore.exceptionsrZ
awscli.compatrrrZawscli.customizations.commandsrZawscli.customizations.utilsrZ	getLoggerr�rVruZ	timedeltarwrqrt�compile�IrArZr[re�lstripr{r
r
rrprrrrrr�<module>
s@


M