Skip to content

Commit

Permalink
feat: Node.js 运行环境增加自定义启动命令 (#2677)
Browse files Browse the repository at this point in the history
Refs #2559
  • Loading branch information
zhengkunwang223 committed Oct 25, 2023
1 parent 87cd5c9 commit 5db6e7e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 25 deletions.
18 changes: 18 additions & 0 deletions backend/app/service/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,24 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error {
}
}
}

appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(runtime.AppDetailID))
if err != nil {
return err
}
app, err := appRepo.GetFirst(commonRepo.WithByID(appDetail.AppId))
if err != nil {
return err
}
fileOp := files.NewFileOp()
appVersionDir := path.Join(constant.AppResourceDir, app.Resource, app.Key, appDetail.Version)
if !fileOp.Stat(appVersionDir) || appDetail.Update {
if err := downloadApp(app, appDetail, nil); err != nil {
return err
}
_ = fileOp.Rename(path.Join(runtime.GetPath(), "run.sh"), path.Join(runtime.GetPath(), "run.sh.bak"))
_ = fileOp.CopyFile(path.Join(appVersionDir, "run.sh"), runtime.GetPath())
}
}

projectDir := path.Join(constant.RuntimeDir, runtime.Type, runtime.Name)
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,8 @@ const message = {
module: 'Module',
nodeOperatorHelper:
'Is {0} {1} module? The operation may cause abnormality in the operating environment, please confirm before proceeding',
customScript: 'Custom startup command',
customScriptHelper: 'Please fill in the complete startup command, for example: npm run start',
},
process: {
pid: 'Process ID',
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/tw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,8 @@ const message = {
moduleManager: '模塊管理',
module: '模',
nodeOperatorHelper: '是{0} {1} 模組操作可能導致運轉環境異常請確認後操作',
customScript: '自訂啟動指令',
customScriptHelper: '請填寫完整的啟動指令例如npm run start',
},
process: {
pid: '進程ID',
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lang/modules/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,8 @@ const message = {
moduleManager: '模块管理',
module: '模',
nodeOperatorHelper: '是{0} {1} 模块操作可能导致运行环境异常请确认后操作',
customScript: '自定义启动命令',
customScriptHelper: '请填写完整的启动命令例如npm run start',
},
process: {
pid: '进程ID',
Expand Down
84 changes: 59 additions & 25 deletions frontend/src/views/website/runtime/node/operate/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,45 @@
</template>
</el-input>
</el-form-item>
<el-form-item :label="$t('runtime.runScript')" prop="params.EXEC_SCRIPT">
<el-select v-model="runtime.params['EXEC_SCRIPT']">
<el-option
v-for="(script, index) in scripts"
:key="index"
:label="script.name + '' + script.script + ''"
:value="script.name"
>
<el-row :gutter="10">
<el-col :span="4">{{ script.name }}</el-col>
<el-col :span="10">{{ '' + script.script + '' }}</el-col>
</el-row>
</el-option>
</el-select>
<span class="input-help">{{ $t('runtime.runScriptHelper') }}</span>
</el-form-item>
<el-row :gutter="20">
<el-col :span="18">
<el-form-item :label="$t('runtime.runScript')" prop="params.EXEC_SCRIPT">
<el-select
v-model="runtime.params['EXEC_SCRIPT']"
v-if="runtime.params['CUSTOM_SCRIPT'] == '0'"
>
<el-option
v-for="(script, index) in scripts"
:key="index"
:label="script.name + '' + script.script + ''"
:value="script.name"
>
<el-row :gutter="10">
<el-col :span="4">{{ script.name }}</el-col>
<el-col :span="10">{{ '' + script.script + '' }}</el-col>
</el-row>
</el-option>
</el-select>
<el-input v-else v-model="runtime.params['EXEC_SCRIPT']"></el-input>
<span class="input-help" v-if="runtime.params['CUSTOM_SCRIPT'] == '0'">
{{ $t('runtime.runScriptHelper') }}
</span>
<span class="input-help" v-else>
{{ $t('runtime.customScriptHelper') }}
</span>
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item :label="$t('runtime.customScript')" prop="params.CUSTOM_SCRIPT">
<el-switch
v-model="runtime.params['CUSTOM_SCRIPT']"
:active-value="'1'"
:inactive-value="'0'"
@change="changeScriptType"
/>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="9">
<el-form-item :label="$t('runtime.appPort')" prop="params.NODE_APP_PORT">
Expand All @@ -94,13 +117,13 @@
<span class="input-help">{{ $t('runtime.externalPortHelper') }}</span>
</el-form-item>
</el-col>

<el-col :span="6">
<el-form-item :label="$t('app.allowPort')" prop="params.HOST_IP">
<el-select v-model="runtime.params['HOST_IP']">
<el-option :label="$t('runtime.open')" value="0.0.0.0"></el-option>
<el-option :label="$t('runtime.close')" value="127.0.0.1"></el-option>
</el-select>
<el-switch
v-model="runtime.params['HOST_IP']"
:active-value="'0.0.0.0'"
:inactive-value="'127.0.0.1'"
/>
</el-form-item>
</el-col>
</el-row>
Expand Down Expand Up @@ -178,6 +201,7 @@ const initData = (type: string) => ({
params: {
PACKAGE_MANAGER: 'npm',
HOST_IP: '0.0.0.0',
CUSTOM_SCRIPT: '0',
},
type: type,
resource: 'appstore',
Expand All @@ -193,12 +217,14 @@ const rules = ref<any>({
codeDir: [Rules.requiredInput],
port: [Rules.requiredInput, Rules.paramPort, checkNumberRange(1, 65535)],
source: [Rules.requiredSelect],
params: {
NODE_APP_PORT: [Rules.requiredInput, Rules.paramPort, checkNumberRange(1, 65535)],
PACKAGE_MANAGER: [Rules.requiredSelect],
HOST_IP: [Rules.requiredSelect],
EXEC_SCRIPT: [Rules.requiredSelect],
CONTAINER_NAME: [Rules.requiredInput, Rules.containerName],
CUSTOM_SCRIPT: [Rules.requiredInput],
},
});
const scripts = ref<Runtime.NodeScripts[]>([]);
Expand Down Expand Up @@ -250,6 +276,13 @@ const getPath = (codeDir: string) => {
getScripts();
};
const changeScriptType = () => {
runtime.params['EXEC_SCRIPT'] = '';
if (runtime.params['CUSTOM_SCRIPT'] == '0') {
getScripts();
}
};
const getScripts = () => {
GetNodeScripts({ codeDir: runtime.codeDir }).then((res) => {
scripts.value = res.data;
Expand Down Expand Up @@ -359,10 +392,11 @@ const getRuntime = async (id: number) => {
port: data.port,
});
editParams.value = data.appParams;
if (mode.value == 'edit') {
searchApp(data.appID);
searchApp(data.appID);
if (data.params['CUSTOM_SCRIPT'] == '0') {
getScripts();
}
getScripts();
open.value = true;
} catch (error) {}
};
Expand All @@ -372,10 +406,10 @@ const acceptParams = async (props: OperateRrops) => {
if (props.mode === 'create') {
Object.assign(runtime, initData(props.type));
searchApp(null);
open.value = true;
} else {
getRuntime(props.id);
}
open.value = true;
};
defineExpose({
Expand Down

0 comments on commit 5db6e7e

Please sign in to comment.