Skip to content

Commit

Permalink
feature: 通过接口获得操作系统类型(close TencentBlueKing#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Nov 17, 2021
1 parent 21e3fc0 commit 0be549c
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 50 deletions.
2 changes: 1 addition & 1 deletion apps/node_man/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def get_optional_items(cls) -> List[str]:
OS_TUPLE = ("LINUX", "WINDOWS", "AIX", "SOLARIS")
OS_CHOICES = tuple_choices(OS_TUPLE)
OsType = choices_to_namedtuple(OS_CHOICES)
OS_CHN = {"WINDOWS": "Windows", "LINUX": "Linux", "AIX": "Aix", "SOLARIS": "Solaris"}
OS_CHN = {os_type: os_type if os_type == OsType.AIX else os_type.capitalize() for os_type in OS_TUPLE}
BK_OS_TYPE = {"LINUX": "1", "WINDOWS": "2", "AIX": "3", "SOLARIS": "5"}

# 操作系统->系统账户映射表
Expand Down
19 changes: 15 additions & 4 deletions apps/node_man/handlers/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
specific language governing permissions and limitations under the License.
"""
import re
from typing import Tuple

from django.conf import settings
from django.db import connection
Expand Down Expand Up @@ -145,7 +146,7 @@ def fetch_host_condition(self):
for index, item in enumerate(sublist):
col_map[index].add(item)

os_types_children = [{"name": constants.OS_CHN.get(os, os), "id": os} for os in os_types if os != ""]
os_types_children = self.fetch_os_type_children(tuple(os_types))
statuses_children = [
{"name": constants.PROC_STATUS_CHN.get(status, status), "id": status} for status in statuses if status != ""
]
Expand Down Expand Up @@ -354,9 +355,7 @@ def fetch_plugin_host_condition(self):
special_os_type = [constants.OsType.AIX, constants.OsType.SOLARIS]
if os_type in special_os_type and settings.BKAPP_RUN_ENV == constants.BkappRunEnvType.CE.value:
continue
os_dict["children"].append(
{"id": os_type, "name": os_type if os_type == constants.OsType.AIX else os_type.capitalize()}
)
os_dict["children"].append({"id": os_type, "name": constants.OS_CHN.get(os_type, os_type)})

ret_value.append(os_dict)

Expand Down Expand Up @@ -437,6 +436,15 @@ def fetch_plugin_version_condition():

return plugin_result

@staticmethod
def fetch_os_type_children(os_types: Tuple = constants.OsType):
os_type_children = []
for os_type in os_types:
if os_type == "":
continue
os_type_children.append({"id": os_type, "name": constants.OS_CHN.get(os_type, os_type)})
return os_type_children

def filter_condition(self, category):
"""
获取过滤条件
Expand All @@ -460,6 +468,9 @@ def filter_condition(self, category):
elif category == "plugin_host":
ret = self.fetch_plugin_host_condition()
return ret
elif category == "os_type":
ret = self.fetch_os_type_children()
return ret

def search(self, key):
"""
Expand Down
7 changes: 1 addition & 6 deletions apps/node_man/tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,7 @@ def test_fetch_plugin_list_condition_no_permission(self, *args, **kwargs):
{
"name": "操作系统",
"id": "os_type",
"children": [
{"id": "LINUX", "name": "Linux"},
{"id": "WINDOWS", "name": "Windows"},
{"id": "AIX", "name": "AIX"},
{"id": "SOLARIS", "name": "Solaris"},
],
"children": MetaHandler.fetch_os_type_children(),
},
)
self.assertEqual(
Expand Down
3 changes: 3 additions & 0 deletions dev_log/2.1.356/kiozhang_202111171615.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
feature:
- "通过接口获得操作系统类型 (closed #230)"
15 changes: 4 additions & 11 deletions frontend/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MainStore } from '@/store';

export interface IAuth {
id: string
name: string
Expand Down Expand Up @@ -34,17 +36,8 @@ const getAuthentication = () => {
}
return auths;
};
const getSysOptions = () => {
const options: {id: string, name: string }[] = [
{ id: 'WINDOWS', name: 'Windows' },
{ id: 'LINUX', name: 'Linux' },
];
if (window.PROJECT_CONFIG.BKAPP_RUN_ENV !== 'ce') {
options.push({ id: 'AIX', name: 'AIX' }, { id: 'SOLARIS', name: 'Solaris' });
}
return options;
};

export const authentication = getAuthentication();
export const defaultPort = window.PROJECT_CONFIG.DEFAULT_SSH_PORT ? Number(window.PROJECT_CONFIG.DEFAULT_SSH_PORT) : 22;
export const sysOptions = getSysOptions();
export const sysOptions = MainStore.osList;
export default authentication;
9 changes: 9 additions & 0 deletions frontend/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const router = new VueRouter({
routes,
});

const loadOsRoute = ['agentSetup', 'agentImport', 'agentEdit'];
const cancelRequest = async () => {
const allRequest = http.queue.get() as any[];
const requestQueue = allRequest.filter(request => request.cancelWhenRouteChange);
Expand Down Expand Up @@ -72,6 +73,14 @@ const beforeRouterMethod = async (to: Route, next: any) => {
// 重置业务权限
MainStore.updateBizAction(authority ? authority.page : '');
await cancelRequest();
if (!MainStore.osList) {
if (loadOsRoute.includes(to.name)) {
const list = await MainStore.getOsList();
MainStore.updateOsList(list);
} else {
MainStore.getOsList().then(list => MainStore.updateOsList(list));
}
}
next();
};

Expand Down
6 changes: 2 additions & 4 deletions frontend/src/store/modules/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,8 @@ export default class AgentStore extends VuexModule {
* @param {*} params
*/
@Action
public async getFilterCondition() {
let data: ISearchItem[] = await getFilterCondition({
category: 'host',
}).catch(() => []);
public async getFilterCondition(category = 'host') {
let data: ISearchItem[] = await getFilterCondition({ category }).catch(() => []);
data = data.map((item) => {
if (item.children && item.children.length) {
item.multiable = true;
Expand Down
18 changes: 17 additions & 1 deletion frontend/src/store/modules/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Vue from 'vue';
import http from '@/api';
import navList from '@/router/navigation-config';
import { retrieveBiz, fetchTopo } from '@/api/modules/cmdb';
import { retrieveGlobalSettings } from '@/api/modules/meta';
import { retrieveGlobalSettings, getFilterCondition } from '@/api/modules/meta';
import { fetchPublicKeys } from '@/api/modules/rsa';
import {
fetchPermission,
Expand Down Expand Up @@ -65,6 +65,8 @@ export default class Main extends VuexModule {
// cache view
public cacheViews: string[] = [];
public routerBackName = '';
public osList: any = null;
public osMap: Dictionary = null;

// 公共 mutations
/**
Expand Down Expand Up @@ -274,6 +276,15 @@ export default class Main extends VuexModule {
public updateRouterBackName(name = '') {
this.routerBackName = name;
}
@Mutation
public updateOsList(list = []) {
this.osList = list;
this.osMap = list.reduce((map, item) => {
map[item.id] = item.name;
return map;
}, {});
}

/**
* 获取用户信息
*
Expand Down Expand Up @@ -379,4 +390,9 @@ export default class Main extends VuexModule {
const data: IKeyItem[] = await fetchPublicKeys(params).catch(() => []);
return data.find(item => item.name === 'DEFAULT') || {};
}
@Action
public async getOsList(category = 'os_type'): Promise<Dictionary> {
const res = await getFilterCondition({ category }).catch(() => []);
return res || [];
}
}
2 changes: 1 addition & 1 deletion frontend/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface IAuthApply {
}

export type RequestMethods = 'delete' | 'get' | 'head' | 'post' | 'put' | 'patch';
export type IOs = 'LINUX' | 'WINDOWS' | 'AIX';
export type IOs = 'LINUX' | 'WINDOWS' | 'AIX' | 'SOLARIS';

export interface IUserConfig {
// http 请求默认 id
Expand Down
9 changes: 3 additions & 6 deletions frontend/src/views/agent/agent-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,6 @@ export default class AgentList extends Mixins(pollMixin, TableHeaderMixins, auth
terminated: this.$t('异常'),
unknown: this.$t('未知'),
};
private osMap = {
LINUX: 'Linux',
WINDOWS: 'Windows',
AIX: 'AIX',
SOLARIS: 'Solaris',
};
// 批量操作
private operate: IOperateItem[] = [
{
Expand Down Expand Up @@ -768,6 +762,9 @@ export default class AgentList extends Mixins(pollMixin, TableHeaderMixins, auth
private operateBiz: IBizValue[] =[]; // 有操作权限的业务
private cloudAgentNum = 0; // 从云区域点击跳转过来的主机数量,区分是否因为权限问题看不到主机
private get osMap() {
return MainStore.osMap;
}
private get fontSize() {
return MainStore.fontSize;
}
Expand Down
8 changes: 3 additions & 5 deletions frontend/src/views/plugin/plugin-list/plugin-list-table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@ export default class PluginRuleTable extends Mixins(FormLabelMixin, HeaderRender
private currentHostId = -1;
private currentHostStatus = '';
private showSlider = false;
private osMap = {
LINUX: 'Linux',
WINDOWS: 'Windows',
AIX: 'AIX',
};
// 本地存储Key
private localMark = 'plugin_list_table';
private filterField: ITabelFliter[] = [];
Expand All @@ -220,6 +215,9 @@ export default class PluginRuleTable extends Mixins(FormLabelMixin, HeaderRender
failed: this.$t('异常'), // 失败
};
private get osMap() {
return MainStore.osMap;
}
private get selectionCount() {
if (this.checkType === 'current') {
return this.selections.length;
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/views/plugin/plugin-list/plugin-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,6 @@ export default class PluginList extends Mixins(HeaderFilterMixins) {
private runningCount = 0;
private checkValue: CheckValueEnum = 0;
private hasOldRouteParams = false;
private osMap = {
LINUX: 'Linux',
WINDOWS: 'Windows',
AIX: 'AIX',
};
private strategyValue: Array<number | string> = []; // 插件id number, 策略名称 string
private sortData: ISortData = {
head: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,19 +296,16 @@ export default class PerformPreview extends Mixins(HeaderRenderMixin, HeaderFilt
head: '',
sort_type: '',
};
private osMap: Dictionary = {
LINUX: 'Linux',
WINDOWS: 'Windows',
AIX: 'AIX',
};
private statusMap = {
running: window.i18n.t('正常'),
terminated: window.i18n.t('异常'),
not_installed: window.i18n.t('未安装'),
};
public filterData: ISearchItem[] = [];
private get osMap() {
return MainStore.osMap;
}
private get statisticsList() {
return [
{ id: 'running', count: this.runningNum, path: '正常agent个数' },
Expand Down

0 comments on commit 0be549c

Please sign in to comment.