Browse Source

New structure, getting ready for PyPI

changes/67/134467/1
Dmitry Tantsur 8 years ago
parent
commit
45872dcc88
  1. 4
      .gitignore
  2. 0
      ironic_discoverd/__init__.py
  3. 49
      ironic_discoverd/__main__.py
  4. 50
      ironic_discoverd/discoverd.py
  5. 11
      setup.py

4
.gitignore vendored

@ -1,2 +1,6 @@
.*.swp
.env
*.pyc
build/
dist/
*.egg-info/

0
ironic_discoverd/__init__.py

49
ironic_discoverd/__main__.py

@ -0,0 +1,49 @@
import logging
import threading
import time
from flask import Flask, request
from ironic_discoverd.discoverd import (LOG, process, start,
Firewall, get_client)
app = Flask(__name__)
@app.route('/continue', methods=['POST'])
def post_continue():
data = request.get_json(force=True)
LOG.debug("Got JSON %s, going into processing thread", data)
threading.Thread(target=process, args=(data,)).start()
return "{}", 202, {"content-type": "application/json"}
@app.route('/start', methods=['POST'])
def post_start():
data = request.get_json(force=True)
LOG.debug("Got JSON %s, going into processing thread", data)
threading.Thread(target=start, args=(data,)).start()
return "{}", 202, {"content-type": "application/json"}
def periodic_update(event, ironic):
while not event.is_set():
LOG.debug('Running periodic update of filters')
Firewall.update_filters(ironic)
for _ in range(15):
if event.is_set():
return
time.sleep(1)
logging.basicConfig(level=logging.INFO)
ironic = get_client()
Firewall.init()
event = threading.Event()
threading.Thread(target=periodic_update, args=(event, ironic)).start()
try:
app.run(debug=True, host='0.0.0.0', port=5050)
finally:
LOG.info('Waiting for background thread to shutdown')
event.set()

50
discoverd.py → ironic_discoverd/discoverd.py

@ -3,14 +3,10 @@ import os
import re
from subprocess import call, check_call
import threading
import time
from flask import Flask, request
from ironicclient import client, exceptions
app = Flask(__name__)
LOG = logging.getLogger("discoverd")
OS_ARGS = dict((k.lower(), v)
for (k, v) in os.environ.items()
@ -18,6 +14,10 @@ OS_ARGS = dict((k.lower(), v)
ALLOW_SEARCH_BY_MAC = True
def get_client():
return client.get_client(1, **OS_ARGS)
def is_valid_mac(address):
m = "[0-9a-f]{2}(:[0-9a-f]{2}){5}$"
return (isinstance(address, (str, unicode))
@ -54,7 +54,7 @@ def process(node_info):
dict((key, node_info.get(key))
for key in keys + ('ipmi_address',)))
ironic = client.get_client(1, **OS_ARGS)
ironic = get_client()
bmc_known = bool(node_info.get('ipmi_address'))
if bmc_known:
# TODO(dtantsur): bulk loading
@ -202,7 +202,7 @@ class Firewall(object):
def start(uuids):
"""Initiate discovery for given node uuids."""
ironic = client.get_client(1, **OS_ARGS)
ironic = get_client()
LOG.debug('Validating nodes %s', uuids)
nodes = []
for uuid in uuids:
@ -238,41 +238,3 @@ def start(uuids):
for node in nodes:
ironic.node.set_power_state(node.uuid, 'on')
@app.route('/continue', methods=['POST'])
def post_continue():
data = request.get_json(force=True)
LOG.debug("Got JSON %s, going into processing thread", data)
threading.Thread(target=process, args=(data,)).start()
return "{}", 202, {"content-type": "application/json"}
@app.route('/start', methods=['POST'])
def post_start():
data = request.get_json(force=True)
LOG.debug("Got JSON %s, going into processing thread", data)
threading.Thread(target=start, args=(data,)).start()
return "{}", 202, {"content-type": "application/json"}
def periodic_update(event, ironic):
while not event.is_set():
LOG.debug('Running periodic update of filters')
Firewall.update_filters(ironic)
for _ in range(15):
if event.is_set():
return
time.sleep(1)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
ironic = client.get_client(1, **OS_ARGS)
Firewall.init()
event = threading.Event()
threading.Thread(target=periodic_update, args=(event, ironic)).start()
try:
app.run(debug=True, host='0.0.0.0', port=5050)
finally:
LOG.info('Waiting for background thread to shutdown')
event.set()

11
setup.py

@ -0,0 +1,11 @@
from setuptools import setup
setup(
name = "ironic-discoverd",
version = "0.0.1",
author = "Dmitry Tantsur",
author_email = "dtansur@redhat.com",
packages=['ironic_discoverd'],
install_requires = ['Flask', 'python-ironicclient'],
)
Loading…
Cancel
Save