-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue883/add check for different front end (#933)
* Create client database and populate with the host that beeflow is running on and prevent beeflow commands (start, status, reset, and stop) to work on a different front end
- Loading branch information
Showing
4 changed files
with
152 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""Client database code.""" | ||
|
||
from collections import namedtuple | ||
|
||
from beeflow.common.db import bdb | ||
|
||
|
||
class ClientInfo: | ||
"""Client Info object.""" | ||
|
||
def __init__(self, db_file): | ||
"""Initialize info and db file.""" | ||
self.Info = namedtuple("Info", "id hostname") # noqa Snake Case | ||
self.db_file = db_file | ||
|
||
def set_hostname(self, new_hostname): | ||
"""Set hostname for current front end.""" | ||
stmt = "UPDATE info set hostname=?" | ||
bdb.run(self.db_file, stmt, [new_hostname]) | ||
|
||
def get_hostname(self): | ||
"""Return hostname for current front end.""" | ||
stmt = "SELECT hostname FROM info" | ||
result = bdb.getone(self.db_file, stmt)[0] | ||
hostname = result | ||
return hostname | ||
|
||
|
||
class ClientDB: | ||
"""Client database.""" | ||
|
||
def __init__(self, db_file): | ||
"""Construct a new client database connection.""" | ||
self.db_file = db_file | ||
self._init_tables() | ||
|
||
def _init_tables(self): | ||
"""Initialize the client table if it doesn't exist.""" | ||
info_stmt = """CREATE TABLE IF NOT EXISTS info ( | ||
id INTEGER PRIMARY KEY ASC, | ||
hostname TEXT);""" | ||
if not bdb.table_exists(self.db_file, 'info'): | ||
bdb.create_table(self.db_file, info_stmt) | ||
# insert a new workflow into the database | ||
stmt = """INSERT INTO info (hostname) VALUES(?);""" | ||
tmp = "" | ||
bdb.run(self.db_file, stmt, [tmp]) | ||
|
||
@property | ||
def info(self): | ||
"""Get info from the database.""" | ||
return ClientInfo(self.db_file) | ||
|
||
|
||
def open_db(db_file): | ||
"""Open and return a new database.""" | ||
return ClientDB(db_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Tests of the client database.""" | ||
import tempfile | ||
import os | ||
|
||
import pytest | ||
|
||
from beeflow.common.db import client_db | ||
|
||
|
||
@pytest.fixture | ||
def temp_db(): | ||
"""Create a fixture for making a temporary database.""" | ||
fname = tempfile.mktemp() | ||
db = client_db.open_db(fname) | ||
yield db | ||
os.remove(fname) | ||
|
||
|
||
def test_empty(temp_db): | ||
"""Test the empty database.""" | ||
db = temp_db | ||
|
||
host_name = db.info.get_hostname() | ||
assert host_name == "" | ||
|
||
|
||
def test_info(temp_db): | ||
"""Test setting the info.""" | ||
db = temp_db | ||
|
||
db.info.set_hostname('front_end_name') | ||
host_name = db.info.get_hostname() | ||
|
||
assert host_name == 'front_end_name' | ||
# Ignore W0621: PyLama complains about redefining 'temp_db' from the outer | ||
# scope. This is how pytest fixtures work. | ||
# pylama:ignore=W0621 |