def addserver(): form = forms.AddServer() if form.validate_on_submit(): mom = models.Server(name=request.form['name'], controller=request.form['controller'], pin=request.form['pin'], lastseen=datetime.datetime.now()) mom.put() models.Visit(user_ip=request.remote_addr, action='PUT SERVER ' + str(mom.key.integer_id())).put() flash('added ' + str(mom.key.integer_id())) return redirect('/admin/servers') return render_template('addserver.html', form=form)
def post(self, *args, **kwargs): db_session = models.create_db_session() server_id = self.get_argument('server_id', None) server_name = self.get_argument('server_name', 'My Server') host = self.get_argument('host') port = int(self.get_argument('port', 22)) username = self.get_argument('username') password = self.get_argument('password') user_id = int(self.get_current_user_id()) if server_id: if user_id: try: server = db_session.query(models.Server).filter(models.Server.id == int(server_id)).first() if server.user_id == int(user_id): server_info = dict(server_name=server_name, host=host, port=port, username=username, password=password) server.server_name = server_name server.host = host server.port = port server.username = username server.password = password db_session.commit() else: pass except Exception as e: my_log.exception(e) finally: db_session.close() self.redirect('/') else: pass else: try: server_info = dict(server_name=server_name, host=host, port=port, username=username, password=password, user_id=user_id) server = models.Server(**server_info) db_session.add(server) db_session.commit() except Exception as e: my_log.exception(e) finally: db_session.close() self.redirect('/')
def __create_server_info(self, ignore_errs=False): try: self.__verify_field(self.clean_data, 'model', str) if not len( self.response['error'] ) or ignore_errs == True: #no processing when there's no error happend data_set = { 'asset_id': self.asset_obj.id, 'raid_type': self.clean_data.get('raid_type'), 'model': self.clean_data.get('model'), 'os_type': self.clean_data.get('os_type'), 'os_distribution': self.clean_data.get('os_distribution'), 'os_release': self.clean_data.get('os_release'), } obj = models.Server(**data_set) obj.save() return obj except Exception, e: self.response_msg('error', 'ObjectCreationException', 'Object [server] %s' % str(e))
def sync_database_all(): print("sync at starting") vdc_obj = auth_models.VDC.objects.all() for i in range(len(vdc_obj)): if not vdc_obj[i].backend_info: continue print(vdc_obj[i].backend_info) opensatck_user = ksclient.Client().register_user( key=str(vdc_obj[i].backend_info)) instance_json = nvclient.Client().show_servers(user=opensatck_user) instance_dict = json.loads(instance_json) if instance_dict["detect"]["code"] == 1: vdc_models.Server.objects.filter(created_in=vdc_obj[i].id).delete() for j in range(len(instance_dict["servers"])): temp_dict = instance_dict["servers"][j] vdc_models.ServerAddresses.objects.filter( server_id=temp_dict["id"]).delete() vdc_models.ServerVolume.objects.filter( server_id=temp_dict["id"]).delete() vdc_models.ServerSecurityGroup.objects.filter( server_id=temp_dict["id"]).delete() if temp_dict.get("os-extended-volumes:volumes_attached"): volume_dict = temp_dict[ "os-extended-volumes:volumes_attached"] volume_num = len(volume_dict) for k in range(volume_num): instance_volume_obj = vdc_models.ServerVolume( server_id=temp_dict["id"], volume_id=volume_dict[k]["id"]) instance_volume_obj.save() if temp_dict.get("addresses"): for key in temp_dict["addresses"].keys(): addr_dict = temp_dict["addresses"][key] addr_num = len(addr_dict) for k in range(addr_num): instance_addr_obj = vdc_models.ServerAddresses( server_id=temp_dict["id"], net_name=key, net_addr=addr_dict[k]["addr"], net_type=addr_dict[k]["OS-EXT-IPS:type"], net_mac_addr=addr_dict[k] ["OS-EXT-IPS-MAC:mac_addr"]) instance_addr_obj.save() if temp_dict.get("security_groups"): security_groups_num = len(temp_dict["security_groups"]) # delete same items no_same_security_groups = [] for sg in temp_dict["security_groups"]: if sg not in no_same_security_groups: no_same_security_groups.append(sg) for k in range(security_groups_num): instance_sg_obj = vdc_models.ServerSecurityGroup( server_id=temp_dict["id"], security_group_name=temp_dict["security_groups"][k] ["name"]) instance_sg_obj.save() if not temp_dict["image"]: image_params = "" else: image_params = temp_dict["image"]["id"] if not temp_dict["metadata"].get("owner"): owner_params = "" else: owner_params = temp_dict["metadata"]["owner"] instance_obj = vdc_models.Server( name=temp_dict["name"], identification=temp_dict["id"], created_by=owner_params, # user_id created_in=vdc_obj[i].id, # project_id flavor_id=temp_dict["flavor"]["id"], image_id=image_params, host_id=temp_dict["hostId"], status=temp_dict["status"], task_state=temp_dict["OS-EXT-STS:task_state"], vm_state=temp_dict["OS-EXT-STS:vm_state"], created_time=temp_dict["created"], updated_time=temp_dict["updated"], key_name=temp_dict["key_name"], ) instance_obj.save() else: print("sync error!") ksclient.Client().revoke_user(opensatck_user)
def server(self, name): return models.Server(self, name)