-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsucli.py
175 lines (147 loc) · 4.9 KB
/
tsucli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# !/usr/bin/env python
# coding: utf-8
#
# Copyright 2020 Tsecond Inc. All Rights Reserved.
#
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential
#
# Written by: Manavalan Krishnan 11/12/20
#
import click
from tsulib import Bryck, get_version, get_tsutil_name, set_log_level
import sys
class Options(object):
""" Global options for the CLI commands"""
def __init__(self, verbose=False,json=False):
self.verbose = verbose
self.json = json
# Command group for bryck management
@click.group()
def bryck():
""" Bryck Management"""
pass
# Command group for system management
@click.group()
def system():
""" System Management """
pass
# Command group for CLI
@click.group()
@click.pass_context
@click.option("--verbose", is_flag=True, help="Enable verbose logs")
@click.option("--json", is_flag=True, help="Enable output in JSON format")
@click.version_option(get_version(), prog_name=get_tsutil_name())
def cli(ctx, verbose,json):
ctx.obj = Options(verbose,json)
pass
# Bryck format sub command definition
@bryck.command()
@click.pass_obj
@click.option("--no_enc", is_flag=True, help="Disable data encryption")
@click.option("--key_file", type=click.Path(exists=True),
help="Secret key file")
@click.option("--no_erase", is_flag=True, help="Disable secure erasing of data")
@click.option("--raid_chunk", default=1,
help="Raid chunk size in MBs. Default 1MB")
@click.option("--raid_level", type=click.Choice(['0', '5', '6']), default='5',
help="Raid level for data protection. Default 5")
@click.confirmation_option(prompt='\n**Formatting deletes Bryck content' +
'permanently**\n**Do you want to continue?')
def format(obj, no_enc, no_erase, raid_chunk, raid_level, key_file):
""" Format the Bryck """
set_log_level(obj.verbose)
rc, msg = Bryck(obj.verbose).format(no_auth=False, no_enc=no_enc,
no_erase=no_erase,
raid_chunk=raid_chunk,
raid_level=int(raid_level),
key_file=key_file)
if not rc:
msg = "Bryck formatted"
click.echo(msg)
sys.exit(rc)
# Bryck mount sub command definition
@bryck.command()
@click.pass_obj
@click.option("--key_file", type=click.Path(), help="Secret key file")
@click.argument("mount_dir", type=click.Path(exists=True))
def mount(obj, key_file, mount_dir):
""" Mount the Bryck """
set_log_level(obj.verbose)
rc, msg = Bryck(obj.verbose).mount(key_file=key_file, mount_dir=mount_dir)
if not rc:
msg = "Bryck mounted"
click.echo(msg)
sys.exit(rc)
# Bryck eject sub command definition
@bryck.command()
@click.pass_obj
def eject(obj):
""" Eject the Bryck """
set_log_level(obj.verbose)
rc, msg = Bryck(obj.verbose).eject()
if not rc:
msg = "Bryck ejected"
click.echo(msg)
sys.exit(rc)
# Bryck eject sub command definition
@bryck.command()
@click.pass_obj
@click.confirmation_option(prompt='\n**Erasing deletes Bryck content ' +
'permanently**\n**Do you want to continue?')
def erase(obj):
""" Erase the Bryck data """
set_log_level(obj.verbose)
rc, msg = Bryck(obj.verbose).erase()
if not rc:
msg = "Bryck erased"
click.echo(msg)
sys.exit(rc)
# Bryck setkey sub command definition
@bryck.command()
@click.pass_obj
@click.argument("old_key", type=click.Path(exists=True))
@click.argument("new_key", type=click.Path(exists=True))
def setkey(obj, old_key, new_key):
""" Change the encryption key """
set_log_level(obj.verbose)
rc, msg = Bryck(obj.verbose).setkey(old_key=old_key, new_key=new_key)
if not rc:
msg = "Secret key changed"
click.echo(msg)
sys.exit(rc)
# Bryck info sub command definition
@bryck.command()
@click.pass_obj
def info(obj):
""" Display Bryck information"""
set_log_level(obj.verbose)
rc, binfo = Bryck(obj.verbose).info()
if rc:
click.echo("Bryck not found")
else:
if(obj.json):
""" Print in JSON format"""
binfo = dict(map(lambda x: x.split(':',1), binfo.split('\n')[:-1]))
click.echo(binfo)
sys.exit(rc)
# Bryck list sub command definition
@bryck.command()
@click.pass_obj
def list(obj):
""" Display Bryck information"""
set_log_level(obj.verbose)
rc, binfo = Bryck(obj.verbose).list()
if rc:
click.echo("Bryck not found")
else:
click.echo(binfo)
sys.exit(rc)
def build_cli():
""" Builds CLI sub commands"""
cli.add_command(bryck)
cli.add_command(system)
def run_cli():
""" Runs the CLI"""
build_cli()
cli()