client: support for adding clients

Add a new client with command:

$ ogcli add client --repo-id <repo id>
      --netmask <netmask>
      --room-id <room id>
      --hostname <hostname>
      --mac <mac address>
      --ip <ip address>
      --boot-mode <boot mode>

For example: To add a new client with hostname "javipc", netmask
255.255.255.0, mac address a2:54:00:2e:05:7a, ip address 192.168.56.101,
assign it to repo id 1, room id 1 and set its boot mode to oglive
use:

$ ogcli add client --hostname javipc --netmask 255.255.255.0 --mac
a2:54:00:2e:05:7a --ip 192.168.56.101 --repo-id 1 --room-id 1
--boot-mode oglive

Notes:

- ogserver API REST json payload does not allow to specify a server
  (table 'entorno') for the client. It will automatically assign
  client to server with id 1

- ogserver API REST json payload specifies mac address without ':'.

- payload must contain 'netiface', ogcli specifies 'eth0' is specified
  which is the default value in netiface column in db (table 'ordenadores')

- netdriver column uses "generic" as default, this field is not used
  anymore, but it is set to "generic" by now.

- ogserver should validate that boot mode set is correct.
master v0.3.3
Javier Hernandez 2023-10-27 11:38:24 +02:00 committed by Jose M. Guisado
parent 37c4065f06
commit 4bf4f91f54
3 changed files with 79 additions and 3 deletions

View File

@ -215,7 +215,7 @@ class OgCLI():
OgRoom.delete_room(self.rest, args[1:])
def add(self, args):
choices = ['server', 'repo', 'center', 'room']
choices = ['server', 'repo', 'center', 'room', 'client']
parser = argparse.ArgumentParser(prog='ogcli add')
parser.add_argument('add_obj', choices=choices)
@ -233,3 +233,5 @@ class OgCLI():
OgCenter.add_center(self.rest, args[1:])
elif parsed_args.add_obj == 'room':
OgRoom.add_room(self.rest, args[1:])
elif parsed_args.add_obj == 'client':
OgClient.add_client(self.rest, args[1:])

View File

@ -6,9 +6,9 @@
# (at your option) any later version.
import argparse
import requests
from cli.utils import print_json
from cli.utils import print_json, check_address, check_mac_address
class OgClient():
@ -56,3 +56,68 @@ class OgClient():
payload = {'clients': parsed_args.client_ip}
rest.post('/refresh', payload=payload)
@staticmethod
def add_client(rest, args):
parser = argparse.ArgumentParser(prog='ogcli add client')
parser.add_argument('--repo-id',
type=int,
nargs='?',
required=True,
help='specify the id of the repo')
parser.add_argument('--netmask',
nargs='?',
required=True,
help='specify netmask address')
parser.add_argument('--room-id',
nargs='?',
type=int,
required=True,
help='specify the id of the room where the client is')
parser.add_argument('--hostname',
nargs='?',
required=True,
help='give a name to the client')
parser.add_argument('--mac',
nargs='?',
required=True,
help='mac address should have this format xx:xx:xx:xx:xx:xx')
parser.add_argument('--ip',
nargs='?',
required=True,
help='specify the ip address of the client')
r = rest.get('/mode')
boot_choices = r.json()['modes']
parser.add_argument('--boot-mode',
nargs='?',
required=True,
choices=boot_choices,
help='set a valid boot mode for the client')
parsed_args = parser.parse_args(args)
err = False
if not check_address(parsed_args.netmask):
print('netmask address is not valid', file=sys.stderr)
err = True
if not check_address(parsed_args.ip):
print('client\'s ip address is not valid', file=sys.stderr)
err = True
if not check_mac_address(parsed_args.mac):
print('client\'s mac address is not valid', file=sys.stderr)
err = True
if err:
parser.print_help(file=sys.stderr)
sys.exit(1)
payload = {
'name': parsed_args.hostname,
'netmask': parsed_args.netmask,
'ip': parsed_args.ip,
'mac': parsed_args.mac.replace(":", ""),
'repo_id': parsed_args.repo_id,
'room': parsed_args.room_id,
'boot': parsed_args.boot_mode,
'netiface': 'eth0',
'netdriver': 'generic',
'livedir': 'ogLive'
}
rest.post('/client/add', payload=payload)

View File

@ -7,6 +7,7 @@
import json
import ipaddress
import re
def scope_lookup(scope_id, scope_type, d):
if scope_id == d.get('id') and scope_type == d.get('type'):
@ -39,3 +40,11 @@ def check_address(addr):
return True
except:
return False
def check_mac_address(addr):
if re.match("[a-f0-9]{2}(:[a-f0-9]{2}){5}$", addr.lower()):
return True
else:
print(addr.lower())
return False