模板檔案
{ "ROSTemplateFormatVersion": "2015-09-01", "Parameters": { "Description": { "Type": "String", "Description": "Description of the instance, [2, 256] characters. Do not fill or empty, the default is empty.", "Default": "msync" }, "DataDiskName": { "Type": "String", "Default": "datadisk" }, "DataDiskSize": { "Type": "Number", "Default": 100 }, "SystemDiskSize": { "Type": "Number", "Description": "Disk size of the system disk, range from 40 to 500 GB. If you specify with your own image, make sure the system disk size bigger than image size. ", "MinValue": 40, "MaxValue": 500, "Default": 80 }, "MaxAmount": { "Type": "Number", "Description": "Max number of instances to create, should be smaller than 'MaxAmount' and smaller than 100.", "MinValue": 1, "MaxValue": 100, "Default": 1 }, "NetworkType": { "Type": "String", "Description": "Instance network type. Support 'vpc' and 'classic', for compatible reason, default is 'classic'. If vswitch id and vpc id is specified, the property will be forced to be set to 'vpc' ", "Default": "classic" }, "MinAmount": { "Type": "Number", "Description": "Max number of instances to create, should be bigger than 'MinAmount' and smaller than 100.", "MinValue": 1, "MaxValue": 100, "Default": 1 }, "ImageId": { "Type": "String", "Description": "Image ID to create ecs instance.", "Default": "m-2ze5ahokm5p8zeblr3sm" }, "InstanceType": { "Type": "String", "Description": "Ecs instance supported instance type, make sure it should be correct.", "Default": "ecs.s3.large" }, "AllocatePublicIP": { "Type": "Boolean", "Description": "The public ip for ecs instance, if properties is true, will allocate public ip. If property InternetMaxBandwidthOut set to 0, it will not assign public ip.", "Default": false }, "HostName": { "Type": "String", "Description": "Host name of created ecs instance. at least 2 characters, and '.' '-' Is not the first and last characters as hostname, not continuous use. Windows platform can be up to 15 characters, allowing letters (without limiting case), numbers and '-', and does not support the number of points, not all is digital ('.').Other (Linux, etc.) platform up to 30 characters, allowing support number multiple points for the period between the points, each permit letters (without limiting case), numbers and '-' components.", "Default": "ebs-ali-beijing-msync" }, "Password": { "Type": "String", "Description": "Password of created ecs instance. Must contain at least 3 types of special character, lower character, upper character, number.", "Default": "passwordishere" }, "IoOptimized": { "Type": "String", "Description": "The 'optimized' instance can provide better IO performance. Support 'none' and 'optimized' only, default is 'none'.", "Default": "optimized" }, "ZoneId": { "Type": "String", "Description": "current zone to create the instance.", "Default": "cn-beijing-a" }, "SecurityGroupId": { "Type": "String", "Description": "Security group to create ecs instance. For classic instance need the security group not belong to VPC, for VPC instance, please make sure the security group belong to specified VPC.", "Default": "sg-2zegx612vcgzlkzpydsi" }, "InternetChargeType": { "Type": "String", "Description": "Instance internet access charge type.Support 'PayByBandwidth' and 'PayByTraffic' only. Default is PayByTraffic", "Default": "PayByBandwidth" }, "SystemDiskCategory": { "Type": "String", "Description": "Category of system disk. Default is cloud.support cloud|cloud_efficiency|cloud_ssd|ephemeral_ssd", "Default": "cloud_efficiency" }, "InstanceName": { "Type": "String", "Description": "Display name of the instance, [2, 128] English or Chinese characters, must start with a letter or Chinese in size, can contain numbers, '_' or '.', '-'", "Default": "msync" }, "InternetMaxBandwidthOut": { "Type": "Number", "Description": "Set internet output bandwidth of instance. Unit is Mbps(Mega bit per second). Range is [0,100]. Default is 1.While the property is not 0, public ip will be assigned for instance.", "MinValue": 0, "MaxValue": 200, "Default": 1 }, "InternetMaxBandwidthIn": { "Type": "Number", "Description": "Max internet out band width setting, unit in Mbps(Mega bit per second). The range is [1,200], default is 200 Mbps.", "MinValue": 1, "MaxValue": 200, "Default": 200 } }, "Resources": { "InstanceGroup": { "Type": "ALIYUN::ECS::InstanceGroup", "Properties": { "Description": { "Ref": "Description" }, "DiskMappings": [ { "Size": { "Ref": "DataDiskSize" }, "DiskName": { "Ref": "DataDiskName" } } ], "SystemDiskSize": { "Ref": "SystemDiskSize" }, "MaxAmount": { "Ref": "MaxAmount" }, "NetworkType": { "Ref": "NetworkType" }, "MinAmount": { "Ref": "MinAmount" }, "ImageId": { "Ref": "ImageId" }, "InstanceType": { "Ref": "InstanceType" }, "AllocatePublicIP": { "Ref": "AllocatePublicIP" }, "HostName": { "Ref": "HostName" }, "Password": { "Ref": "Password" }, "IoOptimized": { "Ref": "IoOptimized" }, "ZoneId": { "Ref": "ZoneId" }, "SecurityGroupId": { "Ref": "SecurityGroupId" }, "InternetChargeType": { "Ref": "InternetChargeType" }, "InstanceName": { "Ref": "InstanceName" }, "InternetMaxBandwidthOut": { "Ref": "InternetMaxBandwidthOut" }, "InternetMaxBandwidthIn": { "Ref": "InternetMaxBandwidthIn" } } } }, "Outputs": { "HostNames": { "Description": "Host names of created instance.", "Value": { "Fn::GetAtt": [ "InstanceGroup", "HostNames" ] } }, "InnerIps": { "Description": "Inner IP address list of the specified instance. Only for classical instance.", "Value": { "Fn::GetAtt": [ "InstanceGroup", "InnerIps" ] } }, "ZoneIds": { "Description": "Zone id of created instance.", "Value": { "Fn::GetAtt": [ "InstanceGroup", "ZoneIds" ] } }, "OrderId": { "Description": "The order id list of created instance.", "Value": { "Fn::GetAtt": [ "InstanceGroup", "OrderId" ] } }, "InstanceIds": { "Description": "The instance id list of created ecs instance", "Value": { "Fn::GetAtt": [ "InstanceGroup", "InstanceIds" ] } } } }
程式碼:
class AliyunROS(object): def __init__(self, region_id, template_render): tpl = Environment(loader=FileSystemLoader(searchpath='./')) \ .get_template('msync.template') template = tpl.render(template_render.get(region_id)) self.region_id = region_id self.client = AcsClient('aaa', 'bbb', region_id) self.create_stack_body = ''' { "Name": "%s", "TimeoutMins": %d, "DisableRollback": false, "Template": %s } ''' % ('msync_%s' % (time.strftime('%Y%m%d_%H%M%S')), 15, template.encode('utf-8')) print(self.create_stack_body) raise SystemExit def create_ecs_instace(self): """ :return: {"Id":"String", "Name":"String"} """ req = CreateStacksRequest.CreateStacksRequest() req.set_headers({'x-acs-region-id': self.region_id}) req.set_content(self.create_stack_body) return self.client.do_action_with_exception(req)