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: Node.js 运行环境增加自定义启动命令 #2677

Merged
merged 1 commit into from
Oct 25, 2023
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
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
Loading