dockerController.js 10.3 KB
const { sequelize, Sequelize, User, Dockerfile, Image, Container, Port} = require('../models')
const { sendResponse, sendError } = require('../utils/response')
const { currentUser } = require('../utils/auth')
const { logging } = require('../utils/log')
const { checkRequiredExist, setValues } = require('../utils/validation')
const fs = require('fs')
const randomstring = require("randomstring");

const Op = Sequelize.Op

exports.addDockerfile = async (req, res) => {
    const id = req.decoded.id
    if (!id) {
        return sendError(res, 401, 'InvalidToken')
    }
    const requiredKey = ['content']
    const required = checkRequiredExist(req.body, requiredKey)
    if (required) {
        logging('dockerfile', 'error', { code: 400, message: `missingKey:${required}` }, req)
        return sendError(res, 400, `missingKey:${required}`)
    }
    try {
        let dockerfileInfo = {}
        dockerfileInfo.userId = id
        dockerfileInfo.content = req.body.content
        let dockerfileDir = randomstring.generate(16);
        fs.mkdirSync('./dockerfiles/'+dockerfileDir)
        dockerfileInfo.filepath = './dockerfiles/'+dockerfileDir+'/Dockerfile'
        fs.writeFile(dockerfileInfo.filepath, dockerfileInfo.content, function (err) {
            if (err) {
                logging('dockerfile', 'error', { code: 400, message: `dockerfile write error` }, req)
                return sendError(res, 400, `dockerfile write error`)
            }
        })
        let user = await Dockerfile.create(dockerfileInfo)
        logging('dockerfile', 'add dockerfile', user, req)
        return sendResponse(res, user, 200)
    } catch (error) {
        logging('dockerfile', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }

}

exports.listDockerfile = async (req, res) => {
    const id = req.decoded.id
    if (!id) {
        return sendError(res, 401, 'InvalidToken')
    }
    try{
        let dockerfiles = await Dockerfile.findAndCountAll({
            where: {
                userId: id
            },
            order: [
                ['createdAt', 'desc']
            ]
        })

        const result = {
            count: dockerfiles.count,
            data: dockerfiles.rows
        }
        return sendResponse(res, result, 200)
    } catch(error) {
        logging('dockerfile', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }
}

exports.removeDockerfile = async (req, res) => {
    const requiredKey = ['id']
    const required = checkRequiredExist(req.body, requiredKey)
    if (required) {
        logging('dockerfile', 'error', { code: 400, message: 'missingKey:${required}' }, req)
        return sendError(res, 400, `missingKey:${required}`)
    }
    try {
        const user = await currentUser(req.headers.authorization)

        const dockerfileId = req.body.id
        let dockerfile = await Dockerfile.findByPk(dockerfileId)
        if (!dockerfile) {
            logging('dockerfile', 'error', { code: 404, message: 'NoDockerfileFound' }, req)
            return sendError(res, 404, 'NoDockerfileFound')
        }
        if (!user || user.id !== dockerfile.userId) {
            logging('dockerfile', 'error', { code: 403, message: 'Unauthorized' }, req)
            return sendError(res, 403, 'Unauthorized')
        }

        await Dockerfile.destroy({
            where: {
                id: dockerfile.id
            }
        })
        logging('dockerfile', 'delete', null, req)
        return sendResponse(res, true, 201)
    } catch (error) {
        logging('dockerfile', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }
}

exports.listImage = async (req, res) => {
    const id = req.decoded.id
    if (!id) {
        return sendError(res, 401, 'InvalidToken')
    }
    try{
        let dockerfiles = await Dockerfile.findAndCountAll({
            where: {
                userId: id
            },
            order: [
                ['createdAt', 'desc']
            ]
        })

        const result = {
            count: dockerfiles.count,
            data: dockerfiles.rows
        }
        return sendResponse(res, result, 200)
    } catch(error) {
        logging('image', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }
}
exports.buildImage = async (req, res) => {

}
exports.removeImage = async (req, res) => {
    const requiredKey = ['id']
    const required = checkRequiredExist(req.body, requiredKey)
    if (required) {
        logging('image', 'error', { code: 400, message: 'missingKey:${required}' }, req)
        return sendError(res, 400, `missingKey:${required}`)
    }
    try {
        const user = await currentUser(req.headers.authorization)

        const imageId = req.body.id
        let image = await Image.findByPk(imageId)
        if (!image) {
            logging('image', 'error', { code: 404, message: 'NoDockerfileFound' }, req)
            return sendError(res, 404, 'NoDockerfileFound')
        }
        let dockerfile = await Dockerfile.findByPk(image.dockerfileId)
        if (!user || user.id !== dockerfile.userId) {
            logging('image', 'error', { code: 403, message: 'Unauthorized' }, req)
            return sendError(res, 403, 'Unauthorized')
        }

        await Image.destroy({
            where: {
                id: image.id
            }
        })
        logging('image', 'delete', null, req)
        return sendResponse(res, true, 201)
    } catch (error) {
        logging('image', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }
}

exports.listContainer = async (req, res) => {

}
exports.createContainer = async (req, res) => {

}
exports.startContainer = async (req, res) => {

}
exports.stopContainer = async (req, res) => {

}
exports.removeContainer = async (req, res) => {

}

//admin
exports.adminListDockerfile = async (req, res) => {
    try{
        let dockerfiles = await Dockerfile.findAndCountAll({
            order: [
                ['createdAt', 'desc']
            ]
        })

        const result = {
            count: dockerfiles.count,
            data: dockerfiles.rows
        }
        return sendResponse(res, result, 200)
    } catch(error) {
        logging('dockerfile', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }
}
exports.adminDeleteDockerfile = async (req, res) => {
    const requiredKey = ['id']
    const required = checkRequiredExist(req.body, requiredKey)
    if (required) {
        logging('dockerfile', 'error', { code: 400, message: 'missingKey:${required}' }, req)
        return sendError(res, 400, `missingKey:${required}`)
    }
    try {
        const dockerfileId = req.body.id
        let dockerfile = await Dockerfile.findByPk(dockerfileId)
        if (!dockerfile) {
            logging('dockerfile', 'error', { code: 404, message: 'NoDockerfileFound' }, req)
            return sendError(res, 404, 'NoDockerfileFound')
        }

        await Dockerfile.destroy({
            where: {
                id: dockerfile.id
            }
        })
        logging('dockerfile', 'delete', null, req)
        return sendResponse(res, true, 201)
    } catch (error) {
        logging('dockerfile', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }
}

exports.adminListImage = async (req, res) => {
    try{
        let images = await Image.findAndCountAll({
            order: [
                ['createdAt', 'desc']
            ]
        })

        const result = {
            count: images.count,
            data: images.rows
        }
        return sendResponse(res, result, 200)
    } catch(error) {
        logging('image', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }
}
exports.adminDeleteImage = async (req, res) => {
    const requiredKey = ['id']
    const required = checkRequiredExist(req.body, requiredKey)
    if (required) {
        logging('image', 'error', { code: 400, message: 'missingKey:${required}' }, req)
        return sendError(res, 400, `missingKey:${required}`)
    }
    try {
        const imageId = req.body.id
        let image = await Image.findByPk(imageId)
        if (!image) {
            logging('image', 'error', { code: 404, message: 'NoImageFound' }, req)
            return sendError(res, 404, 'NoImageFound')
        }

        await Image.destroy({
            where: {
                id: image.id
            }
        })
        logging('image', 'delete', null, req)
        return sendResponse(res, true, 201)
    } catch (error) {
        logging('image', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }
}

exports.adminListContainer = async (req, res) => {
    try{
        let containers = await Container.findAndCountAll({
            order: [
                ['createdAt', 'desc']
            ]
        })

        const result = {
            count: containers.count,
            data: containers.rows
        }
        return sendResponse(res, result, 200)
    } catch(error) {
        logging('container', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }
}
exports.adminDeleteContainer = async (req, res) => {
    const requiredKey = ['id']
    const required = checkRequiredExist(req.body, requiredKey)
    if (required) {
        logging('container', 'error', { code: 400, message: 'missingKey:${required}' }, req)
        return sendError(res, 400, `missingKey:${required}`)
    }
    try {
        const containerId = req.body.id
        let container = await Image.findByPk(containerId)
        if (!container) {
            logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req)
            return sendError(res, 404, 'NoContainerFound')
        }

        await Container.destroy({
            where: {
                id: container.id
            }
        })
        logging('container', 'delete', null, req)
        return sendResponse(res, true, 201)
    } catch (error) {
        logging('container', 'error', { code: 500, message: error.message }, req)
        return sendError(res, 500, error.message)
    }
}