Showing
1 changed file
with
38 additions
and
1 deletions
... | @@ -4,7 +4,9 @@ const { currentUser } = require('../utils/auth') | ... | @@ -4,7 +4,9 @@ const { currentUser } = require('../utils/auth') |
4 | const { logging } = require('../utils/log') | 4 | const { logging } = require('../utils/log') |
5 | const { checkRequiredExist, setValues } = require('../utils/validation') | 5 | const { checkRequiredExist, setValues } = require('../utils/validation') |
6 | const fs = require('fs') | 6 | const fs = require('fs') |
7 | -const randomstring = require("randomstring"); | 7 | +const randomstring = require("randomstring") |
8 | +const Docker = require('dockerode') | ||
9 | +const docker = new Docker({socketPath: '/var/run/docker.sock'}) | ||
8 | 10 | ||
9 | const Op = Sequelize.Op | 11 | const Op = Sequelize.Op |
10 | 12 | ||
... | @@ -136,7 +138,42 @@ exports.listImage = async (req, res) => { | ... | @@ -136,7 +138,42 @@ exports.listImage = async (req, res) => { |
136 | } | 138 | } |
137 | } | 139 | } |
138 | exports.buildImage = async (req, res) => { | 140 | exports.buildImage = async (req, res) => { |
141 | + const requiredKey = ['id', 'name'] | ||
142 | + const required = checkRequiredExist(req.body, requiredKey) | ||
143 | + if (required) { | ||
144 | + logging('image', 'error', { code: 400, message: 'missingKey:${required}' }, req) | ||
145 | + return sendError(res, 400, `missingKey:${required}`) | ||
146 | + } | ||
147 | + try { | ||
148 | + const user = await currentUser(req.headers.authorization) | ||
139 | 149 | ||
150 | + const dockerfileId = req.body.id | ||
151 | + const imageName = req.body.name | ||
152 | + let dockerfile = await Dockerfile.findByPk(dockerfileId) | ||
153 | + if (!dockerfile) { | ||
154 | + logging('dockerfile', 'error', { code: 404, message: 'NoDockerfileFound' }, req) | ||
155 | + return sendError(res, 404, 'NoDockerfileFound') | ||
156 | + } | ||
157 | + if (!user || user.id !== dockerfile.userId) { | ||
158 | + logging('dockerfile', 'error', { code: 403, message: 'Unauthorized' }, req) | ||
159 | + return sendError(res, 403, 'Unauthorized') | ||
160 | + } | ||
161 | + | ||
162 | + docker.buildImage({ | ||
163 | + context: dockerfile.filepath, | ||
164 | + src: ['Dockerfile'] | ||
165 | + }, {t: imageName}, function (err, response) { | ||
166 | + if(!err) { | ||
167 | + return sendResponse(res, response, 200) | ||
168 | + } | ||
169 | + else { | ||
170 | + return sendError(res, 500, err.message) | ||
171 | + } | ||
172 | + }) | ||
173 | + } catch (error) { | ||
174 | + logging('image', 'error', { code: 500, message: error.message }, req) | ||
175 | + return sendError(res, 500, error.message) | ||
176 | + } | ||
140 | } | 177 | } |
141 | exports.removeImage = async (req, res) => { | 178 | exports.removeImage = async (req, res) => { |
142 | const requiredKey = ['id'] | 179 | const requiredKey = ['id'] | ... | ... |
-
Please register or login to post a comment