python 调用阿里云服务器api创建服务器
首先安装阿里云SDK
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-ecs
可以配合jenkins传递参数
#!/usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2018/1/26 0026 12:01 # @Author : sanmuyan # @FileName: aliyun_ecs_cre.py import json,time,os from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkecs.request.v20140526 import CreateInstanceRequest,StartInstanceRequest,DescribeInstancesRequest AccessKey = os.environ.get('AccessKey') Secret = os.environ.get('Secret') Regions = os.environ.get('Regions') # 创建 AcsClient 实例 client = AcsClient( AccessKey, Secret, Regions ); def ecs_start(InstanceId): request = StartInstanceRequest.StartInstanceRequest() request.set_InstanceId(InstanceId) response = client.do_action_with_exception(request) print(response,'\n',"INFO:Starting results") def ecs_des(InstanceId): request = DescribeInstancesRequest.DescribeInstancesRequest() request.set_InstanceIds(InstanceId) response = client.do_action_with_exception(request) data = json.loads(response) data = json.dumps(data, sort_keys=True, indent=2) print(data) def ecs_cre(): request = CreateInstanceRequest.CreateInstanceRequest() request.set_ImageId(ImageId) request.set_InstanceType(InstanceType) request.set_SecurityGroupId(SecurityGroupId) request.set_ZoneId(ZoneId) request.set_InstanceName(InstanceName) request.set_HostName(HostName) request.set_Password(Password) request.set_SystemDiskCategory(SystemDiskCategory) request.set_DataDisks(({'DataDisk':'1'},{'Size':DataDiskSize},{'Category':'cloud_efficiency'})) request.set_SystemDiskSize(SystemDiskSize) request.set_VSwitchId(VSwitchId) request.set_InstanceChargeType(InstanceChargeType) request.set_Period(Period) if (Period == "PrePaid"): request.set_AutoRenew(AutoRenew) request.set_AutoRenewPeriod(AutoRenewPeriod) response = client.do_action_with_exception(request) response = client.do_action_with_exception(request) data = json.loads(response) print(data,'\n'"INFO:Create success") InstanceId = (data['InstanceId']) time.sleep(10) ecs_start(InstanceId=InstanceId) time.sleep(20) ecs_des(InstanceId=[InstanceId]) if __name__ == '__main__': ImageId = os.environ.get('ImageId') # 镜像 InstanceType = os.environ.get('InstanceType') # 实例规格 SecurityGroupId = os.environ.get('SecurityGroupId') # 安全组 ZoneId = os.environ.get('ZoneId') # 可用区 InstanceName = os.environ.get('InstanceName') # 实例名 HostName = os.environ.get('HostName') # 主机名 Password = os.environ.get('Password') # 主机密码 SystemDiskCategory = os.environ.get('SystemDiskCategory') # 系统盘类型 SystemDiskSize = os.environ.get('SystemDiskSize') # 系统盘大小 DataDiskSize = os.environ.get('DataDiskSize') # 数据盘大小 VSwitchId = os.environ.get('VSwitchId') # 交换机 InstanceChargeType = os.environ.get('InstanceChargeType') # 实例付费类型 Period = os.environ.get('Period') # 购买时长 AutoRenew = os.environ.get('AutoRenew') # 是否开启自动续费 AutoRenewPeriod = os.environ.get('AutoRenewPeriod') # 自动续费时长 ecs_cre()