Beispiel #1
0
 def fetch_rds_instances(self):
     """
     获取所有rds实例id
     """
     ins_list = []
     client = self.connect()
     request = DescribeDBInstancesRequest()
     request.set_accept_format('json')
     response_str = client.do_action_with_exception(request).decode("utf-8")
     response_dict = json.loads(response_str)['Items']['DBInstance']
     for i in response_dict:
         ins_list.append(i['DBInstanceId'])
     return ins_list
Beispiel #2
0
 def get_rds_id(self, p_name):
     request = DescribeDBInstancesRequest()
     request.set_accept_format('json')
     try:
         response = self._client.do_action_with_exception(request)
         info = json.loads(str(response, encoding='utf-8'))
         db_instance = info["Items"]["DBInstance"]
         for instance in db_instance:
             if p_name == instance["DBInstanceDescription"]:
                 rds_instance = instance["DBInstanceId"]
         return rds_instance
     except:
         print('%s 获取RDS实例失败!!!!' % p_name)
 def query_rds_instance_list(self):
     # query_rds_instance_list用于请求RDS数据库实例和返回数据库实例状态列表
     page_num = 1
     request = DescribeDBInstancesRequest()
     request.set_PageSize(self.page_size)
     request.set_accept_format('json')
     rds_instance_list = []
     now = datetime.datetime.now().timestamp()
     while True:
         try:
             request.set_PageNumber(page_num)
             response = json.loads(
                 self.client.do_action_with_exception(request).decode(
                     'utf-8'))
             api_request_summry.labels(
                 api='DescribeDBInstancesRequest').observe(
                     amount=(datetime.datetime.now().timestamp() - now))
             api_request_count.inc()
         except Exception as e:
             logging.error('Error request Aliyun api', exc_info=e)
             api_request_failed_summry.labels(
                 api='DescribeDBInstancesRequest').observe(
                     amount=(datetime.datetime.now().timestamp() - now))
             api_request_count.inc()
             return []
         if response['PageRecordCount'] == 0:
             break
         DBInstance_list = response['Items']['DBInstance']
         rds_instance_list.extend(DBInstance_list)
         page_num += 1
     logging.debug("size of rds_instance_list = {}".format(
         sys.getsizeof(rds_instance_list)))
     return rds_instance_list
 def get_instance_list(self, page_size=30):
     """
     获取RDS实例列表
     :return list: 返回嵌套了实例信息字典的实例列表
     """
     rds_instance_list = list()
     request = DescribeDBInstancesRequest()
     request.set_accept_format('json')
     request.set_PageSize(page_size)
     received_item = 0
     page_num = 1
     while True:
         request.set_PageNumber(page_num)
         response = self.AcsClient.do_action_with_exception(request)
         response_dic = json.loads(response)
         total_count = response_dic["TotalRecordCount"]
         instances = response_dic['Items']['DBInstance']
         for rds_instance in instances:
             rds_instance_info = dict()
             rds_instance_info["InstanceID"] = rds_instance['DBInstanceId']
             rds_instance_info["DBInstanceDescription"] = rds_instance[
                 'DBInstanceDescription']
             rds_instance_list.append(rds_instance_info)
         received_item += page_size
         if total_count > received_item:  # 翻页
             page_num += 1
         else:
             return rds_instance_list
Beispiel #5
0
 def getAliCloundRdsList(self,RegionId='cn-shenzhen',PageSize=15,Page=1,Tags=[]):
     client=AcsClient(self.secreyKey,self.accesssecret,RegionId)
     req=DescribeDBInstancesRequest()
     req.set_PageSize(PageSize)
     req.set_PageNumber(Page)
     if len(Tags)>0:
         req.set_Tags(Tags)
     req.set_accept_format('json')
     try:
         data=client.do_action_with_exception(req)
         if data:
             return data
         else:
             return False
     except Exception as e:
         logger.error(e)
         return False
Beispiel #6
0
 def get_instance_list(self):
     request = DescribeDBInstancesRequest()
     request.set_accept_format('json')
     response = self.client.do_action_with_exception(request)
     instances = json.loads(response)['Items']['DBInstance']
     for rds_instance in instances:
         try:
             rds_instance_info = dict()
             rds_instance_info["{#DBINSTANCEID}"] = rds_instance[
                 'DBInstanceId']
             rds_instance_info["{#DBINSTANCEDESCRIPTION}"] = rds_instance[
                 'DBInstanceDescription']
             self.rds_instance_list.append(rds_instance_info)
         except Exception as e:
             print(Exception, ":", e)
     else:
         return self.rds_instance_list
Beispiel #7
0
    def get_rdses(self, page_num=1, page_size=20):
        request = DescribeDBInstancesRequest()
        request.set_accept_format('json')
        request.set_PageNumber(page_num)
        request.set_PageSize(page_size)

        data = self._request(request)
        total = data.get('TotalRecordCount')
        data = data.get('Items')
        data_list = data.get('DBInstance')
        data = {
            'total': total,
            'data_list': data_list,
        }
        return data
Beispiel #8
0
    def get_db_instances(self, page_size=30, page_number=1):
        """
        :param aliyun_region:
        :param page_size:
        :param page_number:
        :return: db instances
        """
        request = DescribeDBInstancesRequest()
        request.set_accept_format('json')
        # request.add_query_param('RegionId', aliyun_region)
        request.add_query_param('PageSize', page_size)
        request.add_query_param('PageNumber', page_number)
        try:
            response = self._connection.do_action_with_exception(request)
            instance_dict = json.loads(response)
        except Exception as e:
            print(e)


        instances = list()
        for instance in instance_dict['Items']['DBInstance']:
            instance = (instance['DBInstanceId'], instance['DBInstanceDescription'], instance['RegionId'])
            instances.append(instance)
        return instances
 def __get_total_page_num(self, PageNum=1, PageSize=1):
     '''
     获取RDS总数,及当前页RDS列表
     :param PageNum: 页ID
     :param PageSize: 页大小
     :return:
     '''
     request = DescribeDBInstancesRequest()
     request.set_PageNumber(PageNum)
     request.set_PageSize(PageSize)
     response = self.__do_action(request)
     if self.TotalPageNum != 0:
         ins_obj = response['Items']['DBInstance']
         self.instance_list_total.extend(ins_obj)
         return
     else:
         if int(response['TotalRecordCount']) % self.PageSize != 0:
             self.TotalPageNum = int(
                 response['TotalRecordCount'] / self.PageSize) + 1
         else:
             self.TotalPageNum = int(response['TotalRecordCount'] /
                                     self.PageSize)
         return self.TotalPageNum
Beispiel #10
0
 def set_desc_request(self):
     request = DescribeDBInstancesRequest()
     request.set_accept_format('json')
     request.set_PageNumber(self.page_number)
     request.set_PageSize(self.page_size)
     return request
 def __init__(self, client):
     super(AliyunRds, self).__init__()
     self.client = client
     self.request = DescribeDBInstancesRequest()
     self.product = 'rds'
Beispiel #12
0
def handler(event, context):
  logger = logging.getLogger()
  logger.info(event)

  ACCESS_KEY_ID = os.environ['ACCESS_KEY_ID']
  ACCESS_KEY_SECRET = os.environ['ACCESS_KEY_SECRET']

  # RDS 確認用の初期設定, max time = 240s
  LIMITTED_COUNT  = 10
  SLEEP_TIME      = 24

  # ECS, RDS関連初期設定
  InstanceIdSet = []
  UserName      = ""
  Region        = ""
  TagName       = os.environ['TAG_NAME']

  # OSS関連初期設定
  Event       = json.loads(event.decode('utf-8').replace("'", '"'))
  OssRegion   = Event["events"][0]["region"]
  BuketName   = Event["events"][0]["oss"]["bucket"]["name"]
  ObjectName  = Event["events"][0]["oss"]["object"]["key"]
  OssEndPoint = "oss-" + OssRegion +".aliyuncs.com"



  # OSS, client生成
  auth = oss2.Auth(ACCESS_KEY_ID, ACCESS_KEY_SECRET)
  bucket = oss2.Bucket(auth, OssEndPoint, BuketName)

  # ディレクトリの初期化
  tmpdir = '/tmp/download/'
  os.system("rm -rf /tmp/*")
  os.mkdir(tmpdir)

  # 対象ActionTrailログをOSSからダウンロード
  bucket.get_object_to_file(ObjectName , tmpdir + 'trail_log.gz')
  os.system("gunzip /tmp/download/trail_log.gz")

  with open('/tmp/download/trail_log') as data:
    OssNotification = json.load(data)

  for actionTrailLog in OssNotification:
    logger.info("eventName : " + actionTrailLog["eventName"])
    logger.info("acsRegion : " + actionTrailLog["acsRegion"])

    TARGET_EVENTS = ["RunInstances", "CreateInstance", "CreateDBInstance"]
    if actionTrailLog["eventName"] in TARGET_EVENTS :

      if actionTrailLog["eventName"] == "RunInstances" :
        InstanceIdSet = actionTrailLog["responseElements"]["InstanceIdSets"]["InstanceIdSet"]

      if actionTrailLog["eventName"] == "CreateInstance" :
        InstanceIdSet.append(actionTrailLog["responseElements"]["InstanceId"])

      if actionTrailLog["eventName"] == "CreateDBInstance":
        
        if actionTrailLog["requestParameters"]["Quantity"] == 1:
          # Quantity = 1
          InstanceIdSet.append(actionTrailLog["responseElements"]["DBInstanceId"])
        else: 
          # Quantity > 1
          # [注意] type(actionTrailLog["responseElements"]["DBInstanceId"]) == str !
          tmp_data = actionTrailLog["responseElements"]["DBInstanceId"]
          InstanceIdSet = tmp_data.replace('[', '').replace(']', '').replace('"', '').split(",")

      UserName = actionTrailLog["userIdentity"]["userName"]
      AcsRegion = actionTrailLog["acsRegion"]

    else:
      logger.info("Isn't target event !")
      return 0

  #TARGET_EVENTS instanceにOwnerタグを追加
  client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, AcsRegion)

  for instance in InstanceIdSet :
    
    if actionTrailLog["eventName"] == "CreateDBInstance":
    # CreateDBInstance

      # 新規作成DBの情報を取得できたかの確認
      count = 0
      while count < LIMITTED_COUNT:
        request = DescribeDBInstancesRequest()
        response = json.loads(client.do_action_with_exception(request))
      
        # 既存のDBの情報を取得
        existed_db_list = []
        for data in response["Items"]["DBInstance"]:
          existed_db_list.append(data["DBInstanceId"])
        
        # 確認
        if instance not in existed_db_list:
          time.sleep(SLEEP_TIME)
          count += 1
        else:
          break

      # タグ付け
      request = AddTagsToResourceRequest()
      request.set_accept_format('json')
      request.set_Tags({TagName: UserName})
      request.set_DBInstanceId(instance)
      
    else:
    # RunInstances,CreateInstance  
      Tags = [{"Key": TagName,"Value": UserName}]
      request = AddTagsRequest.AddTagsRequest()
      request.set_ResourceType("instance")
      request.set_Tags(Tags)
      request.set_ResourceId(instance)

    client.do_action_with_exception(request)
  return 0
Beispiel #13
0
#!/usr/bin/env python
# #coding=utf-8
import alibabacloud
from aliyunsdkcore.client import AcsClient
# from aliyunsdkcore.acs_exception.exceptions import ClientException
# from aliyunsdkcore.acs_exception.exceptions import ServerException
# from aliyunsdkrds.request.v20140815.DescribeBackupsRequest import DescribeBackupsRequest
from aliyunsdkrds.request.v20140815.DescribeDBInstancesRequest import DescribeDBInstancesRequest
import sys
# sys.path.append("..")
from config.config import *

# ecs = alibabacloud.get_resource(
#     'ecs',
#     access_key_id=ACCESS_KEY_ID,
#     # access_key_id=config.ACCESS_KEY_ID, 后面研究下是怎么写的
#     access_key_secret=ACCESS_KEY_SECRET,
#     region_id="cn-hangzhou",
# )
#
# for instance in ecs.instances.all():
#     print(instance.instance_id)

client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, "cn-hangzhou")

request = DescribeDBInstancesRequest()
request.set_accept_format('json')

response = client.do_action_with_exception(request)
print(response)
# print(str(response, encoding='utf-8'))