class SyncAssetsDaemon(SyncBaseDeamon): def __init__(self, interval=5): super(SyncAssetsDaemon, self).__init__(interval) if settings['DEBUG']: self.lock_path = ASSEST_LOCK_TEST_PATH else: self.lock_path = ASSEST_LOCK_PATH self.logger = logging.getLogger("assets_syncing_deamon") def sync(self, test=False): while not self.exit_flag.isSet(): sig = self.channel.get() if sig == "exit": self.logger.info("receive exit signal from channel") break self.logger.info("sync start") self._sync(test) self.logger.info("sync finished") gevent.sleep(1) def _sync(self, test=False): old_server = self._get_db_servers() if not old_server.servers: self.init_servers() else: new_server = self._get_assets_servers() if not new_server: return for k in old_server.subkeys(new_server): server = Server.query.filter_by(sn=k).options(joinedload(Server.nodes)).first() if server.nodes: for node in server.nodes: if not test: node.delete() self.logger.info("delete server node %s" % node.id) try: if not test: server.delete() except Exception, e: self.logger.error(e) self.logger.info("delete server %s sn %s" % (server.hostname, server.sn)) for k in old_server.changedkeys(new_server): server = Server.query.filter_by(sn=k).first() new_name = new_server.servers[k] old_name = server.hostname server.hostname = new_name if not test: try: server.save() except Exception, e: self.logger.error(e) self.logger.info("chaneg server %s to %s" % (old_name, new_name)) for k in new_server.subkeys(old_server): server = Server(hostname=new_server.servers[k], sn=k) if not test: try: server.save() except Exception, e: self.logger.error(e) self.logger.info("add server %s sn %s" % (server.hostname, server.sn))
def server(): return Server( **{ "name": "test", "host": "localhost", "connect_username": "******", "connect_port": "22", } )
def authorize_server(id, name, token): """Authorize a server to connect to the API""" server = Server(server_id=id, server_name=name, server_token=token) try: db.session.add(server) db.session.commit() except IntegrityError: db.session.rollback() app.logger.error('Server authentication token already in use')
def init_servers(self): s = self._get_assets_servers() _ = lambda x: x.decode("utf8") for _server in s.servers: try: server = Server(hostname=_(_server['hostname']), sn=_(_server['sn'])) server.save() except IntegrityError: print "hostname:%s duplicated" % _server['hostname'] continue
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() s = Server('192.168.1.1', 'Some Dev Server', True) db.session.add(s) db.session.commit() self.client = self.app.test_client()
def setUp(self): BaseTestCase.setUp(self) self.server = Server(server_token="my-server-token", server_id='my-server-id', server_name='My Server Name') self.user = User(user_name='My name', user_id='my-server-id/old-name', user_email='*****@*****.**') self.server.users.append(self.user) db.session.add(self.server) db.session.commit()
def post(self, request): ret = {'status': 0, 'errmsg': 'ok'} data = QueryDict(request.body) data = dict(data.items()) try: server = Server(**data) server.save() except Exception as e: msg = "user {} add server error:{}".format(request.user.username, e.args) logger.error(msg) ret['status'] = 1 ret['errmsg'] = msg return JsonResponse(ret, safe=True)
def add_server(form_data): """ 添加服务器 :param form_data: :return: """ server_name = form_data['server_name'] server_ip = form_data['server_ip'] server_description = form_data['server_description'] if form_data['server_description'] else '' # 描述 try: s = Server.objects.get(server_ip=server_ip) return False, '机器已经存在,请核实!' except Server.DoesNotExist: s = Server(server_name=server_name, server_ip=server_ip, server_description=server_description) s.save() return True, '机器添加成功!'
def add(request): if request.method == "GET": server_form = AddServer() log.info('Into Add Server Page.') return render(request, 'server/add.html', locals()) else: log.info("start submit Add Server Data.") server_form = AddServer(request.POST) if server_form.is_valid(): try: data = server_form.cleaned_data srv = Server( name=data['name'], inner_ip=data['inner_ip'], outer_ip=data['outer_ip'], area=data['area'], user=data['user'], password=data['password'], port=data['port'], bandwidth=data['bandwidth'], monitor=data['monitor'], remark=data['remark'] ) if srv.insert(enc=True): # 更新数据字典,增加机房(如果不存在) DataOption( category='area', keyword=data['area'] ).insert() # 连接到主机,拷贝公钥 connect_server.delay(srv.id) log.info("Insert Server data success!") message = {'result': True, 'message': 'Server添加成功!', 'title': '添加Serve结果'} return render(request, 'server/add-result.html', locals()) except Exception as e: log.error('Insert Server get the ERROR: ' + traceback.format_exc()) message = {'result': False, 'message': '服务器发生异常:'+e.message + " 请稍候重试!"} return render(request, 'server/add.html', locals()) else: log.error("表单验证失败! " + str(server_form.errors)) message = {'result': False, 'message': '表单验证失败!请检查您的填写。' + str(server_form.errors)} return render(request, 'server/add.html', locals())
def set_server_info(form_data): """ 添加服务器 :param form_data: :return: """ server_name = form_data['server_name'] server_ip = form_data['server_ip'] server_description = '自动' # 描述 try: s = Server.objects.get(server_ip=server_ip) s.server_name = server_name s.server_ip = server_ip s.save() msg = 'server information update success!' except Server.DoesNotExist: s = Server(server_name=server_name, server_ip=server_ip, server_description=server_description) s.save() msg = 'server auto reg success!!' return True, msg
def add_server_fast(form_data, user_id): """ 批量增加主机 :param form_data: :param user_id: :return: """ server_list = form_data['server_list'] msg = '新增结果:' flag = True for s in server_list.split('\n'): server_name = s.split(',')[0].strip() # 主机名 server_ip = s.split(',')[1].strip() # 主机名 try: s = Server.objects.get(server_ip=server_ip) flag = False msg = msg + '失败:' + server_name.strip() + '|' + server_ip.strip() + '' except Server.DoesNotExist: s = Server(server_name=server_name, server_ip=server_ip) s.save() if flag: return True, '机器添加成功!' else: return False, msg
def savedata(request, result): if (result['status'] == "timeout"): print result['ip'] + ' ' + result['data'] return "timeout" if 'server_sn' not in result['data']: print result['ip'] + ' ' + "sn error" return "sn error" server = result['data'] sn = server['server_sn'] ip = server['server_ip'] brand = server['server_brand'] model = server['server_model'] cpu = server['server_cpu'] memory = server['server_memory'] network = server['server_default_ipv4'] all_ip = server['server_all_ipv4_address'] system = server['server_system'] hostname = server['server_hostname'] user = '******' status = 'product' # 如果序列号已经存在,只做更新 if Server.objects.filter(sn=sn): olddata = Server.objects.filter(sn=sn) id = olddata[0].id server_update = Server.objects.get(id=id) server_update.ip = ip server_update.brand = brand server_update.model = model server_update.cpu = cpu server_update.memory = memory server_update.network = network server_update.all_ip = all_ip server_update.system = system server_update.hostname = hostname server_update.user = user server_update.status = status server_update.save() print ip + ' is update >>>>' return "update" # 插入数据库 server_add = Server( sn=sn, ip=ip, brand=brand, model=model, cpu=cpu, memory=memory, network=network, all_ip=all_ip, system=system, hostname=hostname, user=user, status=status, ) server_add.save() print ip + ' is add ****' return "add"
def upload(request): if request.method == "GET": uf = FileForm() log.info("Into Server CSV upload page.") return render(request, 'server/upload.html', locals()) log.info("CSV file uploaded and analyze...") uf = FileForm(request.POST, request.FILES) table_header = [ '主机名', '内网IP', '外网IP', '用户名', '密码', '端口', '机房', '机架', '带宽', '监控', '备注' ] message = {} if uf.is_valid(): upload_file = uf.cleaned_data['upload_file'] if upload_file.name.lower().endswith('.csv'): saved_path = os.path.join(settings.UPLOAD_URL, 'server_template') if not os.path.exists(saved_path): os.makedirs(saved_path) now = datetime.datetime.now() saved_file = os.path.join(saved_path, now.strftime('%Y%m%d%H%M%S_%f') + '.csv') with open(saved_file, 'wb') as f: f.write(upload_file.read()) log.info("file saved in: " + saved_file) try: data = csv.reader(open(saved_file, 'rb')) i = 0 # 添加数量和未添加server列表 added_count = 0 ignore_list = [] for row in data: row = [x.decode("GBK") for x in row] i += 1 if i == 1: csv_header = row diff = set(table_header) - set(csv_header) if diff: missing_header = ','.join(diff) log.error("CSV file's table header not Support. Missing: " + missing_header) return JsonResponse({'result': False, 'message': '表头信息不符!缺少表头:' + missing_header}) continue srv = Server( name=row[csv_header.index('主机名')], inner_ip=row[csv_header.index('内网IP')], outer_ip=row[csv_header.index('外网IP')], # domain=row[csv_header.index(u'域名')], area=row[csv_header.index('机房')], rack=row[csv_header.index('机架')], user=row[csv_header.index('用户名')], password=row[csv_header.index('密码')], port=row[csv_header.index('端口')], bandwidth=row[csv_header.index('带宽')], monitor=row[csv_header.index('监控')], remark=row[csv_header.index('备注')] ) if srv.insert(enc=True): # 更新数据字典,增加机房(如果不存在) DataOption( category='area', keyword=row[csv_header.index('机房')] ).insert() added_count += 1 # 连接到主机,拷贝公钥 connect_server.delay(srv.id) else: ignore_list.append(srv.to_dict()) message = {'result': True, 'message': '文件上传成功!', 'added_count': added_count, 'ignore_list': ignore_list} log.info("file upload success and save to database.") except Exception: traceback.print_exc() else: log.error("file upload success But File Format not Support!") message = {'result': False, 'message': '文件上传失败!仅支持CSV格式'} else: message = {'result': False, 'message': '请先选择文件上传'} log.error('no file found.') log.info("upload result: " + str(message)) return JsonResponse(message)