mirror of
https://github.com/OpenNebula/one.git
synced 2026-12-06 15:13:39 +09:00
B OpenNebula/one#7252 remove tmp files after image creation (#3826)
Co-authored-by: Tino Vázquez <cvazquez@opennebula.io>
This commit is contained in:
1 parent
2e31cb04c1
commit
a5e90e74ee
5 files changed
+104
-5
No files matched your search
@@ -48,6 +48,8 @@ export function CreateImage() {
|
||||
const [upload] = ImageAPI.useUploadImageMutation()
|
||||
const { enqueueSuccess, enqueueError, uploadSnackbar } = useGeneralApi()
|
||||
const { adminGroup, oneConfig } = useSystemData()
|
||||
const [cleanup] = ImageAPI.useCleanupImageMutation()
|
||||
const uploadedPath = []
|
||||
DatastoreAPI.useGetDatastoresQuery(undefined, {
|
||||
refetchOnMountOrArgChange: false,
|
||||
})
|
||||
@@ -69,6 +71,7 @@ export function CreateImage() {
|
||||
}).unwrap()
|
||||
|
||||
template.PATH = fileUploaded[0]
|
||||
uploadedPath.push(fileUploaded[0])
|
||||
} catch {}
|
||||
}
|
||||
|
||||
@@ -83,7 +86,13 @@ export function CreateImage() {
|
||||
history.push(PATH.STORAGE.IMAGES.LIST)
|
||||
enqueueSuccess(T.SuccessImageCreated, newTemplateId)
|
||||
}
|
||||
} catch {}
|
||||
} catch {} finally {
|
||||
if (uploadedPath[0]) {
|
||||
try {
|
||||
await cleanup({ path: uploadedPath[0] }).unwrap()
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -257,6 +257,29 @@ const imageApi = oneApi.injectEndpoints({
|
||||
}
|
||||
},
|
||||
}),
|
||||
cleanupImage: builder.mutation({
|
||||
/**
|
||||
* Cleanup image.
|
||||
*
|
||||
* @param {object} params - Request params
|
||||
* @param {string} params.path - The path of the image to be cleaned up
|
||||
* @returns {number} Image id
|
||||
* @throws Fails when response isn't code 200
|
||||
*/
|
||||
queryFn: async ({ path }) => {
|
||||
try {
|
||||
const response = await http.request({
|
||||
url: '/api/image/cleanup',
|
||||
method: 'POST',
|
||||
data: { path },
|
||||
})
|
||||
return { data: response.data }
|
||||
} catch (axiosError) {
|
||||
const { response } = axiosError
|
||||
return { error: { status: response?.status, data: response?.data } }
|
||||
}
|
||||
}
|
||||
}),
|
||||
cloneImage: builder.mutation({
|
||||
/**
|
||||
* Clones an existing image.
|
||||
@@ -616,6 +639,7 @@ const imageQueries = (({
|
||||
useUnlockImageMutation,
|
||||
useUploadImageMutation,
|
||||
useRestoreBackupMutation,
|
||||
useCleanupImageMutation,
|
||||
}) => ({
|
||||
// Queries
|
||||
useGetImageQuery,
|
||||
@@ -646,6 +670,7 @@ const imageQueries = (({
|
||||
useUnlockImageMutation,
|
||||
useUploadImageMutation,
|
||||
useRestoreBackupMutation,
|
||||
useCleanupImageMutation,
|
||||
}))(imageApi)
|
||||
|
||||
export default imageQueries
|
||||
@@ -17,9 +17,12 @@ const { defaults, httpCodes } = require('server/utils/constants')
|
||||
|
||||
const { httpResponse } = require('server/utils/server')
|
||||
|
||||
const { getSunstoneConfig } = require('server/utils/yml')
|
||||
const path = require('path')
|
||||
|
||||
const { defaultEmptyFunction } = defaults
|
||||
|
||||
const { ok, badRequest } = httpCodes
|
||||
const { ok, badRequest, internalServerError } = httpCodes
|
||||
|
||||
const httpBadRequest = httpResponse(badRequest, '', '')
|
||||
|
||||
@@ -53,7 +56,52 @@ const upload = (
|
||||
next()
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup temporary file.
|
||||
*
|
||||
* @param {object} res - response http
|
||||
* @param {Function} next - express stepper
|
||||
* @param {string} params - data response http
|
||||
* @param {object} userData - user of http request
|
||||
*/
|
||||
const cleanup = (
|
||||
res = {},
|
||||
next = defaultEmptyFunction,
|
||||
params = {},
|
||||
userData = {}
|
||||
) => {
|
||||
const { path: filePath } = params
|
||||
const { user, password } = userData
|
||||
|
||||
if (!(filePath && user && password)) {
|
||||
res.locals.httpCode = httpBadRequest
|
||||
next()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const appConfig = getSunstoneConfig()
|
||||
const tmpdir = appConfig.tmpdir || defaults.defaultTmpPath
|
||||
const resolvedTmp = path.resolve(tmpdir) + path.sep
|
||||
const resolvedFile = path.resolve(filePath)
|
||||
|
||||
if (!resolvedFile.startsWith(resolvedTmp)) {
|
||||
res.locals.httpCode = httpResponse(internalServerError, '', 'Invalid file path')
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
const { removeFile } = require('server/utils/server')
|
||||
const removed = removeFile(resolvedFile)
|
||||
res.locals.httpCode = httpResponse(removed ? ok : internalServerError, '', '')
|
||||
} catch (e) {
|
||||
res.locals.httpCode = httpResponse(internalServerError, '', e?.message || '')
|
||||
}
|
||||
next()
|
||||
}
|
||||
const functionRoutes = {
|
||||
upload,
|
||||
cleanup,
|
||||
}
|
||||
module.exports = functionRoutes
|
||||
@@ -15,13 +15,17 @@
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
const { Actions, Commands } = require('server/routes/api/image/routes')
|
||||
const { upload } = require('server/routes/api/image/functions')
|
||||
const { upload, cleanup } = require('server/routes/api/image/functions')
|
||||
|
||||
const { IMAGE_UPLOAD } = Actions
|
||||
const { IMAGE_UPLOAD, IMAGE_CLEANUP } = Actions
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
...Commands[IMAGE_UPLOAD],
|
||||
action: upload,
|
||||
},
|
||||
{
|
||||
...Commands[IMAGE_CLEANUP],
|
||||
action: cleanup,
|
||||
},
|
||||
]
|
||||
@@ -14,15 +14,18 @@
|
||||
* limitations under the License. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
const { httpMethod } = require('../../../utils/constants/defaults')
|
||||
const { httpMethod, from } = require('../../../utils/constants/defaults')
|
||||
|
||||
const { POST } = httpMethod
|
||||
const { postBody } = from
|
||||
|
||||
const basepath = '/image'
|
||||
const IMAGE_UPLOAD = 'image.upload'
|
||||
const IMAGE_CLEANUP = 'image.cleanup'
|
||||
|
||||
const Actions = {
|
||||
IMAGE_UPLOAD,
|
||||
IMAGE_CLEANUP,
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
@@ -38,5 +41,15 @@ module.exports = {
|
||||
},
|
||||
},
|
||||
},
|
||||
[IMAGE_CLEANUP]: {
|
||||
path: `${basepath}/cleanup`,
|
||||
httpMethod: POST,
|
||||
auth: true,
|
||||
params: {
|
||||
path: {
|
||||
from: postBody,
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
Reference in new issue
Block a user