정민우

Merge branch 'backend-docker' into 'master'

[New] Dockerfile add, list, delete 추가



See merge request !6
let express = require('express')
let cookieParser = require('cookie-parser')
var cors = require('cors')
var corsConfig = require('./config/cors')
let cors = require('cors')
let corsConfig = require('./config/cors')
const indexRouter = require('./routes/index')
const authRouter = require('./routes/authRouter')
const userRouter = require('./routes/userRouter')
const dockerRouter = require('./routes/dockerRouter')
let app = express()
app.use(cors(corsConfig))
......@@ -17,6 +18,7 @@ app.use(cookieParser())
app.use('/', indexRouter)
app.use('/', authRouter)
app.use('/', userRouter)
app.use('/', dockerRouter)
app.use(function(req, res) {
res.status(400)
......
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) => {
}
FROM ubuntu:20.04
RUN apt-get update -y
RUN apt-get install -y openssh-server vim
# change root password
RUN echo 'root:admin123' | chpasswd
# change ssh configure
RUN echo PermitRootLogin yes >> /etc/ssh/sshd_config && service ssh restart
RUN service ssh restart
CMD tail -f /dev/null
\ No newline at end of file
FROM ubuntu:20.04
RUN apt-get update -y
RUN apt-get install -y openssh-server vim
# change root password
RUN echo 'root:admin123' | chpasswd
# change ssh configure
RUN echo PermitRootLogin yes >> /etc/ssh/sshd_config && service ssh restart
RUN service ssh restart
CMD tail -f /dev/null
\ No newline at end of file
FROM ubuntu:20.04
RUN apt-get update -y
RUN apt-get install -y openssh-server vim
# change root password
RUN echo 'root:admin123' | chpasswd
# change ssh configure
RUN echo PermitRootLogin yes >> /etc/ssh/sshd_config && service ssh restart
RUN service ssh restart
CMD tail -f /dev/null
This diff is collapsed. Click to expand it.
......@@ -9,10 +9,13 @@
"bcrypt": "^5.0.1",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"crypto-random-string": "^4.0.0",
"debug": "~2.6.9",
"dockerode": "^3.3.0",
"dotenv": "^8.2.0",
"express": "~4.16.1",
"mysql2": "^2.1.0",
"randomstring": "^1.2.1",
"sequelize": "^6.3.5"
},
"devDependencies": {
......
const express = require ('express');
const router = express.Router();
const dockerController = require ('../controllers/dockerController')
const { guestOnly, memberOnly, adminOnly } = require ('../middlewares/auth')
router.post('/dockerfile/add', memberOnly, dockerController.addDockerfile)
router.post('/dockerfile/list', memberOnly, dockerController.listDockerfile)
router.post('/dockerfile/remove', memberOnly, dockerController.removeDockerfile)
router.post('/image/list', memberOnly, dockerController.listImage)
router.post('/image/build', memberOnly, dockerController.buildImage)
router.post('/image/remove', memberOnly, dockerController.removeImage)
router.post('/container/list', memberOnly, dockerController.listContainer)
router.post('/container/create', memberOnly, dockerController.createContainer)
router.post('/container/start', memberOnly, dockerController.startContainer)
router.post('/container/stop', memberOnly, dockerController.stopContainer)
router.post('/container/remove', memberOnly, dockerController.removeContainer)
router.post('/admin/dockerfile/list', adminOnly, dockerController.adminListDockerfile)
router.post('/admin/dockerfile/remove', adminOnly, dockerController.adminDeleteDockerfile)
router.post('/admin/image/list', adminOnly, dockerController.adminListImage)
router.post('/admin/image/remove', adminOnly, dockerController.adminDeleteImage)
router.post('/admin/container/list', adminOnly, dockerController.adminListContainer)
router.post('/admin/container/remove', adminOnly, dockerController.adminDeleteContainer)
module.exports = router
var Docker = require('dockerode')
var docker = new Docker({socketPath: '/var/run/docker.sock'})
docker.buildImage({
context: '../dockerfiles/sk2jsif92jajsdi1',
src: ['Dockerfile']
}, {t: 'testimage1337'}, function (err, response) {
console.log(response)
})
const jwt = require('jsonwebtoken')
const config = require(__dirname + '/../config/config')
const { User } = require('../models')
exports.currentUser = async (token) => {
try{
if (!token) {
return false
}
let decoded = jwt.verify(token, config.JWT_KEY)
if (!decoded) {
return false
}
let userId = decoded.id
let user = await User.findByPk(userId)
if (!user) {
return false
}
return user
} catch (error) {
throw error
}
}