Skip to content

Commit

Permalink
feat: 配置文件支持下发等
Browse files Browse the repository at this point in the history
  • Loading branch information
tingfuyeh committed Jan 21, 2025
1 parent aa7fba0 commit 6e14323
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 20 deletions.
18 changes: 9 additions & 9 deletions web/src/polaris/common/ducks/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,15 @@ get preSagas(){
* 判断是否初始化了主账户
*/
*checkAdminUserExist() {
checkExistAdminUser()
.then(res => {
if (!res.user) {
router.navigate('/init')
}
})
.catch(err => {
console.log(err)
})
// checkExistAdminUser()
// .then(res => {
// if (!res?.user) {
// router.navigate('/init')
// }
// })
// .catch(err => {
// console.log(err)
// })
return true
}

Expand Down
66 changes: 65 additions & 1 deletion web/src/polaris/configuration/fileGroup/detail/file/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import {
Modal,
notification,
Alert,
Popover,
Upload,
} from 'tea-component'
import { FileStatus, FileStatusMap } from './constants'
import { ConfigFileModeMap, FileStatus, FileStatusMap, SaveFileEncodingMap } from './constants'
import { autotip, radioable, scrollable } from 'tea-component/lib/table/addons'
import FileDiff from './FileDiff'
import MonacoEditor from '@src/polaris/common/components/MocacoEditor'
Expand Down Expand Up @@ -307,6 +309,12 @@ export default function Page(props: DuckCmpProps<Duck>) {
</Text>
</FormText>
</FormItem>
<FormItem label={'推送方式'}>
<FormText>{ConfigFileModeMap[currentNode.supported_client]}</FormText>
</FormItem>
<FormItem label={'配置下发路径'}>
<FormText>{currentNode?.persistent?.path || '-'}</FormText>
</FormItem>
</Col>
<Col span={12}>
<FormItem label='最后修改人'>
Expand All @@ -321,6 +329,31 @@ export default function Page(props: DuckCmpProps<Duck>) {
<FormItem label='格式'>
<FormText>{currentNode.format || '-'}</FormText>
</FormItem>
<FormItem label={'文件保存编码'}>
<FormText>{SaveFileEncodingMap[currentNode?.persistent?.encoding] || '-'}</FormText>
</FormItem>
<FormItem label={'后置脚本命令'}>
<FormText>
<div style={{ textOverflow: 'ellipsis', width: '150px', display: 'inline-block' }}>
{currentNode?.persistent?.postCmd || '-'}
</div>
{currentNode?.persistent?.postCmd && (
<Popover
placement='top-start'
overlay={
<Card>
<Card.Body>
<pre>{currentNode?.persistent?.postCmd}</pre>
</Card.Body>
</Card>
}
trigger={'click'}
>
{'显示全部'}
</Popover>
)}
</FormText>
</FormItem>
</Col>
</Row>
</Form>
Expand Down Expand Up @@ -380,6 +413,37 @@ export default function Page(props: DuckCmpProps<Duck>) {
取消
</Button>
)}
<Upload
maxSize={500 * 1024}
beforeUpload={async (file, files, isAccepted) => {
if (isAccepted) {
const readFileTask = new Promise<string>((resolve, reject) => {
const reader = new FileReader()
reader.readAsText(file, 'utf8')
reader.onload = event => {
resolve(event.target.result.toString())
}
reader.onerror = event => reject(`文件读取出错: ${event.target.error.toString()}`)
})
await readFileTask
.then(res => {
handlers.editCurrentNode()
handlers.setEditContent(res)
})
.catch(err => {
notification.error({ description: err.toString() })
})
} else {
notification.error({
description: `文件大小大于${500 * 1024}bytes,请重新上传`,
})
}
// 文件不符合 accept,无法被选中
return false
}}
>
<Button>{'上传文件'}</Button>
</Upload>
</>
}
/>
Expand Down
40 changes: 40 additions & 0 deletions web/src/polaris/configuration/fileGroup/detail/file/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,43 @@ export const FileStatusMap = {
},
}
export const NeedCheckFormat = [FileFormat.YAML, FileFormat.JSON]
export enum ConfigFileMode {
Default = 'CLIENT_SDK', //SDKonly
FileMode = 'CLIENT_AGENT', //AgentOnly
ShareMode = 'CLIENT_ALL', //both
}
export const ConfigFileModeMap = {
[ConfigFileMode.Default]: '仅SDK读取',
[ConfigFileMode.FileMode]: '仅Agent读取',
[ConfigFileMode.ShareMode]: 'SDK和Agent同时读取',
}
export const ConfigFileModeOptions = [
{
value: String(ConfigFileMode.Default),
text: ConfigFileModeMap[ConfigFileMode.Default],
tooltip: 'SDK框架接入时,不感知配置文件编码、下发路径和后置脚本命令,无需配置。',
},
{
value: String(ConfigFileMode.FileMode),
text: ConfigFileModeMap[ConfigFileMode.FileMode],
},
{
value: String(ConfigFileMode.ShareMode),
text: ConfigFileModeMap[ConfigFileMode.ShareMode],
},
]

export enum SaveFileEncoding {
UTF8 = 'UTF-8',
GBK = 'GBK',
}
export const SaveFileEncodingMap = {
[SaveFileEncoding.UTF8]: 'utf-8',
[SaveFileEncoding.GBK]: 'gbk',
}
export const SaveFileEncodingOptions = Object.keys(SaveFileEncoding).map((key: string) => {
return {
value: SaveFileEncoding[key],
text: SaveFileEncodingMap[key],
}
})
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from 'react'
import { DuckCmpProps, purify } from 'saga-duck'
import Duck from './CreateDuck'
import { Form, Select, FormItem, Switch, InputAdornment } from 'tea-component'
import { Form, Select, FormItem, Switch, InputAdornment, Button, Icon, Segment } from 'tea-component'
import Dialog from '@src/polaris/common/duckComponents/Dialog'
import FormField from '@src/polaris/common/duckComponents/form/Field'
import Input from '@src/polaris/common/duckComponents/form/Input'
import { TagTable } from '@src/polaris/common/components/TagTable'
import { ConfigFileMode, ConfigFileModeOptions, SaveFileEncodingOptions } from '../constants'
import MonacoEditor from '@src/polaris/common/components/MocacoEditor'

export default function Create(props: DuckCmpProps<Duck>) {
const { duck, store, dispatch } = props
Expand Down Expand Up @@ -45,7 +47,20 @@ const CreateForm = purify(function CreateForm(props: DuckCmpProps<Duck>) {
} = duck

const formApi = form.getAPI(store, dispatch)
const { namespace, comment, name, group, format, tags, encrypted, encryptAlgo } = formApi.getFields([
const [showAdvance, setShowAdvance] = React.useState(false)

const {
namespace,
comment,
name,
group,
format,
tags,
encrypted,
encryptAlgo,
supported_client,
persistent,
} = formApi.getFields([
'namespace',
'name',
'comment',
Expand All @@ -54,7 +69,10 @@ const CreateForm = purify(function CreateForm(props: DuckCmpProps<Duck>) {
'tags',
'encrypted',
'encryptAlgo',
'supported_client',
'persistent',
])
const { encoding, path, postCmd } = persistent.getFields(['encoding', 'path', 'postCmd'])
const options = selectors.options(store)

return (
Expand Down Expand Up @@ -149,6 +167,47 @@ const CreateForm = purify(function CreateForm(props: DuckCmpProps<Duck>) {
</>
)}
</FormField>
<Button type={'link'} onClick={() => setShowAdvance(!showAdvance)} style={{ cursor: 'pointer' }}>
<Icon type={showAdvance ? 'arrowup' : 'arrowdown'} />
{'高级设置'}
</Button>
{showAdvance && (
<>
<FormItem label={'推送方式'}>
<Segment
options={ConfigFileModeOptions}
value={supported_client.getValue()}
onChange={v => supported_client.setValue(v)}
/>
</FormItem>
{supported_client.getValue() !== ConfigFileMode.Default && (
<>
<FormItem label={'文件编码方式'}>
<Segment
options={SaveFileEncodingOptions}
value={encoding.getValue()}
onChange={v => encoding.setValue(v)}
/>
</FormItem>
<FormField field={path} label={'配置下发路径'}>
<Input field={path} placeholder={'e.g. /etc/nginx/conf.d/'} size={'m'} />
</FormField>
<FormField
field={postCmd}
label={'后置脚本命令'}
message={'选填,不超过200个字符,如 sh /etc/nginx/conf.d/start.sh'}
>
<MonacoEditor
height={300}
width={500}
value={postCmd.getValue()}
onChange={v => postCmd.setValue(v)}
/>
</FormField>
</>
)}
</>
)}
</Form>
</>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { reduceFromPayload } from 'saga-duck'
import { notification } from 'tea-component'
import { isReadOnlyConfigGroup, isReadOnlyNamespace } from '@src/polaris/service/utils'
import { FileFormat } from './Create'
import { ConfigFileMode, SaveFileEncoding } from '../constants'

export interface DialogOptions {
namespaceList?: NamespaceItem[]
Expand Down Expand Up @@ -56,9 +57,18 @@ export default class CreateDuck extends FormDialog {
selectors,
} = this

const { name, comment, namespace, group, format, tags, encrypted, encryptAlgo } = form.selectors.values(
yield select(),
)
const {
name,
comment,
namespace,
group,
format,
tags,
encrypted,
encryptAlgo,
persistent,
supported_client,
} = form.selectors.values(yield select())
const options = selectors.options(yield select())
const data = selectors.data(yield select())
const parsedName = name
Expand All @@ -77,6 +87,8 @@ export default class CreateDuck extends FormDialog {
tags,
encrypted,
encryptAlgo,
supported_client,
...(supported_client === ConfigFileMode.Default ? { persistent: undefined } : { persistent }),
})
if (configFile?.name) {
notification.success({ description: '编辑成功' })
Expand All @@ -96,6 +108,8 @@ export default class CreateDuck extends FormDialog {
content: format === FileFormat.JSON ? '{}' : '',
encrypted: encrypted,
encryptAlgo: encryptAlgo,
supported_client,
...(supported_client === ConfigFileMode.Default ? {} : { persistent }),
})
if (configFile?.name) {
notification.success({ description: '创建成功' })
Expand Down Expand Up @@ -213,6 +227,10 @@ export default class CreateDuck extends FormDialog {
yield put(form.creators.setMeta(options))
yield put(
form.creators.setValues({
supported_client: ConfigFileMode.Default,
persistent: {
encoding: SaveFileEncoding.UTF8,
},
...data,
}),
)
Expand All @@ -229,6 +247,12 @@ export interface Values {
tags?: Array<KeyValuePair>
encrypted: boolean
encryptAlgo: string
supported_client: string
persistent: {
encoding: string
path: string
postCmd: string
}
}
class CreateForm extends Form {
Values: Values
Expand Down Expand Up @@ -269,4 +293,11 @@ const validator = CreateForm.combineValidators<Values, any>({
format(v) {
if (!v) return '请选择格式'
},
persistent: {
postCmd(v) {
if (v?.length > 200) {
return '命令长度不能超过200'
}
},
},
})
13 changes: 12 additions & 1 deletion web/src/polaris/configuration/fileGroup/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ export interface CreateConfigFileParams {
createBy?: string
encrypted: boolean
encryptAlgo: string
supported_client: string
persistent?: {
encoding: string
path: string
postCmd: string
}
}
export interface CreateConfigFileResult {
configFile: ConfigFile
Expand Down Expand Up @@ -161,6 +167,12 @@ export interface ModifyConfigFileParams {
modifyBy?: string
encrypted: boolean
encryptAlgo: string
supported_client: string
persistent: {
encoding: string
path: string
postCmd: string
}
}
export interface ModifyConfigFileResult {
configFile: ConfigFile
Expand Down Expand Up @@ -242,7 +254,6 @@ export interface DescribeConfigFileReleaseParams {
name: string
}


/**
* **DescribeConfigFileRelease出参**
*
Expand Down
6 changes: 6 additions & 0 deletions web/src/polaris/configuration/fileGroup/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export interface ConfigFile {
releaseBy: string
encrypted: boolean
encryptAlgo: string
supported_client: string
persistent: {
encoding: string
path: string
postCmd: string
}
}
export interface KeyValuePair {
key: string
Expand Down
2 changes: 1 addition & 1 deletion web/src/polaris/namespace/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function ServicePage(props: DuckCmpProps<NamespaceDuck>) {
const columns = getColumns(props)
const handlers = getHandlers(props)
const multiRegConfig = useServerConfig('multiregistries')
const multiRegConfigEnabled = multiRegConfig.open
const multiRegConfigEnabled = multiRegConfig?.open
const { sync_to_global_registry } = selector(store)
return (
<BasicLayout title={'命名空间'} store={store} selectors={duck.selectors} header={<></>}>
Expand Down
Loading

0 comments on commit 6e14323

Please sign in to comment.