1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

add Docker image presets, populate algo Docker field from them

This commit is contained in:
Matthias Kretschmann 2021-11-29 13:25:30 +00:00
parent 7c98659c86
commit 3aa739fce0
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 79 additions and 50 deletions

View File

@ -47,7 +47,9 @@
"label": "Docker Image",
"help": "Please select an image to run your algorithm.",
"type": "boxSelection",
"options": ["node:latest", "python:latest", "Custom"],
"options": [
"populated from algorithmContainerPresets in Publish/_constants"
],
"required": true
},
{

View File

@ -1,12 +1,14 @@
interface MetadataAlgorithmContainer {
entrypoint: string
image: string
tag: string
checksum: string
}
interface MetadataAlgorithm {
language?: string
version?: string
container: {
entrypoint: string
image: string
tag: string
checksum: string
}
container: MetadataAlgorithmContainer
}
interface Metadata {

View File

@ -8,22 +8,13 @@ import { getFieldContent } from '../_utils'
import IconDataset from '@images/dataset.svg'
import IconAlgorithm from '@images/algorithm.svg'
import styles from './index.module.css'
import { algorithmContainerPresets } from '../_constants'
const assetTypeOptionsTitles = getFieldContent(
'type',
content.metadata.fields
).options
const dockerImageOptionsTitles = getFieldContent(
'dockerImage',
content.metadata.fields
).options
const dockerImageOptions = dockerImageOptionsTitles.map((title) => ({
name: title.toLowerCase(),
title
}))
export default function MetadataFields(): ReactElement {
// connect with Form state, use for conditional field rendering
const { values } = useFormikContext<FormPublishData>()
@ -45,6 +36,17 @@ export default function MetadataFields(): ReactElement {
}
]
// Populate the Docker image field with our presets in _constants,
// transformPublishFormToDdo will do the rest.
const dockerImageOptions: BoxSelectionOption[] =
algorithmContainerPresets.map((preset) => ({
name: `${preset.image}:${preset.tag}`,
title: `${preset.image}:${preset.tag}`,
checked: values.metadata.dockerImage === `${preset.image}:${preset.tag}`
}))
dockerImageOptions.push({ name: 'custom', title: 'Custom', checked: false })
return (
<>
<Field

View File

@ -84,22 +84,17 @@ export const initialValues: FormPublishData = {
}
}
// export const initialValuesAlgo: Partial<MetadataPublishFormAlgorithm> = {
// name: '',
// author: '',
// dataTokenOptions: {
// name: '',
// symbol: ''
// },
// dockerImage: 'node:latest',
// image: 'node',
// containerTag: 'latest',
// entrypoint: 'node $ALGO',
// files: '',
// description: '',
// algorithmPrivacy: false,
// termsAndConditions: false,
// tags: '',
// timeout: 'Forever',
// providerUri: ''
// }
export const algorithmContainerPresets: MetadataAlgorithmContainer[] = [
{
image: 'node',
tag: 'latest',
entrypoint: 'node $ALGO',
checksum: '' // TODO: how to get?
},
{
image: 'python',
tag: 'latest',
entrypoint: 'python $ALGO',
checksum: ''
}
]

View File

@ -37,6 +37,7 @@ export interface FormPublishData {
dockerImageCustom?: string
dockerImageCustomTag?: string
dockerImageCustomEntrypoint?: string
dockerImageCustomChecksum?: string
}
services: FormPublishService[]
pricing: PriceOptions

View File

@ -2,6 +2,7 @@ import { mapTimeoutStringToSeconds } from '@utils/ddo'
import { getEncryptedFileUrls } from '@utils/provider'
import { sha256 } from 'js-sha256'
import slugify from 'slugify'
import { algorithmContainerPresets } from './_constants'
import { FormPublishData } from './_types'
export function getFieldContent(
@ -16,6 +17,17 @@ function getUrlFileExtension(fileUrl: string): string {
return splittedFileUrl[splittedFileUrl.length - 1]
}
function getAlgorithmContainerPreset(
dockerImage: string
): MetadataAlgorithmContainer {
if (dockerImage === '') return
const preset = algorithmContainerPresets.find(
(preset) => `${preset.image}:${preset.tag}` === dockerImage
)
return preset
}
function dateToStringNoMS(date: Date): string {
return date.toISOString().replace(/\.[0-9]{3}Z/, 'Z')
}
@ -42,9 +54,11 @@ export async function transformPublishFormToDdo(
tags,
author,
termsAndConditions,
dockerImage,
dockerImageCustom,
dockerImageCustomTag,
dockerImageCustomEntrypoint
dockerImageCustomEntrypoint,
dockerImageCustomChecksum
} = metadata
const { access, files, links, providerUrl, timeout } = services[0]
@ -68,20 +82,33 @@ export async function transformPublishFormToDdo(
additionalInformation: {
termsAndConditions
},
...(type === 'algorithm' && {
// TODO: This needs some set of predefined values for `container`,
// depending on user selection in the form.
algorithm: {
language: files?.length ? getUrlFileExtension(filesTransformed[0]) : '',
version: '0.1',
container: {
entrypoint: dockerImageCustomEntrypoint,
image: dockerImageCustom,
tag: dockerImageCustomTag,
checksum: '' // TODO: how to get? Is it user input?
...(type === 'algorithm' &&
dockerImage !== '' && {
algorithm: {
language: filesTransformed?.length
? getUrlFileExtension(filesTransformed[0])
: '',
version: '0.1',
container: {
entrypoint:
dockerImage === 'custom'
? dockerImageCustomEntrypoint
: getAlgorithmContainerPreset(dockerImage).entrypoint,
image:
dockerImage === 'custom'
? dockerImageCustom
: getAlgorithmContainerPreset(dockerImage).image,
tag:
dockerImage === 'custom'
? dockerImageCustomTag
: getAlgorithmContainerPreset(dockerImage).tag,
checksum:
dockerImage === 'custom'
? dockerImageCustomChecksum
: getAlgorithmContainerPreset(dockerImage).checksum
}
}
}
})
})
}
// Encrypt just created string[] of urls