Skip to content

Commit

Permalink
feat: versionName template
Browse files Browse the repository at this point in the history
  • Loading branch information
robot9706 committed Dec 14, 2024
1 parent f58c601 commit 66ed46d
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 35 deletions.
1 change: 1 addition & 0 deletions web/crux-ui/locales/en/container.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"json": "JSON",

"sensitiveKey": "Sensitive config data should be stored as secrets",
"templateTips": "Available templates in environments and storage: {{versionName}}",

"base": {
"all": "All",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,16 @@ const ContainerConfigPage = (props: ContainerConfigPageProps) => {
</PageHeading>

<DyoCard className="p-4">
<div className="flex mb-4 justify-between items-start">
<div className="flex mb-1 justify-between items-start">
<DyoHeading element="h4" className="text-xl text-bright">
{getName()}
</DyoHeading>

{getViewStateButtons()}
</div>

<DyoMessage className="text-xs mt-2 mb-4" message={t('templateTips')} messageType="info" />

{viewState === 'editor' && <ContainerConfigFilters onChange={setFilters} filters={filters} />}
</DyoCard>

Expand Down
26 changes: 26 additions & 0 deletions web/crux/src/app/deploy/deploy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
instanceConfigOf,
mergePrefixNeighborSecrets,
} from 'src/domain/start-deployment'
import { applyStringTemplate, applyUniqueKeyValueTemplate } from 'src/domain/template'
import { DeploymentTokenPayload, tokenSignOptionsFor } from 'src/domain/token'
import { collectChildVersionIds, collectParentVersionIds, toPrismaJson } from 'src/domain/utils'
import { copyDeployment } from 'src/domain/version-increase'
Expand Down Expand Up @@ -70,6 +71,10 @@ import {
WS_TYPE_INSTANCE_DELETED,
} from './deploy.message'

type InstanceConfigTemplate = {
versionName?: string
}

@Injectable()
export default class DeployService {
private readonly logger = new Logger(DeployService.name)
Expand Down Expand Up @@ -512,6 +517,10 @@ export default class DeployService {
})
}

const templateParams: InstanceConfigTemplate = {
versionName: deployment.version.name,
}

const invalidSecrets: InvalidSecrets[] = []

// deployment config
Expand All @@ -531,6 +540,8 @@ export default class DeployService {
deployment.instances.map(instance => {
const instanceConfig = instanceConfigOf(deployment, deploymentConfig, instance)

this.applyInstanceConfigTemplate(instanceConfig, templateParams)

return [instance.id, instanceConfig]
}),
)
Expand Down Expand Up @@ -1245,4 +1256,19 @@ export default class DeployService {

return versions.flatMap(it => it.deployments)
}

private applyInstanceConfigTemplate(config: ConcreteContainerConfigData, template: InstanceConfigTemplate) {
if (config.environment) {
applyUniqueKeyValueTemplate(config.environment, template)
}

if (config.storageSet && config.storageConfig) {
config.storageConfig.bucket = config.storageConfig.bucket
? applyStringTemplate(config.storageConfig.bucket, template)
: null
config.storageConfig.path = config.storageConfig.path
? applyStringTemplate(config.storageConfig.path, template)
: null
}
}
}
4 changes: 2 additions & 2 deletions web/crux/src/app/pipeline/pipeline.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import {
PipelineHookOptions,
PipelineRunStatusEvent,
PipelineRunWithPipline,
applyPipelineInputTemplate,
mergeEventWatcherInputs,
registryV2EventToTemplates,
} from 'src/domain/pipeline'
import { REGISTRY_EVENT_V2_PULL, REGISTRY_EVENT_V2_PUSH, RegistryV2Event } from 'src/domain/registry'
import { applyUniqueKeyValueTemplate } from 'src/domain/template'
import { JwtTokenPayload } from 'src/domain/token'
import { generateNonce } from 'src/domain/utils'
import { CruxBadRequestException } from 'src/exception/crux-exception'
Expand Down Expand Up @@ -399,7 +399,7 @@ export default class PipelineService {

const inputs = mergeEventWatcherInputs(trigger.inputs, eventWatcherTrigger.inputs)

applyPipelineInputTemplate(inputs, templates)
applyUniqueKeyValueTemplate(inputs, templates)

await this.createRun({
creatorType: 'eventWatcher',
Expand Down
26 changes: 13 additions & 13 deletions web/crux/src/domain/container-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,31 @@ const mergeBoolean = (strong: boolean, weak: boolean): boolean => {

type StorageProperties = Pick<ContainerConfigData, 'storageSet' | 'storageId' | 'storageConfig'>
const mergeStorage = (strong: StorageProperties, weak: StorageProperties): StorageProperties => {
const set = mergeBoolean(strong.storageSet, weak.storageSet)
if (!set) {
// neither of them are set
if (strong.storageSet) {
// strong is set

return {
storageSet: false,
storageId: null,
storageConfig: null,
storageSet: true,
storageId: strong.storageId,
storageConfig: strong.storageConfig,
}
}

if (typeof strong.storageSet === 'boolean') {
// strong is set
if (weak.storageSet) {
// weak is set

return {
storageSet: true,
storageId: strong.storageId,
storageConfig: strong.storageConfig,
storageId: weak.storageId,
storageConfig: weak.storageConfig,
}
}

// neither of them are set
return {
storageSet: true,
storageId: weak.storageId,
storageConfig: weak.storageConfig,
storageSet: false,
storageId: null,
storageConfig: null,
}
}

Expand Down
19 changes: 0 additions & 19 deletions web/crux/src/domain/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,6 @@ export type PipelineCreateRunOptions = {
}
)

const applyTemplate = (template: string, inputValue: string, templateValue: string): string =>
inputValue.replace(new RegExp(`{{\\s*${template}\\s*}}`), templateValue)

const applyTemplatesOnInput = (input: UniqueKeyValue, templates: Record<string, string>) => {
Object.entries(templates).forEach(entry => {
const [template, templateValue] = entry

input.value = applyTemplate(template, input.value, templateValue)
})
}

/**
* @param inputs
* @param templates JSON object of replacable property names with the actual values. Example: { 'imageName': 'alpine', 'imageTag': 'latest', 'label:debug': 'true' }
*/
export const applyPipelineInputTemplate = (inputs: UniqueKeyValue[], templates: Record<string, string>) => {
inputs.forEach(it => applyTemplatesOnInput(it, templates))
}

export const mergeEventWatcherInputs = (
pipelineInputs: UniqueKeyValue[],
eventWatcherInputs: UniqueKeyValue[],
Expand Down
36 changes: 36 additions & 0 deletions web/crux/src/domain/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { UniqueKeyValue } from './container'

const applyTemplate = (template: string, inputValue: string, templateValue: string): string =>
inputValue.replace(new RegExp(`{{\\s*${template}\\s*}}`), templateValue)

const applyTemplatesOnInput = (input: UniqueKeyValue, templates: Record<string, string>) => {
Object.entries(templates).forEach(entry => {
const [template, templateValue] = entry
if (!templateValue) {
return
}

input.value = applyTemplate(template, input.value, templateValue)
})
}

/**
* @param inputs
* @param templates JSON object of replacable property names with the actual values. Example: { 'imageName': 'alpine', 'imageTag': 'latest', 'label:debug': 'true' }
*/
export const applyUniqueKeyValueTemplate = (inputs: UniqueKeyValue[], templates: Record<string, string>) => {
inputs.forEach(it => applyTemplatesOnInput(it, templates))
}

export const applyStringTemplate = (input: string, templates: Record<string, string>): string => {
Object.entries(templates).forEach(entry => {
const [template, templateValue] = entry
if (!templateValue) {
return
}

input = applyTemplate(template, input, templateValue)
})

return input
}

0 comments on commit 66ed46d

Please sign in to comment.