From fa81433ef1afa40bdfa524551aeb9c662a8621a0 Mon Sep 17 00:00:00 2001 From: Emiliano Vavassori Date: Mon, 15 Jun 2015 03:32:57 +0200 Subject: [PATCH] Altri file necessari per mac2hostname. --- roles/pxe/files/mac2hostname.py | 65 ++++++++++++++++++++++++++++++++ roles/pxe/tasks/mac2hostname.yml | 24 ++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 roles/pxe/files/mac2hostname.py create mode 100644 roles/pxe/tasks/mac2hostname.yml diff --git a/roles/pxe/files/mac2hostname.py b/roles/pxe/files/mac2hostname.py new file mode 100644 index 0000000..958733f --- /dev/null +++ b/roles/pxe/files/mac2hostname.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +"""API to map MAC addresses to hostnames""" + +from contextlib import contextmanager, closing +from sqlite3 import connect +from bottle import route, run, request +from json import dumps +import os + +__author__ = "Enrico Bacis" +__email__ = "enrico.bacis@gmail.com" + +# Aggiunta di Emiliano Vavassori +# Configuring default directories for the program +dbdir = '/var/lib/mac2hostname' +logdir = '/var/log/mac2hostname' +piddir = '/var/run/mac2hostname' + +# Will create a PID file to be used with init.d script +pid = os.getpid() +op = open(piddir + '/service.pid', 'w') +op.write("%s" % pid) +op.close() + +@contextmanager +def getcursor(db = dbdir + 'mac2hostname.db'): + with connect(db) as connection: + with closing(connection.cursor()) as cursor: + yield cursor + +def init_tables(): + with getcursor() as cursor: + cursor.execute('CREATE TABLE IF NOT EXISTS client (id INT PRIMARY KEY,' + 'hostname TEXT NOT NULL UNIQUE, mac TEXT UNIQUE)') + cursor.execute('CREATE INDEX IF NOT EXISTS idxmac ON client(mac)') + +@route('/hosts') +def hosts(): + with getcursor() as cursor: + return dumps([dict((meta[0], data) + for meta, data in zip(cursor.description, row)) + for row in cursor.execute('SELECT hostname, mac FROM client ORDER BY id')], indent=4) + +def normalizemac(mac): + return ':'.join(x.zfill(2) for x in mac.split(':')).upper() + +def gethostname(mac, base=None): + mac, base = normalizemac(mac), base or 'lab' + with getcursor() as cursor: + (newid,) = cursor.execute('SELECT COALESCE(MAX(id)+1, 1) FROM client').fetchone() + cursor.execute('INSERT OR IGNORE INTO client VALUES (%s, "%s-%s", "%s")' % (newid, base, newid, mac)) + (hostname,) = cursor.execute('SELECT hostname FROM client WHERE mac = "%s"' % mac) + return hostname + +@route('/mac2hostname') +def mac2hostname(): + mac = request.query.mac + if not mac: + return 'Usage: GET /mac2hostname?mac=XX_XX_XX_XX_XX_XX[&base=YYY]' + return gethostname(mac, base) + +if __name__ == '__main__': + init_tables() + run(host='0.0.0.0', port=3000) diff --git a/roles/pxe/tasks/mac2hostname.yml b/roles/pxe/tasks/mac2hostname.yml new file mode 100644 index 0000000..e6d63cc --- /dev/null +++ b/roles/pxe/tasks/mac2hostname.yml @@ -0,0 +1,24 @@ +--- +# Installa e configura il servizio per rinominare il client +# Grazie per il servizio a Enrico Bacis + +- name: Installazione dipendenze Python + yum: pkg=python-bottle state=present + sudo: yes + +- name: Creazione directory necessarie + file: path={{ item }} state=directory + with_items: + - /var/lib/mac2hostname + - /var/run/mac2hostname + - /var/log/mac2hostname + - /usr/share/mac2hostname + sudo: yes + +- name: Copia dello script + copy: src=mac2hostname.py dest=/usr/share/mac2hostname/mac2hostname.py mode=0755 + sudo: yes + +- name: Finalizzazione della copia + file: src=/usr/share/mac2hostname/mac2hostname.py dest=/usr/bin/mac2hostname state=link + sudo: yes