Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cyber-sandbox-creator
Manage
Activity
Members
Labels
Plan
Issues
10
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MUNI-KYPO-CSC
cyber-sandbox-creator
Commits
fd92116c
Commit
fd92116c
authored
5 years ago
by
Attila Farkas
Browse files
Options
Downloads
Patches
Plain Diff
refactoring, add netmask support
parent
15b80dc1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!1
Devel
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
generate.py
+16
-16
16 additions, 16 deletions
generate.py
modules/network_parser.py
+28
-0
28 additions, 0 deletions
modules/network_parser.py
with
44 additions
and
16 deletions
generate.py
+
16
−
16
View file @
fd92116c
...
@@ -4,6 +4,8 @@ import sys
...
@@ -4,6 +4,8 @@ import sys
import
yaml
import
yaml
import
jinja2
import
jinja2
from
modules.network_parser
import
add_networks
if
(
len
(
sys
.
argv
)
!=
2
):
if
(
len
(
sys
.
argv
)
!=
2
):
print
(
"
Error: Expecting 1 argument (yml file).
"
)
print
(
"
Error: Expecting 1 argument (yml file).
"
)
sys
.
exit
();
sys
.
exit
();
...
@@ -26,17 +28,20 @@ def do_mapping(name, command_type):
...
@@ -26,17 +28,20 @@ def do_mapping(name, command_type):
def
add_formated_command
(
key
,
value
):
def
add_formated_command
(
key
,
value
):
if
(
key
in
vagrant_mappings
[
'
string
'
]):
if
(
key
in
vagrant_mappings
[
'
string
'
]):
devices
[
host
[
'
name
'
]].
append
(
'
device.
'
+
str
(
do_mapping
(
key
,
'
string
'
))
+
'
=
\"
'
+
str
(
value
)
+
'
\"
'
)
devices
[
host
[
'
name
'
]].
append
(
'
device.
'
+
str
(
do_mapping
(
key
,
'
string
'
))
+
'
=
\"
'
+
str
(
value
)
+
'
\"
'
)
elif
(
key
in
vagrant_mappings
[
'
integer
'
]):
elif
(
key
in
vagrant_mappings
[
'
integer
'
]):
devices
[
host
[
'
name
'
]].
append
(
'
device.
'
+
str
(
do_mapping
(
key
,
'
integer
'
))
+
'
=
'
+
str
(
value
))
devices
[
host
[
'
name
'
]].
append
(
'
device.
'
+
str
(
do_mapping
(
key
,
'
integer
'
))
+
'
=
'
+
str
(
value
))
elif
(
key
in
vagrant_mappings
[
'
boolean
'
]):
elif
(
key
in
vagrant_mappings
[
'
boolean
'
]):
devices
[
host
[
'
name
'
]].
append
(
'
device.
'
+
str
(
do_mapping
(
key
,
'
boolean
'
))
+
'
=
'
+
str
(
value
).
lower
())
devices
[
host
[
'
name
'
]].
append
(
'
device.
'
+
str
(
do_mapping
(
key
,
'
boolean
'
))
+
'
=
'
+
str
(
value
).
lower
())
def
find_ip
(
name
):
for
net_mapping
in
yml
[
'
net_mappings
'
]:
if
(
net_mapping
[
'
host
'
]
==
name
):
return
net_mapping
[
'
ip
'
]
# Building "devices" structure that will be passed to Jinja2
devices
=
{}
devices
=
{}
for
host
in
yml
[
'
hosts
'
]:
for
host
in
yml
[
'
hosts
'
]:
...
@@ -44,18 +49,13 @@ for host in yml['hosts']:
...
@@ -44,18 +49,13 @@ for host in yml['hosts']:
for
key
,
value
in
host
.
items
():
for
key
,
value
in
host
.
items
():
if
(
key
!=
'
name
'
):
if
(
key
!=
'
name
'
):
add_formated_command
(
key
,
value
)
add_formated_command
(
key
,
value
)
if
(
yml
[
'
net_mappings
'
]):
add_networks
(
host
[
'
name
'
],
yml
,
devices
)
devices
[
host
[
'
name
'
]].
append
(
'
device.vm.network :private_network, ip:
\"
'
+
str
(
find_ip
(
host
[
'
name
'
]))
+
'
\"
'
)
# Generating output via Jinja2
templateLoader
=
jinja2
.
FileSystemLoader
(
searchpath
=
"
templates
"
)
templateLoader
=
jinja2
.
FileSystemLoader
(
searchpath
=
"
templates
"
)
templateEnv
=
jinja2
.
Environment
(
loader
=
templateLoader
,
trim_blocks
=
True
)
templateEnv
=
jinja2
.
Environment
(
loader
=
templateLoader
,
trim_blocks
=
True
)
baseTemplate
=
templateEnv
.
get_template
(
"
base
"
)
baseTemplate
=
templateEnv
.
get_template
(
"
base
"
)
output
=
baseTemplate
.
render
(
devices
=
devices
)
output
=
baseTemplate
.
render
(
devices
=
devices
)
print
(
output
.
encode
(
'
utf-8
'
))
print
(
output
)
#vagrantfile = open('Vagrantfile','w')
#vagrantfile.write(output)
This diff is collapsed.
Click to expand it.
modules/network_parser.py
0 → 100644
+
28
−
0
View file @
fd92116c
def
_find_ip
(
hostname
,
net_mappings
):
for
net_mapping
in
net_mappings
:
if
(
net_mapping
[
'
host
'
]
==
hostname
):
return
net_mapping
[
'
ip
'
]
def
_add_ip
(
hostname
,
net_mappings
,
devices
):
devices
[
hostname
].
append
(
'
device.vm.network :private_network, ip:
\"
'
+
str
(
_find_ip
(
hostname
,
net_mappings
))
+
'
\"
'
)
def
_add_netmask
(
hostname
,
my_network
,
networks
,
devices
):
for
network
in
networks
:
if
(
network
[
'
name
'
]
==
my_network
):
address
,
mask
=
network
[
'
cidr
'
].
split
(
'
/
'
)
devices
[
hostname
][
-
1
]
+=
(
'
, netmask:
'
+
mask
)
def
add_networks
(
hostname
,
yml
,
devices
):
if
(
not
yml
[
'
net_mappings
'
]):
return
for
mapping
in
yml
[
'
net_mappings
'
]:
if
(
mapping
[
'
host
'
]
==
hostname
):
_add_ip
(
hostname
,
yml
[
'
net_mappings
'
],
devices
)
if
(
yml
[
'
networks
'
]):
_add_netmask
(
hostname
,
mapping
[
'
network
'
],
yml
[
'
networks
'
],
devices
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment