Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Ansible Inventory Generator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
GÉANT Orchestration and Automation Team
GAP
Ansible Inventory Generator
Commits
2c38ae14
Commit
2c38ae14
authored
9 months ago
by
Mohammad Torkashvand
Browse files
Options
Downloads
Patches
Plain Diff
respect existing folders
parent
ba795dc7
No related branches found
No related tags found
No related merge requests found
Pipeline
#88831
passed
9 months ago
Stage: tox
Changes
4
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+2
-0
2 additions, 0 deletions
.gitignore
ansible_inventory_generator/app.py
+15
-9
15 additions, 9 deletions
ansible_inventory_generator/app.py
tests/test_inventory_generator.py
+18
-0
18 additions, 0 deletions
tests/test_inventory_generator.py
tox.ini
+1
-1
1 addition, 1 deletion
tox.ini
with
36 additions
and
10 deletions
.gitignore
+
2
−
0
View file @
2c38ae14
...
@@ -23,4 +23,6 @@ docs/source/lso.rst
...
@@ -23,4 +23,6 @@ docs/source/lso.rst
docs/source/lso.*.rst
docs/source/lso.*.rst
docs/source/modules.rst
docs/source/modules.rst
artifactory/
.env
.env
This diff is collapsed.
Click to expand it.
ansible_inventory_generator/app.py
+
15
−
9
View file @
2c38ae14
import
os
import
shutil
import
shutil
import
sys
import
sys
import
tempfile
import
tempfile
...
@@ -78,9 +79,11 @@ def generate_host_vars_and_hosts_file(router_subscriptions: list, temp_dir: Path
...
@@ -78,9 +79,11 @@ def generate_host_vars_and_hosts_file(router_subscriptions: list, temp_dir: Path
@contextmanager
@contextmanager
def
safe_write
(
temp_dir
:
Path
,
old_host_vars_dir
:
Path
)
->
Generator
[
Path
,
None
,
None
]:
def
safe_write
(
temp_dir
:
Path
)
->
Generator
[
Path
,
None
,
None
]:
settings
=
load_settings
()
temp_dir
.
mkdir
(
exist_ok
=
True
)
temp_dir
.
mkdir
(
exist_ok
=
True
)
ansible_inventory_path
=
Path
(
settings
.
aig_ansible_inventory_path
)
ansible_inventory_path
.
mkdir
(
exist_ok
=
True
)
try
:
try
:
yield
temp_dir
yield
temp_dir
except
Exception
as
e
:
except
Exception
as
e
:
...
@@ -88,11 +91,16 @@ def safe_write(temp_dir: Path, old_host_vars_dir: Path) -> Generator[Path, None,
...
@@ -88,11 +91,16 @@ def safe_write(temp_dir: Path, old_host_vars_dir: Path) -> Generator[Path, None,
typer
.
echo
(
f
"
Error:
{
e
}
"
)
typer
.
echo
(
f
"
Error:
{
e
}
"
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
else
:
else
:
if
old_host_vars_dir
.
exists
():
host_vars
=
ansible_inventory_path
/
"
host_vars
"
shutil
.
rmtree
(
old_host_vars_dir
)
if
host_vars
.
exists
():
shutil
.
rmtree
(
host_vars
)
shutil
.
copytree
(
temp_dir
,
old_host_vars_dir
)
# Copy new host_vars dir
hosts_file
=
ansible_inventory_path
/
"
hosts.yaml
"
shutil
.
copy
(
temp_dir
/
"
hosts.yaml
"
,
old_host_vars_dir
)
# Copy new hosts.yaml file
if
hosts_file
.
exists
():
os
.
remove
(
hosts_file
)
shutil
.
copytree
(
temp_dir
/
"
host_vars
"
,
ansible_inventory_path
/
"
host_vars
"
)
# Copy new host_vars dir
shutil
.
copy
(
temp_dir
/
"
hosts.yaml
"
,
ansible_inventory_path
)
# Copy new hosts.yaml file
shutil
.
rmtree
(
temp_dir
)
shutil
.
rmtree
(
temp_dir
)
...
@@ -111,9 +119,7 @@ def generate_inventory_from_api() -> None:
...
@@ -111,9 +119,7 @@ def generate_inventory_from_api() -> None:
sys
.
exit
(
1
)
sys
.
exit
(
1
)
temp_dir
=
Path
(
tempfile
.
mkdtemp
())
temp_dir
=
Path
(
tempfile
.
mkdtemp
())
old_host_vars_dir
=
Path
(
settings
.
aig_ansible_inventory_path
)
with
safe_write
(
temp_dir
)
as
temp_dir
:
with
safe_write
(
temp_dir
,
old_host_vars_dir
)
as
temp_dir
:
generate_host_vars_and_hosts_file
(
router_subscriptions
,
temp_dir
)
generate_host_vars_and_hosts_file
(
router_subscriptions
,
temp_dir
)
...
...
This diff is collapsed.
Click to expand it.
tests/test_inventory_generator.py
+
18
−
0
View file @
2c38ae14
...
@@ -248,3 +248,21 @@ def test_inventory_generation_handles_exceptions_and_preserves_file_integrity(
...
@@ -248,3 +248,21 @@ def test_inventory_generation_handles_exceptions_and_preserves_file_integrity(
with
path
.
open
(
"
r
"
)
as
f
:
with
path
.
open
(
"
r
"
)
as
f
:
content
=
f
.
read
()
content
=
f
.
read
()
assert
content
==
file_contents
[
path_key
]
assert
content
==
file_contents
[
path_key
]
def
test_generate_inventory_from_api_empty_json
(
setup_test
,
tmp_dir
):
with
patch
(
"
requests.get
"
)
as
mock_get
:
mock_get
.
return_value
.
json
.
return_value
=
[]
mock_get
.
return_value
.
status_code
=
200
generate_inventory_from_api
()
# Check if hosts.yaml file is created
hosts_file
=
tmp_dir
/
"
hosts.yaml
"
assert
hosts_file
.
exists
()
# Check that the hosts.yaml file is empty or contains no hosts
with
hosts_file
.
open
()
as
f
:
content
=
yaml
.
safe_load
(
f
)
expected_content
=
{
"
all
"
:
{
"
children
"
:
{
"
routers
"
:
{
"
children
"
:
{}}}}}
assert
content
==
expected_content
This diff is collapsed.
Click to expand it.
tox.ini
+
1
−
1
View file @
2c38ae14
...
@@ -30,7 +30,7 @@ commands =
...
@@ -30,7 +30,7 @@ commands =
coverage
run
--source
ansible_inventory_generator
-m
pytest
{posargs}
coverage
run
--source
ansible_inventory_generator
-m
pytest
{posargs}
coverage
xml
coverage
xml
coverage
html
coverage
html
sh
-c
"if
[ $SKIP_ALL_TESTS -eq 1 ]
; then echo 'Skipping coverage report'; else coverage report --fail-under
4
0; fi"
sh
-c
"if
[ $SKIP_ALL_TESTS -eq 1 ]
; then echo 'Skipping coverage report'; else coverage report --fail-under
3
0; fi"
allowlist_externals
=
allowlist_externals
=
sh
sh
\ No newline at end of file
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