def main(): """ 从接口获取已经启用的配置 :return: """ mc = MyCryptV2() huawei_configs_list = get_configs() if not huawei_configs_list: ins_log.read_log('error', '没有获取到华为云资产配置信息,跳过') return False for config in huawei_configs_list: access_id = config.get('access_id') access_key = mc.my_decrypt(config.get('access_key')) # 解密后使用 region = config.get('region') huawei_cloud = config.get('huawei_cloud') project_id = config.get('project_id') default_admin_user = config.get('default_admin_user') obj = HuaweiEcsApi(access_id=access_id, access_key=access_key, region=region, cloud=huawei_cloud, project_id=project_id, default_admin_user=default_admin_user) obj.sync_cmdb()
def get_rds_info(self): """ 只获取到想要的信息 :return: """ rds_attribute_data_list = self.get_attribute_response() if not rds_attribute_data_list: ins_log.read_log('error', 'Not fount rds attribute info...') return False rds_list = [] for i in rds_attribute_data_list: asset_data = dict() asset_data['db_instance_id'] = i.get('DBInstanceId') asset_data['db_code'] = i.get('DBInstanceDescription') asset_data['db_class'] = i.get('DBInstanceClass') asset_data['db_host'] = i.get('ConnectionString') asset_data['db_port'] = i.get('Port') asset_data['db_disk'] = i.get('DBInstanceStorage') asset_data['db_type'] = i.get('Engine') asset_data['db_version'] = i.get('EngineVersion') asset_data['state'] = i.get('DBInstanceStatus') asset_data['db_mark'] = i.get('DBInstanceType') asset_data['db_region'] = i.get('ZoneId') asset_data['db_detail'] = i.get('DBInstanceDescription') rds_list.append(asset_data) return rds_list
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)
def get_elbv2_list(self): """ 获取返回值 :return: """ response_data, err = self.get_elbv2_response() if err: ins_log.read_log('error', '获取失败:{}'.format(err)) return False for each in response_data["LoadBalancers"]: elb = {} elb["LoadBalancerArn"] = each.get("LoadBalancerArn") elb["name"] = each.get("LoadBalancerName") elb["dnsname"] = each.get("DNSName") instance_list = [] response = self.elbv2_client.describe_target_groups( LoadBalancerArn=each.get("LoadBalancerArn")) if response['TargetGroups']: for i in response['TargetGroups']: response1 = self.elbv2_client.describe_target_health( TargetGroupArn=i['TargetGroupArn']) if response1['TargetHealthDescriptions']: for m in response1['TargetHealthDescriptions']: instance_list.append(m["Target"]["Id"]) elb["instance"] = list(set(instance_list)) self.elb_list.append(elb) return self.elb_list
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))
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()
def get_tags(self): result = [] self.client = boto3.client('resourcegroupstaggingapi', region_name=self.region, aws_access_key_id=self.access_id, aws_secret_access_key=self.access_key) try: paginator = self.client.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'].split(':')[5:] 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) print(len(result)) return result except Exception as e: ins_log.read_log('error', e) # print(e) return False
def get_describe_reserved_db_data(self): """ 获取返回值 :return: """ response_data, err = self.get_describe_reserved_db_instances_response() if err: ins_log.read_log('error', '获取失败:{}'.format(err)) return False for each in response_data['ReservedDBInstances']: rds_data = {} rds_data['ReservedDBInstanceId'] = each.get('ReservedDBInstanceId') rds_data['ReservedDBInstancesOfferingId'] = each.get('ReservedDBInstancesOfferingId') rds_data['DBInstanceClass'] = each.get('DBInstanceClass') rds_data['Duration'] = each.get('Duration') rds_data['FixedPrice'] = each.get('FixedPrice') rds_data['UsagePrice'] = each.get('UsagePrice') rds_data['CurrencyCode'] = each.get('CurrencyCode') rds_data['DBInstanceCount'] = each.get('DBInstanceCount') rds_data['ProductDescription'] = each.get('ProductDescription') rds_data['OfferingType'] = each.get('OfferingType') rds_data['MultiAZ'] = each.get('MultiAZ') rds_data['State'] = each.get('State') rds_data['RecurringCharges'] = str(each.get('RecurringCharges')) rds_data['ReservedDBInstanceArn'] = each.get('ReservedDBInstanceArn') self.rds_ri_list.append(rds_data) return self.rds_ri_list
def main(): """ 检查用户是否配置了同步codo-task MySQL信息, 如果检测到配置,初始化MySQL,同步数据 拿不到ORM直接使用原生SQL语句操作 :return: """ host = CODO_TASK_DB_INFO.get('host') port = CODO_TASK_DB_INFO.get('port') user = CODO_TASK_DB_INFO.get('user') passwd = CODO_TASK_DB_INFO.get('passwd') db = CODO_TASK_DB_INFO.get('db') if not host or not port or not user or not passwd or not db: print('[Error]: Not fount CODO_TASK_DB_INFO, auto pass...') return False try: mb = MysqlBase(**CODO_TASK_DB_INFO) obj = SyncTagTree(mb) obj.server_sync_task() except Exception as e: msg = '[Error]: 请确认下CODO_TASK 数据库配置信息是否正确' ins_log.read_log('error', e) return msg
def get_server_info(self): response = self.get_response() if not response: ins_log.read_log( 'error', 'Not fount response, please check your access_id and access_key...' ) # print('[Error]: Not fount response, please check your access_id and access_key...') return False ret = response['Reservations'] server_list = [] if ret: for r in ret: for i in r['Instances']: asset_data = dict() try: asset_data['hostname'] = i.get('Tags')[0].get('Value') except (KeyError, TypeError): asset_data['hostname'] = i.get( 'InstanceId', 'Null') #拿不到hostnameg给instance_id asset_data['region'] = i['Placement'].get( 'AvailabilityZone', 'Null') asset_data['instance_id'] = i.get('InstanceId', 'Null') asset_data['instance_type'] = i.get('InstanceType', 'Null') asset_data['instance_state'] = i['State'].get('Name', '') asset_data['private_ip'] = i.get('PrivateIpAddress', 'Null') asset_data['public_ip'] = i.get( 'PublicIpAddress', i.get('PrivateIpAddress', 'Null')) print(asset_data) server_list.append(asset_data) return server_list
def get_describe_reserved_cache_nodes_data(self): """ 获取返回值 :return: """ response_data, err = self.get_describe_reserved_cache_nodes_response() if err: ins_log.read_log('error', '获取失败:{}'.format(err)) return False for each in response_data['ReservedCacheNodes']: data = {} data['ReservedCacheNodeId'] = each.get('ReservedCacheNodeId') data['ReservedCacheNodesOfferingId'] = each.get('ReservedCacheNodesOfferingId') data['CacheNodeType'] = each.get('CacheNodeType') data['Duration'] = each.get('Duration') data['FixedPrice'] = each.get('FixedPrice') data['UsagePrice'] = each.get('UsagePrice') data['CacheNodeCount'] = each.get('CacheNodeCount') data['ProductDescription'] = each.get('ProductDescription') data['OfferingType'] = each.get('OfferingType') data['State'] = each.get('State') data['RecurringCharges'] = str(each.get('RecurringCharges')) self.elasticache_ri_list.append(data) return self.elasticache_ri_list
def sync_cmdb(self): """ 入库 :return: """ redis_info_list = self.get_redis_all() if not redis_info_list: return False with DBContext('w') as session: for data in redis_info_list: for redis in data: ins_log.read_log('info', 'redis信息:{}'.format(redis)) db_code = redis.get('db_code') exist_redis = session.query(DB).filter(DB.db_code == db_code).first() if exist_redis: session.query(DB).filter(DB.db_code == db_code).update({ DB.idc: self.idc, DB.db_class: redis.get('db_class'), DB.db_host: redis.get('db_host'), DB.db_port: redis.get('db_port'), DB.db_user: redis.get('db_user'), DB.db_disk: redis.get('db_disk'), DB.db_region: redis.get('db_region'), DB.db_type: redis.get('db_type'), DB.db_version: redis.get('db_version'), DB.state: redis.get('state'), DB.db_mark: redis.get('db_mark'), DB.db_public_ip: redis.get('db_public_ip'), DB.db_instance_id: redis.get('db_instance_id')}) else: new_db = DB(idc=self.idc, db_code=db_code, db_class=redis.get('db_class'), db_host=redis.get('db_host'), db_port=redis.get('db_port'), db_user=redis.get('db_user'), db_region=redis.get('db_region'), db_type=redis.get('db_type'), db_disk=redis.get('db_disk'), db_version=redis.get('db_version'), state=redis.get('state'), db_public_ip=redis.get('db_public_ip'), db_instance_id=redis.get('db_instance_id')) session.add(new_db) session.commit()
def sync_host_usage_from_zabbix_by_day(date): uz = UserZbx(time=date) usage_info = uz.get_ec2_usage_info() date = date.replace(hour=0, minute=0, second=0, microsecond=0) with DBContext('w', const.DEFAULT_DB_KEY, need_commit=True) as session: for k, v in usage_info.items(): exist_usage = session \ .query(ResourceUsage) \ .filter(ResourceUsage.ec2_id ==v["ec2_id"], ResourceUsage.date == date, ).first() if exist_usage: session \ .query(ResourceUsage) \ .filter(ResourceUsage.ec2_id ==v["ec2_id"], ResourceUsage.date == date, ).update( { ResourceUsage.ec2_id: v["ec2_id"], ResourceUsage.cpu_usage: v["cpu"], ResourceUsage.mem_usage: v["mem"], ResourceUsage.disk_usage:v["disk"], ResourceUsage.date: date, } ) else: new_record = ResourceUsage(ec2_id=v["ec2_id"], cpu_usage=v["cpu"], mem_usage=v["mem"], disk_usage=v["disk"], date=date) session.add(new_record) session.commit() ins_log.read_log('info', '添加{}的数据成功'.format(date))
def sync_cmdb(self): """ 将RDS信息入库 :return: """ rds_list = self.get_rds_info() if not rds_list: ins_log.read_log('error', 'Not Fount rds info...') return False with DBContext('w') as session: for rds in rds_list: ins_log.read_log('info', 'RDS信息:{}'.format(rds)) db_code = rds.get('db_code') exist_rds = session.query(DB).filter(DB.db_code == db_code).first() if exist_rds: session.query(DB).filter(DB.db_code == db_code).update({ DB.idc: self.idc, DB.db_class: rds.get('db_class'), DB.db_host: rds.get('db_host'), DB.db_port: rds.get('db_port'), DB.db_disk: rds.get('db_disk'), DB.db_region: rds.get('db_region'), DB.db_type: rds.get('db_type'), DB.db_version: rds.get('db_version'), DB.state: rds.get('state'), DB.db_env: 'Null', DB.db_instance_id: rds.get('db_instance_id'), DB.tag: rds.get('tag'),DB.db_multiaz: rds.get('db_multiaz'),DB.Iops: rds.get('Iops')}) else: new_db = DB(idc=self.idc, db_code=db_code, db_class=rds.get('db_class'), db_host=rds.get('db_host'), db_port=rds.get('db_port'), db_user=rds.get('db_user'), db_disk=rds.get('db_disk'), db_region=rds.get('db_region'), db_type=rds.get('db_type'), db_version=rds.get('db_version'), state=rds.get('state'), db_env='Null', db_instance_id=rds.get('db_instance_id'),tag=rds.get('tag'), db_multiaz=rds.get('db_multiaz'),Iops=rds.get('Iops')) session.add(new_db) session.commit()
def get_rds_info(self): """ 获取RDS的信息 :return: """ response_data, err = self.get_response() if err: ins_log.read_log('error', '获取失败:{}'.format(err)) return False ret = response_data['DBInstances'] rds_list = [] if ret: for i in ret: rds_data = dict() try: rds_data['db_code'] = i.get('DBInstanceIdentifier') except (KeyError, TypeError): rds_data['db_code'] = i.get('DBName', 'Null') # 拿不到RDS标识名字给DBNAME rds_data['db_region'] = i.get('AvailabilityZone') rds_data['db_class'] = i.get('DBInstanceClass') rds_data['db_type'] = i.get('Engine') rds_data['state'] = i.get('DBInstanceStatus') rds_data['db_user'] = i.get('MasterUsername') rds_data['db_host'] = i.get('Endpoint').get('Address') rds_data['db_port'] = i.get('Endpoint').get('Port') rds_data['db_disk'] = i.get('AllocatedStorage') rds_data['db_version'] = i.get('EngineVersion') rds_data['db_instance_id'] = i.get('DbiResourceId') rds_list.append(rds_data) return rds_list
def sync_cmdb(self): """ 入库 :return: """ redis_info_list = self.get_region_redis() memcached_info_list = self.get_region_memcached() if not redis_info_list and not memcached_info_list: return False cache_list = [] cache_list.extend(redis_info_list) cache_list.extend(memcached_info_list) if not cache_list: return False with DBContext('w') as session: for i in cache_list: ins_log.read_log('info', 'Cache info:{}'.format(i)) db_code = i.get('db_code') exist_redis = session.query(DB).filter( DB.db_code == db_code).first() if exist_redis: session.query(DB).filter(DB.db_code == db_code).update({ DB.idc: self.idc, DB.db_class: i.get('db_class'), DB.db_host: i.get('db_host'), DB.db_port: i.get('db_port'), DB.db_region: i.get('db_region'), DB.db_type: i.get('db_type'), DB.db_version: i.get('db_version'), DB.db_mark: i.get('db_mark'), DB.state: i.get('state') }) else: new_db = DB(idc=self.idc, db_code=db_code, db_class=i.get('db_class'), db_host=i.get('db_host'), db_port=i.get('db_port'), db_mark=i.get('db_mark'), db_user=i.get('db_user'), db_region=i.get('db_region'), db_type=i.get('db_type'), db_version=i.get('db_version'), state=i.get('state'), db_detail=i.get('db_detail')) session.add(new_db) session.commit()
def check_tag_services(): """定时任务:检查检测aws服务的标签是否合规,并将结果入库""" tag_list = get_tags() tag_key_list = [] with DBContext('r') as session: tag_info = session.query(TagList).all() for tag in tag_info: data = model_to_dict(tag) tag_key_list.append(data["key"]) # 获取标签所对应的服务名称 try: with DBContext('w') as session: session.query(ResultList).delete( synchronize_session=False) # 清空数据库的所有记录 for tag_key in tag_key_list: tag_info = session.query(TagList).filter( TagList.key == tag_key).first() tag_id = model_to_dict(tag_info)['id'] if model_to_dict(tag_info)['value'] is not None: value_list = model_to_dict(tag_info)['value'].split(',') else: value_list = None data_info = session.query(ServicesTagMiddle).filter( ServicesTagMiddle.tag_id == tag_id).all() if data_info is not None: services_list = [] for data in data_info: services_id = model_to_dict(data)['services_id'] services_info = session.query(ServicesList).filter( ServicesList.id == services_id).first() services_name = model_to_dict( services_info)['services_name'] services_list.append(services_name) for services_name in services_list: for tag in tag_list: is_valid = 1 if tag["tag_aws_service"] == services_name and tag[ "tag_name"] == tag_key: if value_list is not None and tag[ "tag_value"] not in value_list: is_valid = 0 new_data = ResultList( services_name=services_name, services_resource_id=tag[ "tag_description"], key=tag["tag_name"], value=tag["tag_value"], is_valid=is_valid) session.add(new_data) session.commit() except Exception as e: ins_log.read_log('error', e)
def conn(self): try: client = boto3.client('elasticache', region_name=self.region, aws_access_key_id=self.access_id, aws_secret_access_key=self.access_key) return client except Exception as err: ins_log.read_log('error', 'Error:{err}'.format(err=err)) return False
def sync_cmdb(self): """ 写入CMDB :return: """ server_list = self.get_server_info() if not server_list: ins_log.read_log('info', 'Not fount server info...') # print('Not Fount Server Info') return False with DBContext('w') as session: for server in server_list: ins_log.read_log('info', '资产信息:{}'.format(server)) ip = server.get('public_ip') instance_id = server.get('instance_id', 'Null') hostname = server.get('hostname', instance_id) if not hostname.strip(): hostname = instance_id region = server.get('region', 'Null') instance_type = server.get('instance_type', 'Null') instance_state = server.get('instance_state', 'Null') # AWS=接口没看到CPU这类信息 cpu = server.get('cpu', 'Null') # CPU型号 cpu_cores = server.get('cpu_cores', 'Null') memory = server.get('memory', 'Null') disk = server.get('disk', 'Null') os_type = server.get('os_type', 'Null') os_kernel = server.get('os_kernel', 'Null') sn = server.get('sn', 'Null') exist_hostname = session.query(Server).filter(Server.hostname == hostname).first() # exist_ip = session.query(Server).filter(Server.ip == ip).first() if exist_hostname: session.query(Server).filter(Server.hostname == hostname).update( {Server.ip: ip, Server.public_ip: ip, Server.idc: 'AWS', Server.region: region}) else: new_server = Server(ip=ip, public_ip=ip, hostname=hostname, port=22, idc=self.account, region=region, state=self.state, admin_user=self.default_admin_user) session.add(new_server) exist_ip = session.query(ServerDetail).filter(ServerDetail.ip == ip).first() if exist_ip: session.query(ServerDetail).filter(ServerDetail.ip == ip).update( {ServerDetail.instance_id: instance_id, ServerDetail.instance_type: instance_type, ServerDetail.instance_state: instance_state}) else: new_serve_detail = ServerDetail(ip=ip, instance_id=instance_id, instance_type=instance_type, instance_state=instance_state, cpu=cpu, cpu_cores=cpu_cores, memory=memory, disk=disk, os_type=os_type, os_kernel=os_kernel, sn=sn) session.add(new_serve_detail) session.commit()
def get_server_count(self): """ 获取机器总数 :return: """ response_data, err = self.get_response() if err != None: ins_log.read_log('error', err) return False count = response_data['TotalCount'] return count
def list_domain_names(self): """ """ try: response = self.client.list_domain_names() DomainNames = response.get('DomainNames') if not DomainNames: return [] res = list(map(self.format_DomainNames, DomainNames)) return res except Exception as err: ins_log.read_log('error', 'Error:{err}'.format(err=err)) return []
def get_rds_count(self): """ 获取机器总数 :return: """ response_data, err = self.get_desc_response() if err != None: ins_log.read_log('error', err) return False count = response_data['TotalRecordCount'] # print('RdsCount: {count}'.format(count=count)) return count
def sync_cmdb(self): """ 写入CMDB :return: """ db_list = self.get_udb_all() if not db_list: ins_log.read_log('info', 'Not fount db info...') return False with DBContext('w') as session: for data in db_list: for rds in data: ins_log.read_log('info', '资产信息:{}'.format(rds)) try: db_user = rds['user'] except KeyError: db_user = '******' db_name = rds.get('db_name') exist_rds = session.query(DB).filter( DB.db_code == db_name).first() if exist_rds: session.query(DB).filter(DB.db_code == db_name).update( { DB.idc: self.idc, DB.db_host: rds.get('db_host'), DB.db_port: rds.get('db_port'), DB.db_user: db_user, DB.db_disk: rds.get('db_disk'), DB.db_region: rds.get('db_region'), DB.db_type: rds.get('db_type'), DB.db_version: rds.get('db_version'), DB.state: rds.get('instance_state'), DB.db_mark: rds.get('db_role'), DB.db_instance_id: rds.get('db_instance_id') }) else: new_db = DB(idc=self.idc, db_code=db_name, db_host=rds.get('db_host'), db_port=rds.get('db_port'), db_user=db_user, db_disk=rds.get('db_disk'), db_region=rds.get('db_region'), db_type=rds.get('db_type'), db_mark=rds.get('db_role'), db_version=rds.get('db_version'), state=rds.get('instance_state'), db_instance_id=rds.get('db_instance_id')) session.add(new_db) session.commit()
def get_response(self): """ 获取返回 :return: """ try: response = self.client.describe_instances() return response except Exception as e: ins_log.read_log('error', e) # print(e) return False
def main(): provider_list = getproviderlist() if not provider_list: ins_log.read_log('error', "没有狗爹平台账户,跳过。") return False for privider in provider_list: provider_id = privider.get("id") provider_apikey = privider.get("pro_api_key") provider_apisecret = privider.get("pro_api_secret") obj = GodaddyDomains(provider_id, provider_apikey, provider_apisecret) obj.sync_dns()
def get_uptime_value(): """获取uptime标签的value""" with DBContext('r') as session: try: tag_info = session.query(TagList).filter( TagList.key == 'uptime').first() if tag_info: data = model_to_dict(tag_info) tag_value = data['value'] except Exception as e: ins_log.read_log('info', e) return tag_value
def rsync_public_key(self): '''推送公钥到新加的主机里面''' id_list = self.check_server_state() if not id_list: ins_log.read_log('info', '[PASS]: No new server found, automatically skipping push public key') # print('[PASS]: No new server found, automatically skipping push public key') return # 根据ID列表查询,获取管理权限推送PublicKey到主机 new_server_list = [] rsync_sucess_list = [] rsync_error_list = [] with DBContext('r') as session: for i in id_list: connect_info = session.query(Server.ip, Server.port, AdminUser.system_user, AdminUser.user_key).outerjoin(AdminUser, AdminUser.admin_user == Server.admin_user).filter( Server.id == i).all() new_server_list.append(connect_info) # 检查密钥 sync_key_obj = RsyncPublicKey() check = sync_key_obj.check_rsa() if check: # print('new_server_list-->', new_server_list) res_data = start_rsync(new_server_list) # print(res_data) for res in res_data: if not res.get('status'): rsync_error_list.append(res) else: # 只返回密钥推送成功的进行更新资产 rsync_sucess_list.append(res) if rsync_error_list: with DBContext('w') as session: for i in rsync_error_list: ip = i.get('ip') msg = i.get('msg') error_log = '推送公钥失败, 错误信息:{}'.format(msg) ins_log.read_log('error', error_log) session.query(Server).filter(Server.ip == ip).update({Server.state: 'false'}) exist_ip = session.query(AssetErrorLog).filter(AssetErrorLog.ip == ip).first() if exist_ip: session.query(AssetErrorLog).filter(AssetErrorLog.ip == ip).update( {AssetErrorLog.error_log: error_log}) else: new_error_log = AssetErrorLog(ip=ip, error_log=error_log) session.add(new_error_log) session.commit() return rsync_sucess_list
def call_back(self, ch, method, properties, body): try: ins_log.read_log('info', 'get message') #获取消息 self.on_message(body) #执行父类DealMQ中的 on_message 函数 #body消息内容,内容为 [task_list ID] if not self.__no_ack: ch.basic_ack(delivery_tag=method.delivery_tag) print(" [x] Received %r" % body) except: ins_log.read_log('error', traceback.format_exc()) if not self.__no_ack: ch.basic_nack(delivery_tag=method.delivery_tag)
def get_host_info(self): '''获取主机信息,并写入数据库''' id_list = self.check_server_state() with DBContext('r') as session: for i in id_list: server_list = session.query( Server.ip, Server.port, AdminUser.system_user).outerjoin( AdminUser, AdminUser.admin_user == Server.admin_user).filter( Server.id == i).all() asset_data = get_server_sysinfo(server_list) ins_log.read_log('info', '资产信息:{}'.format(asset_data)) self.update_asset(asset_data)
def on_message(self, body): print('[on_message]') #return 1 #body消息内容,内容为 [task ID] ins_log.read_log('info', 'flow_id is {}'.format(body)) flow_id = int(body) with DBContext('readonly') as session: status = session.query(Publish).filter( Publish.id == flow_id).first().status print(status) if status == '0': self.exec_task(flow_id, 'qa') ####### while循环监控,若status=2,则表示审核通过,开始执行正式 print('[test环境ok]') with DBContext('readonly') as session: dev_status = session.query(Publish).filter( Publish.id == flow_id).first() if not dev_status: #如果测试环境没执行成功,则不会往下执行 print('test not ok...') return False elif status == '12' or status == '2': pass else: print('任务已经执行过') return int_sleep, end_sleep = 1, 1 while True: ### 挂起的任务设置休眠时间 ins_log.read_log( 'info', 'The task-{0} is not ready, retry after {1} s of sleep'.format( flow_id, int_sleep)) time.sleep(int_sleep) int_sleep += 2 end_sleep += int_sleep if int_sleep > 15: int_sleep = 15 with DBContext('readonly') as session: check_status = session.query(Publish).filter( Publish.id == flow_id, Publish.status == '2').first() #status==2,那么继续循环并sleep任务,直到任务被接手 if check_status: break if end_sleep > 150: raise SystemExit('message timeout') #开始发正式环境 print('start release......') self.exec_task(flow_id, 'release') print('[release环境ok]')