Ensure reliable /etc/hosts handling for EasyEngine in containerized environments
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 42/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- docker, php
调研方向
从 src/helper/site-utils.php 中的 remove_etc_hosts_entry 开始,检查 /etc/hosts 的读取和更新方式。在挂载了 /etc/hosts 的 Docker 容器和主机级环境中分别验证其行为,包括读取或写入失败的情况。完成的标准是保留现有的删除行为,同时容器化操作不再静默失败。
由索引模型根据 Issue 内容生成。
描述
Description:
This change ensures that EasyEngine can reliably handle /etc/hosts entries when deployed in Docker or containerized environments, without affecting functionality on host systems.
Context:
When EasyEngine is run inside a container, its attempt to update the host system's /etc/hosts file via a mounted volume may silently fail due to permission or compatibility issues in the Filesystem()->dumpFile() method.
/**
* Removes entry of the site from /etc/hosts
*
* @param string $site_url site name.
*
*/
function remove_etc_hosts_entry( $site_url ) {
$fs = new Filesystem();
$hosts_file = file_get_contents( '/etc/hosts' );
$site_url_escaped = preg_replace( '/\./', '\.', $site_url );
$hosts_file_new = preg_replace( "/127\.0\.0\.1\s+$site_url_escaped\n/", '', $hosts_file );
$fs->dumpFile( '/etc/hosts', $hosts_file_new );
}
Objective:
Not to change existing behavior, but to ensure consistent and reliable operation of the remove_etc_hosts_entry function across environments, especially in container deployments.
Modifications:
Refactored remove_etc_hosts_entry in src/helper/site-utils.php to use PHP's native file I/O operations with added safety and compatibility:
- Replaced
Filesystem()->dumpFile()withfile_put_contents()andfile_get_contents(). - Added
LOCK_EXto ensure atomic writes. - Used
stream_context_createwith'file' => ['privileged' => true]to improve write permissions handling in volume-mounted/etc/hosts. - Implemented
preg_quote()to safely match hostnames. - Included explicit error handling for file read/write failures.
Benefits:
- Container Compatibility: Improves EasyEngine’s robustness when run in Docker or similar environments.
- Backward-Safe: Function continues to work without modification on traditional (host-level) deployments.
- Resilience: Adds clear error messages in case of failure, reducing silent breakages.
Testing:
-
Verified successful read/write operations to
/etc/hostsin:- Docker container with volume-mounted
/etc/hosts - Host-level execution without container
- Docker container with volume-mounted
Updated Function:
/**
* Removes entry of the site from /etc/hosts
*
* @param string $site_url site name.
*
*/
function remove_etc_hosts_entry($site_url) {
$hosts_file = @file_get_contents('/etc/hosts');
if ($hosts_file === false) {
throw new Exception("Failed to read /etc/hosts");
}
$site_url_escaped = preg_quote($site_url, '/');
$hosts_file_new = preg_replace("/127\.0\.0\.1\s+$site_url_escaped\n/", '', $hosts_file);
$context = stream_context_create([
'file' => ['privileged' => true]
]);
$result = file_put_contents('/etc/hosts', $hosts_file_new, LOCK_EX, $context);
if ($result === false) {
throw new Exception("Failed to update /etc/hosts");
}
}
- 主要语言
- PHP
- 星标
- 18
- 派生
- 27
- 平均合并
- 5 小时 1 分钟
- 30 天内合并 PR
- 7
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
EasyEngine/site-command 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 30/100
EasyEngine/site-command#395 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 45/100
EasyEngine/site-command#377 ·
-
难度 3/5 1-2 天 新手友好度 25/100
EasyEngine/site-command#375 ·
-
难度 4/5 3-5 天 新手友好度 20/100
EasyEngine/site-command#373 ·
-
难度 4/5 3-5 天 新手友好度 38/100
EasyEngine/site-command#330 ·
查看 EasyEngine/site-command 的全部 Issue
相似的 Issue
-
sync-en
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
sync-en
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 4 天内回复
-
Перевод устарел
难度 1/5 1 小时以内 新手友好度 85/100
-
bug
难度 2/5 半天 新手友好度 76/100
m3ue/m3u-editor#1604 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
femiwiki/docker-mediawiki#1497 ·
维护者通常 1 天内回复