Skip to content

Commit 6122aa3

Browse files
authored
feat: Increase version restrictions for node switching (#7631)
1 parent c7936e6 commit 6122aa3

File tree

8 files changed

+88
-11
lines changed

8 files changed

+88
-11
lines changed

agent/configs/system.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package configs
33
type System struct {
44
Mode string `mapstructure:"mode"`
55

6-
Port string `mapstructure:"version"`
6+
Port string `mapstructure:"port"`
77
Version string `mapstructure:"version"`
88
BaseDir string `mapstructure:"base_dir"`
99
EncryptKey string `mapstructure:"encrypt_key"`

agent/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/minio/minio-go/v7 v7.0.74
3232
github.com/nicksnyder/go-i18n/v2 v2.4.0
3333
github.com/opencontainers/image-spec v1.1.0
34+
github.com/oschwald/maxminddb-golang v1.13.1
3435
github.com/patrickmn/go-cache v2.1.0+incompatible
3536
github.com/pkg/errors v0.9.1
3637
github.com/pkg/sftp v1.13.6
@@ -171,7 +172,6 @@ require (
171172
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
172173
github.com/opencontainers/go-digest v1.0.0 // indirect
173174
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
174-
github.com/oschwald/maxminddb-golang v1.13.1 // indirect
175175
github.com/pelletier/go-toml v1.9.5 // indirect
176176
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
177177
github.com/pierrec/lz4/v4 v4.1.15 // indirect

frontend/src/api/interface/setting.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,6 @@ export namespace Setting {
227227
id: number;
228228
addr: string;
229229
status: string;
230+
version: string;
230231
}
231232
}

frontend/src/lang/modules/en.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,10 @@ const message = {
17431743
upgradeNow: 'Upgrade now',
17441744
source: 'Download source',
17451745
hasNewVersion: 'New version Available',
1746+
versionHigher:
1747+
'Detected that node {0} version is higher than the main node, switching is not supported at this time. Please upgrade the main node system version and try again!',
1748+
versionLower:
1749+
'Detected that node {0} version is lower than the main node, switching is not supported at this time. Please upgrade the system version of this node and try again!',
17461750

17471751
about: 'About',
17481752
project: 'Project Address',

frontend/src/lang/modules/tw.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,8 @@ const message = {
15481548
upgradeNow: '立即更新',
15491549
source: '下載源',
15501550
hasNewVersion: '有新版本',
1551+
versionHigher: '檢測到節點 {0} 版本高於主節點,暫不支持切換,請先升級主節點系統版本後重試!',
1552+
versionLower: '檢測到節點 {0} 版本低於主節點,暫不支持切換,請先升級該節點系統版本後重試!',
15511553

15521554
safe: '安全',
15531555
bindInfo: '監聽地址',

frontend/src/lang/modules/zh.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,8 @@ const message = {
15461546
upgradeNow: '立即更新',
15471547
source: '下载源',
15481548
hasNewVersion: '有新版本',
1549+
versionHigher: '检测到节点 {0} 版本高于主节点,暂不支持切换,请先升级主节点系统版本后重试!',
1550+
versionLower: '检测到节点 {0} 版本低于主节点,暂不支持切换,请先升级该节点系统版本后重试!',
15491551

15501552
safe: '安全',
15511553
bindInfo: '监听地址',

frontend/src/layout/components/Sidebar/index.vue

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
>
99
<Logo :isCollapse="isCollapse" />
1010
<div class="el-dropdown-link flex justify-between items-center">
11-
<el-button type="text" @click="openChangeNode" @mouseenter="openChangeNode">
12-
<span>
13-
{{ loadCurrentName() }}
14-
</span>
11+
<el-button link class="ml-4" @click="openChangeNode" @mouseenter="openChangeNode">
12+
{{ loadCurrentName() }}
1513
</el-button>
1614
<div>
1715
<el-dropdown
@@ -25,11 +23,12 @@
2523
<template #dropdown>
2624
<el-dropdown-menu>
2725
<el-dropdown-item command="local">
28-
<SvgIcon class="ico" iconName="p-host" />
26+
<el-button link icon="CircleCheck" type="success" />
2927
{{ $t('terminal.local') }}
3028
</el-dropdown-item>
3129
<el-dropdown-item v-for="item in nodes" :key="item.name" :command="item.name">
32-
<SvgIcon class="ico" iconName="p-host" />
30+
<el-button v-if="item.status === 'Healthy'" link icon="CircleCheck" type="success" />
31+
<el-button v-else link icon="Warning" type="danger" />
3332
{{ item.name }}
3433
</el-dropdown-item>
3534
</el-dropdown-menu>
@@ -74,16 +73,18 @@ import { logOutApi } from '@/api/modules/auth';
7473
import i18n from '@/lang';
7574
import { DropdownInstance, ElMessageBox } from 'element-plus';
7675
import { GlobalStore, MenuStore } from '@/store';
77-
import { MsgSuccess } from '@/utils/message';
76+
import { MsgError, MsgSuccess } from '@/utils/message';
7877
import { isString } from '@vueuse/core';
7978
import { getSettingInfo, listNodeOptions } from '@/api/modules/setting';
8079
import { countExecutingTask } from '@/api/modules/log';
80+
import { compareVersion } from '@/utils/version';
8181
8282
const route = useRoute();
8383
const menuStore = MenuStore();
8484
const globalStore = GlobalStore();
8585
const nodes = ref([]);
8686
const nodeChangeRef = ref<DropdownInstance>();
87+
const version = ref();
8788
defineProps({
8889
menuRouter: {
8990
type: Boolean,
@@ -172,8 +173,32 @@ const loadNodes = async () => {
172173
});
173174
};
174175
const changeNode = (command: string) => {
175-
globalStore.currentNode = command || 'local';
176-
location.reload();
176+
if (globalStore.currentNode === command) {
177+
return;
178+
}
179+
if (command == 'local') {
180+
globalStore.currentNode = command || 'local';
181+
location.reload();
182+
return;
183+
}
184+
for (const item of nodes.value) {
185+
if (item.name == command) {
186+
if (version.value == item.version) {
187+
globalStore.currentNode = command || 'local';
188+
location.reload();
189+
return;
190+
}
191+
let compareItem = compareVersion(item.version, version.value);
192+
if (compareItem) {
193+
MsgError(i18n.global.t('setting.versionHigher', [command]));
194+
return;
195+
}
196+
if (!compareItem) {
197+
MsgError(i18n.global.t('setting.versionLower', [command]));
198+
return;
199+
}
200+
}
201+
}
177202
};
178203
179204
function extractLabels(node: Node, result: string[]): void {
@@ -195,6 +220,7 @@ function getCheckedLabels(json: Node): string[] {
195220
196221
const search = async () => {
197222
const res = await getSettingInfo();
223+
version.value = res.data.systemVersion;
198224
const json: Node = JSON.parse(res.data.xpackHideMenu);
199225
if (json.isCheck === false) {
200226
json.children.forEach((child: any) => {

frontend/src/utils/version.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export function compareVersion(version1: string, version2: string): boolean {
2+
const v1s = extractNumbers(version1);
3+
const v2s = extractNumbers(version2);
4+
5+
const maxLen = Math.max(v1s.length, v2s.length);
6+
v1s.push(...new Array(maxLen - v1s.length).fill('0'));
7+
v2s.push(...new Array(maxLen - v2s.length).fill('0'));
8+
9+
for (let i = 0; i < maxLen; i++) {
10+
const v1 = parseInt(v1s[i], 10);
11+
const v2 = parseInt(v2s[i], 10);
12+
if (v1 !== v2) {
13+
return v1 > v2;
14+
}
15+
}
16+
return true;
17+
}
18+
19+
function extractNumbers(version: string): string[] {
20+
const numbers: string[] = [];
21+
let start = -1;
22+
for (let i = 0; i < version.length; i++) {
23+
if (isDigit(version[i])) {
24+
if (start === -1) {
25+
start = i;
26+
}
27+
} else {
28+
if (start !== -1) {
29+
numbers.push(version.slice(start, i));
30+
start = -1;
31+
}
32+
}
33+
}
34+
if (start !== -1) {
35+
numbers.push(version.slice(start));
36+
}
37+
return numbers;
38+
}
39+
40+
function isDigit(char: string): boolean {
41+
return /^\d$/.test(char);
42+
}

0 commit comments

Comments
 (0)