def create_or_update_server(user, server_dict): group = server_dict.pop("group") name = server_dict.pop("name") password = server_dict.pop("password") group_id = get_or_create_group_id(group, user) base = {"user_id": user.id, "servergroup_id": group_id, "name": name} server = Server.query.filter_by(**base).first() if server is None: server = Server(**base) print( "Created server {!r} in group {!r} for user {!r}".format( name, group, user.email ) ) db.session.add(server) server.password = encrypt(password, user.password) server.discovery_id = DISCOVERY_ID for attr, value in server_dict.items(): setattr(server, attr, value) commit( "Error updating server {} in group {}".format(name, group), "Successfully saved.", ) return server
import config from pgadmin.utils.crypto import encrypt from pgadmin.model import db, User, Server app = create_app(config.APP_NAME) with app.app_context(): user = User.query.first() key = user.password i = 0 while True: i = i + 1 s = Server() sfx = str(i) if i > 1 else '' s.name = os.getenv('SETUP_SERVER' + sfx + '_NAME') s.host = os.getenv('SETUP_SERVER' + sfx + '_HOST') s.port = os.getenv('SETUP_SERVER' + sfx + '_PORT') s.username = os.getenv('SETUP_SERVER' + sfx + '_USER') if not s.name or not s.host or not s.port or not s.username: break pwd = os.getenv('SETUP_SERVER' + sfx + '_PASS') if pwd: s.password = encrypt(pwd, key) s.save_password = 1 s.user_id = user.id s.servergroup_id = 1 s.maintenance_db = 'postgres' s.ssl_mode = 'prefer' db.session.add(s) db.session.commit()