Skip to content

Commit 9bc79f9

Browse files
committed
Merge branch 'linter-pr' into main
2 parents 9a281c3 + 400e76d commit 9bc79f9

File tree

4 files changed

+3874
-280
lines changed

4 files changed

+3874
-280
lines changed

.eslintrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"commonjs": true,
5+
"es2021": true,
6+
"mocha": true
7+
},
8+
"extends": [
9+
"standard"
10+
],
11+
"parserOptions": {
12+
"ecmaVersion": 12
13+
},
14+
"rules": {
15+
"indent": ["error", 4]
16+
}
17+
}

docker.js

Lines changed: 102 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,67 @@
1-
const got = require('got');
2-
const Docker = require('dockerode');
1+
const got = require('got')
2+
const Docker = require('dockerode')
33

44
/**
55
* Docker Container driver
6-
*
6+
*
77
* Handles the creation and deletation of containers to back Projects
8-
*
9-
* This driver creates Projects backed by Docker
10-
*
8+
*
9+
* This driver creates Projects backed by Docker
10+
*
1111
* @module docker
1212
* @memberof forge.containers.drivers
13-
*
13+
*
1414
*/
1515
module.exports = {
16-
/**
16+
/**
1717
* Initialises this driver
18-
* @param {string} app - the Vue application
18+
* @param {string} app - the Vue application
1919
* @param {object} options - A set of configuration options for the driver
2020
* @return {forge.containers.ProjectArguments}
2121
*/
2222
init: async (app, options) => {
2323
this._app = app
2424
this._docker = new Docker({
25-
socketPath: app.config.driver.options?.socket || '/var/run/docker.sock'
25+
socketPath: app.config.driver.options?.socket || '/var/run/docker.sock'
2626
})
2727
this._options = options
2828

29-
if (! options.registry) {
30-
options.registry = app.config.driver.options?.registry || "" //use docker hub
29+
if (!options.registry) {
30+
options.registry = app.config.driver.options?.registry || '' // use docker hub
3131
}
3232

3333
// let projects = await this._app.db.models.DockerProject.findAll()
34-
let projects = await this._app.db.models.Project.findAll()
34+
const projects = await this._app.db.models.Project.findAll()
3535
projects.forEach(async (project) => {
36-
const projectSettings = await project.getAllSettings();
36+
// const projectSettings = await project.getAllSettings();
3737
// let forgeProject = await this._app.db.models.Project.byId(project.id);
3838
if (project) {
3939
let container
4040
try {
41-
container = await this._docker.listContainers({filter: `name=${project.id}`})
41+
container = await this._docker.listContainers({ filter: `name=${project.id}` })
4242
if (container[0]) {
4343
container = await this._docker.getContainer(container[0].Id)
4444
} else {
4545
container = undefined
4646
}
4747
} catch (err) {
48-
console.log("Container not found")
48+
console.log('Container not found')
4949
}
5050
if (container) {
51-
let state = await container.inspect()
51+
const state = await container.inspect()
5252
if (!state.State.Running) {
53-
if (project.state == 'running') {
54-
//need to restart existing container
53+
if (project.state === 'running') {
54+
// need to restart existing container
5555
container.start()
5656
}
57-
}
57+
}
5858
} else {
59-
//need to create
60-
let name = project.name
59+
// need to create
6160
this._app.containers._createContainer(project,
62-
{},//JSON.parse(project.options),
63-
this._options.domain,
61+
{}, // JSON.parse(project.options),
62+
this._options.domain,
6463
this._options.containers[project.type]
65-
);
64+
)
6665
}
6766
}
6867
})
@@ -76,11 +75,8 @@ module.exports = {
7675
* @return {forge.containers.Project}
7776
*/
7877
create: async (project, options) => {
79-
8078
// console.log(options)
8179
// console.log("---")
82-
83-
8480
return await this._app.containers._createContainer(project, options, this._options.domain, this._options.containers[project.type])
8581
},
8682
/**
@@ -89,33 +85,33 @@ module.exports = {
8985
* @return {Object}
9086
*/
9187
remove: async (project) => {
92-
console.log("removing ", project.id)
88+
console.log('removing ', project.id)
9389
try {
9490
// let forgeProject = await this._app.db.models.Project.byId(id);
95-
let container = await this._docker.getContainer(project.id)//forgeProject.name);
91+
const container = await this._docker.getContainer(project.id)
9692
await container.stop()
9793
await container.remove()
9894
// let project = await this._app.db.models.DockerProject.byId(id)
9995
// await project.destroy()
100-
return {status: "okay"}
96+
return { status: 'okay' }
10197
} catch (err) {
10298
console.log(err)
103-
return {error: err}
99+
return { error: err }
104100
}
105101
},
106102
/**
107103
* Retrieves details of a project's container
108104
* @param {string} id - id of project to query
109-
* @return {Object}
105+
* @return {Object}
110106
*/
111107
details: async (project) => {
112-
let infoURL = "http://"+ project.id + ":2880/flowforge/info"
108+
const infoURL = 'http://' + project.id + ':2880/flowforge/info'
113109
try {
114-
let info = JSON.parse((await got.get(infoURL)).body)
115-
return info
110+
const info = JSON.parse((await got.get(infoURL)).body)
111+
return info
116112
} catch (err) {
117-
//TODO
118-
return
113+
// TODO
114+
// return
119115
}
120116

121117
// try {
@@ -137,28 +133,28 @@ module.exports = {
137133
* Returns the settings for the project
138134
*/
139135
settings: async (project) => {
140-
// let project = await this._app.db.models.DockerProject.byId(id)
141-
const projectSettings = await project.getAllSettings()
142-
//let options = JSON.parse(project.options)
143-
let settings = {}
144-
settings.projectID = project.id
145-
settings.port = 1880
146-
settings.rootDir = "/"
147-
settings.userDir = "data"
148-
settings.baseURL = project.url
149-
settings.forgeURL = this._app.config.base_url
136+
// let project = await this._app.db.models.DockerProject.byId(id)
137+
// const projectSettings = await project.getAllSettings()
138+
// let options = JSON.parse(project.options)
139+
const settings = {}
140+
settings.projectID = project.id
141+
settings.port = 1880
142+
settings.rootDir = '/'
143+
settings.userDir = 'data'
144+
settings.baseURL = project.url
145+
settings.forgeURL = this._app.config.base_url
150146

151-
return settings
147+
return settings
152148
},
153149
/**
154150
* Lists all containers
155151
* @param {string} filter - rules to filter the containers
156152
* @return {Object}
157153
*/
158154
list: async (filter) => {
159-
let containers = await this._docker.listContainers({all: true})
160-
//console.log(containers)
161-
return containers.map(c => { return c.Names[0].substring(1)})
155+
const containers = await this._docker.listContainers({ all: true })
156+
// console.log(containers)
157+
return containers.map(c => { return c.Names[0].substring(1) })
162158
},
163159
/**
164160
* Starts a Project's container
@@ -173,16 +169,16 @@ module.exports = {
173169

174170
// }
175171

176-
await got.post("http://" + project.id + ":2880/flowforge/command",{
177-
json: {
178-
cmd: "start"
179-
}
172+
await got.post('http://' + project.id + ':2880/flowforge/command', {
173+
json: {
174+
cmd: 'start'
175+
}
180176
})
181177

182-
project.state = "starting"
178+
project.state = 'starting'
183179
project.save()
184180

185-
return {status: "okey"}
181+
return { status: 'okey' }
186182
},
187183
/**
188184
* Stops a Proejct's container
@@ -197,48 +193,47 @@ module.exports = {
197193

198194
// }
199195

200-
await got.post("http://" + project.id + ":2880/flowforge/command",{
201-
json: {
202-
cmd: "stop"
203-
}
196+
await got.post('http://' + project.id + ':2880/flowforge/command', {
197+
json: {
198+
cmd: 'stop'
199+
}
204200
})
205-
project.state = "stopped";
201+
project.state = 'stopped'
206202
project.save()
207-
return Promise.resolve({status: "okay"})
203+
return Promise.resolve({ status: 'okay' })
208204
},
209205
/**
210206
* Restarts a Project's container
211207
* @param {string} id - id of project to restart
212208
* @return {forge.Status}
213209
*/
214210
restart: async (project) => {
215-
await got.post("http://" + project.id + ":2880/flowforge/command",{
216-
json: {
217-
cmd: "restart"
218-
}
211+
await got.post('http://' + project.id + ':2880/flowforge/command', {
212+
json: {
213+
cmd: 'restart'
214+
}
219215
})
220216

221-
return {state: "okay"}
217+
return { state: 'okay' }
222218
},
223219
logs: async (project) => {
224220
try {
225-
let result = await got.get("http://" + project.id + ":2880/flowforge/logs").json()
221+
const result = await got.get('http://' + project.id + ':2880/flowforge/logs').json()
226222
return result
227223
} catch (err) {
228224
console.log(err)
229-
return ""
225+
return ''
230226
}
231227
},
232228
_createContainer: async (project, options, domain, image) => {
233-
234-
let networks = await this._docker.listNetworks({filters:{label: ["com.docker.compose.network=flowforge"]}})
229+
const networks = await this._docker.listNetworks({ filters: { label: ['com.docker.compose.network=flowforge'] } })
235230

236231
if (options.registry) {
237-
image = options.registry + "/" + image
232+
image = options.registry + '/' + image
238233
}
239-
let contOptions = {
234+
const contOptions = {
240235
Image: image,
241-
name: project.id, //options.name,
236+
name: project.id, // options.name,
242237
Env: [],
243238
Labels: {},
244239
AttachStdin: false,
@@ -249,59 +244,56 @@ module.exports = {
249244
}
250245
}
251246
if (options.env) {
252-
Object.keys(options.env).forEach(k=>{
247+
Object.keys(options.env).forEach(k => {
253248
if (k) {
254-
contOptions.Env.push(k+"="+options.env[k])
249+
contOptions.Env.push(k + '=' + options.env[k])
255250
}
256251
})
257252
}
258253

259-
//TODO http/https needs to be dynamic (or we just enforce https?)
260-
//and port number
261-
let projectURL = `http://${project.name}.${this._options.domain}`
254+
// TODO http/https needs to be dynamic (or we just enforce https?)
255+
// and port number
256+
const projectURL = `http://${project.name}.${this._options.domain}`
262257

263-
authTokens = await project.refreshAuthTokens()
258+
const authTokens = await project.refreshAuthTokens()
264259

265-
//AuthProvider
266-
contOptions.Env.push("FORGE_CLIENT_ID="+authTokens.clientID);
267-
contOptions.Env.push("FORGE_CLIENT_SECRET="+authTokens.clientSecret);
268-
//TODO this needs to come from a central point
269-
contOptions.Env.push("FORGE_URL="+this._app.config.api_url);
270-
contOptions.Env.push(`BASE_URL=${projectURL}`);
271-
//Only if we are using nginx ingress proxy
272-
contOptions.Env.push(`VIRTUAL_HOST=${project.name}.${domain}`);
273-
contOptions.Env.push(`VIRTUAL_PORT=1880`);
274-
//httpStorage settings
260+
// AuthProvider
261+
contOptions.Env.push('FORGE_CLIENT_ID=' + authTokens.clientID)
262+
contOptions.Env.push('FORGE_CLIENT_SECRET=' + authTokens.clientSecret)
263+
// TODO this needs to come from a central point
264+
contOptions.Env.push('FORGE_URL=' + this._app.config.api_url)
265+
contOptions.Env.push(`BASE_URL=${projectURL}`)
266+
// Only if we are using nginx ingress proxy
267+
contOptions.Env.push(`VIRTUAL_HOST=${project.name}.${domain}`)
268+
contOptions.Env.push('VIRTUAL_PORT=1880')
269+
// httpStorage settings
275270
contOptions.Env.push(`FORGE_PROJECT_ID=${project.id}`)
276271
contOptions.Env.push(`FORGE_PROJECT_TOKEN=${authTokens.token}`)
277-
272+
278273
try {
279-
let container = await this._docker.createContainer(contOptions);
280-
274+
const container = await this._docker.createContainer(contOptions)
281275

282276
project.url = projectURL
283277
project.save()
284278

285279
container.start()
286-
.then(() => {
287-
project.state = "running";
288-
project.save();
289-
})
290-
.catch(err => {
291-
console.log( err)
292-
});
293-
294-
// console.log("all good")
280+
.then(() => {
281+
project.state = 'running'
282+
project.save()
283+
})
284+
.catch(err => {
285+
console.log(err)
286+
})
295287

296288
return {
297-
id: project.id,
298-
status: "okay",
289+
id: project.id,
290+
status: 'okay',
299291
url: projectURL,
300292
meta: container
301-
};
302-
} catch(err) {
303-
console.log("error:", err)
304-
return {error: err}
293+
}
294+
} catch (err) {
295+
console.log('error:', err)
296+
return { error: err }
305297
}
306298
}
307299
}

0 commit comments

Comments
 (0)