示例#1
0
def stop_services():
    """定时任务:关机服务,目前只针对ec2服务进行关机"""
    t, t1 = get_downtime()
    value = get_downtime_value()
    times = datetime.now()
    down_time_start = datetime.strptime(
        str(datetime.now().date()) + t, '%Y-%m-%d%H:%M')
    down_time_end = datetime.strptime(
        str(datetime.now().date()) + t1, '%Y-%m-%d%H:%M')
    if down_time_start < croniter(value,
                                  times).get_next(datetime) < down_time_end:
        ins_log.read_log('info', "开始关机")
        s = get_aws_session(**settings.get("aws_key"))
        client = s.client("ec2")
        list_instances = set()
        data = client.describe_instances(Filters=[
            {
                'Name': 'tag:' + 'downtime',
                'Values': ['*']
            },
        ], )
        for i in data['Reservations']:
            instance_id = i["Instances"][0]["InstanceId"]
            list_instances.add(instance_id)
        try:
            with DBContext('w') as session:
                for instances_id in list_instances:
                    new_instances = RecordInstanceId(instances_id=instances_id)
                    session.add(new_instances)
                session.commit()
        except Exception as e:
            ins_log.read_log('error', e)
        # 关机方法
        client.stop_instances(InstanceIds=list(list_instances))
示例#2
0
def change_aws_value():
    """定时任务:修改aws服务上的标签value"""
    with DBContext('w') as session:
        change_value_list = []
        data_info = session.query(ChangeResultList).all()
        if data_info:
            for data in data_info:
                dict_data = model_to_dict(data)
                change_value_list.append(dict_data)

        s = get_aws_session(**settings.get("aws_key"))
        clients = s.client("resourcegroupstaggingapi")
        try:
            for tag_value in change_value_list:
                services_resource_id = tag_value["services_resource_id"]
                key = tag_value["key"]
                value = tag_value["value"]
                # 去除资源标签
                clients.untag_resources(ResourceARNList=[
                    services_resource_id,
                ],
                                        TagKeys=[
                                            key,
                                        ])
                # 添加资源标签
                clients.tag_resources(ResourceARNList=[
                    services_resource_id,
                ],
                                      Tags={key: value})
                session.query(ChangeResultList).filter_by(
                    services_resource_id=services_resource_id,
                    key=key).delete()
                session.commit()
        except Exception as e:
            ins_log.read_log('error', e)
示例#3
0
def start_service():
    """定时任务:开机服务,目前只针对ec2服务进行开机"""
    t, t1 = get_uptime()
    print(t, t1)
    times = datetime.now()
    value = get_uptime_value()
    print(value)
    up_time_start = datetime.strptime(
        str(datetime.now().date()) + t, '%Y-%m-%d%H:%M')
    up_time_end = datetime.strptime(
        str(datetime.now().date()) + t1, '%Y-%m-%d%H:%M')
    if up_time_start < croniter(value, times).get_next(datetime) < up_time_end:
        ins_log.read_log('info', "开始开机")
        s = get_aws_session(**settings.get("aws_key"))
        client = s.client("ec2")
        with DBContext('w') as session:
            instance_info = session.query(RecordInstanceId).all()
        list_instances = []
        try:
            for instance in instance_info:
                data = model_to_dict(instance)
                instance_id = data["instances_id"]
                list_instances.append(instance_id)
                client.start_instances(InstanceIds=list_instances)  # 开机方法
        except Exception as e:
            ins_log.read_log('error', e)
        session.query(RecordInstanceId).delete(
            synchronize_session=False)  # 清空数据库的所有记录
        session.commit()
示例#4
0
def main():
    """
    从接口获取配置
    :return:
    """
    session = get_aws_session(**settings.get("aws_key"))
    elb_api = ELBApi(session)
    sync_cmdb(elb_api)
示例#5
0
def main():
    """
    从接口获取配置
    :return:
    """

    session = get_aws_session(**settings.get("aws_key"))
    rds_api = RiRdsApi(session)
    rds_api.sync_cmdb()
示例#6
0
def main():
    """
    从接口获取配置
    :return:
    """

    session = get_aws_session(**settings.get("aws_key"))
    elasticache_api = RiElastiCacheApi(session)
    elasticache_api.sync_cmdb()
示例#7
0
def get_ec2_list():
    """获取ec2信息数据"""
    s = get_aws_session(**settings.get("aws_key"))
    clients = s.client("ec2")
    instance_list = []
    all_instances = clients.describe_instances()
    for reservation in all_instances["Reservations"]:
        for instance in reservation["Instances"]:
            instance_list.append(instance)
    return instance_list
示例#8
0
文件: ec2.py 项目: lyblyc121/aws-cmdb
def get_instance_type_dict():
    """
         获取主机类型的cpu和mem信息。
     :return:
     """
    ret = {}
    s = get_aws_session(**settings.get("aws_key"))
    client = s.client('ec2')
    result = client.describe_instance_types()
    for i in result["InstanceTypes"]:
        dict = {}
        dict.update({"cpu_num": i["VCpuInfo"]["DefaultVCpus"]})
        dict.update({"mem": i["MemoryInfo"]["SizeInMiB"]})
        ret.update({i["InstanceType"]: dict})
    return ret
示例#9
0
def get_tags():
    result = []
    s = get_aws_session(**settings.get("aws_key"))
    clients = s.client("resourcegroupstaggingapi")
    try:
        paginator = clients.get_paginator('get_resources')
        tag_mapping = chain.from_iterable(page['ResourceTagMappingList']
                                          for page in paginator.paginate())
        for resource in tag_mapping:
            tags_collection = {}
            tags_collection.clear()
            tags_collection['tag_aws_service'] = resource['ResourceARN'].split(
                ':')[2]
            tags_collection['tag_description'] = resource['ResourceARN']
            # tags_collection['tag_description'] = ":".join(tags_collection['tag_description'])  ###字符串格式入库
            for pairs in resource['Tags']:
                tags_dict = deepcopy(tags_collection)
                tags_dict['tag_name'] = pairs['Key']
                tags_dict['tag_value'] = pairs['Value']
                result.append(tags_dict)
        return result
    except Exception as e:
        ins_log.read_log('error', e)
        return False
示例#10
0
def get_image_resource(image_id=None):
    assert image_id is not None
    s = get_aws_session(**settings.get("aws_key"))
    ec2 = s.resource('ec2')
    image = ec2.Image(image_id)
    return image
示例#11
0
def get_ec2_resource(ec2_id=None):
    assert ec2_id is not None
    s = get_aws_session(**settings.get("aws_key"))
    ec2 = s.resource('ec2')
    instance = ec2.Instance(ec2_id)
    return instance
示例#12
0
 def __init__(self):
     self._session = get_aws_session(**settings.get("aws_key"))
     self._client = self._session.client('ec2')
示例#13
0
 def put(self, *args, **kwargs):
     """设置开关机时间标签"""
     instance_id = self.get_argument('instance_id', default=None, strip=True)  # 传一个列表
     uptime = self.get_argument('uptime', default=None, strip=True)
     downtime = self.get_argument('downtime', default=None, strip=True)  # 字符串类型的时间 '8:30'
     week = self.get_argument('week', default=None, strip=True)  # 传一个列表类型的数据
     temp = [uptime, downtime]
     instance_id_list = instance_id.split(',')
     s = get_aws_session(**settings.get("aws_key"))
     client = s.client("ec2")
     for i, data in enumerate(temp):
         data_list = data.split(':')
         s = int(data_list[1])
         h = int(data_list[0])
         w = '*'
         if week:
             str_week = [str(i) for i in week]
             w = ','.join(str_week)
         value = f'{s} {h} * * {w}'
         if i == 0:
             try:
                 client.create_tags(
                     Resources=instance_id_list,
                     Tags=[
                         {
                             'Key': 'uptime',
                             'Value': value,
                         },
                     ],
                 )
             except Exception as e:
                 if "UnauthorizedOperation" in str(e):
                     return self.write(dict(code=0, msg='没有权限'))
                 else:
                     return self.write(dict(code=0, msg='其他错误'))
         else:
             try:
                 client.create_tags(
                     Resources=instance_id_list,
                     Tags=[
                         {
                             'Key': 'downtime',
                             'Value': value,
                         },
                     ],
                 )
             except Exception as e:
                 if "UnauthorizedOperation" in str(e):
                     return self.write(dict(code=0, msg='没有权限'))
                 else:
                     return self.write(dict(code=0, msg='其他错误'))
     # 更改完之后,去aws服务上获取更改过后的数据同步到数据库
     response = client.describe_tags(
         Filters=[
             {
                 'Name': 'resource-id',
                 'Values': instance_id_list
             },
         ],
     )
     with DBContext('w') as session:
         for tag in response["Tags"]:
             ec2_info = session.query(EC2Instance).filter(
                 EC2Instance.instance_id == tag["ResourceId"]).first()
             if tag["Key"] == "uptime":
                 ec2_info.uptime = tag["Value"]
                 session.commit()
             elif tag["Key"] == "downtime":
                 ec2_info.downtime = tag["Value"]
                 session.commit()
 def put(self, *args, **kwargs):
     """修改开关机时间,并更改aws服务上的标签"""
     instance_id = self.get_argument('instance_id', default=None, strip=True)  # 传一个列表
     cron_times = self.get_argument('cron_times', default=None, strip=True)
     switch_up_down = self.get_argument('switch_up_down', default=None, strip=True)
     instance_id_list = instance_id.split(',')
     s = get_aws_session(**settings.get("aws_key"))
     client = s.client("ec2")
     try:
         croniter(cron_times, datetime.now())
     except Exception as e:
         ins_log.read_log('info', e)
         return self.write(dict(code=0, msg='输入的时间格式错误'))
     if switch_up_down == "开机":
         try:
             client.create_tags(
                 Resources=instance_id_list,
                 Tags=[
                     {
                         'Key': 'uptime',
                         'Value': cron_times,
                     },
                 ],
             )
         except Exception as e:
             if "UnauthorizedOperation" in str(e):
                 return self.write(dict(code=0, msg='没有权限'))
             else:
                 return self.write(dict(code=0, msg='其他错误'))
     else:
         try:
             client.create_tags(
                 Resources=instance_id_list,
                 Tags=[
                     {
                         'Key': 'downtime',
                         'Value': cron_times,
                     },
                 ],
             )
         except Exception as e:
             if "UnauthorizedOperation" in str(e):
                 return self.write(dict(code=0, msg='没有权限'))
             else:
                 return self.write(dict(code=0, msg='其他错误'))
     # 更改完之后,去aws服务上获取更改过后的数据同步到数据库
     response = client.describe_tags(
         Filters=[
             {
                 'Name': 'resource-id',
                 'Values': instance_id_list
             },
         ],
     )
     with DBContext('w') as session:
         for tag in response["Tags"]:
             ec2_info = session.query(EC2Instance).filter(EC2Instance.instance_id == tag["ResourceId"]).first()
             if tag["Key"] == "uptime":
                 ec2_info.uptime = tag["Value"]
                 session.commit()
             elif tag["Key"] == "downtime":
                 ec2_info.downtime = tag["Value"]
                 session.commit()