forked from LibreSchool/doraemon
New version: provides two new routes for dynamic inventory with ansible.
This commit is contained in:
parent
f9e0e2b417
commit
ed2bf318c1
72
doraemon.py
72
doraemon.py
@ -46,6 +46,12 @@ class MyApp:
|
|||||||
# Assures table creation
|
# Assures table creation
|
||||||
self.__init_tables()
|
self.__init_tables()
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def __getcursor(self):
|
||||||
|
with connect(self.__dbfile) as connection:
|
||||||
|
with closing(connection.cursor()) as cursor:
|
||||||
|
yield cursor
|
||||||
|
|
||||||
# Private methods
|
# Private methods
|
||||||
def __route(self):
|
def __route(self):
|
||||||
self.__app.get('/mac2hostname', callback=self.mac2hostname)
|
self.__app.get('/mac2hostname', callback=self.mac2hostname)
|
||||||
@ -54,6 +60,8 @@ class MyApp:
|
|||||||
self.__app.get('/domain', callback=self.domain)
|
self.__app.get('/domain', callback=self.domain)
|
||||||
self.__app.get('/mgmtkey', callback=self.mgmtkey)
|
self.__app.get('/mgmtkey', callback=self.mgmtkey)
|
||||||
self.__app.get('/vaultpass', callback=self.vaultpass)
|
self.__app.get('/vaultpass', callback=self.vaultpass)
|
||||||
|
self.__app.get('/ansible_list', callback=self.ansible_list)
|
||||||
|
self.__app.get('/ansible_host', callback=self.ansible_host)
|
||||||
|
|
||||||
def __init_tables(self):
|
def __init_tables(self):
|
||||||
with self.__getcursor() as cursor:
|
with self.__getcursor() as cursor:
|
||||||
@ -85,11 +93,34 @@ class MyApp:
|
|||||||
(hostname,) = cursor.execute('SELECT hostname FROM client WHERE mac = "%s"' % mac).fetchone()
|
(hostname,) = cursor.execute('SELECT hostname FROM client WHERE mac = "%s"' % mac).fetchone()
|
||||||
return hostname
|
return hostname
|
||||||
|
|
||||||
@contextmanager
|
# This should be used only after registration of the client
|
||||||
def __getcursor(self):
|
# (whatsmyhostname)
|
||||||
with connect(self.__dbfile) as connection:
|
def __getrole(self):
|
||||||
with closing(connection.cursor()) as cursor:
|
macaddress = self.__normalizemac(self.__getmac(request['REMOTE_ADDR']))
|
||||||
yield cursor
|
hostname = self.__gethostname(macaddress)
|
||||||
|
|
||||||
|
with self.__getcursor() as cursor:
|
||||||
|
(role,) = cursor.execute("SELECT role FROM client WHERE hostname = '%s' AND mac = '%s'" % (hostname, macaddress)).fetchone()
|
||||||
|
return role
|
||||||
|
|
||||||
|
# Mocks up variables to be returned.
|
||||||
|
# When will be ready Enrico's work on the interface, this should be simpler
|
||||||
|
# to implement.
|
||||||
|
def __rolevars(self, role=None):
|
||||||
|
retval = {}
|
||||||
|
if role == 'client':
|
||||||
|
retval = {
|
||||||
|
'role': role,
|
||||||
|
'addpkg': [ 'gimp' ],
|
||||||
|
'delpkg': []
|
||||||
|
}
|
||||||
|
elif role == 'docente':
|
||||||
|
retval = {
|
||||||
|
'role': role,
|
||||||
|
'addpkg': [],
|
||||||
|
'delpkg': []
|
||||||
|
}
|
||||||
|
return retval
|
||||||
|
|
||||||
# Instance methods AKA routes
|
# Instance methods AKA routes
|
||||||
def mac2hostname(self):
|
def mac2hostname(self):
|
||||||
@ -147,6 +178,37 @@ class MyApp:
|
|||||||
# Encode in base64 because of unicode strings
|
# Encode in base64 because of unicode strings
|
||||||
return base64.b64encode(crypted)
|
return base64.b64encode(crypted)
|
||||||
|
|
||||||
|
def ansible_list(self):
|
||||||
|
# Faking calls
|
||||||
|
if request.query.role:
|
||||||
|
role = request.query.role
|
||||||
|
else:
|
||||||
|
role = self.__getrole()
|
||||||
|
|
||||||
|
result = {
|
||||||
|
'localhost': {
|
||||||
|
'hosts': [ 'localhost' ],
|
||||||
|
'vars': {
|
||||||
|
'ansible_connection': 'local'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'_meta': {
|
||||||
|
'hostvars': {
|
||||||
|
'localhost': self.__rolevars(role)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dumps(result)
|
||||||
|
|
||||||
|
def ansible_host(self):
|
||||||
|
# Ignoring 'host' parameter, since it should always be 'localhost'.
|
||||||
|
# Instead, for testing purposes, if asked for a role, use it
|
||||||
|
if request.query.role:
|
||||||
|
role = request.query.role
|
||||||
|
else:
|
||||||
|
role = self.__getrole()
|
||||||
|
return dumps(self.__rolevars(role))
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
# Opens up a PID file
|
# Opens up a PID file
|
||||||
pid = os.getpid()
|
pid = os.getpid()
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
Summary: Helps client to join domain and maintain itself
|
Summary: Helps client to join domain and maintain itself
|
||||||
Name: doraemon
|
Name: doraemon
|
||||||
Version: 1.0.0
|
Version: 1.1.0
|
||||||
Release: 2.ns6
|
Release: 1.ns6
|
||||||
URL: https://github.com/bglug-it/doraemon/
|
URL: https://github.com/bglug-it/doraemon/
|
||||||
License: GPLv2+
|
License: GPLv2+
|
||||||
Group: System Environment/Daemons
|
Group: System Environment/Daemons
|
||||||
@ -9,7 +9,7 @@ BuildRoot: %{_tmppath}/%{name}-root
|
|||||||
Requires: python python-bottle python-crypto2.6 nethserver-base
|
Requires: python python-bottle python-crypto2.6 nethserver-base
|
||||||
Requires(post): chkconfig nethserver-base
|
Requires(post): chkconfig nethserver-base
|
||||||
Requires(preun): chkconfig initscripts nethserver-base
|
Requires(preun): chkconfig initscripts nethserver-base
|
||||||
Source0: doraemon-1.0.0.tar.gz
|
Source0: doraemon-1.1.0.tar.gz
|
||||||
BuildArch: noarch
|
BuildArch: noarch
|
||||||
|
|
||||||
%description
|
%description
|
||||||
@ -76,6 +76,9 @@ rm -rf %{buildroot}
|
|||||||
%{_sysconfdir}/e-smith/db/configuration/defaults/%{name}/TCPPort
|
%{_sysconfdir}/e-smith/db/configuration/defaults/%{name}/TCPPort
|
||||||
|
|
||||||
%changelog
|
%changelog
|
||||||
|
* Sun Sep 06 2015 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 1.1.0-1.ns6
|
||||||
|
- Packing new version with new functionalities.
|
||||||
|
|
||||||
* Wed Jul 15 2015 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 1.0.0-2.ns6
|
* Wed Jul 15 2015 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 1.0.0-2.ns6
|
||||||
- Added logrotate file.
|
- Added logrotate file.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user