Skip to content

Commit

Permalink
fix: s3fs挂载与卸载
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Dec 29, 2024
1 parent f67c02a commit 75dcaca
Show file tree
Hide file tree
Showing 5 changed files with 405 additions and 390 deletions.
48 changes: 24 additions & 24 deletions internal/apps/s3fs/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func (s *App) List(w http.ResponseWriter, r *http.Request) {
var s3fsList []Mount
list, err := s.settingRepo.Get("s3fs", "[]")
if err != nil {
service.Error(w, http.StatusInternalServerError, "获取 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to get s3fs list")
return
}

if err = json.Unmarshal([]byte(list), &s3fsList); err != nil {
service.Error(w, http.StatusInternalServerError, "获取 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to get s3fs list")
return
}

Expand All @@ -64,58 +64,58 @@ func (s *App) Create(w http.ResponseWriter, r *http.Request) {

// 检查下地域节点中是否包含bucket,如果包含了,肯定是错误的
if strings.Contains(req.URL, req.Bucket) {
service.Error(w, http.StatusUnprocessableEntity, "地域节点不能包含 Bucket 名称")
service.Error(w, http.StatusUnprocessableEntity, "endpoint should not contain bucket")
return
}

// 检查挂载目录是否存在且为空
if !io.Exists(req.Path) {
if err = io.Mkdir(req.Path, 0755); err != nil {
service.Error(w, http.StatusUnprocessableEntity, "挂载目录创建失败")
service.Error(w, http.StatusUnprocessableEntity, "failed to create mount path: %v", err)
return
}
}
if !io.Empty(req.Path) {
service.Error(w, http.StatusUnprocessableEntity, "挂载目录必须为空")
service.Error(w, http.StatusUnprocessableEntity, "mount path is not empty")
return
}

var s3fsList []Mount
list, err := s.settingRepo.Get("s3fs", "[]")
if err != nil {
service.Error(w, http.StatusInternalServerError, "获取 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to get s3fs list")
return
}
if err = json.Unmarshal([]byte(list), &s3fsList); err != nil {
service.Error(w, http.StatusInternalServerError, "获取 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to get s3fs list")
return
}

for _, s := range s3fsList {
if s.Path == req.Path {
service.Error(w, http.StatusUnprocessableEntity, "路径已存在")
service.Error(w, http.StatusUnprocessableEntity, "mount path already exists")
return
}
}

id := time.Now().UnixMicro()
password := req.Ak + ":" + req.Sk
if err = io.Write("/etc/passwd-s3fs-"+cast.ToString(id), password, 0600); err != nil {
service.Error(w, http.StatusInternalServerError, "添加 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to create passwd file: %v", err)
return
}
if _, err = shell.Execf(`echo 's3fs#%s %s fuse _netdev,allow_other,nonempty,url=%s,passwd_file=/etc/passwd-s3fs-%s 0 0' >> /etc/fstab`, req.Bucket, req.Path, req.URL, cast.ToString(id)); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
if mountCheck, err := shell.Execf("mount -a"); err != nil {
if _, err = shell.Execf("mount -a"); err != nil {
_, _ = shell.Execf(`sed -i 's@^s3fs#%s\s%s.*$@@g' /etc/fstab`, req.Bucket, req.Path)
service.Error(w, http.StatusInternalServerError, "/etc/fstab有误:%s", mountCheck)
service.Error(w, http.StatusInternalServerError, "invalid /etc/fstab: %v", err)
return
}
if _, err := shell.Execf(`df -h | grep '%s'`, req.Path); err != nil {
if _, err = shell.Execf(`df -h | grep '%s'`, req.Path); err != nil {
_, _ = shell.Execf(`sed -i 's@^s3fs#%s\s%s.*$@@g' /etc/fstab`, req.Bucket, req.Path)
service.Error(w, http.StatusInternalServerError, "挂载失败,请检查配置是否正确")
service.Error(w, http.StatusInternalServerError, "mount failed: %v", err)
return
}

Expand All @@ -127,12 +127,12 @@ func (s *App) Create(w http.ResponseWriter, r *http.Request) {
})
encoded, err := json.Marshal(s3fsList)
if err != nil {
service.Error(w, http.StatusInternalServerError, "添加 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to add s3fs mount")
return
}

if err = s.settingRepo.Set("s3fs", string(encoded)); err != nil {
service.Error(w, http.StatusInternalServerError, "添加 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to add s3fs mount")
return
}

Expand All @@ -150,11 +150,11 @@ func (s *App) Delete(w http.ResponseWriter, r *http.Request) {
var s3fsList []Mount
list, err := s.settingRepo.Get("s3fs", "[]")
if err != nil {
service.Error(w, http.StatusInternalServerError, "获取 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to get s3fs list")
return
}
if err = json.Unmarshal([]byte(list), &s3fsList); err != nil {
service.Error(w, http.StatusInternalServerError, "获取 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to get s3fs list")
return
}

Expand All @@ -166,24 +166,24 @@ func (s *App) Delete(w http.ResponseWriter, r *http.Request) {
}
}
if mount.ID == 0 {
service.Error(w, http.StatusUnprocessableEntity, "挂载不存在")
service.Error(w, http.StatusUnprocessableEntity, "mount not found")
return
}

if _, err = shell.Execf(`fusermount -u '%s'`, mount.Path); err != nil {
if _, err = shell.Execf(`fusermount -uz '%s'`, mount.Path); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
if _, err = shell.Execf(`umount '%s'`, mount.Path); err != nil {
if _, err = shell.Execf(`umount -lfq '%s'`, mount.Path); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
if _, err = shell.Execf(`sed -i 's@^s3fs#%s\s%s.*$@@g' /etc/fstab`, mount.Bucket, mount.Path); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
if mountCheck, err := shell.Execf("mount -a"); err != nil {
service.Error(w, http.StatusInternalServerError, "/etc/fstab有误:%s", mountCheck)
if _, err = shell.Execf("mount -a"); err != nil {
service.Error(w, http.StatusInternalServerError, "invalid /etc/fstab: %v", err)
return
}
if err = io.Remove("/etc/passwd-s3fs-" + cast.ToString(mount.ID)); err != nil {
Expand All @@ -199,11 +199,11 @@ func (s *App) Delete(w http.ResponseWriter, r *http.Request) {
}
encoded, err := json.Marshal(newS3fsList)
if err != nil {
service.Error(w, http.StatusInternalServerError, "删除 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to delete s3fs mount")
return
}
if err = s.settingRepo.Set("s3fs", string(encoded)); err != nil {
service.Error(w, http.StatusInternalServerError, "删除 S3fs 挂载失败")
service.Error(w, http.StatusInternalServerError, "failed to delete s3fs mount")
return
}

Expand Down
58 changes: 29 additions & 29 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build": "run-s gen-auto-import type-check build-only copy",
"preview": "vite preview",
"build-only": "vite build",
"type-check": "vue-tsc --build --force",
"type-check": "vue-tsc --noEmit",
"lint": "run-s gen-auto-import lint-only",
"lint-only": "eslint . --fix",
"format": "prettier --write src/",
Expand All @@ -27,68 +27,68 @@
"@fontsource-variable/jetbrains-mono": "^5.1.1",
"@guolao/vue-monaco-editor": "^1.5.4",
"@vue-js-cron/naive-ui": "^2.0.9",
"@vueuse/core": "^12.0.0",
"@vueuse/core": "^12.2.0",
"@xterm/addon-attach": "^0.11.0",
"@xterm/addon-clipboard": "^0.1.0",
"@xterm/addon-fit": "^0.10.0",
"@xterm/addon-unicode11": "^0.8.0",
"@xterm/addon-web-links": "^0.11.0",
"@xterm/addon-webgl": "^0.18.0",
"@xterm/xterm": "^5.5.0",
"alova": "^3.2.4",
"axios": "^1.7.7",
"alova": "^3.2.7",
"axios": "^1.7.9",
"cronstrue": "^2.52.0",
"echarts": "^5.5.1",
"echarts": "^5.6.0",
"install": "^0.13.0",
"lodash-es": "^4.17.21",
"luxon": "^3.5.0",
"marked": "^15.0.2",
"marked": "^15.0.4",
"mitt": "^3.0.1",
"node-forge": "^1.3.1",
"pinia": "^2.2.6",
"pinia-plugin-persistedstate": "^4.1.3",
"pinia": "^2.3.0",
"pinia-plugin-persistedstate": "^4.2.0",
"remove": "^0.1.5",
"vue": "^3.5.13",
"vue-echarts": "^7.0.3",
"vue-i18n": "^11.0.0",
"vue-router": "^4.4.5"
"vue-i18n": "^11.0.1",
"vue-router": "^4.5.0"
},
"devDependencies": {
"@iconify/json": "^2.2.275",
"@iconify/vue": "^4.1.2",
"@iconify/json": "^2.2.290",
"@iconify/vue": "^4.2.0",
"@rushstack/eslint-patch": "^1.10.4",
"@tsconfig/node22": "^22.0.0",
"@types/lodash-es": "^4.17.12",
"@types/luxon": "^3.4.2",
"@types/node": "^22.9.1",
"@types/node": "^22.10.2",
"@types/node-forge": "^1.3.11",
"@unocss/eslint-config": "^0.65.0",
"@vitejs/plugin-vue": "^5.2.0",
"@unocss/eslint-config": "^0.65.3",
"@vitejs/plugin-vue": "^5.2.1",
"@vue/eslint-config-prettier": "^10.1.0",
"@vue/eslint-config-typescript": "^14.1.3",
"@vue/eslint-config-typescript": "^14.2.0",
"@vue/tsconfig": "^0.7.0",
"colord": "^2.9.3",
"cpx2": "^8.0.0",
"eslint": "^9.15.0",
"eslint-plugin-vue": "^9.31.0",
"md-editor-v3": "^5.0.2",
"monaco-editor": "^0.52.0",
"naive-ui": "^2.40.1",
"npm-run-all2": "^7.0.1",
"prettier": "^3.3.3",
"eslint": "^9.17.0",
"eslint-plugin-vue": "^9.32.0",
"md-editor-v3": "^5.1.1",
"monaco-editor": "^0.52.2",
"naive-ui": "^2.40.4",
"npm-run-all2": "^7.0.2",
"prettier": "^3.4.2",
"prettier-plugin-organize-imports": "^4.1.0",
"sass": "^1.81.0",
"sass": "^1.83.0",
"tsx": "^4.19.2",
"typescript": "5.7.2",
"unocss": "^0.65.0",
"unocss": "^0.65.3",
"unplugin-auto-import": "^0.19.0",
"unplugin-icons": "^0.22.0",
"unplugin-vue-components": "^0.28.0",
"vite": "^6.0.0",
"vite": "^6.0.6",
"vite-plugin-html": "^3.2.2",
"vite-plugin-mock": "^3.0.2",
"vite-plugin-static-copy": "^2.1.0",
"vite-plugin-vue-devtools": "^7.6.4",
"vue-tsc": "^2.1.10"
"vite-plugin-static-copy": "^2.2.0",
"vite-plugin-vue-devtools": "^7.6.8",
"vue-tsc": "^2.2.0"
}
}
Loading

0 comments on commit 75dcaca

Please sign in to comment.