Skip to content
Snippets Groups Projects
Commit 2e9362c0 authored by Erik Reid's avatar Erik Reid
Browse files

use ssh_config as load_line_cards input param

parent 554110e8
No related branches found
No related tags found
No related merge requests found
...@@ -54,49 +54,7 @@ def _validate_config(_unused_ctx, _unused_param, file): ...@@ -54,49 +54,7 @@ def _validate_config(_unused_ctx, _unused_param, file):
raise click.BadParameter(e.message) raise click.BadParameter(e.message)
@click.group() def _save_router_info(fqdn, fpcs):
def cli():
pass
@click.command()
@click.option(
'--config',
required=True,
type=click.File('r'),
help='config filename',
callback=_validate_config)
@click.option(
'--fqdn',
required=True,
type=click.STRING,
help='config filename')
def create(config, fqdn):
"""
notes:
- first call init_db(dns=mysql_dsn(params from config))
- then create a session (as in unit test test_db_model.py)
- ... and perform the db queries
"""
router_config = config["router"]
line_cards = list(load_line_cards(fqdn, router_config))
# logger.info("Obtained the following hardware info about router: {0}"
# .format(router))
try:
validate(line_cards, LINE_CARDS_LIST_SCHEMA)
except ValidationError as e:
raise click.BadParameter(e.message)
mysql_config = config["mysql"]
dsn = mysql_dsn(mysql_config["username"],
mysql_config["password"],
mysql_config["hostname"],
mysql_config["dbname"])
logger.info("MYSQL DSN is: {0}".format(dsn))
init_db_model(dsn)
with session_scope(_callback_create) as session: with session_scope(_callback_create) as session:
node = model.Node(fqdn=fqdn) node = model.Node(fqdn=fqdn)
...@@ -133,6 +91,42 @@ def create(config, fqdn): ...@@ -133,6 +91,42 @@ def create(config, fqdn):
session.flush() session.flush()
logger.info("Registered port with id {0}".format(port.id)) logger.info("Registered port with id {0}".format(port.id))
@click.command()
@click.option(
'--config',
required=True,
type=click.File('r'),
help='config filename',
callback=_validate_config)
@click.option(
'--fqdn',
required=True,
type=click.STRING,
help='config filename')
def create(config, fqdn):
"""
notes:
- first call init_db(dns=mysql_dsn(params from config))
- then create a session (as in unit test test_db_model.py)
- ... and perform the db queries
"""
line_cards = list(load_line_cards(fqdn, config['ssh']))
# sanity check, shouldn't fail ... otherwise die badly
validate(line_cards, LINE_CARDS_LIST_SCHEMA)
mysql_config = config["mysql"]
dsn = mysql_dsn(mysql_config["username"],
mysql_config["password"],
mysql_config["hostname"],
mysql_config["dbname"])
logger.info("MYSQL DSN is: {0}".format(dsn))
init_db_model(dsn)
_save_router_info(fqdn, line_cards)
def _callback_create(): def _callback_create():
logger.info("Node successfully commited to database") logger.info("Node successfully commited to database")
......
...@@ -39,9 +39,9 @@ CONFIG_SCHEMA = { ...@@ -39,9 +39,9 @@ CONFIG_SCHEMA = {
'type': 'object', 'type': 'object',
'properties': { 'properties': {
'mysql': {'$ref': '#/definitions/database-credentials'}, 'mysql': {'$ref': '#/definitions/database-credentials'},
'router': {'$ref': '#/definitions/ssh-credentials'} 'ssh': {'$ref': '#/definitions/ssh-credentials'}
}, },
'required': ['mysql', 'router'], 'required': ['mysql', 'ssh'],
'additionalProperties': False 'additionalProperties': False
} }
......
...@@ -48,12 +48,21 @@ LINE_CARDS_LIST_SCHEMA = { ...@@ -48,12 +48,21 @@ LINE_CARDS_LIST_SCHEMA = {
} }
@contextlib.contextmanager @contextlib.contextmanager
def device(hostname, username, key_filename, port=830): def device(hostname, ssh_config, port=830):
dev = Device(
host=hostname, params = {
port=port, 'host': hostname,
user=username, 'port': port,
ssh_private_key_file=key_filename) 'user': ssh_config['username']
}
if 'private-key' in ssh_config:
params['ssh_private_key_file'] = ssh_config['private-key']
if 'password' in ssh_config:
params['passwd'] = ssh_config['password']
if 'ssh_config' in ssh_config:
params['ssh_config'] = ssh_config['ssh_config']
dev = Device(**params)
try: try:
dev.open() dev.open()
yield dev yield dev
...@@ -61,7 +70,7 @@ def device(hostname, username, key_filename, port=830): ...@@ -61,7 +70,7 @@ def device(hostname, username, key_filename, port=830):
dev.close() dev.close()
def load_line_cards(hostname, username, key_filename, port=830): def load_line_cards(hostname, ssh_config, port=830):
""" """
Loads all line cards from the given hostname Loads all line cards from the given hostname
...@@ -73,8 +82,7 @@ def load_line_cards(hostname, username, key_filename, port=830): ...@@ -73,8 +82,7 @@ def load_line_cards(hostname, username, key_filename, port=830):
TODO: change to take ssh* part of the config dict as input TODO: change to take ssh* part of the config dict as input
:param hostname: router hostname :param hostname: router hostname
:param username: router username :param ssh_config: the "ssh" element from the app config
:param key_filename: ssh private key filename
:param port: netconf port :param port: netconf port
:return: yields elements of the LINE_CARDS_LIST_SCHEMA :return: yields elements of the LINE_CARDS_LIST_SCHEMA
""" """
...@@ -82,8 +90,7 @@ def load_line_cards(hostname, username, key_filename, port=830): ...@@ -82,8 +90,7 @@ def load_line_cards(hostname, username, key_filename, port=830):
with device( with device(
hostname=hostname, hostname=hostname,
port=port, port=port,
username=username, ssh_config=ssh_config) as router:
key_filename=key_filename) as router:
phy_port_table = PhyPortTable(router) phy_port_table = PhyPortTable(router)
phy_port_table.get() phy_port_table.get()
...@@ -138,10 +145,13 @@ if __name__ == '__main__': ...@@ -138,10 +145,13 @@ if __name__ == '__main__':
# core2: 62.40.111.120:20830 # core2: 62.40.111.120:20830
# core3: 62.40.111.120:30830 # core3: 62.40.111.120:30830
for port in (10830, 20830, 30830): for port in (10830, 20830, 30830):
ssh = {
'username': 'ansible',
'private-key': '/Users/reid/.ssh/eve_ng_ansible_rsa'
}
fpcs = load_line_cards( fpcs = load_line_cards(
hostname='62.40.111.120', hostname='62.40.111.120',
port=port, port=port,
username='ansible', ssh_config=ssh)
key_filename='/Users/reid/.ssh/eve_ng_ansible_rsa')
fpcs = list(fpcs) fpcs = list(fpcs)
print(json.dumps(fpcs, indent=2)) print(json.dumps(fpcs, indent=2))
...@@ -68,35 +68,6 @@ HOSTNAMES = [ ...@@ -68,35 +68,6 @@ HOSTNAMES = [
] ]
@pytest.fixture
def config_data():
return {
'mysql': {
'username': 'bogus-user',
'password': 'bogus-password',
'hostname': 'bogus hostname',
'dbname': 'bogus database name'
},
'router': {}
}
@pytest.fixture
def config_filename(config_data):
# for os indepence see:
# https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file
# Generate a random temporary file name
file_name = os.path.join(tempfile.gettempdir(), os.urandom(24).hex())
# Ensure the file is created
open(file_name, "x").close()
# Open the file in the given mode
tempFile = open(file_name, 'w')
tempFile .write(json.dumps(config_data))
tempFile .flush()
yield tempFile .name
@pytest.fixture @pytest.fixture
def resources_db(): def resources_db():
...@@ -178,3 +149,35 @@ def mocked_router_input_tables(router_name): ...@@ -178,3 +149,35 @@ def mocked_router_input_tables(router_name):
'FpcHwTable': _load_pyez_json_table(router_name, 'FpcHwTable'), 'FpcHwTable': _load_pyez_json_table(router_name, 'FpcHwTable'),
'PhyPortTable': _load_pyez_json_table(router_name, 'PhyPortTable') 'PhyPortTable': _load_pyez_json_table(router_name, 'PhyPortTable')
} }
@pytest.fixture
def config_data(router_name):
return {
'mysql': {
'username': 'bogus-user',
'password': 'bogus-password',
'hostname': 'bogus hostname',
'dbname': 'bogus database name'
},
'ssh': {
'username': 'another-bogus-user',
'private-key': 'bogus private key filename'
}
}
@pytest.fixture
def config_filename(config_data, router_name):
# for os indepence see:
# https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file
# Generate a random temporary file name
file_name = os.path.join(tempfile.gettempdir(), os.urandom(24).hex())
# Ensure the file is created
open(file_name, "x").close()
# Open the file in the given mode
tempFile = open(file_name, 'w')
tempFile .write(json.dumps(config_data))
tempFile .flush()
yield tempFile .name
...@@ -6,8 +6,11 @@ from resource_management.hardware.router \ ...@@ -6,8 +6,11 @@ from resource_management.hardware.router \
def test_load_line_cards(mocked_router, mocked_router_input_tables): def test_load_line_cards(mocked_router, mocked_router_input_tables):
fpcs = load_line_cards( ssh = {
hostname='bogus', username='bogus', key_filename='no file') 'username': 'bogus',
'private-key': 'no file'
}
fpcs = load_line_cards(hostname='bogus', ssh_config=ssh)
fpcs = list(fpcs) fpcs = list(fpcs)
validate(fpcs, LINE_CARDS_LIST_SCHEMA) validate(fpcs, LINE_CARDS_LIST_SCHEMA)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment