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