1. 程式人生 > >堡壘機表結構設計

堡壘機表結構設計

profile .com fun string ssh 設計 ble urn int

一、堡壘機表結構

 技術分享圖片

二、創建表

技術分享圖片
# -*- coding: UTF-8 -*-


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, UniqueConstraint, Table, ForeignKey, DateTime
from sqlalchemy.orm import relationship
from sqlalchemy_utils import ChoiceType


Base = declarative_base()

# 堡壘機用戶和遠程主機的關聯 user_m2m_bindhost = Table("user_m2m_bindhost", Base.metadata, Column("id", Integer, primary_key=True), Column(user_profile_id, Integer, ForeignKey("user_profile.id")), Column(bind_host_id, Integer, ForeignKey("
bind_host.id"))) # 遠程主機和主機組關聯 bindhost_m2m_hostgroup = Table("bindhost_m2m_hostgroup", Base.metadata, Column("id", Integer, primary_key=True), Column(host_groups_id, Integer, ForeignKey("host_groups.id")), Column(
bind_host_id, Integer, ForeignKey("bind_host.id"))) # 堡壘機用戶和主機組關聯 user_m2m_hostgroup = Table("user_m2m_hostgroup", Base.metadata, Column("id", Integer, primary_key=True), Column(user_profile_id, Integer, ForeignKey("user_profile.id")), Column(host_group_id, Integer, ForeignKey("host_groups.id"))) class Host(Base): # 主機表 __tablename__ = host id = Column(Integer, primary_key=True) hostname = Column(String(64), unique=True) ip = Column(String(64), unique=True) port = Column(Integer, default=22) def __repr__(self): return self.hostname class HostGroup(Base): # 主機組表 __tablename__ = host_groups id = Column(Integer, primary_key=True) name = Column(String(64), unique=True) function = Column(String(64)) bind_hosts = relationship(BindHost, secondary=bindhost_m2m_hostgroup, backref=host_group) def __repr__(self): return self.name class RemoteUser(Base): # 遠程用戶表 __tablename__ = remote_user # 聯合唯一 __table_args__ = (UniqueConstraint("auth_type", "username", "password", name="_user_password_uc"), ) id = Column(Integer, primary_key=True) username = Column(String(32)) password = Column(String(128)) # 第1個值存數據庫,第2個值是sqlalchemy顯示的 AuthTypes = [ ("ssh-password", "SSH/Password"), ("ssh-key", "SSH/KEY") ] # 設置枚舉值 auth_type = Column(ChoiceType(AuthTypes)) def __repr__(self): return self.username class BindHost(Base): ‘‘‘ 綁定主機組、主機和遠程用戶 ‘‘‘ __tablename__ = bind_host __table_args__ = (UniqueConstraint("host_id", "remoteuser_id", name="_host_remoteuser"),) id = Column(Integer, primary_key=True) host_id = Column(Integer, ForeignKey(host.id)) remoteuser_id = Column(Integer, ForeignKey(remote_user.id)) host = relationship(Host, backref=bind_hosts) remoteuser = relationship(RemoteUser, backref=bind_hosts) def __repr__(self): return "%s %s" % (self.host.ip, self.remoteuser.username) class UserProfile(Base): # 堡壘機用戶表 __tablename__ = user_profile id = Column(Integer, primary_key=True) username = Column(String(32), unique=True) password = Column(String(128)) bind_hosts = relationship("BindHost", secondary=user_m2m_bindhost, backref=user_profile) host_groups = relationship("HostGroup", secondary=user_m2m_hostgroup, backref=user_profile) def __repr__(self): return self.username class AuditLog(Base): # 日誌表 __tablename__ = audit_log id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey(user_profile.id)) bind_host_id = Column(Integer, ForeignKey(bind_host.id)) action_choices = [ (0, CMD), (1, Login), (2, Logout), (3, GetFile), (4, SendFile), (5, Exception), ] action_choices2 = [ (ucmd, uCMD), (ulogin, uLogin), (ulogout, uLogout), # (3,‘GetFile‘), # (4,‘SendFile‘), # (5,‘Exception‘), ] action_type = Column(ChoiceType(action_choices2)) # action_type = Column(String(64)) cmd = Column(String(255)) date = Column(DateTime) user_profile = relationship("UserProfile", backref=audit_logs) bind_host = relationship("BindHost", backref=audit_logs) def __repr__(self): return "%s %s %s" % (self.date, self.action_type, self.cmd) if __name__ == __main__: pass
建表

堡壘機表結構設計