# #!/usr/bin/env python
# #coding=utf-8
#
import  json
import logging
import time
import requests
import  sys
from aliyunsdkcore import client
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526.CreateInstanceRequest import CreateInstanceRequest
from aliyunsdkecs.request.v20140526.StartInstanceRequest import StartInstanceRequest
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
from aliyunsdkecs.request.v20140526.RunInstancesRequest import RunInstancesRequest
from aliyunsdkecs.request.v20140526.DescribeNetworkInterfacesRequest import DescribeNetworkInterfacesRequest
from aliyunsdkslb.request.v20140515.AddBackendServersRequest import AddBackendServersRequest
from aliyunsdkecs.request.v20140526.RunCommandRequest import RunCommandRequest


logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S')
ak_id = "LTAI4GBWbFvvXoXsSVBe1o9f"
ak_secret = "kRAikWitb4kDxaAyBqNrmLmllMEDO3"
region_id = "cn-hangzhou"
clt = client.AcsClient(ak_id, ak_secret, region_id)
instance_type = "ecs.c6.2xlarge"
vswitch_id = "vsw-bp19lpjwtc6j0p0m9mdc2"
#image_id = "m-bp15xqcuacm4zw2h2gi6"
image_id = "m-bp12bkuvg20k6ueqmb4v"
security_group_id = "sg-bp1irhrkr4vfj272hk4y"
amount = int(sys.argv[1])
AccessKey = 'LTAIuPbTPL3LDDKN'
AccessSecret = 'ORcNedKwWuwVtcq4IRFtUDZgS0b1le'
RegionId = 'cn-hangzhou'
disk_size = "200"
instance_name = "ESS-vlogapi-[1,2]"
zone_id = "cn-hangzhou-h"
disk_category = "cloud_efficiency"
key_pair_name = "stuuudy"
#slb_id = "lb-bp1r9duz4k7z0riz9zs3n"
#vlogapi
slb_id_2 = "lb-bp1qk9mkvjtundlzz7owm"
slb_id_3 = "lb-bp1akia6vlguiwg4znaq8"
slb_id_4 = "lb-bp1pj2v06ladvgftgxcp0"
slb_id_5 = "lb-bp1y63rnrb2e64whryghz"

client = AcsClient (AccessKey, AccessSecret, RegionId)


def addBackendServers(slb_id,instance_id):
    request = AddBackendServersRequest()
    request.set_accept_format('json')
    ipaddr = getIpaddr(instance_id)
    request.set_LoadBalancerId(slb_id)

    request.set_BackendServers([{"ServerId": instance_id, "Weight": "100", "Type": "ecs", "ServerIp": ipaddr}])
    response = client.do_action_with_exception(request)
    logging.info(response)

def getIpaddr(instance_id):
  request = DescribeNetworkInterfacesRequest()
  request.set_accept_format('json')
  request.set_InstanceId(instance_id)
  response = clt.do_action_with_exception(request)
  request_content = json.loads(response)
  ipaddr = request_content['NetworkInterfaceSets']['NetworkInterfaceSet'][0]['PrivateIpAddress']
  return  ipaddr

def healthCheck(instance_id):
  ipaddr = getIpaddr(instance_id)
  while True:
      health_url = 'http://%s:8182/longvideoapi/test' %(ipaddr)
      http_code = requests.get(health_url).status_code
      if  http_code == 200:
          break
      else:
          time.sleep(10)
  time.sleep(300)
#  addBackendServers(slb_id_1,instance_id)
  addBackendServers(slb_id_2,instance_id)
  addBackendServers(slb_id_3,instance_id)
  addBackendServers(slb_id_4,instance_id)
  addBackendServers(slb_id_5,instance_id)




# 创建ECS实例并启动。
def create_multiple_instances():
    request = build_request()
    request.set_Amount(amount)
    _execute_request(request)


def _execute_request(request):
    response = _send_request(request)
    if response.get('Code') is None:
        instance_ids = response.get('InstanceIdSets').get('InstanceIdSet')
        running_amount = 0
        while running_amount < amount:
            time.sleep(10)
            running_amount = check_instance_running(instance_ids)


    print(" %s is running" %(instance_ids))
    time.sleep(60)
    runCommand(instance_ids)
    for  instance_id  in  instance_ids:
      healthCheck(instance_id)


def check_instance_running(instance_ids):
    request = DescribeInstancesRequest()
    request.set_InstanceIds(json.dumps(instance_ids))
    response = _send_request(request)
    if response.get('Code') is None:
        instances_list = response.get('Instances').get('Instance')
        running_count = 0
        for instance_detail in instances_list:
            if instance_detail.get('Status') == "Running":
                running_count += 1
        return running_count
#购买服务器参数配置
def build_request():
    request = RunInstancesRequest()
    request.set_ImageId(image_id)
    request.set_VSwitchId(vswitch_id)
    request.set_SecurityGroupId(security_group_id)
    request.set_ZoneId(zone_id)
    request.set_InstanceType(instance_type)
    # request.set_UserData(init_data)
    request.set_InstanceName(instance_name)
    request.set_SystemDiskSize(disk_size)
    request.set_SystemDiskCategory(disk_category)
    request.set_KeyPairName(key_pair_name)
    request.set_Tags([
      {
        "Key": "ecs",
        "Value": "vlogapi.prod"
      } 
    ])
    return request

#批量执行shell脚本
def  runCommand(instance_ids):
  request = RunCommandRequest()
  request.set_accept_format('json')

  request.set_Type("RunShellScript")
  request.set_CommandContent("sh /home/sh/start.sh")
  request.set_InstanceIds(instance_ids)

  response = clt.do_action_with_exception(request)
  logging.info(response)
# 发送API请求
def _send_request(request):
    request.set_accept_format('json')
    try:
        response_str = clt.do_action(request)
        logging.info(response_str)
        response_detail = json.loads(response_str)
        return response_detail
    except Exception as e:
        logging.error(e)

if __name__ == '__main__':
    print ("Start Create Instances")
    # 创建ECS实例并启动。
    create_multiple_instances()



