Sonic 云真机平台部署

55 阅读8分钟

Sonic 云真机平台部署

1 简介

  • Sonic 云真机平台,是一个免费开源的云真机测试平台,旨在提供高效、可扩展的虚拟设备访问服务,它允许开发者和测试人员在云端远程访问和管理真实的硬件设备。
  • Sonic 支持哪些设备?
  • Android 设备、iOS 设备、车载设备、智能手表。
  • 为什么使用 Sonic
  • (1) 0 编码 UI 自动化:积木式创建步骤,一键分发多设备任务;
  • (2) 在线代理抓包:一键连接代理抓包,团队协作轻松快捷;
  • (3) 设备分布式集群:支持跨网段多机房,支持 WindowsMacLinux 平台;
  • (4) 优秀周边生态:安卓 ApkiOS 调试工具、Jenkins 插件、在线抓包工具;
  • (5) 低成本维护:拖拽式排序交互,使用简单便捷;
  • (6) 在线 WebView:提供在线调试 WebView,一键便利连接 devtools
  • (7) 可视化报表:测试截图、日志、录像,图表展示项目运行情况;
  • (8) 轻松部署维护:Docker 一键去中心化分布式,扩容缩容不再繁琐。

2 环境准备

  • Sonic 部署分为 前后端 部署以及 Agent 端 部署
  • Windows 11 系统,虚拟机:VMware Workstation 17 Pro(17.0.0 build-20800274)
  • Linux 系统:Ubuntu 22.04.3 64 位
  • JDK17+
  • Sonic 版本:2.6.4

3 安装配置与部署

3.1 安装 MySQL

  • (1) 更新 Ubuntu 源列表:
sudo apt-get update
  • (2) 安装 MySQL 服务:
sudo apt-get install mysql-server

# 安装成功后,查看安装的 mysql 版本
root@ubuntu:/opt/sonic# mysql --version
mysql  Ver 8.0.36-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
  • (3) 初始化 MySQL 配置:
root@ubuntu:~# mysql_secure_installation


# 是否使用验证密码组件?
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: N


# 是否移除匿名用户?
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.


# 是否禁止 root 远程登录?
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N


# 是否删除 test 数据库?
 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.


# 是否重新加载权限表?
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done!
  • (4) 在 Ubuntu 服务器上配置 MySQL 以允许远程连接:
  • 编辑 MySQL 的配置文件(通常是 /etc/mysql/mysql.conf.d/mysqld.cnf/etc/mysql/my.cnf),注释掉或修改 bind-address 行,将其从 127.0.0.1(只允许本地连接)更改为 0.0.0.0(允许任何 IP 连接):
port = 3306

bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0

.cnf文件.png

  • 重启 MySQL 服务以应用更改:
sudo systemctl restart mysql
  • (5) 为 MySQL 设置 root 用户远程访问权限:
root@ubuntu:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.36-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | debian-sys-maint |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.01 sec)

mysql> CREATE USER 'root'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.02 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
  • (6) 创建 sonic 数据库:
mysql> CREATE DATABASE sonic CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected, 2 warnings (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sonic              |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
  • (7) 确保 Ubuntu 服务器的防火墙允许 MySQL 端口 (默认是: 3306):
sudo ufw allow 3306
  • (8) 从你的 Mac 电脑连接到 MySQL 数据库:
mysql -h <Ubuntu服务器IP> -u <username> -p
  • (9) 卸载 MySQL
# 卸载 MySQL Server
sudo apt-get remove mysql-server

# 完全卸载 MySQL Server
sudo apt-get purge mysql-server

# 清理依赖包
sudo apt-get autoremove

# 删除数据库和日志文件 (可选):
sudo rm -rf /var/lib/mysql
sudo rm -rf /var/log/mysql

# 更新列表
sudo apt-get update

3.2 安装 docker

  • (1) 安装必要的证书并允许 apt 包管理器使用以下命令通过 HTTPS 使用存储库:
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release
  • (2) 添加 Docker 的官方 GPG 密钥:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  • (3) 添加 Docker 官方库:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  • (4) 更新 Ubuntu 源列表:
sudo apt update
  • (5) 安装最新 Docker CE
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
  • (6) 查看 docker 是否安装成功:
root@ubuntu:~# systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset>
     Active: active (running) since Mon 2024-03-25 05:55:05 PDT; 48s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 9709 (dockerd)
      Tasks: 16
     Memory: 30.1M
     CGroup: /system.slice/docker.service
             └─9709 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/cont>

Mar 25 05:55:05 ubuntu systemd[1]: Starting Docker Application Container Engine>
Mar 25 05:55:05 ubuntu dockerd[9709]: time="2024-03-25T05:55:05.135314728-07:00>
Mar 25 05:55:05 ubuntu dockerd[9709]: time="2024-03-25T05:55:05.135975403-07:00>
Mar 25 05:55:05 ubuntu dockerd[9709]: time="2024-03-25T05:55:05.200514782-07:00>
Mar 25 05:55:05 ubuntu dockerd[9709]: time="2024-03-25T05:55:05.296978228-07:00>
Mar 25 05:55:05 ubuntu dockerd[9709]: time="2024-03-25T05:55:05.306969646-07:00>
Mar 25 05:55:05 ubuntu dockerd[9709]: time="2024-03-25T05:55:05.307023898-07:00>
Mar 25 05:55:05 ubuntu dockerd[9709]: time="2024-03-25T05:55:05.332335835-07:00>
Mar 25 05:55:05 ubuntu systemd[1]: Started Docker Application Container Engine.
lines 1-20/20 (END)
  • (7) 设置开启启动:
sudo systemctl enable docker

3.3 部署 sonic server

  • (1) 下载 sonic server
# 创建 sonic 文件夹
sudo mkdir /opt/sonic/sonic-server

# 赋予 sonic 文件夹权限
chmod -R 777 /opt/sonic/sonic-server

# 进入目录
cd /opt/sonic/sonic-server

# 下载 sonic server v2.6.4 版本 .zip 包
wget https://github.com/SonicCloudOrg/sonic-server/releases/download/v2.6.4/sonic-server-v2.6.4.zip

# 解压 .zip 包
unzip sonic-server-v2.6.4.zip
  • (2) 编辑 .env 文件 (无需更改 docker-compose.yml 文件):
root@ubuntu:/opt/sonic/sonic-server# sudo vim .env

################################################
#         Do you need any help?                #
#     Visit https://sonic-cloud.cn/deploy      #
################################################

##################
# Service Config #
##################
SONIC_SERVER_HOST=xxxx # 宿主机的 IP 地址
SONIC_SERVER_PORT=3000 # 默认 3000 即可
SONIC_EUREKA_USERNAME=sonic
SONIC_EUREKA_PASSWORD=sonic
SONIC_EUREKA_PORT=8761

################
# MySQL Config #
################
MYSQL_HOST=xxxx # MySQL 容器所在的宿主机的 IP 地址
MYSQL_PORT=3306 # 默认 3306 即可
MYSQL_DATABASE=sonic # 数据库名称
MYSQL_USERNAME=root # 数据库登录名称
MYSQL_PASSWORD=123456 # 数据库登录密码

################
# User Config  # # 以下部分没有用到ldap协议的用户信息不用修改,默认官网信息即可
################
SECRET_KEY=sonic
EXPIRE_DAY=14
PERMISSION_ENABLE=true
PERMISSION_SUPER_ADMIN=sonic
REGISTER_ENABLE=true
NORMAL_USER_ENABLE=true
LDAP_USER_ENABLE=false
LDAP_USER_ID=cn
LDAP_BASE_DN=ou=users
LDAP_BASE=ou=system
LDAP_USERNAME=uid=admin,ou=system
LDAP_PASSWORD=sonic
LDAP_URL=ldap://xxxx:10389 # 宿主机的 IP 地址
LDAP_OBJECT_CLASS=person
  • (3) 确保 Ubuntu 服务器的防火墙允许相关端口:
sudo ufw allow 3000
sudo ufw allow 3306
sudo ufw allow 8761
  • (4) 启动 sonic server 容器:
# 进入目录并启动容器
cd /opt/sonic/sonic-server
docker compose up -d
  • (5) 浏览器访问 http://serverip:3000serveripsonic server 部署的宿主机的 IP,进入 sonic 的注册登录界面且无报错则 sonic server 搭建完成。 主页

3.4 部署 sonic agent

  • 注意:sonic agent 端可以部署在 MacWindowsLinux 等操作系统机器上,且支持部署多个,这里以 Windows 为例进行部署操作说明;
  • (1) 在 sonic server 平台 Agent中心 新增 1 个 Agent,并复制 Agent KeyAgent中心
  • (2) 创建 sonic-agent 文件夹:
  • 新建 sonic-agent 文件夹
# 新建文件夹
mkdir C:\Users\<用户名>\work\sonic-agent

# 赋予权限
icacls "C:\Users\<用户名>\work\sonic-agent" /grant <用户名>:F /t
  • Windows/grant 选项后面是要赋予权限的用户名和权限类型(:F 表示完全控制),/t 是递归地应用到所有子目录和文件;
  • (3) 下载 sonic agentWindows 直接浏览器访问下载然后解压即可):
# 下载文件
wget https://github.com/SonicCloudOrg/sonic-agent/releases/download/v2.6.4/sonic-agent-v2.6.4-windows_x86_64.zip

# 解压文件
unzip onic-agent-v2.6.4-windows_x86_64.zip
  • (4) 修改 /config/application-sonic-agent.yml 文件配置信息:
sonic:
  agent:
    # 替换为部署 Agent 机器的 ipv4
    host: xx.xxx.xx.x
    # 替换为 Agent 服务的端口,可以自行更改
    port: 7777
    # 替换为前端新增 Agent 生成的 key
    key: 47821753-6831-4de5-834c-4f59e3fa406c
  server:
    # 改成 server 的 SONIC_SERVER_HOST
    host: xx.xxx.xx.xx
    # 改成 server 的 SONIC_SERVER_PORT
    port: 3000

# 以下未来会迁移到 server 配置
modules:
  # 安卓模块配置
  android:
    enable: true
    # 是否开启远程 adb 调试功能
    use-adbkit: true
  ios:
    # 替换为 wda 的 bundleId,如果没有 .xctrunner 后缀会自动补全
    wda-bundle-id: com.sonic.WebDriverAgentRunner
  • (5) 启动 sonic agent
# 在根目录下执行修改为 utf-8 编码
chcp 65001

# 启动 sonic agent
java -jar sonic-agent-windows-x86_64.jar
  • 启动成功后刷新平台设备中心,可以看到设备在线,并可以正常使用: sonic agent

4 设备接入

4.1 Android 设备接入

4.2 iOS 设备接入

5 参考文档