summaryrefslogtreecommitdiffstats
path: root/server/api/registrations.js
blob: 2253be6a8585040d90507fc46d09318423eb70b3 (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
/* global __appdir */
var path = require('path')
var express = require('express')
var router = express.Router()
var noAuthRouter = express.Router()
var db = require(path.join(__appdir, 'lib', 'sequelize'))
const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))

// GET requests.

/*
 * TODO: CURRENTLY TEST FUNCTION
 */
router.get('/', (req, res) => {
  db.backend.findOne({ where: { id: 1 } }).then(result => {
    const b = new ExternalBackends()
    const instance = b.getInstance(result.type)
    instance.getClient(result.credentials, {}).then(result => {
      res.status(200).send(result)
    })
  })
})

module.exports.router = router

// GET requests.

// POST requests.

/*
 * Returns all root parents or all childs of a group.
 *
 * @return: Ipxe menu with a list of groups.
 */
noAuthRouter.post('/group', (req, res) => {
  const id = req.body.id
  var parents = []
  console.log(req.body.parents)
  if (req.body.parents) parents = JSON.parse(req.body.parents)
  console.log(parents)
  if (id === '0') {
    db.group.findAll({ where: { '$parents.id$': null }, include: ['parents'] }).then(groups => {
      if (groups) {
        res.send(buildIpxeMenu(id, 'Root', groups, []))
      } else {
        res.status(404).end()
      }
    })
  } else {
    db.group.findOne({ where: { id: id }, include: ['parents', 'subgroups', 'clients'] }).then(group => {
      if (group) {
        res.send(buildIpxeMenu(id, group.name, group.subgroups, parents))
      } else {
        res.status(404).end()
      }
    })
  }
})

/*
 * Adds the client in the database and set parents if a parent was selected.
 */
noAuthRouter.post('/add', (req, res) => {
  const mac = req.body.mac
  const uuid = req.body.uuid
  const ip = req.body.ip
  const name = req.body.name
  const parentId = req.body.id
  db.client.findOne({ where: { uuid: uuid } }).then(client => {
    if (client) res.status(200).send('#!ipxe\r\necho Client already exists\r\necho Press any key to continue ...\r\nread x\r\nreboot')
    else {
      db.client.create({ name: name, ip: ip, mac: mac, uuid: uuid }).then(client => {
        if (parentId) {
          client.addGroup(parentId)
        }
        res.send('#!ipxe\r\nreboot')
      })
    }
  })
})

module.exports.noAuthRouter = noAuthRouter

/*
 * id: id of the current selected location.
 * name: Name of the current selected location
 * groups: List of group [{ id: <GROUP_ID>, name: <GROUP_NAME> }, ...]
 *
 * Build the ipxe menu out of the list of groups.
 * Used by the manual registration.
 */
function buildIpxeMenu (id, name, groups, parents) {
  console.log(parents)
  var script = '#!ipxe\r\n'
  // script = script.concat(`console --picture \${img} --x 800 --y 600 || shell\r\n`)

  // Add parent to keep track of the path we clicked through.
  parents.push({ id: id, name: name })
  script = script.concat(`set space:hex 20:20\r\n`)
  script = script.concat(`set space \${space:string}\r\n`)
  script = script.concat(`set parents `, JSON.stringify(parents), '\r\n\r\n')

  // Menu
  var menuscript = ''
  script = script.concat(':start\r\n')
  script = script.concat('menu Choose the group you want the client to be saved in\r\n')

  // Parent menu entries.
  var spacer = ''
  parents.forEach(parent => {
    script = script.concat('item --gap ', spacer, '[', parent.id, '] ', parent.name, '\r\n')
    spacer = spacer.concat(`\${space}`)
  })

  // Group menu entries. First 1-9 are pressable via key?
  var counter = '1'
  groups.forEach(group => {
    // script = script.concat('echo', ' [', subgroup.id, '] ', subgroup.name, '\r\n')
    script = script.concat('item --key ', counter, ' ', counter, ' ', spacer, '[', group.id, '] ', group.name, '\r\n')
    menuscript = menuscript.concat(':', counter, '\r\n', 'params\r\nparam id ', group.id, `\r\nparam parents \${parents}`)
    menuscript = menuscript.concat('\r\nchain https://bas.stfu-kthx.net:8888/api/registrations/group##params\r\n\r\n')
    counter++
  })

  // Add client menu
  script = script.concat('item select Add client to ', name, '\r\n')
  menuscript = menuscript.concat(`:select\r\necho Enter client name\r\nread clientname\r\nparams\r\nparam name \${clientname}\r\n`)
  menuscript = menuscript.concat('param id ', id, `\r\nparam mac \${net0/mac}\r\nparam uuid \${uuid}\r\nparam ip \${net0/ip}\r\n`)
  menuscript = menuscript.concat('chain https://bas.stfu-kthx.net:8888/api/registrations/add##params\r\n\r\n')

  // Goto start menu
  if (id !== '0') {
    script = script.concat('item reset Go to start\r\n')
    menuscript = menuscript.concat(':reset\r\nparams\r\nparam id ', 0, '\r\nchain https://bas.stfu-kthx.net:8888/api/registrations/group##params\r\n\r\n')
  }

  // Exit menu
  script = script.concat('item exit Exit manual registration\r\n')
  menuscript = menuscript.concat(':exit\r\nexit 1\r\n\r\n')

  // Concat script + menuscript and return it.
  script = script.concat(`choose target && goto \${target}\r\n\r\n`)
  script = script.concat(menuscript)
  return script
}