o
    $ºiwH  ã                   @   s:  d dl Z d dlZd dl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 d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z, d dl-m.Z. d dl/m0Z0 G dd„ dƒZ1dS )é    N)Úclient)ÚClientExceptionÚServerException)ÚAllocatePublicIpAddressRequest)ÚAuthorizeSecurityGroupRequest)ÚCreateInstanceRequest)ÚCreateKeyPairRequest)ÚCreateSecurityGroupRequest)ÚCreateVpcRequest)ÚCreateVSwitchRequest)ÚDeleteInstanceRequest)ÚDeleteInstancesRequest)ÚDeleteKeyPairsRequest)ÚDescribeInstancesRequest)ÚDescribeKeyPairsRequest)ÚDescribeSecurityGroupsRequest)ÚDescribeVpcsRequest)ÚDescribeVSwitchesRequest)ÚImportKeyPairRequest)ÚRunInstancesRequest)ÚStartInstanceRequest)ÚStopInstanceRequest)ÚStopInstancesRequest)ÚTagResourcesRequestc                   @   sð   e Zd ZdZdd„ Zd:dd„Z					
	d;dd„Z						
	d<dd„Zdd„ Zd:dd„Z	dd„ Z
dd„ Zdd„ Zdd„ Zd=dd„Zd d!„ Zd>d#d$„Zd?d&d'„Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd@d4d5„Zd@d6d7„Zd8d9„ ZdS )AÚ	AcsClientaä  
    A wrapper around Aliyun SDK. We use this wrapper in aliyun node provider.

    Parameters:
        access_key: The AccessKey ID of your aliyun account.
        access_key_secret: The AccessKey secret of your aliyun account.
        region_id: A region is a geographic area where a data center resides.
                   Region_id is the ID of region (e.g., cn-hangzhou,
                   us-west-1, etc.)
        max_retries: The maximum number of retries each connection.
    c                 C   s   t j||||d| _d S )N)ÚakÚsecretÚmax_retry_timeÚ	region_id)r   r   Úcli)ÚselfÚ
access_keyÚaccess_key_secretr   Úmax_retries© r$   úa/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/autoscaler/_private/aliyun/utils.pyÚ__init__8   s   üzAcsClient.__init__Nc                 C   sT   t ƒ }|dur| |¡ |dur| |¡ |  |¡}|dur(| d¡ d¡}|S dS )zÛQuery the details of one or more Elastic Compute Service (ECS) instances.

        :param tags: The tags of the instance.
        :param instance_ids: The IDs of ECS instances
        :return: ECS instance list
        NÚ	InstancesÚInstance)r   Úset_TagsÚset_InstanceIdsÚ_send_requestÚget)r    ÚtagsÚinstance_idsÚrequestÚresponseÚinstance_listr$   r$   r%   Údescribe_instances@   s   


zAcsClient.describe_instancesÚ	optimizedÚPostPaidÚSpotWithPriceLimitÚPayByTrafficé   c
                 C   sš   t ƒ }
|
 |¡ |
 |¡ |
 |¡ |
 |¡ |
 |¡ |
 |¡ |
 |	¡ |
 |¡ |
 	|¡ |  
|
¡}|durF| d¡}t d|¡ |S t d¡ dS )aé  Create a subscription or pay-as-you-go ECS instance.

        :param instance_type: The instance type of the ECS.
        :param image_id: The ID of the image used to create the instance.
        :param tags: The tags of the instance.
        :param key_pair_name: The name of the key pair to be bound to
                              the instance.
        :param optimized: Specifies whether the instance is I/O optimized
        :param instance_charge_type: The billing method of the instance.
                                     Default value: PostPaid.
        :param spot_strategy: The preemption policy for the pay-as-you-go
                              instance.
        :param internet_charge_type: The billing method for network usage.
                                     Default value: PayByTraffic.
        :param internet_max_bandwidth_out: The maximum inbound public
                                           bandwidth. Unit: Mbit/s.
        :return: The created instance ID.
        NÚ
InstanceIdz-instance %s created task submit successfully.úinstance created failed.)r   Úset_InstanceTypeÚset_ImageIdÚset_IoOptimizedÚset_InstanceChargeTypeÚset_SpotStrategyÚset_InternetChargeTypeÚset_InternetMaxBandwidthOutÚset_KeyPairNamer)   r+   r,   ÚloggingÚinfoÚerror)r    Úinstance_typeÚimage_idr-   Úkey_pair_namer3   Úinstance_charge_typeÚspot_strategyÚinternet_charge_typeÚinternet_max_bandwidth_outr/   r0   Úinstance_idr$   r$   r%   Úcreate_instanceR   s"   











zAcsClient.create_instanceé   c                 C   s²   t ƒ }| |¡ | |¡ | |¡ | |	¡ | |
¡ | |¡ | |¡ | |¡ | 	|¡ | 
|¡ | |¡ | |¡ |  |¡}|durR| d¡ d¡}|S t d¡ dS )aÜ  Create one or more pay-as-you-go or subscription
            Elastic Compute Service (ECS) instances

        :param instance_type: The instance type of the ECS.
        :param image_id: The ID of the image used to create the instance.
        :param tags: The tags of the instance.
        :param security_group_id: The ID of the security group to which to
                                  assign the instance. Instances in the same
                                  security group can communicate with
                                  each other.
        :param vswitch_id: The ID of the vSwitch to which to connect
                           the instance.
        :param key_pair_name: The name of the key pair to be bound to
                              the instance.
        :param amount: The number of instances that you want to create.
        :param optimized: Specifies whether the instance is I/O optimized
        :param instance_charge_type: The billing method of the instance.
                                     Default value: PostPaid.
        :param spot_strategy: The preemption policy for the pay-as-you-go
                              instance.
        :param internet_charge_type: The billing method for network usage.
                                     Default value: PayByTraffic.
        :param internet_max_bandwidth_out: The maximum inbound public
                                           bandwidth. Unit: Mbit/s.
        :return: The created instance IDs.
        NÚInstanceIdSetsÚInstanceIdSetr9   )r   r:   r;   r<   r=   r>   r?   r@   r)   Ú
set_AmountÚset_SecurityGroupIdÚset_VSwitchIdrA   r+   r,   rB   rD   )r    rE   rF   r-   Úsecurity_group_idÚ
vswitch_idrG   Úamountr3   rH   rI   rJ   rK   r/   r0   r.   r$   r$   r%   Úrun_instancesƒ   s&   )













zAcsClient.run_instancesc                 C   s4   t ƒ }| |¡ |  |¡}|dur| d¡}|S dS )z¸Create a security group

        :param vpc_id: The ID of the VPC in which to create
                       the security group.
        :return: The created security group ID.
        NÚSecurityGroupId)r	   Ú	set_VpcIdr+   r,   )r    Úvpc_idr/   r0   rT   r$   r$   r%   Úcreate_security_groupÁ   s   


zAcsClient.create_security_groupc                 C   s^   t ƒ }|dur| |¡ |dur| |¡ |  |¡}|dur(| d¡ d¡}|S t d¡ dS )zÞQuery basic information of security groups.

        :param vpc_id: The ID of the VPC to which the security group belongs.
        :param tags: The tags of the security group.
        :return: Security group list.
        NÚSecurityGroupsÚSecurityGroupzdescribe security group failed.)r   rY   r)   r+   r,   rB   rD   )r    rZ   r-   r/   r0   Úsecurity_groupsr$   r$   r%   Údescribe_security_groupsÐ   s   



z"AcsClient.describe_security_groupsc                 C   s<   t ƒ }| |¡ | |¡ | |¡ | |¡ |  |¡ dS )aÅ  Create an inbound security group rule.

        :param ip_protocol: The transport layer protocol.
        :param port_range: The range of destination ports relevant to
                           the transport layer protocol.
        :param security_group_id: The ID of the destination security group.
        :param source_cidr_ip: The range of source IPv4 addresses.
                               CIDR blocks and IPv4 addresses are supported.
        N)r   Úset_IpProtocolÚset_PortRangerR   Úset_SourceCidrIpr+   )r    Úip_protocolÚ
port_rangerT   Úsource_cidr_ipr/   r$   r$   r%   Úauthorize_security_groupã   s   



z"AcsClient.authorize_security_groupc                 C   sP   t ƒ }| |¡ | |¡ | |¡ |  |¡}|dur | d¡S t d|¡ dS )a?  Create vSwitches to divide the VPC into one or more subnets

        :param vpc_id: The ID of the VPC to which the VSwitch belongs.
        :param zone_id: The ID of the zone to which
                        the target VSwitch belongs.
        :param cidr_block: The CIDR block of the VSwitch.
        :return:
        NÚ	VSwitchIdz!create_v_switch vpc_id %s failed.)r   Ú
set_ZoneIdrY   Úset_CidrBlockr+   r,   rB   rD   )r    rZ   Úzone_idÚ
cidr_blockr/   r0   r$   r$   r%   Úcreate_v_switchö   s   	




zAcsClient.create_v_switchc                 C   s&   t ƒ }|  |¡}|dur| d¡S dS )zUCreates a virtual private cloud (VPC).

        :return: The created VPC ID.
        NÚVpcId)r
   r+   r,   ©r    r/   r0   r$   r$   r%   Ú
create_vpc
  s
   

zAcsClient.create_vpcc                 C   s,   t ƒ }|  |¡}|dur| d¡ d¡S dS )zJQueries one or more VPCs in a region.

        :return: VPC list.
        NÚVpcsÚVpc)r   r+   r,   rn   r$   r$   r%   Údescribe_vpcs  s
   
zAcsClient.describe_vpcsÚinstancec                 C   sV   t ƒ }| |¡ | |¡ | |¡ |  |¡}|dur#t d|¡ dS t d|¡ dS )zÖCreate and bind tags to specified ECS resources.

        :param resource_ids: The IDs of N resources.
        :param tags: The tags of the resource.
        :param resource_type: The type of the resource.
        Nz$instance %s create tag successfully.zinstance %s create tag failed.)r   r)   Úset_ResourceTypeÚset_ResourceIdsr+   rB   rC   rD   )r    Úresource_idsr-   Úresource_typer/   r0   r$   r$   r%   Útag_resource   s   



zAcsClient.tag_resourcec                 C   sB   t ƒ }| |¡ |  |¡}|durt d|¡ dS t d|¡ dS )zQStart an ECS instance.

        :param instance_id: The Ecs instance ID.
        Nzinstance %s start successfully.zinstance %s start failed.)r   Úset_InstanceIdr+   rB   rC   rD   ©r    rL   r/   r0   r$   r$   r%   Ústart_instance1  s   

zAcsClient.start_instanceFc                 C   s4   t ƒ }| |¡ | |¡ t d|¡ |  |¡ dS )zÊStop an ECS instance that is in the Running state.

        :param instance_id: The Ecs instance ID.
        :param force_stop: Specifies whether to forcibly stop the instance.
        :return:
        z$Stop %s command submit successfully.N)r   ry   Úset_ForceStoprB   rC   r+   )r    rL   Ú
force_stopr/   r$   r$   r%   Ústop_instance?  s
   

zAcsClient.stop_instanceÚStopChargingc                 C   s>   t ƒ }| |¡ | |¡ |  |¡}|du rt d¡ dS dS )a  Stop one or more ECS instances that are in the Running state.

        :param instance_ids: The IDs of instances.
        :param stopped_mode: Specifies whether billing for the instance
                             continues after the instance is stopped.
        Nzstop_instances failed)r   r*   Úset_StoppedModer+   rB   rD   )r    r.   Ústopped_moder/   r0   r$   r$   r%   Ústop_instancesL  s   


ÿzAcsClient.stop_instancesc                 C   s4   t ƒ }| |¡ | d¡ t d|¡ |  |¡ dS )z¨Release a pay-as-you-go instance or
            an expired subscription instance.

        :param instance_id: The ID of the instance that you want to release.
        Tz%Delete %s command submit successfullyN)r   ry   Ú	set_ForcerB   rC   r+   )r    rL   r/   r$   r$   r%   Údelete_instanceZ  s
   

zAcsClient.delete_instancec                 C   s(   t ƒ }| d¡ | |¡ |  |¡ dS )z°Release one or more pay-as-you-go instances or
            expired subscription instances.

        :param instance_ids: The IDs of instances that you want to release.
        TN)r   rƒ   r*   r+   )r    r.   r/   r$   r$   r%   Údelete_instancesf  s   

zAcsClient.delete_instancesc                 C   s0   t ƒ }| |¡ |  |¡}|dur| d¡S dS )zÚAssign a public IP address to an ECS instance.

        :param instance_id: The ID of the instance to which you want to
                            assign a public IP address.
        :return: The assigned ip.
        NÚ	IpAddress)r   ry   r+   r,   rz   r$   r$   r%   Úallocate_public_addressq  s   


ÿz!AcsClient.allocate_public_addressc                 C   sF   t ƒ }| |¡ |  |¡}|durt d| d¡¡ |S t d¡ dS )z„Create an SSH key pair.

        :param key_pair_name: The name of the key pair.
        :return: The created keypair data.
        NzCreate Key Pair %s SuccessfullyÚ	KeyPairIdzCreate Key Pair Failed)r   rA   r+   rB   rC   r,   rD   ©r    rG   r/   r0   r$   r$   r%   Úcreate_key_pair~  s   


zAcsClient.create_key_pairc                 C   s(   t ƒ }| |¡ | |¡ |  |¡ dS )zéImport the public key of an RSA-encrypted key pair
            that is generated by a third-party tool.

        :param key_pair_name: The name of the key pair.
        :param public_key_body: The public key of the key pair.
        N)r   rA   Úset_PublicKeyBodyr+   )r    rG   Úpublic_key_bodyr/   r$   r$   r%   Úimport_key_pairŽ  s   

zAcsClient.import_key_pairc                 C   s   t ƒ }| |¡ |  |¡ dS )zuDelete one or more SSH key pairs.

        :param key_pair_names: The name of the key pair.
        :return:
        N)r   Úset_KeyPairNamesr+   )r    Úkey_pair_namesr/   r$   r$   r%   Údelete_key_pairsš  s   
zAcsClient.delete_key_pairsc                 C   s>   t ƒ }|dur| |¡ |  |¡}|dur| d¡ d¡S dS )zoQuery one or more key pairs.

        :param key_pair_name: The name of the key pair.
        :return:
        NÚKeyPairsÚKeyPair)r   rA   r+   r,   r‰   r$   r$   r%   Údescribe_key_pairs¤  s   

zAcsClient.describe_key_pairsc                 C   sH   t ƒ }|dur| |¡ |  |¡}|dur| d¡ d¡S t d¡ dS )zŽQueries one or more VSwitches.

        :param vpc_id: The ID of the VPC to which the VSwitch belongs.
        :return: VSwitch list.
        NÚ	VSwitchesÚVSwitchzDescribe VSwitches Failed.)r   rY   r+   r,   rB   rD   )r    rZ   r/   r0   r$   r$   r%   Údescribe_v_switches³  s   


zAcsClient.describe_v_switchesc              
   C   sj   |  d¡ z| j |¡}t |¡}|W S  ttfy4 } zt | 	¡ ¡ t |¡ W Y d}~dS d}~ww )zsend open api requestÚjsonN)
Úset_accept_formatr   Údo_action_with_exceptionr—   Úloadsr   r   rB   rD   Úget_action_name)r    r/   Úresponse_strÚresponse_detailÚer$   r$   r%   r+   Ã  s   


€ýzAcsClient._send_request)NN)r3   r4   r5   r6   r7   )rN   r3   r4   r5   r6   rN   )rs   )F)r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   r2   rM   rW   r[   r_   rf   rl   ro   rr   rx   r{   r~   r‚   r„   r…   r‡   rŠ   r   r   r“   r–   r+   r$   r$   r$   r%   r   +   sH    

ö9
ó>






r   )2r—   rB   Úaliyunsdkcorer   Ú&aliyunsdkcore.acs_exception.exceptionsr   r   Ú=aliyunsdkecs.request.v20140526.AllocatePublicIpAddressRequestr   Ú<aliyunsdkecs.request.v20140526.AuthorizeSecurityGroupRequestr   Ú4aliyunsdkecs.request.v20140526.CreateInstanceRequestr   Ú3aliyunsdkecs.request.v20140526.CreateKeyPairRequestr   Ú9aliyunsdkecs.request.v20140526.CreateSecurityGroupRequestr	   Ú/aliyunsdkecs.request.v20140526.CreateVpcRequestr
   Ú3aliyunsdkecs.request.v20140526.CreateVSwitchRequestr   Ú4aliyunsdkecs.request.v20140526.DeleteInstanceRequestr   Ú5aliyunsdkecs.request.v20140526.DeleteInstancesRequestr   Ú4aliyunsdkecs.request.v20140526.DeleteKeyPairsRequestr   Ú7aliyunsdkecs.request.v20140526.DescribeInstancesRequestr   Ú6aliyunsdkecs.request.v20140526.DescribeKeyPairsRequestr   Ú<aliyunsdkecs.request.v20140526.DescribeSecurityGroupsRequestr   Ú2aliyunsdkecs.request.v20140526.DescribeVpcsRequestr   Ú7aliyunsdkecs.request.v20140526.DescribeVSwitchesRequestr   Ú3aliyunsdkecs.request.v20140526.ImportKeyPairRequestr   Ú2aliyunsdkecs.request.v20140526.RunInstancesRequestr   Ú3aliyunsdkecs.request.v20140526.StartInstanceRequestr   Ú2aliyunsdkecs.request.v20140526.StopInstanceRequestr   Ú3aliyunsdkecs.request.v20140526.StopInstancesRequestr   Ú2aliyunsdkecs.request.v20140526.TagResourcesRequestr   r   r$   r$   r$   r%   Ú<module>   s4    