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
e187cb1e
Commit
e187cb1e
authored
5 years ago
by
Attila Farkas
Browse files
Options
Downloads
Patches
Plain Diff
simplify file generation from template
parent
6bc615ba
No related branches found
No related tags found
1 merge request
!7
Resolve Refactoring
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
modules/ansible_generator.py
+8
-21
8 additions, 21 deletions
modules/ansible_generator.py
modules/file_manager.py
+15
-11
15 additions, 11 deletions
modules/file_manager.py
modules/vagrant_generator.py
+2
-13
2 additions, 13 deletions
modules/vagrant_generator.py
with
25 additions
and
45 deletions
modules/ansible_generator.py
+
8
−
21
View file @
e187cb1e
from
modules.file_manager
import
load_template
,
generate_file
from
modules.file_manager
import
generate_file
def
_create_inventory
(
input_definitions
):
def
_create_inventory
(
input_definitions
):
"""
Creates an inventory file with host groups.
"""
"""
Creates an inventory file with host groups.
"""
...
@@ -11,9 +11,7 @@ def _create_inventory(input_definitions):
...
@@ -11,9 +11,7 @@ def _create_inventory(input_definitions):
for
router
in
input_definitions
[
"
routers
"
]:
for
router
in
input_definitions
[
"
routers
"
]:
router_names
.
append
(
router
[
"
name
"
])
router_names
.
append
(
router
[
"
name
"
])
inventory_template
=
load_template
(
"
inventory
"
)
generate_file
(
"
inventory
"
,
"
provisioning/inventory.ini
"
,
hosts
=
host_names
,
routers
=
router_names
)
output
=
inventory_template
.
render
(
hosts
=
host_names
,
routers
=
router_names
)
generate_file
(
"
provisioning/inventory.ini
"
,
output
)
def
_create_config
(
input_definitions
,
flags
):
def
_create_config
(
input_definitions
,
flags
):
...
@@ -31,9 +29,7 @@ def _create_config(input_definitions, flags):
...
@@ -31,9 +29,7 @@ def _create_config(input_definitions, flags):
new_router
[
"
name
"
]
=
router
[
"
name
"
]
new_router
[
"
name
"
]
=
router
[
"
name
"
]
routers
.
append
(
new_router
)
routers
.
append
(
new_router
)
config_template
=
load_template
(
"
config
"
)
generate_file
(
"
config
"
,
"
base_provisioning/config.yml
"
,
hosts
=
hosts
,
routers
=
routers
)
output
=
config_template
.
render
(
hosts
=
hosts
,
routers
=
routers
)
generate_file
(
"
base_provisioning/config.yml
"
,
output
)
def
_create_config_playbooks
(
input_definitions
,
flags
):
def
_create_config_playbooks
(
input_definitions
,
flags
):
"""
Generates playbooks and roles for basic device configuration.
"""
"""
Generates playbooks and roles for basic device configuration.
"""
...
@@ -44,24 +40,15 @@ def _create_config_playbooks(input_definitions, flags):
...
@@ -44,24 +40,15 @@ def _create_config_playbooks(input_definitions, flags):
def
_create_user_playbooks
(
input_definitions
):
def
_create_user_playbooks
(
input_definitions
):
"""
Generates template playbooks and roles for users.
"""
"""
Generates template playbooks and roles for users.
"""
playbook_template
=
load_template
(
"
playbook
"
)
generate_file
(
"
playbook
"
,
"
provisioning/playbook.yml
"
)
generate_file
(
"
provisioning/playbook.yml
"
,
playbook_template
.
render
())
generate_file
(
"
user_hosts
"
,
"
provisioning/roles/hosts/tasks/main.yml
"
)
generate_file
(
"
user_routers
"
,
"
provisioning/roles/routers/tasks/main.yml
"
)
user_hosts_template
=
load_template
(
"
user_hosts
"
)
generate_file
(
"
provisioning/roles/hosts/tasks/main.yml
"
,
user_hosts_template
.
render
())
user_routers_template
=
load_template
(
"
user_routers
"
)
generate_file
(
"
provisioning/roles/routers/tasks/main.yml
"
,
user_routers_template
.
render
())
user_host_template
=
load_template
(
"
user_separate_hosts
"
)
for
host
in
input_definitions
[
"
hosts
"
]:
for
host
in
input_definitions
[
"
hosts
"
]:
output
=
user_host_template
.
render
(
host_name
=
host
[
"
name
"
])
generate_file
(
"
user_separate_hosts
"
,
"
provisioning/roles/
"
+
host
[
"
name
"
]
+
"
/tasks/main.yml
"
,
host_name
=
host
[
"
name
"
])
generate_file
(
"
provisioning/roles/
"
+
host
[
"
name
"
]
+
"
/tasks/main.yml
"
,
output
)
user_router_template
=
load_template
(
"
user_separate_routers
"
)
for
router
in
input_definitions
[
"
routers
"
]:
for
router
in
input_definitions
[
"
routers
"
]:
output
=
user_router_template
.
render
(
router_name
=
router
[
"
name
"
])
generate_file
(
"
user_separate_routers
"
,
"
provisioning/roles/
"
+
router
[
"
name
"
]
+
"
/tasks/main.yml
"
,
router_name
=
router
[
"
name
"
])
generate_file
(
"
provisioning/roles/
"
+
router
[
"
name
"
]
+
"
/tasks/main.yml
"
,
output
)
...
...
This diff is collapsed.
Click to expand it.
modules/file_manager.py
+
15
−
11
View file @
e187cb1e
...
@@ -20,14 +20,22 @@ def open_yaml(file_name):
...
@@ -20,14 +20,22 @@ def open_yaml(file_name):
input_file
.
close
()
input_file
.
close
()
def
generate_file
(
filename
,
output_string
):
def
generate_file
(
template
,
filename
,
**
template_args
):
"""
"""
Generates a file using a template.
Generates a file from output string.
:param filename: name of the file to create
:param template: name of the template file
:param output_string: string to write to the file
:param filename: path to the output file inside the output directory
:param template_args: arbitrary number of named args for the template
"""
"""
inventory_template
=
_load_template
(
template
)
output
=
inventory_template
.
render
(
**
template_args
)
_write_to_file
(
filename
,
output
)
def
_write_to_file
(
filename
,
output_string
):
"""
Generates a file from output string.
"""
try
:
try
:
new_file
=
open
(
OUTPUT_DIRECTORY
+
"
/
"
+
filename
,
"
w
"
)
new_file
=
open
(
OUTPUT_DIRECTORY
+
"
/
"
+
filename
,
"
w
"
)
new_file
.
write
(
output_string
)
new_file
.
write
(
output_string
)
...
@@ -36,12 +44,8 @@ def generate_file(filename, output_string):
...
@@ -36,12 +44,8 @@ def generate_file(filename, output_string):
raise
raise
def
load_template
(
template_name
):
def
_load_template
(
template_name
):
"""
"""
Returns a loaded jinja2 template.
"""
Returns a loaded jinja2 template.
:param template_name: name of the template file
"""
template_loader
=
jinja2
.
FileSystemLoader
(
searchpath
=
"
templates
"
)
template_loader
=
jinja2
.
FileSystemLoader
(
searchpath
=
"
templates
"
)
template_env
=
jinja2
.
Environment
(
loader
=
template_loader
,
trim_blocks
=
True
,
lstrip_blocks
=
True
)
template_env
=
jinja2
.
Environment
(
loader
=
template_loader
,
trim_blocks
=
True
,
lstrip_blocks
=
True
)
...
...
This diff is collapsed.
Click to expand it.
modules/vagrant_generator.py
+
2
−
13
View file @
e187cb1e
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
import
jinja2
import
jinja2
from
modules.file_manager
import
load_template
,
generate_file
,
open_yaml
from
modules.file_manager
import
generate_file
,
open_yaml
VAGRANT_MAPPING
=
open_yaml
(
"
conf/vagrant_mapping.yml
"
)
VAGRANT_MAPPING
=
open_yaml
(
"
conf/vagrant_mapping.yml
"
)
VIRTUALBOX_MAPPING
=
open_yaml
(
"
conf/virtualbox_mapping.yml
"
)
VIRTUALBOX_MAPPING
=
open_yaml
(
"
conf/virtualbox_mapping.yml
"
)
...
@@ -169,17 +169,6 @@ def _build_vagrant_definitions(input_definitions, flags):
...
@@ -169,17 +169,6 @@ def _build_vagrant_definitions(input_definitions, flags):
return
vagrant_definitions
return
vagrant_definitions
def
_build_vagrantfile
(
vagrant_definitions
):
"""
Generates the Vagrantfile using the vagrantfile template and vagrant
definitions.
"""
template
=
load_template
(
"
vagrantfile
"
)
output
=
template
.
render
(
defs
=
vagrant_definitions
)
generate_file
(
"
Vagrantfile
"
,
output
)
def
generate_vagrantfile
(
input_definitions
,
flags
):
def
generate_vagrantfile
(
input_definitions
,
flags
):
"""
"""
This method is responsible for Vagrantfile generation.
This method is responsible for Vagrantfile generation.
...
@@ -195,7 +184,7 @@ def generate_vagrantfile(input_definitions, flags):
...
@@ -195,7 +184,7 @@ def generate_vagrantfile(input_definitions, flags):
raise
raise
try
:
try
:
_build_vagrantfile
(
vagrant_definitions
)
generate_file
(
"
vagrantfile
"
,
"
Vagrantfile
"
,
defs
=
vagrant_definitions
)
except
Exception
:
except
Exception
:
print
(
"
Could not generate Vagrantfile.
"
)
print
(
"
Could not generate Vagrantfile.
"
)
raise
raise
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