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
fa428078
Commit
fa428078
authored
5 years ago
by
Attila Farkas
Browse files
Options
Downloads
Patches
Plain Diff
just copy templates where jinja is not needed
parent
b37361bd
No related branches found
No related tags found
1 merge request
!7
Resolve Refactoring
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
modules/ansible_generator.py
+4
-4
4 additions, 4 deletions
modules/ansible_generator.py
modules/file_manager.py
+15
-15
15 additions, 15 deletions
modules/file_manager.py
with
19 additions
and
19 deletions
modules/ansible_generator.py
+
4
−
4
View file @
fa428078
from
modules.file_manager
import
generate_file
from
modules.file_manager
import
generate_file
,
copy_template_file
from
modules.ansible_vars_generator
import
generate_ansible_vars
...
...
@@ -15,9 +15,9 @@ def _create_config_playbooks(input_definitions, flags):
def
_create_user_playbooks
(
input_definitions
):
"""
Generates template playbooks and roles for users.
"""
gener
ate_file
(
"
playbook
"
,
"
provisioning/playbook.yml
"
)
gener
ate_file
(
"
user_hosts
"
,
"
provisioning/roles/hosts/tasks/main.yml
"
)
gener
ate_file
(
"
user_routers
"
,
"
provisioning/roles/routers/tasks/main.yml
"
)
copy_templ
ate_file
(
"
playbook
"
,
"
provisioning/playbook.yml
"
)
copy_templ
ate_file
(
"
user_hosts
"
,
"
provisioning/roles/hosts/tasks/main.yml
"
)
copy_templ
ate_file
(
"
user_routers
"
,
"
provisioning/roles/routers/tasks/main.yml
"
)
for
host
in
input_definitions
[
"
hosts
"
]:
generate_file
(
"
user_separate_hosts
"
,
"
provisioning/roles/
"
+
host
[
"
name
"
]
+
"
/tasks/main.yml
"
,
host_name
=
host
[
"
name
"
])
...
...
This diff is collapsed.
Click to expand it.
modules/file_manager.py
+
15
−
15
View file @
fa428078
"""
This module handles file imports and creations in general.
"""
import
jinja2
import
os
import
shutil
import
yaml
from
jinja2
import
Environment
,
FileSystemLoader
from
shutil
import
copyfile
,
rmtree
from
yaml
import
dump
,
safe_load
OUTPUT_DIRECTORY
=
"
sandbox
"
...
...
@@ -12,19 +12,24 @@ def open_yaml(file_name):
"""
Opens and returns a file from the argument.
"""
try
:
input_file
=
open
(
str
(
file_name
))
return
yaml
.
safe_load
(
input_file
)
return
safe_load
(
input_file
)
except
IOError
:
print
(
"
Error: Cannot open the required file:
"
+
str
(
file_name
))
raise
finally
:
input_file
.
close
()
def
copy_template_file
(
template
,
destination
):
"""
Copies playbook templates that does not need jinja or yaml to the
destination.
"""
copyfile
(
"
templates/
"
+
template
,
"
sandbox/
"
+
destination
)
try
:
copyfile
(
"
templates/
"
+
template
,
"
sandbox/
"
+
destination
)
except
IOError
:
print
(
"
Error: cannot write to this location.
"
)
raise
def
dump_to_yaml
(
data
,
filename
):
...
...
@@ -36,7 +41,7 @@ def dump_to_yaml(data, filename):
try
:
stream
=
open
(
OUTPUT_DIRECTORY
+
"
/
"
+
filename
,
'
w
'
)
yaml
.
dump
(
data
,
stream
)
dump
(
data
,
stream
)
except
IOError
:
print
(
"
Error: cannot write to this location.
"
)
raise
...
...
@@ -71,10 +76,11 @@ def _write_to_file(filename, output_string):
def
_load_template
(
template_name
):
"""
Returns a loaded jinja2 template.
"""
template_loader
=
jinja2
.
FileSystemLoader
(
searchpath
=
"
templates
"
)
template_env
=
jinja2
.
Environment
(
loader
=
template_loader
,
trim_blocks
=
True
,
lstrip_blocks
=
True
)
template_loader
=
FileSystemLoader
(
searchpath
=
"
templates
"
)
template_env
=
Environment
(
loader
=
template_loader
,
trim_blocks
=
True
,
lstrip_blocks
=
True
)
return
template_env
.
get_template
(
template_name
)
def
_create_provisioning_directories
(
directory
,
device_definitions
):
"""
Creates all subdirectories for privisioning.
"""
...
...
@@ -107,11 +113,10 @@ def _create_provisioning_directories(directory, device_definitions):
raise
def
prepare_directories
(
device_definitions
):
"""
Prepares the necessary directory structure.
"""
shutil
.
rmtree
(
OUTPUT_DIRECTORY
,
True
)
rmtree
(
OUTPUT_DIRECTORY
,
True
)
try
:
os
.
mkdir
(
OUTPUT_DIRECTORY
)
...
...
@@ -121,8 +126,3 @@ def prepare_directories(device_definitions):
_create_provisioning_directories
(
"
base_provisioning
"
,
device_definitions
)
_create_provisioning_directories
(
"
provisioning
"
,
device_definitions
)
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