dockerController.js
5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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: '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) => {
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) => {
}
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) => {
}