|
1
|
|
|
"""kytos - The kytos command line. |
|
2
|
|
|
|
|
3
|
|
|
You are at the "napps" command. |
|
4
|
|
|
|
|
5
|
|
|
Usage: |
|
6
|
|
|
kytos napps create |
|
7
|
|
|
kytos napps upload |
|
8
|
|
|
kytos napps delete <napp>... |
|
9
|
|
|
kytos napps list |
|
10
|
|
|
kytos napps install <napp>... |
|
11
|
|
|
kytos napps uninstall <napp>... |
|
12
|
|
|
kytos napps enable (all| <napp>...) |
|
13
|
|
|
kytos napps disable (all| <napp>...) |
|
14
|
|
|
kytos napps search <pattern> |
|
15
|
|
|
kytos napps -h | --help |
|
16
|
|
|
|
|
17
|
|
|
Options: |
|
18
|
|
|
|
|
19
|
|
|
-h, --help Show this screen. |
|
20
|
|
|
|
|
21
|
|
|
Common napps subcommands: |
|
22
|
|
|
|
|
23
|
|
|
create Create a bootstrap NApp structure for development. |
|
24
|
|
|
upload Upload current NApp to Kytos repository. |
|
25
|
|
|
delete Delete NApps from NApps Server. |
|
26
|
|
|
list List all NApps installed into your system. |
|
27
|
|
|
install Install a local or remote NApp into a controller. |
|
28
|
|
|
uninstall Remove a NApp from your controller. |
|
29
|
|
|
enable Enable a installed NApp. |
|
30
|
|
|
disable Disable a NApp. |
|
31
|
|
|
search Search for NApps in NApps Server. |
|
32
|
|
|
|
|
33
|
|
|
""" |
|
34
|
|
|
import re |
|
35
|
|
|
import sys |
|
36
|
|
|
|
|
37
|
|
|
from docopt import docopt |
|
38
|
|
|
|
|
39
|
|
|
from kytos.cli.commands.napps.api import NAppsAPI |
|
40
|
|
|
from kytos.utils.exceptions import KytosException |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
def parse(argv): |
|
44
|
|
|
"""Parse cli args.""" |
|
45
|
|
|
args = docopt(__doc__, argv=argv) |
|
46
|
|
|
try: |
|
47
|
|
|
call(sys.argv[2], args) |
|
48
|
|
|
except KytosException as e: |
|
49
|
|
|
print("Error parsing args: {}".format(e)) |
|
50
|
|
|
exit() |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def call(subcommand, args): |
|
54
|
|
|
"""Call a subcommand passing the args.""" |
|
55
|
|
|
args['<napp>'] = parse_napps(args['<napp>']) |
|
56
|
|
|
func = getattr(NAppsAPI, subcommand) |
|
57
|
|
|
func(args) |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def parse_napps(napp_ids): |
|
61
|
|
|
"""Return a list of tuples with username, napp_name and version. |
|
62
|
|
|
|
|
63
|
|
|
napp_ids elements are of the form username/name[:version] |
|
64
|
|
|
(version is optional). If no version is found, it will be None. |
|
65
|
|
|
|
|
66
|
|
|
If napp_ids is equal to 'all', this string will be returned. |
|
67
|
|
|
|
|
68
|
|
|
Args: |
|
69
|
|
|
napp_ids (list): NApps from the cli. |
|
70
|
|
|
|
|
71
|
|
|
Return: |
|
72
|
|
|
list: list of tuples with (username, napp_name, version). |
|
73
|
|
|
|
|
74
|
|
|
Raises: |
|
75
|
|
|
KytosException: If a NApp has not the form _username/name_. |
|
76
|
|
|
""" |
|
77
|
|
|
if 'all' in napp_ids: |
|
78
|
|
|
return 'all' |
|
79
|
|
|
|
|
80
|
|
|
return [parse_napp(napp_id) for napp_id in napp_ids] |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
def parse_napp(napp_id): |
|
84
|
|
|
"""Convert a napp_id in tuple with username, napp name and version. |
|
85
|
|
|
|
|
86
|
|
|
Args: |
|
87
|
|
|
napp_id: String with the form 'username/napp[:version]' (version is |
|
88
|
|
|
optional). If no version is found, it will be None. |
|
89
|
|
|
|
|
90
|
|
|
Returns: |
|
91
|
|
|
tuple: A tuple with (username, napp, version) |
|
92
|
|
|
|
|
93
|
|
|
Raises: |
|
94
|
|
|
KytosException: If a NApp has not the form _username/name_. |
|
95
|
|
|
""" |
|
96
|
|
|
# `napp_id` regex, composed by two mandatory parts (username, napp_name) |
|
97
|
|
|
# and one optional (version). |
|
98
|
|
|
# username and napp_name need to start with a letter, are composed of |
|
99
|
|
|
# letters, numbers and uderscores and must have at least three characters. |
|
100
|
|
|
# They are separated by a colon. |
|
101
|
|
|
# version is optional and can take any format. Is is separated by a hyphen, |
|
102
|
|
|
# if a version is defined. |
|
103
|
|
|
regex = r'([a-zA-Z][a-zA-Z0-9_]{2,})/([a-zA-Z][a-zA-Z0-9_]{2,}):?(.+)?' |
|
104
|
|
|
compiled_regex = re.compile(regex) |
|
105
|
|
|
|
|
106
|
|
|
matched = compiled_regex.fullmatch(napp_id) |
|
107
|
|
|
|
|
108
|
|
|
if not matched: |
|
109
|
|
|
msg = '"{}" NApp has not the form username/napp_name[:version].' |
|
110
|
|
|
raise KytosException(msg.format(napp_id)) |
|
111
|
|
|
|
|
112
|
|
|
return matched.groups() |
|
113
|
|
|
|