-
Notifications
You must be signed in to change notification settings - Fork 395
/
new_appliance.py
124 lines (101 loc) · 3.74 KB
/
new_appliance.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
#!/usr/bin/env python
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Create a new appliance from the terminal
"""
import json
import os
import sys
import uuid
def ask_multiple(question, options, optional=False):
while True:
for i, option in enumerate(options):
question += '\n{}) {}'.format(i + 1, option)
question += '\n'
answer = ask(question, type='integer', optional=optional)
if answer is None:
if optional:
return None
else:
if answer > 0 and answer <= len(options):
return options[answer - 1]
def yesno(question):
while True:
answer = ask(question + '[y/n]')
if answer in ['y', 'Y', 'yes']:
return True
if answer in ['n', 'N', 'no']:
return False
def ask(question, type='string', optional=False):
while True:
if optional:
sys.stdout.write(question + "(optional leave blank for skip) : ")
else:
sys.stdout.write(question + ": ")
sys.stdout.flush()
val = sys.stdin.readline().strip()
if len(val) == 0:
if optional:
return None
continue
if type == 'integer':
try:
val = int(val)
except ValueError:
continue
sys.stdout.write("\n")
return val
def ask_from_schema(schema):
data = {}
for key, val in schema['properties'].items():
if key == "appliance_id":
# generate an unique ID for the appliance
result = str(uuid.uuid4())
else:
optional = not key in schema['required']
result = None
if 'enum' in val:
result = ask_multiple(val['title'], val['enum'], optional=optional)
elif val['type'] in ('integer', 'string'):
result = ask(val['title'], type=val['type'], optional=optional)
if result:
data[key] = result
return data
with open(os.path.join('schemas', 'appliance_v5.json')) as f:
schema = json.load(f)
appliance_name = ask('Appliance filename (example: cisco-asav)')
# TODO check if file exists
with open(os.path.join('appliances', appliance_name + '.gns3a'), 'w+') as f:
appliance = {}
appliance = ask_from_schema(schema)
appliance['qemu'] = ask_from_schema(schema['properties']['qemu'])
appliance['images'] = []
files = []
while yesno('Add image?'):
image = ask_from_schema(schema['properties']['images']['items'])
appliance['images'].append(image)
files.append(image['filename'])
appliance['versions'] = []
while yesno('Add appliance version?'):
version = {'images': {}}
version['name'] = ask('Appliance version name')
for disk in ['hda_disk_image', 'hdb_disk_image', 'hdc_disk_image', 'hdd_disk_image', 'cdrom_image', 'initrd_image', 'kernel_image']:
img = ask_multiple('Image for ' + disk, files, optional=True)
if img:
version['images'][disk] = img
appliance['versions'].append(version)
json.dump(appliance, f, indent=4)