summaryrefslogtreecommitdiffstats
path: root/server/api/configloader.js
blob: 699f16b11c194ef39f580aa55dc9a84391b515d8 (plain) (blame)
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
/* global __appdir */
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
var express = require('express')
const { decorateApp } = require('@awaitjs/express')
var noAuthRouter = decorateApp(express.Router())
const config = require(path.join(__appdir, 'config', 'config'))
const url = config.https.host + ':' + config.https.port
const configHelper = require(path.join(__appdir, 'lib', 'confighelper'))
const log = require(path.join(__appdir, 'lib', 'log'))

noAuthRouter.getAsync('/groups/:id', async (req, res) => {
  const list = req.query.list !== undefined && req.query.list !== 'false'
  const group = await db.group.findOne({ where: { id: req.params.id } })
  const config = await configHelper.getGroupConfig(group, list)
  if (!config) return res.status(404).end()
  if (!list) {
    res.set('Content-Type', 'text/plain')
    res.send(config.script)
  } else {
    res.send(config)
  }
})

noAuthRouter.getAsync(['/:uuid', '/'], async (req, res) => {
  const list = req.query.list !== undefined && req.query.list !== 'false'
  const client = await db.client.findOne({ where: { uuid: req.params.uuid } })
  const config = await configHelper.getConfig(client, list)
  if (!list) {
    const logEntry = {
      category: 'CLIENT_BOOT',
      description: 'Client booted iPXE config [' + config.id + '] ' + config.name + '.\n' +
                   'Client UUID: ' + req.params.uuid + '\n' +
                   'Config ID: ' + config.id + '\n' +
                   'Config Name: ' + config.name
    }
    if (client) logEntry.clientId = client.id
    log(logEntry)
    res.set('Content-Type', 'text/plain')
    res.send(config.script)
  } else {
    res.send(config)
  }
})

module.exports.noAuthRouter = noAuthRouter