정민우

Merge branch 'backend-docker' into 'master'

[Add] Container create 추가



See merge request !18
......@@ -256,7 +256,46 @@ exports.listContainer = async (req, res) => {
}
exports.createContainer = async (req, res) => {
const requiredKey = ['id']
const required = checkRequiredExist(req.body, requiredKey)
if (required) {
logging('container', 'error', { code: 400, message: 'missingKey:${required}' }, req)
return sendError(res, 400, `missingKey:${required}`)
}
try {
const imageId = req.body.id
let image = await Image.findByPk(imageId)
if (!image) {
logging('container', 'error', { code: 404, message: 'NoImageFound' }, req)
return sendError(res, 404, 'NoImageFound')
}
docker.createContainer({
Image: image.name,
Cmd: ['CMD tail -f /dev/null'],
HostConfig: {
Privileged: true
}
}, function(err, container) {
container.attach({
stream: true,
stdout: true,
stderr: true,
tty: true
}, function(err, stream) {
if (err) return sendError(res, 500, err.message);
stream.pipe(process.stdout);
container.start(function(err, data) {
if (err) return sendError(res, 500, err.message);
else return sendResponse(res, data, 200)
});
});
});
} catch (error) {
logging('container', 'error', { code: 500, message: error.message }, req)
return sendError(res, 500, error.message)
}
}
exports.startContainer = async (req, res) => {
......@@ -268,7 +307,7 @@ exports.startContainer = async (req, res) => {
}
try {
const containerId = req.body.id
let container = await Image.findByPk(containerId)
let container = await Container.findByPk(containerId)
if (!container) {
logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req)
return sendError(res, 404, 'NoContainerFound')
......@@ -296,7 +335,7 @@ exports.stopContainer = async (req, res) => {
}
try {
const containerId = req.body.id
let container = await Image.findByPk(containerId)
let container = await Container.findByPk(containerId)
if (!container) {
logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req)
return sendError(res, 404, 'NoContainerFound')
......@@ -326,7 +365,7 @@ exports.removeContainer = async (req, res) => {
const user = await currentUser(req.headers.authorization)
const containerId = req.body.id
let container = await Image.findByPk(containerId)
let container = await Container.findByPk(containerId)
if (!container) {
logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req)
return sendError(res, 404, 'NoContainerFound')
......@@ -471,7 +510,7 @@ exports.adminDeleteContainer = async (req, res) => {
}
try {
const containerId = req.body.id
let container = await Image.findByPk(containerId)
let container = await Container.findByPk(containerId)
if (!container) {
logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req)
return sendError(res, 404, 'NoContainerFound')
......