server_operation_mixin#

This module implements all “Operation” mentioned in Operation and Workflow.

Each Operation represents a discrete task on the server. Typically, these operation methods accept the following parameters:

  1. bsm: An AWS Boto3 Session Manager object.

  2. check: A boolean flag that determines whether to verify

    if the current state meets the operation’s prerequisites.

  3. wait: A boolean flag that specifies whether to await

    completion of asynchronous operations.

  4. auto_resolve: A boolean flag that controls automatic state resolution.

    When set to True, the system will:

    • Attempt to wait for the state to meet the operation’s prerequisites

      if they’re not initially satisfied.

    • Throw an exception if it’s impossible to reach the required state

      within a reasonable timeframe.

    It is only used when check is set to True.

class acore_server.server_operation_mixin.ServerOperationMixin[source]#

Server Operation Mixin class that contains all the server operation methods.

build_bootstrap_command(python_version: str = '3', acore_soap_app_version: Optional[str] = None, acore_db_app_version: Optional[str] = None, acore_server_bootstrap_version: Optional[str] = None) str[source]#

构建需要在 EC2 服务器上运行的 bootstrap 命令. 如果没有指定版本号, 则使用 config 中的版本号. 如果 config 中也没有版本号, 那么就使用 main branch 上的最新版本.

Parameters:
  • python_version – pyenv 中的 Python 版本号. 可以是 3, 3.8, 3.9 等.

  • acore_soap_app_versionacore_soap_app 库的版本.

  • acore_db_app_versionacore_db_app 库的版本.

  • acore_server_bootstrap_versionacore_server_bootstrap 库的版本.

create_ec2(bsm: BotoSesManager, stack_exports: StackExports, ami_id: Optional[str] = None, instance_type: Optional[str] = None, python_version: str = '3', acore_soap_app_version: Optional[str] = None, acore_db_app_version: Optional[str] = None, acore_server_bootstrap_version: Optional[str] = None, tags: Optional[Dict[str, str]] = None, check: bool = True, wait: bool = True, **kwargs) ReservationResponseTypeDef[source]#

为服务器创建一台新的 EC2. 注意, 一般先创建 RDS, 等 RDS 已经在运行了, 再创建 EC2. 因为 EC2 有一个 bootstrap 的过程, 这个过程中需要跟数据库通信. 数据库没有准备好 bootstrap 是不可能成功的.

Parameters:
  • bsm – Boto3 Session Manager.

  • stack_exports – cloudformation stack exports object that contains AWS infrastructure information.

  • ami_id – 你要从哪个 AMI 来创建 EC2? 如果不指定, 则使用 config 中的 AMI ID. 这个参数之所以是可选是因为在有的 workflow 中, 我们已经知道 AMI ID 了; 而有的 workflow 中, 我们要临时创建一个新的 AMI ID, 此时还不知道这个 ID.

  • instance_type – EC2 的 instance type 是什么, 如果不指定, 则使用 config 中的值.

  • python_version – see build_bootstrap_command().

  • acore_soap_app_version – see build_bootstrap_command().

  • acore_db_app_version – see build_bootstrap_command().

  • acore_server_bootstrap_version – see build_bootstrap_command().

  • tags – additional AWS tags to add to the EC2 instance.

  • check – if True, check if it meets the prerequisites for this operation.

  • wait – if True, wait for the operation to complete.

create_rds_from_scratch(bsm: BotoSesManager, stack_exports: StackExports, db_instance_class: Optional[str] = None, engine_version: Optional[str] = None, multi_az: Optional[bool] = None, tags: Optional[Dict[str, str]] = None, check: bool = True, wait: bool = True, **kwargs) CreateDBInstanceResultTypeDef[source]#

不使用 DB Snapshot, 创建一台全新的数据库.

Parameters:
  • bsm – Boto3 Session Manager.

  • stack_exports – cloudformation stack exports object that contains AWS infrastructure information.

  • db_instance_class – Database 的 instance type 是什么, 如果不指定, 则使用 config 中的值.

  • engine_version – database engine version, in this project we use 8.X.Y (mysql). 如果不指定, 则使用 config 中的值.

  • multi_az – 是否启用 Multi-AZ 高可用性. 如果不指定, 则使用 config 中的值.

  • tags – additional AWS tags to add to the EC2 instance.

  • check – if True, check if it meets the prerequisites for this operation.

  • wait – if True, wait for the operation to complete.

create_rds_from_snapshot(bsm: BotoSesManager, stack_exports: StackExports, db_snapshot_id: Optional[str] = None, db_instance_class: Optional[str] = None, multi_az: Optional[bool] = None, tags: Optional[Dict[str, str]] = None, check: bool = True, wait: bool = True, **kwargs) RestoreDBInstanceFromDBSnapshotResultTypeDef[source]#

为服务器创建一台新的数据库.

Parameters:
  • bsm – Boto3 Session Manager.

  • stack_exports – cloudformation stack exports object that contains AWS infrastructure information.

  • db_snapshot_id – 要从哪个 DB Snapshot 来创建 RDS? 如果不指定, 则使用 config 中的 DB Snapshot ID. 这个参数之所以是可选是因为在有的 workflow 中, 我们已经知道 DB Snapshot ID 了; 而有的 workflow 中, 我们要临时创建一个新的 DB Snapshot ID, 此时还不知道这个 ID.

  • db_instance_class – Database 的 instance type 是什么, 如果不指定, 则使用 config 中的值.

  • multi_az – 是否启用 Multi-AZ 高可用性. 如果不指定, 则使用 config 中的值.

  • tags – additional AWS tags to add to the EC2 instance.

  • check – if True, check if it meets the prerequisites for this operation.

  • wait – if True, wait for the operation to complete.

start_ec2(bsm: BotoSesManager, check: bool = True, wait: bool = True, auto_resolve: bool = True) Optional[StartInstancesResultTypeDef][source]#

启动关闭着的 EC2.

Parameters:
  • bsm – Boto3 Session Manager.

  • check – if True, check if it meets the prerequisites for this operation.

  • wait – if True, wait for the operation to complete.

  • auto_resolve – if True, wait RDS to be ready to start, when EC2 is still stopping.

start_rds(bsm: BotoSesManager, check: bool = True, wait: bool = True, auto_resolve: bool = True) Optional[StartDBInstanceResultTypeDef][source]#

启动关闭着的 RDS.

Parameters:
  • bsm – Boto3 Session Manager.

  • check – if True, check if it meets the prerequisites for this operation.

  • wait – if True, wait for the operation to complete.

  • auto_resolve – if True, wait RDS to be ready to start, when RDS is still stopping.

associate_eip_address(bsm: BotoSesManager, allow_reassociation: bool = False, check: bool = True) Optional[dict][source]#

给 EC2 关联 EIP.

update_db_master_password(bsm: BotoSesManager, check: bool = True) Optional[dict][source]#

更新数据库的 master password.

stop_ec2(bsm: BotoSesManager, check: bool = True, wait: bool = True, auto_resolve: bool = True) Optional[StopInstancesResultTypeDef][source]#

关闭 EC2.

Parameters:
  • bsm – Boto3 Session Manager.

  • check – if True, check if it meets the prerequisites for this operation.

  • wait – if True, wait for the operation to complete.

  • auto_resolve – if True, wait EC2 to be ready to stop, when EC2 is still pending.

stop_rds(bsm: BotoSesManager, check: bool = True, wait: bool = True, auto_resolve: bool = True) Optional[StopDBInstanceResultTypeDef][source]#

关闭 RDS.

Parameters:
  • bsm – Boto3 Session Manager.

  • check – if True, check if it meets the prerequisites for this operation.

  • wait – if True, wait for the operation to complete.

  • auto_resolve – if True, wait RDS to be ready to stop, when RDS is still starting.

delete_ec2(bsm: BotoSesManager, check: bool = True, wait: bool = True) TerminateInstancesResultTypeDef[source]#

删除 EC2.

Parameters:
  • bsm – Boto3 Session Manager.

  • check – if True, check if it meets the prerequisites for this operation.

  • wait – if True, wait for the operation to complete.

delete_rds(bsm: BotoSesManager, create_final_snapshot: Optional[bool] = None, check: bool = True) DeleteDBInstanceResultTypeDef[source]#

删除 RDS.

Parameters:
  • bsm – Boto3 Session Manager.

  • check – if True, check if it meets the prerequisites for this operation.

todo: add waiter

stop_worldserver(bsm: BotoSesManager)[source]#

停止魔兽世界游戏服务器. 这个命令不会失败. 它只是一个 async API call.

这个命令比较特殊, 它建立在服务器已经成功配置好了 acore_server_bootstrap@1.0.1+ 的前提上 (用到了 stop_server 这个命令). 按理说我们这个库的 requirements 里没有依赖于 acore_server_bootstrap, 但是实际上依赖了. 因为我们在运行 Stop Server workflow 的过程中需要有这一步. 所以这个函数算是例外了.

Parameters:

bsm – Boto3 Session Manager.

property wow_status: str#

从 EC2 的 Tag 中获取魔兽世界服务器的状态.

  1. 如果 EC2 或 RDS 任意一个不存在或是已被删除 则返回 “deleted”.

  2. 如果 EC2 或 RDS 不在线, 则返回 stopped.

  3. 如果 EC2 或 RDS 都在线, tag 中不存在测量数据, 则返回 “stopped”

  4. 如果 EC2 或 RDS 都在线, tag 中有测量数据且没有过期, 则返回 tag 中的数据, 值可能是

    “123 players” (数字是在线人数), 或是 “stopped”

  5. 如果 EC2 或 RDS 都在线, tag 中有测量数据且过期了, 则返回 “stopped”

create_ssh_tunnel(bsm: BotoSesManager, path_pem_file: Optional[Path] = None)[source]#

创建一个本地的 SSH Tunnel, 用于本地数据库开发.

Parameters:
  • bsm – Boto3 Session Manager.

  • path_pem_file – EC2 的 pem 文件路径. 如果不指定, 则使用 _get_path_pem_file() 的逻辑来自动获取.

list_ssh_tunnel(bsm: BotoSesManager, path_pem_file: Path = None)[source]#

列出所有正在运行中的 SSH Tunnel.

Parameters:
  • bsm – Boto3 Session Manager.

  • path_pem_file – EC2 的 pem 文件路径. 如果不指定, 则使用 _get_path_pem_file() 的逻辑来自动获取.

test_ssh_tunnel()[source]#

通过运行一个简单的 SQL 语句来测试 SSH Tunnel 是否正常工作.

kill_ssh_tunnel(bsm: BotoSesManager, path_pem_file: Path = None)[source]#

关闭所有正在运行中的 SSH Tunnel.

Parameters:
  • bsm – Boto3 Session Manager.

  • path_pem_file – EC2 的 pem 文件路径. 如果不指定, 则使用 _get_path_pem_file() 的逻辑来自动获取.

create_ec2_ami(bsm: BotoSesManager, ami_name: Optional[str] = None, utc_now: Optional[datetime] = None, skip_reboot: bool = True, check: bool = True, wait: bool = True) CreateImageResultTypeDef[source]#

Create a new AMI from existing EC2.

Parameters:
  • bsm – Boto3 Session Manager.

  • ami_name – 是否要指定 ami_name, 如不指定则自动生成.

  • utc_now – 是否要指定 utc_now, 如不指定则自动生成.

  • skip_reboot – 是否要不关机直接创建 AMI.

  • check – if True, check if it meets the prerequisites for this operation.

  • wait – if True, wait for the operation to complete.

create_db_snapshot(bsm: BotoSesManager, snapshot_id: Optional[str] = None, check: bool = True, wait: bool = True, auto_resolve: bool = True) CreateDBSnapshotResultTypeDef[source]#

Create a new DB snapshot from existing RDS. Note that you can only create a snapshot when DB instance is available.

Parameters:
  • bsm – Boto3 Session Manager.

  • snapshot_id – 是否要指定 snapshot_id, 如不指定则自动生成.

  • check – if True, check if it meets the prerequisites for this operation.

  • wait – if True, wait for the operation to complete.