Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ansible
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package 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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ines.maya
ansible
Commits
1cd3404f
Commit
1cd3404f
authored
2 years ago
by
ines.maya
Browse files
Options
Downloads
Patches
Plain Diff
add fichier congif python
parent
c2970b68
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
config.py
+79
-0
79 additions, 0 deletions
config.py
with
79 additions
and
0 deletions
config.py
0 → 100644
+
79
−
0
View file @
1cd3404f
#!/usr/bin/env python3
import
requests
import
jinja2
import
json
import
sys
import
os
API_BASE_URL
=
'
https://gns3.hepiapp.ch/v2
'
# effectue une requête GET à une API pour obtenir la liste des projets,
# puis recherche un projet particulier dans la liste en comparant le nom du projet
# avec un nom de projet spécifié en entrée.
# Si le projet est trouvé, renvoie l'identifiant du projet, sinon renvoie None.
def
get_project_uuid
(
project_name
):
url
=
API_BASE_URL
+
'
/projects
'
response
=
requests
.
get
(
url
)
response_json
=
response
.
json
()
for
project
in
response_json
:
if
project
[
'
name
'
]
==
project_name
:
return
project
[
'
project_id
'
]
return
None
def
get_node_data
(
project_uuid
):
url
=
API_BASE_URL
+
f
'
/projects/
{
project_uuid
}
/nodes
'
response
=
requests
.
get
(
url
)
response_json
=
response
.
json
()
node_data
=
[]
for
node
in
response_json
:
node_data
.
append
({
'
name
'
:
node
[
'
name
'
],
'
console_host
'
:
node
[
'
console_host
'
],
'
console_port
'
:
node
[
'
console
'
]
})
return
node_data
def
generate_ssh_config
(
node_data
):
template
=
'''
{% for node in node_data %}
{% if node.console_port != None %}
Host {{ node.name }}
User root
Hostname {{ node.console_host }}
Port {{ node.console_port +1}}
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
IdentityFile ~/.ssh/gns3.rsa
{% endif %}
{% endfor %}
'''
ssh_config
=
jinja2
.
Template
(
template
).
render
(
node_data
=
node_data
)
return
ssh_config
def
main
():
if
len
(
sys
.
argv
)
!=
2
:
print
(
f
"
Usage:
{
sys
.
argv
[
0
]
}
PROJECT_NAME
"
)
return
project_name
=
sys
.
argv
[
1
]
project_uuid
=
get_project_uuid
(
project_name
)
if
project_uuid
is
None
:
print
(
f
"
Project not found:
{
project_name
}
"
)
return
node_data
=
get_node_data
(
project_uuid
)
ssh_config
=
generate_ssh_config
(
node_data
)
ssh_config_dir
=
os
.
path
.
expanduser
(
"
~/.ssh/config.d
"
)
if
not
os
.
path
.
exists
(
ssh_config_dir
):
os
.
makedirs
(
ssh_config_dir
)
ssh_config_file
=
os
.
path
.
join
(
ssh_config_dir
,
f
"
{
project_name
}
.conf
"
)
with
open
(
ssh_config_file
,
'
w
'
)
as
f
:
f
.
write
(
ssh_config
)
print
(
f
"
SSH config written to
{
ssh_config_file
}
"
)
print
(
"
Include config.d/* line must be added to your ~/.ssh/config file.
"
)
if
__name__
==
'
__main__
'
:
main
()
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