Skip to content

Commit

Permalink
feat(app-spec): impl app-spec on app createing.
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Apr 21, 2022
1 parent 20504bd commit 8c05fa6
Show file tree
Hide file tree
Showing 12 changed files with 325 additions and 62 deletions.
18 changes: 16 additions & 2 deletions packages/system-client/src/api/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ export function getMyApplications() {
})
}


/**
* Get avaliable specs
* @returns
*/
export function getSpecs() {
return request({
url: '/sys-api/apps/specs',
method: 'get'
})
}


/**
* 根据 appid 获取应用
* @param {string} appid
Expand All @@ -32,12 +45,13 @@ export async function getApplicationByAppid(appid) {
* @param param0
* @returns
*/
export async function createApplication({ name }) {
export async function createApplication({ name, spec }) {
const res = await request({
url: `/sys-api/apps/create`,
method: 'post',
data: {
name
name,
spec
}
})
return res
Expand Down
26 changes: 22 additions & 4 deletions packages/system-client/src/views/application/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@
<el-form-item label="应用名称" prop="name">
<el-input v-model="form.name" placeholder="应用名称" />
</el-form-item>
<el-form-item label="选择规格" prop="spec">
<el-radio-group v-model="form.spec">
<el-radio :label="spec.name" border v-for="spec in specs" :key="spec.name">
<div class="spec-card" style="display:inline-block;">
{{spec.label}}
[ RAM/{{spec.limit_memory / 1024 / 1024}}M,
OSS/{{spec.storage_capacity / 1024 /1024/ 1024 }}G,
DB/{{spec.database_capacity / 1024 /1024/ 1024}}G ]
</div>
</el-radio>
</el-radio-group>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">
Expand Down Expand Up @@ -227,7 +239,7 @@
</template>

<script>
import { createApplication, getMyApplications, startApplicationService, stopApplicationService, updateApplication, removeApplication, exportApplication, importApplication, openAppConsole } from '@/api/application'
import { createApplication, getMyApplications, startApplicationService, stopApplicationService, updateApplication, removeApplication, exportApplication, importApplication, openAppConsole, getSpecs } from '@/api/application'
import { showError, showInfo, showSuccess } from '@/utils/show'
import { exportRawBlob } from '@/utils/file'
import { parseTime } from '@/utils'
Expand All @@ -237,12 +249,14 @@ function getDefaultFormValue() {
return {
_id: undefined,
appid: undefined,
name: ''
name: '',
spec: ''
}
}
const formRules = {
name: [{ required: true, message: '应用名不可为空', trigger: 'blur' }]
name: [{ required: true, message: '应用名不可为空', trigger: 'blur' }],
spec: [{ required: true, message: '请选择应用规格', trigger: 'blur' }]
}
const importFormRules = {
Expand All @@ -259,6 +273,7 @@ export default {
created: [],
joined: []
},
specs: [],
loading: false,
form: getDefaultFormValue(),
dialogFormVisible: false,
Expand Down Expand Up @@ -288,6 +303,9 @@ export default {
const { created, joined } = res.data
this.applications.created = created
this.applications.joined = joined
const specs = await getSpecs()
this.specs = specs.data
},
toDetail(app) {
if (app.status !== 'running') {
Expand All @@ -311,7 +329,7 @@ export default {
const data = Object.assign({}, this.form)
// 执行创建请求
const res = await createApplication({ name: data.name })
const res = await createApplication({ name: data.name, spec: data.spec })
if (!res.data?.appid) {
this.$notify({
type: 'error',
Expand Down
5 changes: 4 additions & 1 deletion packages/system-server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export const Constants = {
applications: coll_prefix + 'applications',
recycles: coll_prefix + 'recycles',
app_templates: coll_prefix + 'app_templates',
specs: coll_prefix + 'specs',
app_specs: coll_prefix + 'app_specs'
},

/**
Expand All @@ -73,4 +75,5 @@ deepFreeze(Constants)

export const KB = 1024
export const MB = 1024 * KB
export const GB = 1024 * MB
export const GB = 1024 * MB
export const DATE_NEVER = new Date('2099/12/31')
27 changes: 19 additions & 8 deletions packages/system-server/src/handler/application/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { getAccountById } from '../../support/account'
import { ApplicationStruct, createApplicationDb, generateAppid, getApplicationByAppid, getMyApplications } from '../../support/application'
import { MinioAgent } from '../../support/oss'
import Config from '../../config'
import { Constants } from '../../constants'
import { Constants, DATE_NEVER } from '../../constants'
import { DatabaseAgent } from '../../db'
import { logger } from '../../logger'
import { generatePassword } from '../../support/util-passwd'
import { ApplicationSpec } from '../../support/application-spec'

/**
* The handler of creating application
Expand All @@ -39,6 +40,16 @@ export async function handleCreateApplication(req: Request, res: Response) {
})
}

// check the spec
const spec_name = req.body?.spec
const spec = await ApplicationSpec.getSpec(spec_name)
if (!spec || !spec?.enabled) {
return res.send({
code: 'INVALID_SPEC_NAME',
error: 'spec name is not avaliable'
})
}

// build the application config
const app_name = req.body?.name ?? 'default'
const appid = generateAppid()
Expand All @@ -62,20 +73,20 @@ export async function handleCreateApplication(req: Request, res: Response) {
oss_access_secret: generatePassword(64, true, false)
},
runtime: {
image: Config.APP_SERVICE_IMAGE,
resources: {
req_cpu: Config.APP_DEFAULT_RESOURCES.req_cpu,
req_memory: Config.APP_DEFAULT_RESOURCES.req_memory,
limit_cpu: Config.APP_DEFAULT_RESOURCES.limit_cpu,
limit_memory: Config.APP_DEFAULT_RESOURCES.limit_memory,
}
image: Config.APP_SERVICE_IMAGE
},
buckets: [],
packages: [],
created_at: now,
updated_at: now
}

// assign spec to app
const assigned = await ApplicationSpec.assign(appid, spec.name, new Date(), DATE_NEVER)
if (!assigned.insertedId) {
return res.status(400).send('Error: assign spec to app failed')
}

// create oss user
const oss = await MinioAgent.New()
if (false === await oss.createUser(data.appid, data.config.oss_access_secret)) {
Expand Down
26 changes: 26 additions & 0 deletions packages/system-server/src/handler/application/get-specs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 15:22:34
* @LastEditTime: 2021-12-27 11:39:53
* @Description:
*/

import { Request, Response } from 'express'
import { ApplicationSpec } from '../../support/application-spec'


/**
* The handler of getting my applications(created & joined)
*/
export async function handleGetSpecs(req: Request, res: Response) {
const uid = req['auth']?.uid
if (!uid)
return res.status(401).send()

const specs = await ApplicationSpec.listSpecs()
const data = specs.filter(item => item.enabled)

return res.send({
data: data
})
}
6 changes: 6 additions & 0 deletions packages/system-server/src/handler/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { handleGetCollaborators, handleGetRoles, handleInviteCollaborator, handl
import { handleCreateApplication } from './create'
import { handleExportApplication } from './exporter'
import { handleGetApplicationByAppid, handleMyApplications } from './get'
import { handleGetSpecs } from './get-specs'
import { handleImportApplication, handleInitApplicationWithTemplate } from './importer'
import { handleAddPackage, handleGetPackages, handleRemovePackage, handleUpdatePackage } from './packages'
import { handleRemoveApplication } from './remove'
Expand All @@ -38,6 +39,11 @@ export const ApplicationRouter = Router()
*/
ApplicationRouter.get('/my', handleMyApplications)

/**
* Get avaliable specs
*/
ApplicationRouter.get('/specs', handleGetSpecs)

/**
* Get application by id
*/
Expand Down
8 changes: 6 additions & 2 deletions packages/system-server/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ async function main() {
}

// create app user policy
await InitializerApi.initAppUserPolicy()
await InitializerApi.initAppOSSUserPolicy()
logger.info('init app user policy')


if (await InitializerApi.createBuiltinSpecs()) {
logger.info('create builtin specs')
}

// create system extension server app
const app = await getApplicationByAppid(SYSTEM_EXTENSION_APPID)
if (!app) {
await InitializerApi.createSystemExtensionApp(account_id, SYSTEM_EXTENSION_APPID)
logger.info('create system extension server app')

}

// run system extension server app
Expand Down
Loading

0 comments on commit 8c05fa6

Please sign in to comment.