Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: openresty 异常状态下支持修改配置文件 #6538

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion agent/app/service/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func (n NginxService) GetStatus() (response.NginxStatus, error) {
func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error {
fileOp := files.NewFileOp()
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
filePath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "conf", "nginx.conf")
if err != nil {
return err
}
filePath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "conf", "nginx.conf")
if req.Backup {
backupPath := path.Join(path.Dir(filePath), "bak")
if !fileOp.Stat(backupPath) {
Expand All @@ -123,6 +123,14 @@ func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error
if err = fileOp.WriteFile(filePath, strings.NewReader(req.Content), 0644); err != nil {
return err
}
if status, err := checkContainerStatus(nginxInstall.ContainerName); err == nil && status != "running" {
if out, err := compose.DownAndUp(nginxInstall.GetComposePath()); err != nil {
_ = fileOp.SaveFile(filePath, string(oldContent), 0644)
return fmt.Errorf("nginx restart failed: %v", out)
} else {
return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName)
}
}
return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName)
}

Expand Down
16 changes: 16 additions & 0 deletions agent/app/service/runtime_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ func checkContainerName(name string) error {
return nil
}

func checkContainerStatus(name string) (string, error) {
dockerCli, err := docker.NewClient()
if err != nil {
return "", err
}
defer dockerCli.Close()
names, err := dockerCli.ListContainersByName([]string{name})
if err != nil {
return "", err
}
if len(names) > 0 {
return names[0].State, nil
}
return "", nil
}

func unInstallPHPExtension(runtime *model.Runtime, delExtensions []string) error {
dir := runtime.GetPath()
fileOP := files.NewFileOp()
Expand Down
3 changes: 2 additions & 1 deletion agent/app/service/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,8 +1155,9 @@ func (w WebsiteService) UpdateNginxConfigFile(req request.WebsiteNginxUpdate) er
if err != nil {
return err
}

filePath := nginxFull.SiteConfig.FilePath
if err := files.NewFileOp().WriteFile(filePath, strings.NewReader(req.Content), 0755); err != nil {
if err = files.NewFileOp().WriteFile(filePath, strings.NewReader(req.Content), 0755); err != nil {
return err
}
return nginxCheckAndReload(nginxFull.SiteConfig.OldContent, filePath, nginxFull.Install.ContainerName)
Expand Down
19 changes: 9 additions & 10 deletions agent/utils/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import (
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
)

func Pull(filePath string) (string, error) {
stdout, err := cmd.Execf("docker compose -f %s pull", filePath)
return stdout, err
}

func Up(filePath string) (string, error) {
stdout, err := cmd.Execf("docker compose -f %s up -d", filePath)
return stdout, err
Expand All @@ -19,11 +14,6 @@ func Down(filePath string) (string, error) {
return stdout, err
}

func Start(filePath string) (string, error) {
stdout, err := cmd.Execf("docker compose -f %s start", filePath)
return stdout, err
}

func Stop(filePath string) (string, error) {
stdout, err := cmd.Execf("docker compose -f %s stop", filePath)
return stdout, err
Expand All @@ -38,3 +28,12 @@ func Operate(filePath, operation string) (string, error) {
stdout, err := cmd.Execf("docker compose -f %s %s", filePath, operation)
return stdout, err
}

func DownAndUp(filePath string) (string, error) {
stdout, err := cmd.Execf("docker compose -f %s down", filePath)
if err != nil {
return stdout, err
}
stdout, err = cmd.Execf("docker compose -f %s up -d", filePath)
return stdout, err
}
10 changes: 1 addition & 9 deletions frontend/src/components/app-status/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@
{{ $t('app.reload') }}
</el-button>
<el-divider v-if="data.app === 'OpenResty'" direction="vertical" />
<el-button
type="primary"
@click="setting"
link
:disabled="
data.status === 'Installing' ||
(data.status !== 'Running' && data.app === 'OpenResty')
"
>
<el-button type="primary" @click="setting" link :disabled="data.status === 'Installing'">
{{ $t('commons.button.set') }}
</el-button>
<el-divider v-if="data.app === 'OpenResty'" direction="vertical" />
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/routers/modules/website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const webSiteRouter = {
hidden: true,
component: () => import('@/views/website/runtime/java/index.vue'),
meta: {
activeMenu: '/websites/runtimes/java',
activeMenu: '/websites/runtimes/php',
requiresAuth: false,
},
},
Expand All @@ -74,7 +74,7 @@ const webSiteRouter = {
hidden: true,
component: () => import('@/views/website/runtime/go/index.vue'),
meta: {
activeMenu: '/websites/runtimes/go',
activeMenu: '/websites/runtimes/php',
requiresAuth: false,
},
},
Expand Down
24 changes: 20 additions & 4 deletions frontend/src/views/website/website/nginx/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<template>
<LayoutContent :title="$t('nginx.nginxConfig')" :reload="true">
<template #leftToolBar>
<el-button type="primary" :plain="activeName !== '1'" @click="changeTab('1')">
<el-button
type="primary"
:plain="activeName !== '1'"
@click="changeTab('1')"
:disabled="status != 'Running'"
>
{{ $t('nginx.status') }}
</el-button>
<el-button type="primary" :plain="activeName !== '2'" @click="changeTab('2')">
Expand All @@ -10,7 +15,12 @@
<el-button type="primary" :plain="activeName !== '3'" @click="changeTab('3')">
{{ $t('website.nginxPer') }}
</el-button>
<el-button type="primary" :plain="activeName !== '4'" @click="changeTab('4')">
<el-button
type="primary"
:plain="activeName !== '4'"
@click="changeTab('4')"
:disabled="status != 'Running'"
>
{{ $t('website.log') }}
</el-button>
</template>
Expand All @@ -30,8 +40,8 @@ import ContainerLog from '@/components/container-log/index.vue';
import NginxPer from './performance/index.vue';
import Status from './status/index.vue';

let activeName = ref('1');
let dialogContainerLogRef = ref();
const activeName = ref('1');
const dialogContainerLogRef = ref();

const props = defineProps({
containerName: {
Expand All @@ -55,4 +65,10 @@ const changeTab = (index: string) => {
});
}
};

onMounted(() => {
if (props.status != 'Running') {
activeName.value = '2';
}
});
</script>
2 changes: 1 addition & 1 deletion frontend/src/views/website/website/nginx/source/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div v-loading="loading">
<CodemirrorPro v-model="content" mode="nginx"></CodemirrorPro>
<CodemirrorPro v-model="content" mode="nginx" :heightDiff="350"></CodemirrorPro>
<div class="mt-2.5">
<el-button @click="getDefaultConfig()" :disabled="loading">
{{ $t('app.defaultConfig') }}
Expand Down
Loading