dockerController.js 4.61 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}`)
    }
    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)
}

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: 'Unauthoirzed' }, req)
            return sendError(res, 403, 'Unauthoirzed')
        }

        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) => {

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

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

}
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) => {

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

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

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

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

}