Skip to content
This repository was archived by the owner on Apr 27, 2022. It is now read-only.

Commit b953bf2

Browse files
author
Sourabh Bollapragada
committed
Finished CLI and Logging (Ready for PR)
1 parent 885f17f commit b953bf2

File tree

10 files changed

+83
-61
lines changed

10 files changed

+83
-61
lines changed

bmi_config.cfg

-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ url = <base url for haas>
2424
# This section is for iscsi related config
2525
[iscsi]
2626
ip = <ip of iscsi server>
27-
# will be replaced with python script
28-
update_shell_url = <location of iscsci_update shell script>
2927
password = <sudo password for iscsi_update script>
3028

3129
# this section is for rpc server config

ims/cli/cli.py

+37-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
_password = os.environ[constants.HAAS_PASSWORD_VARIABLE]
2323

2424

25+
def bmi_exception_wrapper(func):
26+
def function_wrapper(*args,**kwargs):
27+
try:
28+
return func(*args,**kwargs)
29+
except BMIException as e:
30+
click.echo(str(e))
31+
32+
return function_wrapper
33+
2534
@click.group()
2635
def cli():
2736
"""
@@ -98,6 +107,7 @@ def deprovision(project, node, network, nic):
98107
@cli.command(name='showpro',
99108
short_help='Lists Provisioned Nodes')
100109
@click.argument(constants.PROJECT_PARAMETER)
110+
@bmi_exception_wrapper
101111
def list_provisioned_nodes(project):
102112
"""
103113
Lists Provisioned Nodes under a Project.
@@ -243,14 +253,15 @@ def project_grp():
243253

244254

245255
@project_grp.command(name='ls', short_help='Lists Projects')
256+
@bmi_exception_wrapper
246257
def list_projects():
247258
"""
248259
Lists Projects From DB
249260
250261
\b
251262
WARNING = User Must be An Admin
252263
"""
253-
with BMI(_username, _password, "bmi_admin") as bmi:
264+
with BMI(_username, _password, constants.BMI_ADMIN_PROJECT) as bmi:
254265
ret = bmi.list_projects()
255266
if ret[constants.STATUS_CODE_KEY] == 200:
256267
table = PrettyTable(field_names=["Id", "Name", "Provision Network"])
@@ -266,6 +277,7 @@ def list_projects():
266277
@click.argument(constants.PROJECT_PARAMETER)
267278
@click.argument(constants.NETWORK_PARAMETER)
268279
@click.option('--id', default=None, help='Specify what id to use for project')
280+
@bmi_exception_wrapper
269281
def add_project(project, network, id):
270282
"""
271283
Create Project in DB
@@ -278,7 +290,7 @@ def add_project(project, network, id):
278290
PROJECT = The Name of Project (A HIL Project must exist)
279291
NETWORK = The Name of the Provisioning Network
280292
"""
281-
with BMI(_username, _password, "bmi_admin") as bmi:
293+
with BMI(_username, _password, constants.BMI_ADMIN_PROJECT) as bmi:
282294
ret = bmi.add_project(project, network, id)
283295
if ret[constants.STATUS_CODE_KEY] == 200:
284296
click.echo("Success")
@@ -288,6 +300,7 @@ def add_project(project, network, id):
288300

289301
@project_grp.command(name='rm', help='Deletes Project From DB')
290302
@click.argument(constants.PROJECT_PARAMETER)
303+
@bmi_exception_wrapper
291304
def delete_project(project):
292305
"""
293306
Remove Project From DB
@@ -299,7 +312,7 @@ def delete_project(project):
299312
Arguments:
300313
PROJECT = The Name of Project (A HIL Project must exist)
301314
"""
302-
with BMI(_username, _password, "bmi_admin") as bmi:
315+
with BMI(_username, _password, constants.BMI_ADMIN_PROJECT) as bmi:
303316
ret = bmi.delete_project(project)
304317
if ret[constants.STATUS_CODE_KEY] == 200:
305318
click.echo("Success")
@@ -318,6 +331,7 @@ def db():
318331
@db.command(name='rm', help='Deletes Image From DB')
319332
@click.argument(constants.PROJECT_PARAMETER)
320333
@click.argument(constants.IMAGE_NAME_PARAMETER)
334+
@bmi_exception_wrapper
321335
def delete_image(project, img):
322336
"""
323337
Delete Image in DB
@@ -330,7 +344,7 @@ def delete_image(project, img):
330344
PROJECT = The Name of Project
331345
IMG = The Name of the Image to insert
332346
"""
333-
with BMI(_username, _password, "bmi_admin") as bmi:
347+
with BMI(_username, _password, constants.BMI_ADMIN_PROJECT) as bmi:
334348
ret = bmi.delete_image(project, img)
335349
if ret[constants.STATUS_CODE_KEY] == 200:
336350
click.echo("Success")
@@ -345,6 +359,7 @@ def delete_image(project, img):
345359
@click.option('--snap', is_flag=True, help='If image is snapshot')
346360
@click.option('--parent', default=None, help='Specify parent name')
347361
@click.option('--public', is_flag=True, help='If image is public')
362+
@bmi_exception_wrapper
348363
def add_image(project, img, id, snap, parent, public):
349364
"""
350365
Create Image in DB
@@ -357,7 +372,7 @@ def add_image(project, img, id, snap, parent, public):
357372
PROJECT = The Name of Project (A HIL Project must exist)
358373
IMG = The Name of the Image to insert
359374
"""
360-
with BMI(_username, _password, "bmi_admin") as bmi:
375+
with BMI(_username, _password, constants.BMI_ADMIN_PROJECT) as bmi:
361376
ret = bmi.add_image(project, img, id, snap, parent, public)
362377
if ret[constants.STATUS_CODE_KEY] == 200:
363378
click.echo("Success")
@@ -372,6 +387,7 @@ def add_image(project, img, id, snap, parent, public):
372387
@click.option('--project', default=None, help='Filter By Project')
373388
@click.option('--name', default=None, help='Filter By Name')
374389
@click.option('--ceph', default=None, help="Filter By Ceph Name")
390+
@bmi_exception_wrapper
375391
def list_all_images(s, c, p, project, name, ceph):
376392
"""
377393
List All Image Present in DB
@@ -400,7 +416,7 @@ def second_filter():
400416

401417
return (f1 and f2 and f3) or f4
402418

403-
with BMI(_username, _password, "bmi_admin") as bmi:
419+
with BMI(_username, _password, constants.BMI_ADMIN_PROJECT) as bmi:
404420
ret = bmi.list_all_images()
405421
if ret[constants.STATUS_CODE_KEY] == 200:
406422
table = PrettyTable(
@@ -430,6 +446,7 @@ def second_filter():
430446
@click.argument(constants.PROJECT_PARAMETER)
431447
@click.argument(constants.IMAGE_NAME_PARAMETER)
432448
@click.option('--snap', default=None, help='Specifies what snapshot to import')
449+
@bmi_exception_wrapper
433450
def import_ceph_image(project, img, snap):
434451
"""
435452
Import an existing CEPH image into BMI
@@ -453,10 +470,11 @@ def import_ceph_image(project, img, snap):
453470

454471

455472
@cli.command(name='cp', help="Copy an existing image not clones")
456-
@click.argument('src_project')
457-
@click.argument('img1')
458-
@click.argument('dest_project')
459-
@click.argument('img2', default=None)
473+
@click.argument(constants.SRC_PROJECT_PARAMETER)
474+
@click.argument(constants.IMAGE1_NAME_PARAMETER)
475+
@click.argument(constants.DEST_PROJECT_PARAMETER)
476+
@click.argument(constants.IMAGE2_NAME_PARAMETER, default=None)
477+
@bmi_exception_wrapper
460478
def copy_image(src_project, img1, dest_project, img2):
461479
"""
462480
Copy an image from one project to another
@@ -477,10 +495,11 @@ def copy_image(src_project, img1, dest_project, img2):
477495

478496

479497
@cli.command(name='mv', help='Move Image From Project to Another')
480-
@click.argument('src_project')
481-
@click.argument('img1')
482-
@click.argument('dest_project')
483-
@click.argument('img2', default=None)
498+
@click.argument(constants.SRC_PROJECT_PARAMETER)
499+
@click.argument(constants.IMAGE1_NAME_PARAMETER)
500+
@click.argument(constants.DEST_PROJECT_PARAMETER)
501+
@click.argument(constants.IMAGE2_NAME_PARAMETER, default=None)
502+
@bmi_exception_wrapper
484503
def move_image(src_project, img1, dest_project, img2):
485504
"""
486505
Move an image from one project to another
@@ -508,6 +527,7 @@ def node():
508527
@node.command('ip', help='Get IP on Provisioning Network')
509528
@click.argument(constants.PROJECT_PARAMETER)
510529
@click.argument(constants.NODE_NAME_PARAMETER)
530+
@bmi_exception_wrapper
511531
def get_node_ip(project, node):
512532
"""
513533
Get the IP of Provisioned Node on Provisioning Network
@@ -533,6 +553,7 @@ def iscsi():
533553
@iscsi.command(name='create', help='Create ISCSI Mapping')
534554
@click.argument(constants.PROJECT_PARAMETER)
535555
@click.argument(constants.IMAGE_NAME_PARAMETER)
556+
@bmi_exception_wrapper
536557
def create_mapping(project, img):
537558
"""
538559
Mount image on iscsi server
@@ -556,6 +577,7 @@ def create_mapping(project, img):
556577
@iscsi.command(name='rm', help='Remove ISCSI Mapping')
557578
@click.argument(constants.PROJECT_PARAMETER)
558579
@click.argument(constants.IMAGE_NAME_PARAMETER)
580+
@bmi_exception_wrapper
559581
def delete_mapping(project, img):
560582
"""
561583
Unmount image from iscsi server
@@ -578,6 +600,7 @@ def delete_mapping(project, img):
578600

579601
@iscsi.command(name='ls', help='Show ISCSI Mappings')
580602
@click.argument(constants.PROJECT_PARAMETER)
603+
@bmi_exception_wrapper
581604
def show_mappings(project):
582605
"""
583606
Show mounted images on iscsi

ims/common/config.py

-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def __init__(self, filename):
3636
self.fs = {}
3737
self.uid = None
3838
self.db_url = None
39-
self.iscsi_update = None
4039
self.iscsi_update_password = None
4140
self.iscsi_ip = None
4241
self.haas_url = None
@@ -64,9 +63,6 @@ def parse_config(self):
6463
self.db_url = config.get(constants.DB_CONFIG_SECTION_NAME,
6564
constants.DB_URL_KEY)
6665

67-
self.iscsi_update = config.get(constants.ISCSI_CONFIG_SECTION_NAME,
68-
constants.ISCSI_URL_KEY)
69-
7066
self.iscsi_update_password = config.get(
7167
constants.ISCSI_CONFIG_SECTION_NAME,
7268
constants.ISCSI_PASSWORD_KEY)

ims/common/constants.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
# Non FS Keys in Config File
2727
HAAS_URL_KEY = 'url'
28-
ISCSI_URL_KEY = 'update_shell_url'
2928
ISCSI_PASSWORD_KEY = 'password'
3029
ISCSI_IP_KEY = 'ip'
3130

@@ -86,6 +85,10 @@
8685
IMAGE_NAME_PARAMETER = "img"
8786
SNAP_NAME_PARAMETER = "snap_name"
8887
PROJECT_PARAMETER = "project"
88+
SRC_PROJECT_PARAMETER = 'src_project'
89+
DEST_PROJECT_PARAMETER = "dest_project"
90+
IMAGE1_NAME_PARAMETER = "img1"
91+
IMAGE2_NAME_PARAMETER = "img2"
8992
NETWORK_PARAMETER = "network"
9093
NIC_PARAMETER = "nic"
9194
CHANNEL_PARAMETER = "channel"
@@ -105,5 +108,9 @@
105108
IET_TARGET_STARTING = 'Target'
106109
IET_LUN_STARTING = "Lun"
107110

111+
DNSMASQ_LEASES_LOC = '/var/lib/misc/dnsmasq.leases'
112+
108113
HAAS_CALL_TIMEOUT = 5
109114
DEFAULT_SNAPSHOT_NAME = "snapshot"
115+
116+
BMI_ADMIN_PROJECT = "bmi_infra"

ims/common/log.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def func_wrapper(*args, **kwargs):
3131
ret = func(*args, **kwargs)
3232
if func.__name__ == "__init__":
3333
logger.info(base_msg + "Successfully Initialised %s instance",
34-
type(list(args)[0]).__name__, extra={'special': True})
34+
list(args)[0].__class__.__name__, extra={'special': True})
3535
else:
3636
logger.info(base_msg + "Successfully Executed %s",
3737
func.__name__, extra={'special': True})

ims/einstein/dnsmasq.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from ims.exception.dhcp_exceptions import *
2-
2+
import ims.common.constants as constants
33

44
class DNSMasq:
55
def get_ip(self, mac_addr):
6-
with open('/var/lib/misc/dnsmasq.leases', 'r') as file:
6+
with open(constants.DNSMASQ_LEASES_LOC, 'r') as file:
77
for line in file:
88
parts = line.strip().split(' ')
99
if parts[1] == mac_addr and parts[4] == '01:' + mac_addr:

ims/einstein/hil.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
logger = create_logger(__name__)
1111

1212

13-
class HaaS:
13+
class HIL:
1414
class Request:
1515
def __init__(self, method, data, auth=None):
1616
self.method = method
@@ -71,14 +71,14 @@ def __init__(self, base_url, usr, passwd):
7171
@trace
7272
def __call_rest_api(self, api):
7373
link = urlparse.urljoin(self.base_url, api)
74-
request = HaaS.Request('get', None, auth=(self.usr, self.passwd))
75-
return HaaS.Communicator(link, request).send_request()
74+
request = HIL.Request('get', None, auth=(self.usr, self.passwd))
75+
return HIL.Communicator(link, request).send_request()
7676

7777
@trace
7878
def __call_rest_api_with_body(self, api, body):
7979
link = urlparse.urljoin(self.base_url, api)
80-
request = HaaS.Request('post', body, auth=(self.usr, self.passwd))
81-
return HaaS.Communicator(link, request).send_request()
80+
request = HIL.Request('post', body, auth=(self.usr, self.passwd))
81+
return HIL.Communicator(link, request).send_request()
8282

8383
@log
8484
def list_free_nodes(self):

ims/einstein/iscsi.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import subprocess
22

33
import os
4+
import sh
45

56
import ims.common.constants as constants
67
from ims.exception import *
@@ -82,9 +83,7 @@ def __execute_unmap(self, rbd_name):
8283
output, err = p.communicate()
8384

8485
def __execute_showmapped(self):
85-
p = subprocess.Popen('rbd showmapped', shell=True,
86-
stdout=subprocess.PIPE)
87-
output, err = p.communicate()
86+
output = sh.rbd("showmapped")
8887
lines = output.split('\n')[1:-1]
8988
maps = {}
9089
for line in lines:

0 commit comments

Comments
 (0)