정민우

[New] Dockerfile add, list, delete 추가

1 let express = require('express') 1 let express = require('express')
2 let cookieParser = require('cookie-parser') 2 let cookieParser = require('cookie-parser')
3 -var cors = require('cors') 3 +let cors = require('cors')
4 -var corsConfig = require('./config/cors') 4 +let corsConfig = require('./config/cors')
5 5
6 const indexRouter = require('./routes/index') 6 const indexRouter = require('./routes/index')
7 const authRouter = require('./routes/authRouter') 7 const authRouter = require('./routes/authRouter')
8 const userRouter = require('./routes/userRouter') 8 const userRouter = require('./routes/userRouter')
9 +const dockerRouter = require('./routes/dockerRouter')
9 10
10 let app = express() 11 let app = express()
11 app.use(cors(corsConfig)) 12 app.use(cors(corsConfig))
...@@ -17,6 +18,7 @@ app.use(cookieParser()) ...@@ -17,6 +18,7 @@ app.use(cookieParser())
17 app.use('/', indexRouter) 18 app.use('/', indexRouter)
18 app.use('/', authRouter) 19 app.use('/', authRouter)
19 app.use('/', userRouter) 20 app.use('/', userRouter)
21 +app.use('/', dockerRouter)
20 22
21 app.use(function(req, res) { 23 app.use(function(req, res) {
22 res.status(400) 24 res.status(400)
......
1 +const { sequelize, Sequelize, User, Dockerfile, Image, Container, Port} = require('../models')
2 +const { sendResponse, sendError } = require('../utils/response')
3 +const { currentUser } = require('../utils/auth')
4 +const { logging } = require('../utils/log')
5 +const { checkRequiredExist, setValues } = require('../utils/validation')
6 +const fs = require('fs')
7 +const randomstring = require("randomstring");
8 +
9 +const Op = Sequelize.Op
10 +
11 +exports.addDockerfile = async (req, res) => {
12 + const id = req.decoded.id
13 + if (!id) {
14 + return sendError(res, 401, 'InvalidToken')
15 + }
16 + const requiredKey = ['content']
17 + const required = checkRequiredExist(req.body, requiredKey)
18 + if (required) {
19 + logging('dockerfile', 'error', { code: 400, message: `missingKey:${required}` }, req)
20 + return sendError(res, 400, `missingKey:${required}`)
21 + }
22 + let dockerfileInfo = {}
23 + dockerfileInfo.userId = id
24 + dockerfileInfo.content = req.body.content
25 + let dockerfileDir = randomstring.generate(16);
26 + fs.mkdirSync('./dockerfiles/'+dockerfileDir)
27 + dockerfileInfo.filepath = './dockerfiles/'+dockerfileDir+'/Dockerfile'
28 + fs.writeFile(dockerfileInfo.filepath, dockerfileInfo.content, function (err) {
29 + if (err) {
30 + logging('dockerfile', 'error', { code: 400, message: `dockerfile write error` }, req)
31 + return sendError(res, 400, `dockerfile write error`)
32 + }
33 + })
34 + let user = await Dockerfile.create(dockerfileInfo)
35 + logging('dockerfile', 'add dockerfile', user, req)
36 + return sendResponse(res, user, 200)
37 +}
38 +
39 +exports.listDockerfile = async (req, res) => {
40 + const id = req.decoded.id
41 + if (!id) {
42 + return sendError(res, 401, 'InvalidToken')
43 + }
44 + try{
45 + let dockerfiles = await Dockerfile.findAndCountAll({
46 + where: {
47 + userId: id
48 + },
49 + order: [
50 + ['createdAt', 'desc']
51 + ]
52 + })
53 +
54 + const result = {
55 + count: dockerfiles.count,
56 + data: dockerfiles.rows
57 + }
58 + return sendResponse(res, result, 200)
59 + } catch(error) {
60 + logging('dockerfile', 'error', { code: 500, message: error.message }, req)
61 + return sendError(res, 500, error.message)
62 + }
63 +}
64 +
65 +exports.removeDockerfile = async (req, res) => {
66 + const requiredKey = ['id']
67 + const required = checkRequiredExist(req.body, requiredKey)
68 + if (required) {
69 + logging('dockerfile', 'error', { code: 400, message: 'missingKey:${required}' }, req)
70 + return sendError(res, 400, `missingKey:${required}`)
71 + }
72 + try {
73 + const user = await currentUser(req.headers.authorization)
74 +
75 + const dockerfileId = req.body.id
76 + let dockerfile = await Dockerfile.findByPk(dockerfileId)
77 + if (!dockerfile) {
78 + logging('dockerfile', 'error', { code: 404, message: 'NoDockerfileFound' }, req)
79 + return sendError(res, 404, 'NoDockerfileFound')
80 + }
81 + if (!user || user.id !== dockerfile.userId) {
82 + logging('dockerfile', 'error', { code: 403, message: 'Unauthoirzed' }, req)
83 + return sendError(res, 403, 'Unauthoirzed')
84 + }
85 +
86 + await Dockerfile.destroy({
87 + where: {
88 + id: dockerfile.id
89 + }
90 + })
91 + logging('dockerfile', 'delete', null, req)
92 + return sendResponse(res, true, 201)
93 + } catch (error) {
94 + logging('dockerfile', 'error', { code: 500, message: error.message }, req)
95 + return sendError(res, 500, error.message)
96 + }
97 +}
98 +exports.listImage = async (req, res) => {
99 +
100 +}
101 +exports.buildImage = async (req, res) => {
102 +
103 +}
104 +exports.removeImage = async (req, res) => {
105 +
106 +}
107 +exports.listContainer = async (req, res) => {
108 +
109 +}
110 +exports.createContainer = async (req, res) => {
111 +
112 +}
113 +exports.startContainer = async (req, res) => {
114 +
115 +}
116 +exports.stopContainer = async (req, res) => {
117 +
118 +}
119 +exports.removeContainer = async (req, res) => {
120 +
121 +}
122 +
123 +//admin
124 +exports.adminListDockerfile = async (req, res) => {
125 + try{
126 + let dockerfiles = await Dockerfile.findAndCountAll({
127 + order: [
128 + ['createdAt', 'desc']
129 + ]
130 + })
131 +
132 + const result = {
133 + count: dockerfiles.count,
134 + data: dockerfiles.rows
135 + }
136 + return sendResponse(res, result, 200)
137 + } catch(error) {
138 + logging('dockerfile', 'error', { code: 500, message: error.message }, req)
139 + return sendError(res, 500, error.message)
140 + }
141 +}
142 +exports.adminDeleteDockerfile = async (req, res) => {
143 +
144 +}
145 +exports.adminListImage = async (req, res) => {
146 +
147 +}
148 +exports.adminDeleteImage = async (req, res) => {
149 +
150 +}
151 +exports.adminListContainer = async (req, res) => {
152 +
153 +}
154 +exports.adminDeleteContainer = async (req, res) => {
155 +
156 +}
1 +FROM ubuntu:20.04
2 +RUN apt-get update -y
3 +RUN apt-get install -y openssh-server vim
4 +# change root password
5 +RUN echo 'root:admin123' | chpasswd
6 +# change ssh configure
7 +RUN echo PermitRootLogin yes >> /etc/ssh/sshd_config && service ssh restart
8 +RUN service ssh restart
9 +CMD tail -f /dev/null
...\ No newline at end of file ...\ No newline at end of file
1 +FROM ubuntu:20.04
2 +RUN apt-get update -y
3 +RUN apt-get install -y openssh-server vim
4 +# change root password
5 +RUN echo 'root:admin123' | chpasswd
6 +# change ssh configure
7 +RUN echo PermitRootLogin yes >> /etc/ssh/sshd_config && service ssh restart
8 +RUN service ssh restart
9 +CMD tail -f /dev/null
...\ No newline at end of file ...\ No newline at end of file
1 +FROM ubuntu:20.04
2 +
3 +RUN apt-get update -y
4 +RUN apt-get install -y openssh-server vim
5 +
6 +# change root password
7 +RUN echo 'root:admin123' | chpasswd
8 +
9 +# change ssh configure
10 +RUN echo PermitRootLogin yes >> /etc/ssh/sshd_config && service ssh restart
11 +RUN service ssh restart
12 +
13 +CMD tail -f /dev/null
This diff is collapsed. Click to expand it.
...@@ -9,10 +9,13 @@ ...@@ -9,10 +9,13 @@
9 "bcrypt": "^5.0.1", 9 "bcrypt": "^5.0.1",
10 "cookie-parser": "~1.4.4", 10 "cookie-parser": "~1.4.4",
11 "cors": "^2.8.5", 11 "cors": "^2.8.5",
12 + "crypto-random-string": "^4.0.0",
12 "debug": "~2.6.9", 13 "debug": "~2.6.9",
14 + "dockerode": "^3.3.0",
13 "dotenv": "^8.2.0", 15 "dotenv": "^8.2.0",
14 "express": "~4.16.1", 16 "express": "~4.16.1",
15 "mysql2": "^2.1.0", 17 "mysql2": "^2.1.0",
18 + "randomstring": "^1.2.1",
16 "sequelize": "^6.3.5" 19 "sequelize": "^6.3.5"
17 }, 20 },
18 "devDependencies": { 21 "devDependencies": {
......
1 +const express = require ('express');
2 +const router = express.Router();
3 +
4 +const dockerController = require ('../controllers/dockerController')
5 +const { guestOnly, memberOnly, adminOnly } = require ('../middlewares/auth')
6 +
7 +router.post('/dockerfile/add', memberOnly, dockerController.addDockerfile)
8 +router.post('/dockerfile/list', memberOnly, dockerController.listDockerfile)
9 +router.post('/dockerfile/remove', memberOnly, dockerController.removeDockerfile)
10 +router.post('/image/list', memberOnly, dockerController.listImage)
11 +router.post('/image/build', memberOnly, dockerController.buildImage)
12 +router.post('/image/remove', memberOnly, dockerController.removeImage)
13 +router.post('/container/list', memberOnly, dockerController.listContainer)
14 +router.post('/container/create', memberOnly, dockerController.createContainer)
15 +router.post('/container/start', memberOnly, dockerController.startContainer)
16 +router.post('/container/stop', memberOnly, dockerController.stopContainer)
17 +router.post('/container/remove', memberOnly, dockerController.removeContainer)
18 +
19 +router.post('/admin/dockerfile/list', adminOnly, dockerController.adminListDockerfile)
20 +router.post('/admin/dockerfile/remove', adminOnly, dockerController.adminDeleteDockerfile)
21 +router.post('/admin/image/list', adminOnly, dockerController.adminListImage)
22 +router.post('/admin/image/remove', adminOnly, dockerController.adminDeleteImage)
23 +router.post('/admin/container/list', adminOnly, dockerController.adminListContainer)
24 +router.post('/admin/container/remove', adminOnly, dockerController.adminDeleteContainer)
25 +
26 +module.exports = router
1 +var Docker = require('dockerode')
2 +var docker = new Docker({socketPath: '/var/run/docker.sock'})
3 +
4 +docker.buildImage({
5 + context: '../dockerfiles/sk2jsif92jajsdi1',
6 + src: ['Dockerfile']
7 + }, {t: 'testimage1337'}, function (err, response) {
8 + console.log(response)
9 +})
1 +const jwt = require('jsonwebtoken')
2 +const config = require(__dirname + '/../config/config')
3 +
4 +const { User } = require('../models')
5 +
6 +exports.currentUser = async (token) => {
7 + try{
8 + if (!token) {
9 + return false
10 + }
11 + let decoded = jwt.verify(token, config.JWT_KEY)
12 + if (!decoded) {
13 + return false
14 + }
15 + let userId = decoded.id
16 + let user = await User.findByPk(userId)
17 + if (!user) {
18 + return false
19 + }
20 + return user
21 + } catch (error) {
22 + throw error
23 + }
24 +}