정민우

[Add] Container create 추가

...@@ -256,7 +256,46 @@ exports.listContainer = async (req, res) => { ...@@ -256,7 +256,46 @@ exports.listContainer = async (req, res) => {
256 } 256 }
257 257
258 exports.createContainer = async (req, res) => { 258 exports.createContainer = async (req, res) => {
259 - 259 + const requiredKey = ['id']
260 + const required = checkRequiredExist(req.body, requiredKey)
261 + if (required) {
262 + logging('container', 'error', { code: 400, message: 'missingKey:${required}' }, req)
263 + return sendError(res, 400, `missingKey:${required}`)
264 + }
265 + try {
266 + const imageId = req.body.id
267 + let image = await Image.findByPk(imageId)
268 + if (!image) {
269 + logging('container', 'error', { code: 404, message: 'NoImageFound' }, req)
270 + return sendError(res, 404, 'NoImageFound')
271 + }
272 + docker.createContainer({
273 + Image: image.name,
274 + Cmd: ['CMD tail -f /dev/null'],
275 + HostConfig: {
276 + Privileged: true
277 + }
278 + }, function(err, container) {
279 + container.attach({
280 + stream: true,
281 + stdout: true,
282 + stderr: true,
283 + tty: true
284 + }, function(err, stream) {
285 + if (err) return sendError(res, 500, err.message);
286 +
287 + stream.pipe(process.stdout);
288 +
289 + container.start(function(err, data) {
290 + if (err) return sendError(res, 500, err.message);
291 + else return sendResponse(res, data, 200)
292 + });
293 + });
294 + });
295 + } catch (error) {
296 + logging('container', 'error', { code: 500, message: error.message }, req)
297 + return sendError(res, 500, error.message)
298 + }
260 } 299 }
261 300
262 exports.startContainer = async (req, res) => { 301 exports.startContainer = async (req, res) => {
...@@ -268,7 +307,7 @@ exports.startContainer = async (req, res) => { ...@@ -268,7 +307,7 @@ exports.startContainer = async (req, res) => {
268 } 307 }
269 try { 308 try {
270 const containerId = req.body.id 309 const containerId = req.body.id
271 - let container = await Image.findByPk(containerId) 310 + let container = await Container.findByPk(containerId)
272 if (!container) { 311 if (!container) {
273 logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req) 312 logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req)
274 return sendError(res, 404, 'NoContainerFound') 313 return sendError(res, 404, 'NoContainerFound')
...@@ -296,7 +335,7 @@ exports.stopContainer = async (req, res) => { ...@@ -296,7 +335,7 @@ exports.stopContainer = async (req, res) => {
296 } 335 }
297 try { 336 try {
298 const containerId = req.body.id 337 const containerId = req.body.id
299 - let container = await Image.findByPk(containerId) 338 + let container = await Container.findByPk(containerId)
300 if (!container) { 339 if (!container) {
301 logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req) 340 logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req)
302 return sendError(res, 404, 'NoContainerFound') 341 return sendError(res, 404, 'NoContainerFound')
...@@ -326,7 +365,7 @@ exports.removeContainer = async (req, res) => { ...@@ -326,7 +365,7 @@ exports.removeContainer = async (req, res) => {
326 const user = await currentUser(req.headers.authorization) 365 const user = await currentUser(req.headers.authorization)
327 366
328 const containerId = req.body.id 367 const containerId = req.body.id
329 - let container = await Image.findByPk(containerId) 368 + let container = await Container.findByPk(containerId)
330 if (!container) { 369 if (!container) {
331 logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req) 370 logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req)
332 return sendError(res, 404, 'NoContainerFound') 371 return sendError(res, 404, 'NoContainerFound')
...@@ -471,7 +510,7 @@ exports.adminDeleteContainer = async (req, res) => { ...@@ -471,7 +510,7 @@ exports.adminDeleteContainer = async (req, res) => {
471 } 510 }
472 try { 511 try {
473 const containerId = req.body.id 512 const containerId = req.body.id
474 - let container = await Image.findByPk(containerId) 513 + let container = await Container.findByPk(containerId)
475 if (!container) { 514 if (!container) {
476 logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req) 515 logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req)
477 return sendError(res, 404, 'NoContainerFound') 516 return sendError(res, 404, 'NoContainerFound')
......