2021-12-10 12:33:47 +01:00
|
|
|
import { LoggerInstance } from '@oceanprotocol/lib'
|
2021-10-28 11:38:40 +02:00
|
|
|
import axios from 'axios'
|
2022-09-28 14:47:43 +02:00
|
|
|
import isUrl from 'is-url-superb'
|
2021-10-28 11:38:40 +02:00
|
|
|
import { toast } from 'react-toastify'
|
|
|
|
|
2022-09-28 14:47:43 +02:00
|
|
|
export interface dockerContainerInfo {
|
|
|
|
exists: boolean
|
|
|
|
checksum: string
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getDockerHubImageChecksum(
|
2021-10-28 11:38:40 +02:00
|
|
|
image: string,
|
|
|
|
tag: string
|
2022-09-28 14:47:43 +02:00
|
|
|
): Promise<dockerContainerInfo> {
|
|
|
|
const containerInfo: dockerContainerInfo = {
|
|
|
|
exists: false,
|
|
|
|
checksum: null
|
|
|
|
}
|
2021-10-28 11:38:40 +02:00
|
|
|
try {
|
|
|
|
const response = await axios.post(
|
|
|
|
`https://dockerhub-proxy.oceanprotocol.com`,
|
2022-09-20 10:30:11 +02:00
|
|
|
{
|
|
|
|
image,
|
|
|
|
tag
|
|
|
|
}
|
2021-10-28 11:38:40 +02:00
|
|
|
)
|
|
|
|
if (
|
|
|
|
!response ||
|
|
|
|
response.status !== 200 ||
|
|
|
|
response.data.status !== 'success'
|
|
|
|
) {
|
|
|
|
toast.error(
|
2022-09-28 14:47:43 +02:00
|
|
|
'Could not fetch docker hub image info. Please check container image and tag and try again'
|
|
|
|
)
|
|
|
|
return containerInfo
|
|
|
|
}
|
|
|
|
containerInfo.exists = true
|
|
|
|
containerInfo.checksum = response.data.result.checksum
|
|
|
|
return containerInfo
|
|
|
|
} catch (error) {
|
|
|
|
LoggerInstance.error(error.message)
|
|
|
|
toast.error(
|
|
|
|
'Could not fetch docker hub image info. Please check container image and tag and try again'
|
|
|
|
)
|
|
|
|
return containerInfo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function validateContainerImageUrl(
|
|
|
|
imageURL: string
|
|
|
|
): Promise<dockerContainerInfo> {
|
|
|
|
const containerInfo: dockerContainerInfo = {
|
|
|
|
exists: false,
|
|
|
|
checksum: null
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const response = await axios.head(imageURL)
|
|
|
|
if (!response || response.status !== 200) {
|
|
|
|
toast.error(
|
|
|
|
'Could not fetch docker image info. Please check URL and try again'
|
2021-10-28 11:38:40 +02:00
|
|
|
)
|
2022-09-28 14:47:43 +02:00
|
|
|
return containerInfo
|
2021-10-28 11:38:40 +02:00
|
|
|
}
|
2022-09-28 14:47:43 +02:00
|
|
|
containerInfo.exists = true
|
|
|
|
return containerInfo
|
2021-10-28 11:38:40 +02:00
|
|
|
} catch (error) {
|
2021-12-10 12:33:47 +01:00
|
|
|
LoggerInstance.error(error.message)
|
2021-10-28 11:38:40 +02:00
|
|
|
toast.error(
|
2022-09-28 14:47:43 +02:00
|
|
|
'Could not fetch docker image info. Please check URL and try again'
|
2021-10-28 11:38:40 +02:00
|
|
|
)
|
2022-09-28 14:47:43 +02:00
|
|
|
return containerInfo
|
2021-10-28 11:38:40 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-28 14:47:43 +02:00
|
|
|
|
|
|
|
export async function getContainerChecksum(
|
|
|
|
dockerImage: string,
|
|
|
|
tag: string
|
|
|
|
): Promise<dockerContainerInfo> {
|
|
|
|
const isValid = isUrl(dockerImage)
|
|
|
|
? await validateContainerImageUrl(dockerImage)
|
|
|
|
: await getDockerHubImageChecksum(dockerImage, tag)
|
|
|
|
return isValid
|
|
|
|
}
|