summaryrefslogtreecommitdiffstats
path: root/server/api/ipxe.js
blob: b7e19709a60f370c45f41f6e324d6c36596ffb69 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* global __appdir */
var path = require('path')
const fs = require('fs')
var shell = require(path.join(__appdir, 'lib', 'shell'))
var express = require('express')
var router = express.Router()
var noAuthRouter = express.Router()
const log = require(path.join(__appdir, 'lib', 'log'))

// GET requests.
router.get('/script', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.sendFile(path.join(__appdir, 'ipxe', 'embedded.ipxe'))
})

router.get('/certificate', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.sendFile(path.join(__appdir, 'bin', 'fullchain.pem'))
})

router.get('/general', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.sendFile(path.join(__appdir, 'ipxe', 'general.h'))
})

router.get('/console', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.sendFile(path.join(__appdir, 'ipxe', 'console.h'))
})

router.get('/log', async (req, res) => {
  const max = req.query.max ? req.query.max : -1
  res.setHeader('content-type', 'text/plain')
  const filepath = path.join(__appdir, 'ipxe', 'ipxelog.txt')

  fs.readFile(filepath, 'utf-8', function (err, content) {
    if (err) res.end()
    if (max !== -1 && content) {
      let c = content.split('\n')
      c = c.splice(-max)
      res.send(c.join('\n'))
    } else res.send(content)
  })
})

router.get('/config', (req, res) => {
  delete require.cache[require.resolve(path.join(__appdir, 'config', 'ipxe'))]
  var config = require(path.join(__appdir, 'config', 'ipxe'))
  res.send(config)
})

router.post('/config', (req, res) => {
  let buildTargets = req.body.buildTargets
  const targetList = req.body.targetList
  const branch = req.body.branch
  const repository = req.body.repository
  var config = require(path.join(__appdir, 'config', 'ipxe'))

  if (repository) config.repository = repository
  if (branch) config.branch = branch
  if (targetList && config.targets.custom) config.targets.list = targetList

  if (buildTargets) {
    if (!config.targets.custom) buildTargets = buildTargets.filter(target => config.targets.list.indexOf(target) >= 0)
    config.targets.build = buildTargets
  }

  fs.writeFile(path.join(__appdir, 'config', 'ipxe.json'), JSON.stringify(config, null, 4), {}, (err) => {
    if (err) res.status(500).end()
    else res.send()
  })
})

router.put('/:filename', (req, res) => {
  var filepath = null
  let filename = ''

  // Set the file path
  if (req.params.filename === 'script') {
    filepath = path.join(__appdir, 'ipxe', 'embedded.ipxe')
    filename = 'embedded.ipxe'
  } else if (req.params.filename === 'console' || req.params.filename === 'general') {
    filepath = path.join(__appdir, 'ipxe', req.params.filename + '.h')
    filename = req.params.filename + '.h'
  } else if (req.params.filename === 'certificate') {
    filepath = path.join(__appdir, 'bin', 'fullchain.pem')
    filename = 'fullchain.pem'
  } else {
    log({
      category: 'ERROR_IPXEBUILDER_SAVE',
      description: filename + ' cannot be saved. Filename unknown',
      userId: req.user.id
    })
    res.status(500).send({ status: 'UNKNOWN_FILENAME', error: 'The provided filename is unknown' })
    return
  }

  // Write File
  fs.writeFile(filepath, req.body.data, err => {
    if (err) {
      log({
        category: 'ERROR_IPXEBUILDER_SAVE',
        description: 'Error while saving ' + filename + '. ' + err,
        userId: req.user.id
      })
      res.status(500).send({ status: 'WRITE_FILE_ERROR', error: err })
    } else {
      log({
        category: 'IPXEBUILDER_SAVE',
        description: 'User has saved ' + filename,
        userId: req.user.id
      })
      res.status(200).send({ status: 'SUCCESS' })
    }
  })
})

/*
 * @return: Rebuild the ipxe.
 */
router.get('/build', async (req, res) => {
  log({
    category: 'IPXEBUILDER_BUILD',
    description: 'User initiated the ipxe building process.',
    userId: req.user.id
  })

  delete require.cache[require.resolve(path.join(__appdir, 'config', 'ipxe'))]
  const config = require(path.join(__appdir, 'config', 'ipxe'))
  const build = await shell.buildIpxe(config.targets.build.join(' '), config.repository, config.branch)
  res.send(build)
})

router.get('/cancel', async (req, res) => {
  log({
    category: 'IPXEBUILDER_CANCEL',
    description: 'User canceled the ipxe building process.',
    userId: req.user.id
  })
  const result = await shell.cancelBuilding(req, res)
  res.send(result)
})

router.get('/clean', async (req, res) => {
  log({
    category: 'IPXEBUILDER_CLEAN',
    description: 'User initiated ipxe cleaning process.',
    userId: req.user.id
  })
  const result = await shell.clean()
  res.send(result)
})

router.get('/status', (req, res) => {
  res.send({ status: 'SUCCESS', data: shell.status(req.params.version) })
})

module.exports.router = router

noAuthRouter.get('/load/script', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  fs.readFile(path.join(__appdir, 'ipxe', 'default.ipxe'), 'utf-8', function (err, content) {
    if (err) res.end()
    res.send(content)
  })
})

noAuthRouter.get('/load/registration', (req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.sendFile(path.join(__appdir, 'ipxe', 'registration.ipxe'))
})

module.exports.noAuthRouter = noAuthRouter