Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Re2o
dhcp
Commits
0bf05dba
Commit
0bf05dba
authored
May 01, 2019
by
klafyvel
Browse files
Cleaner api request, logging and use absolute path for files
parent
f007e562
Changes
1
Hide whitespace changes
Inline
Side-by-side
main.py
View file @
0bf05dba
#!/usr/bin/env python3
import
os
from
configparser
import
ConfigParser
import
logging
import
socket
from
re2oapi
import
Re2oAPIClient
LOG_LEVEL
=
logging
.
INFO
logger
=
logging
.
getLogger
()
logger
.
setLevel
(
LOG_LEVEL
)
formatter
=
logging
.
Formatter
(
'%(levelname)s :: %(message)s'
)
stream_handler
=
logging
.
StreamHandler
()
stream_handler
.
setFormatter
(
formatter
)
stream_handler
.
setLevel
(
LOG_LEVEL
)
logger
.
addHandler
(
stream_handler
)
BASE_DIR
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
config
=
ConfigParser
()
config
.
read
(
'config.ini'
)
config
.
read
(
os
.
path
.
join
(
BASE_DIR
,
'config.ini'
)
)
api_hostname
=
config
.
get
(
'Re2o'
,
'hostname'
)
api_password
=
config
.
get
(
'Re2o'
,
'password'
)
...
...
@@ -34,7 +48,7 @@ def regen_dhcp(api_client):
ipv4
=
ipv4
)
for
hostname
,
mac_address
,
ipv4
in
hmi_list
)
filename
=
'generated/dhcp{extension}.list'
.
format
(
extension
=
extension
)
filename
=
os
.
path
.
join
(
BASE_DIR
,
'generated/dhcp{extension}.list'
.
format
(
extension
=
extension
)
)
with
open
(
filename
,
'w+'
)
as
f
:
f
.
write
(
dhcp_leases_content
)
...
...
@@ -42,9 +56,8 @@ api_client = Re2oAPIClient(api_hostname, api_username, api_password)
client_hostname
=
socket
.
gethostname
().
split
(
'.'
,
1
)[
0
]
for
service
in
api_client
.
list
(
"services/regen"
):
if
service
[
'hostname'
]
==
client_hostname
and
\
service
[
'service_name'
]
==
'dhcp'
and
\
service
[
'need_regen'
]:
regen_dhcp
(
api_client
)
api_client
.
patch
(
service
[
'api_url'
],
data
=
{
'need_regen'
:
False
})
for
service
in
api_client
.
list
(
"services/regen"
,
params
=
dict
(
hostname
=
client_hostname
)):
if
service
[
'service_name'
]
==
'dhcp'
and
service
[
'need_regen'
]:
logger
.
info
(
"Regenerating service dhcp"
)
regen_dhcp
(
api_client
)
api_client
.
patch
(
service
[
'api_url'
],
data
=
{
'need_regen'
:
False
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment