%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /proc/self/root/usr/lib/python2.7/site-packages/salt/cloud/clouds/
Upload File :
Create Path :
Current File : //proc/self/root/usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyo

�
���^c@@sdZddlmZmZmZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
ddlmZddlZddlZddlZddljZddlmZmZmZmZddlmZddlmZyddl Z e!Z"Wne#k
r:e$Z"nXej%e&�Z'i
d	d
6dd6d
d6dd6dd6dd6dd6dd6dd6dd6dd6dd 6d!d"6Z(d
Z)d#Z*d$Z+d%�Z,d&�Z-d'�Z.dd(�Z0ddd)�Z1dd*�Z2dd+�Z3dd,�Z4dd-�Z5dd.�Z6dd/�Z7dd0�Z8dd1�Z9d2�Z:d3�Z;d4�Z<d5�Z=dd6�Z>e$dd7�Z?dd8�Z@d9�ZAd:�ZBd;�ZCdd<�ZDd=�ZEdd>�ZFddd?�ZGdd@�ZHdA�ZIddB�ZJddC�ZKdS(Dug
AliYun ECS Cloud Module
=======================

.. versionadded:: 2014.7.0

The Aliyun cloud module is used to control access to the aliyun ECS.
http://www.aliyun.com/

Use of this module requires the ``id`` and ``key`` parameter to be set.
Set up the cloud configuration at ``/etc/salt/cloud.providers`` or
``/etc/salt/cloud.providers.d/aliyun.conf``:

.. code-block:: yaml

    my-aliyun-config:
      # aliyun Access Key ID
      id: wFGEwgregeqw3435gDger
      # aliyun Access Key Secret
      key: GDE43t43REGTrkilg43934t34qT43t4dgegerGEgg
      location: cn-qingdao
      driver: aliyun

:depends: requests
i(tabsolute_importtprint_functiontunicode_literalsN(tsha1(tquote(tSaltCloudNotFoundtSaltCloudSystemExittSaltCloudExecutionFailuretSaltCloudExecutionTimeout(tto_bytes(tsixuAliYun HangZhou Regionucn-hangzhouuAliYun BeiJing Regionu
cn-beijinguAliYun HongKong Regionucn-hongkonguAliYun QingDao Regionu
cn-qingdaouAliYun ShangHai Regionucn-shanghaiuAliYun ShenZheng Regionucn-shenzhenuAliYun DongJing Regionuap-northeast-1uAliYun XinJiaPo Regionuap-southeast-1uAliYun XiNi Regionuap-southeast-2uEU FalaKeFu Regionueu-central-1uME DiBai Regionu	me-east-1uUS FuJiNiYa Regionu	us-east-1uUS GuiGu Regionu	us-west-1u
2014-05-26ualiyuncC@s*t�tkrtSt�tkr&tStS(u)
    Check for aliyun configurations
    (tget_configured_providertFalsetget_dependenciest__virtualname__(((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyt__virtual__Zs
cC@stjttptd�S(u/
    Return the first configured instance.
    uidukey(uidukey(tconfigtis_provider_configuredt__opts__t__active_provider_name__R(((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyRgs	cC@stjtitd6�S(u*
    Warn if dependencies aren't met.
    urequests(Rtcheck_driver_dependenciesRtHAS_REQUESTS(((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyR
rscC@s�|dkrtd��nidd6}td|�}i}xT|ddD]D}i||d<x-|D]%}tj||�||d|<qgWqLW|S(	ub
    Return a dict of all available VM locations on the cloud provider with
    relevant data
    uactionufThe avail_locations function must be called with -f or --function, or with the --list-locations optionuDescribeRegionsuActiontparamsuRegionsuRegionuRegionId(RtqueryR
t	text_type(tcallRtitemstrettregiontitem((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytavail_locations|s

'c	C@s�|dkrtd��nt|t�s3i}nt�}|jdt�}d|krg|d}nidd6|d6dd6}td	|�}i}xT|d
dD]D}i||d<x-|D]%}tj||�||d|<q�Wq�W|S(
u>
    Return a list of the images that are on the provider
    uactionu`The avail_images function must be called with -f or --function, or with the --list-images optionulocationuDescribeImagesuActionuRegionIdu100uPageSizeRuImagesuImageuImageId(	Rt
isinstancetdictRtgettDEFAULT_LOCATIONRR
R(	tkwargsRtprovidertlocationRRRtimageR((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytavail_images�s(		


'cC@s�|dkrtd��nidd6}td|�}i}xT|ddD]D}i||d<x-|D]%}tj||�||d|<qgWqLW|S(	uC
    Return a list of the image sizes that are on the provider
    uactionu^The avail_sizes function must be called with -f or --function, or with the --list-sizes optionuDescribeInstanceTypesuActionRu
InstanceTypesuInstanceTypeuInstanceTypeId(RRR
R(RRRRR&R((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytavail_sizes�s

'cC@s4tjdtjd|pt�tdtdt��S(u�
    Return the aliyun region to use, in this order:
        - CLI parameter
        - VM parameter
        - Cloud profile setting
    ulocationtdefaultt
search_global(RR!Rtget_cloud_config_valueRR"R(tvm_((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytget_location�scC@s�i}idd6t�d6}t|�}xT|ddD]D}i||d<x-|D]%}tj||�||d|<qSWq8W|S(u;
    List all availability zones in the current region
    u
DescribeZonesuActionuRegionIduZonesuZoneuZoneId(R-RR
R(RRRRtzoneR((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytlist_availability_zones�s


'cC@s�|dkrtd��ni}t�}idd6|d6}t|�}tjd|d|�d|ks}|dd	kr�|SxK|d
dD];}i||d<x$|D]}||||d|<q�Wq�W|S(
u�
    Return a list of the VMs that are on the provider. Only a list of VM names,
    and their state, is returned. This is the minimum amount of information
    needed to check for existing VMs.
    uactionuAThe list_nodes_min function must be called with -f or --function.uDescribeInstanceStatusuActionuRegionIdu$Total %s instance found in Region %su
TotalCountuCodeiuInstanceStatusesuInstanceStatusu
InstanceId(RR-Rtlogtdebug(RRR%RtnodestnodeR((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytlist_nodes_min�s&	

cC@s�|dkrtd��nt�}i}xq|D]i}||}i|dd6|dd6|dd6|dd6|dd6tj|d�d6||d<q1W|S(	u;
    Return a list of the VMs that are on the provider
    uactionu=The list_nodes function must be called with -f or --function.uidunameu
public_ipsuprivate_ipsusizeustate(Rtlist_nodes_fullR
R(RR2Rt
instanceIdR3((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyt
list_nodess	

#cC@s�|dkrtd��ni}t�}idd6|d6dd6}td|�}tjd	|d
|�d|ks�|d
dkr�|S|d
d}|d
dkr�d|d<td|�}|j|d
d�nx�|D]y}|jdd�}idd6|d6}td|�}d|kr>tjd|�q�n|d}	i|dd6|	d6|dd6dd6|dd6||	<x�|D]�}
||
}|d*k	r�t	j
|�}n|
dkr�||
d ||	d!<n|
d"krd#||	kr||
d ||	d#<n|
d$krH||
d%d }|rH|||	d#<qHn|||	|
<q�Wq�Wtpgd&}
d'|
kr�|
jd'�}|d}
nt
td(<td)||
t�|S(+u;
    Return a list of the VMs that are on the provider
    uactionuBThe list_nodes_full function must be called with -f or --function.uDescribeInstanceStatusuActionuRegionIdu50uPageSizeRu$Total %s instance found in Region %su
TotalCountuCodeiuInstanceStatusesuInstanceStatusi2u2u
PageNumberu
InstanceIduuDescribeInstanceAttributeu"Query instance:%s attribute faileduInstanceNameuidunameuImageIduimageuTODOusizeuStatusustateuPublicIpAddressu	IpAddressu
public_ipsuInnerIpAddressuprivate_ipsu
VpcAttributesuPrivateIpAddressualiyunu:uupdate_cachedirucloud.cache_node_listN(RR-RR0R1tupdateR!twarningtNoneR
RRtsplittTrueRt	__utils__(RRR%Rtresulttresult_instancestatusR3R6RtnameRtvaluetvpc_ipsR$tcomps((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyR5+sp	








cC@s#tjjjtd�td|�S(uO
    Return a list of the VMs that are on the provider, with select fields
    ufunctionuquery.selection(tsalttutilstcloudtlist_nodes_selectR5R(R((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyRGzscC@s�|dkrtd��nidd6t�d6dd6}t|�}d|krUiSi}xK|d	d
D];}i||d<x$|D]}||||d|<q�WqjW|S(u)
    Return a list of security group
    uactionu=The list_nodes function must be called with -f or --function.uDescribeSecurityGroupsuActionuRegionIdu50uPageSizeuCodeuSecurityGroupsu
SecurityGroupuSecurityGroupId(RR-R(RRR>RtsgR((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytlist_securitygroup�s 


cC@s�t�}tjtjd|tdt��}|sBtd��n|ritj|�|kri||dStdj|���dS(u(
    Return the image object to use
    uimageR*uNo image specified for this VM.uImageIdu/The specified image, '{0}', could not be found.N(	R'R
RRR+RRRtformat(R,timagestvm_image((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyt	get_image�s	cC@syt�}tjd|tdt�}|s9td��n|r`tj|�|kr`||dStdj|���dS(u#
    Return the security group
    u
securitygroupR*u*No securitygroup ID specified for this VM.uSecurityGroupIdu8The specified security group, '{0}', could not be found.N(	RIRR+RRRR
RRJ(R,tsgst
securitygroup((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytget_securitygroup�s	cC@s�t�}tjtjd|tdt��}|sBtd��n|ritj|�|kri||dStdj|���dS(u6
    Return the VM's size. Used by create_node().
    usizeR*uNo size specified for this VM.uInstanceTypeIdu.The specified size, '{0}', could not be found.N(	R(R
RRR+RRRRJ(R,tsizestvm_size((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytget_size�s	cC@s�t�}tjtjd|tdt��}|sBtd��n|ritj|�|kri||dStdj|���dS(u"
    Return the VM's location
    ulocationR*u"No location specified for this VM.uRegionIdu2The specified location, '{0}', could not be found.N(	RR
RRR+RRRRJ(R,t	locationstvm_location((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyt__get_location�s	cC@s_|dkrtd��ntjd|�t|�d}idd6|d6}t|�}|S(uk
    Start a node

    CLI Examples:

    .. code-block:: bash

        salt-cloud -a start myinstance
    uactionu3The stop action must be called with -a or --action.uStarting node %su
InstanceIdu
StartInstanceuAction(RR0tinfot	_get_nodeR(R@RR6RR>((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytstart�s


cC@su|dkrtd��ntjd|�t|�d}idd6|d6tj|�j�d6}t|�}|S(u�
    Stop a node

    CLI Examples:

    .. code-block:: bash

        salt-cloud -a stop myinstance
        salt-cloud -a stop myinstance force=True
    uactionu3The stop action must be called with -a or --action.uStopping node %su
InstanceIduStopInstanceuActionu	ForceStop(RR0RWRXR
RtlowerR(R@tforceRR6RR>((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytstop	scC@s_|dkrtd��ntjd|�t|�d}idd6|d6}t|�}|S(um
    Reboot a node

    CLI Examples:

    .. code-block:: bash

        salt-cloud -a reboot myinstance
    uactionu3The stop action must be called with -a or --action.uRebooting node %su
InstanceIduRebootInstanceuAction(RR0RWRXR(R@Rtinstance_idRR>((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytreboot's


cC@s�t|t�si}nidd6|jdd�d6|jdt�d6|jdd�d	6|jd
d�d6|jdd�d
6}d
dddddddg}x5|D]-}||kr�|ji|||6�q�q�Wt|�}|dS(uK
    Convenience function to make the rest api call for node creation.
    uCreateInstanceuActionusize_iduuInstanceTypeu	region_iduRegionIduimage_iduImageIdusecuritygroup_iduSecurityGroupIdunameuInstanceNameuInternetChargeTypeuInternetMaxBandwidthInuInternetMaxBandwidthOutuHostNameuPassworduSystemDisk.Categoryu	VSwitchIdu
InstanceId(RR R!R"R8R(R#RtoptionalRR>((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytcreate_nodeAs"	
cC@s�y=|dr<tjttpd|dd|�tkr<tSWntk
rPnXtddddj|d�d	td
d|dddd
g�dtddtd�tj	d|d�i|dd6t
|�d6t|�d6t|�d6t
|�d6}d|kr|d|d<nd|kr;|d|d<nd|kratj|d�|d<nd|kr�tj|d�|d<nd |kr�|d |d!<nd"|kr�|d"|d#<nd$|kr�|d$|d%<nd&|kr�|d&|d'<ntddd(d)j|d�d	td
d*|t|��dtddtd�yt|�}Wn=tk
r�}tjd+|dtj|�d,tj�tSXtjd-�id.d/6|d06}t|�d1�}y\tjjj|d2|dfd3tjd4|td5dG�d8tjd9|td5d6��}Wn\ttfk
r�}z*yt |d�Wnt!k
rlnXWd:t!tj|���XnX|d;r�|d;d<}n,|d=r�|d=d<}ntj	d>�tStj"d?|�||d@<tdA|t�}|j#|�tj	dB|d�tj"dC|dt$j%|��tdddDdEj|d�d	td
dF|dddd
g�dtddtd�|S(Hu-
    Create a single VM from a data dict
    uprofileualiyunR,ucloud.fire_eventueventustarting createusalt/cloud/{0}/creatingunametargsucloud.filter_eventucreatinguproviderudrivertsock_dirusock_dirt	transportu	transportuCreating Cloud VM %susize_iduimage_idu	region_idusecuritygroup_idu
vswitch_idu	VSwitchIduinternet_chargetypeuInternetChargeTypeuinternet_maxbandwidthinuInternetMaxBandwidthInuinternet_maxbandwidthoutuinternet_maxbandwidthOutuInternetMaxBandwidthOutuhostnameuHostNameupassworduPasswordu
instance_nameuInstanceNameusystemdisk_categoryuSystemDisk.Categoryurequesting instanceusalt/cloud/{0}/requestingu
requestinguqError creating %s on Aliyun ECS

The following exception was thrown when trying to run the initial deployment: %stexc_info_on_logleveliu
StartInstanceuActionu
InstanceIdcS@s<t|dd�}|stS|jdd�dk	r8|SdS(NRuactionuPublicIpAddress(t
show_instanceRR!R:(tvm_nametdata((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyt__query_node_data�s
tupdate_argsttimeoutuwait_for_ip_timeoutR)i
i<tintervaluwait_for_ip_intervalNu
public_ipsiuprivate_ipsu$No available ip:cant connect to saltuVM %s is now runningussh_hostucloud.bootstrapuCreated Cloud VM '%s'u'%s' VM creation details:
%sucreated instanceusalt/cloud/{0}/createducreatediX(&Rtis_profile_configuredRRRtAttributeErrorR=RJR0RWRSRMRVRPR
RtlistR`t	ExceptionterrortloggingtDEBUGttimetsleepRRDRERFtwait_for_ipR+RRtdestroyRR1R8tpprinttpformat(R,R#RtexcRRhRgtssh_ip((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytcreatecs�	

"









	






"
c
C@s�d�}tt|j��dd��}d}x6|D].\}}|d||�d||�7}q7Wd||d�}tjt|d�|t�}tj|j	��j
�}	|	S(	u+
    Generate aliyun request signature
    cS@s�t|tj�s|S|}tjjdkrF|j�jd�}n|jtjj�jd�}t	|d�}|j
dd�}|j
dd�}|j
dd�}|S(	Nuutf8uu+u%20u*u%2Au%7Eu~(RR
tstring_typestsyststdintencodingR:tdecodetencodet_quotetreplace(tlinetstres((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytpercent_encode�stkeycS@s|dS(Ni((R((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyt<lambda>�suu&u=uGET&%2F&i(tsortedRnRthmactnewR	Rtbase64tencodestringtdigesttstrip(
t
parameterstaccess_key_secretR�tsortedParameterstcanonicalizedQueryStringtktvtstringToSigntht	signature((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyt_compute_signature�s	!#c
C@s|d}tjdt�tdt�}tjdt�tdt�}tjdtj��}idd6td6|d	6d
d6dd
6t	j
tj��d6|d6}|r�|j
|�nt||�}||d<tj|d|dt�}|jdkrtdj|j|j���ntj|j�|j}tjjj|�}	d|	krxttj|	jdi����n|	S(u0
    Make a web call to aliyun ECS REST API
    u$https://ecs-cn-hangzhou.aliyuncs.comuidR*ukeyu%Y-%m-%dT%H:%M:%SZuJSONuFormatuVersionuAccessKeyIdu1.0uSignatureVersionu	HMAC-SHA1uSignatureMethoduSignatureNonceu	TimeStampu	SignatureRtverifyi�uIAn error occurred while querying aliyun ECS. HTTP Code: {0}  Error: '{1}'uCodeuMessage(RR+RRRRststrftimetgmtimetDEFAULT_ALIYUN_API_VERSIONR
Rtuuidtuuid1R8R�trequestsR!R<tstatus_codeRRJttextR0R1turlRDREtjsontloadsRwRx(
Rtpatht
access_key_idR�t	timestampR�R�trequesttcontentR>((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyRs>

	!cC@sRtjjjtjd|t�|ttjjjtjjjt|���}|S(u-
    Return the script deployment object
    uscript(	RDRERFt	os_scriptRR+Rtsalt_config_to_yamlt
minion_config(R,t
deploy_script((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytscriptCscC@s�|dkrtd��ni}idd6|d6}td|�}xT|ddD]D}i||d	<x-|D]%}tj||�||d	|<qnWqSW|S(
u�
    Show the disk details of the instance

    CLI Examples:

    .. code-block:: bash

        salt-cloud -a show_disk aliyun myinstance
    uactionu9The show_disks action must be called with -a or --action.uDescribeInstanceDisksuActionu
InstanceIdRuDisksuDiskuDiskId(RRR
R(R@RRRRtdiskR((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyt	show_diskRs


'cC@s�|dkrtd��nt|t�s3i}ni}idd6t�d6}d|krm|d|d<ntd|�}|d	}xP|d
D]D}i||d<x-|D]%}tj||�||d|<q�Wq�W|S(u*
    Get monitor data of the instance. If instance name is
    missing, will show all the instance monitor data on the region.

    CLI Examples:

    .. code-block:: bash

        salt-cloud -f list_monitor_data aliyun
        salt-cloud -f list_monitor_data aliyun name=AY14051311071990225bd
    ufunctionu;The list_monitor_data must be called with -f or --function.uGetMonitorDatauActionuRegionIdunameu
InstanceIdRuMonitorDatauInstanceMonitorData(RRR R-RR
R(R#RRRRtmonitorDataRgR((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pytlist_monitor_dataps$	


'cC@s%|dkrtd��nt|�S(u/
    Show the details from aliyun instance
    uactionu<The show_instance action must be called with -a or --action.(RRX(R@R((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyRe�scC@sd}x]|dkreyt�|SWq	tk
ra|d8}tjd||�tjd�q	Xq	Wtdj|���dS(Niiiu<Failed to get the data for node '%s'. Remaining attempts: %sg�?u$The specified instance {0} not found(R5tKeyErrorR0R1RsRtRRJ(R@tattempts((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyRX�s


cC@s%|dkrtd��nt|t�s3i}nt�}d|krU|d}nidd6|d6|dd6}i}td	|�}d
|ks�|ddr�td
��ntjd|d|�xT|ddD]D}i||d<x-|D]%}tj	||�||d|<q�Wq�W|S(u,
    Show the details from aliyun image
    ufunctionu=The show_images function must be called with -f or --functionulocationuDescribeImagesuActionuRegionIduimageuImageIdRuCodeuImagesuImageu'The specified image could not be found.u!Total %s image found in Region %su
TotalCount(
RRR R-RRR0R1R
R(R#RR%RRRR&R((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyt
show_image�s0		

'cC@s�|dkrtd��ntddddj|�di|d6d	td
dtd�t|�d
}idd6|d
6}t|�idd6|d
6}t|�}tddddj|�di|d6d	td
dtd�|S(u�
    Destroy a node.

    CLI Example:

    .. code-block:: bash

        salt-cloud -a destroy myinstance
        salt-cloud -d myinstance
    ufunctionuEThe destroy action must be called with -d, --destroy, -a or --action.ucloud.fire_eventueventudestroying instanceusalt/cloud/{0}/destroyingRaunameRbusock_dirRcu	transportu
InstanceIduStopInstanceuActionuDeleteInstanceudestroyed instanceusalt/cloud/{0}/destroyed(RR=RJRRXR(R@RR6tstop_paramsRR3((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyRv�s6






(Lt__doc__t
__future__RRRRsRwRqR�R�R}R�thashlibRtsalt.ext.six.moves.urllib.parseRR�tsalt.utils.cloudRDtsalt.utils.datatsalt.utils.jsontsalt.configRtsalt.exceptionsRRRRtsalt.utils.stringutilsR	tsalt.extR
R�R<RtImportErrorRt	getLoggert__name__R0tALIYUN_LOCATIONSR"R�RRRR
R:RR'R(R-R/R4R7R5RGRIRMRPRSRVRYR\R^R`R{R�RR�R�R�ReRXR�Rv(((s</usr/lib/python2.7/site-packages/salt/cloud/clouds/aliyun.pyt<module>s�"



	
		
#"O						"	�	#8	(	,

Zerion Mini Shell 1.0