Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mabra94 committed Jun 24, 2024
1 parent cc3bf55 commit 46f0245
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 0 deletions.
Empty file.
120 changes: 120 additions & 0 deletions GitLab_cicd_configBackup/backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import yaml
import sys
import os
from pysros.management import connect
from pathlib import Path

def get_connection(host=None, username=None, password=None, port=830, hostkey_verify=False):
"""
Function definition to obtain a Connection object to a specific
SR OS device and access the model-driven information.
This function also checks whether the script is being executed
locally on a pySROS capable SROS device or on a remote machine.
:parameter host: The hostname or IP address of the SR OS node.
:type host: str
:paramater credentials: The username and password to connect
to the SR OS node.
:type credentials: dict
:parameter port: The TCP port for the connection to the SR OS node.
:type port: int
:returns: Connection object for the SR OS node.
:rtype: :py:class:`pysros.management.Connection`
"""
try:
connection_object = connect(
host=host,
username=username,
password=password,
port=port,
hostkey_verify=hostkey_verify
)
except RuntimeError as error1:
print("Failed to connect. Error:", error1)
sys.exit(-1)
return connection_object

def getConfig(connection_object, path):
"""
Dedicated function to retrieve required config.
:parameter connection_object: The connection object
:type connection_object: dict
:paramater path: xpath pointing towards desired config
:type path: str
:returns: A tuple holding the required data.
:rtype: tuple
"""

config = connection_object.running.get(path)

return config

def ConfigToJson(connection_object, path, config):
"""
Dedicated function to convert config to json.
:parameter connection_object: The connection object
:type connection_object: dict
:paramater config: config retrieved from node
:type config: dict
:returns:
:rtype:
"""

jsonConfig = connection_object.convert(path=path, payload=config, source_format="pysros", destination_format="json", pretty_print=True)

return jsonConfig

def loadInventory(inventoryFile):
f = open(inventoryFile)
inv = yaml.safe_load(f)

return inv

def main():
"""
"""
path = '/nokia-conf:configure'

inventory = loadInventory('inventory.yaml')

NeUsername = os.getenv('NEUSERNAME')
NePassword = os.getenv('NEPASSWORD')

for entry in inventory['hosts']:

filepath = Path(entry+'/config.json')

print("Establishing Connection to "+ entry +"\n")
connection_object = get_connection(host=entry, username=NeUsername, password=NePassword)

print("Fetching config from "+ entry +"\n")
actualConfig = getConfig(connection_object, path)
actualJsonConfig = ConfigToJson(connection_object, path, actualConfig)

if filepath.is_file():
print("File exist")
with filepath.open("r", encoding ="utf-8") as f:
storedConfig = f.read()
f.close()

if storedConfig == actualJsonConfig:
print("Existing stored config is identical to the running config on the nodes")
else:
with filepath.open("w", encoding ="utf-8") as f:
f.write(actualJsonConfig)
f.close()
else:
print("File not exist")
with filepath.open("w", encoding ="utf-8") as f:
f.write(actualJsonConfig)
f.close()


if __name__ == "__main__":
main()
77 changes: 77 additions & 0 deletions GitLab_cicd_configBackup/gitlab-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# This file is a template, and might need editing before it works on your project.
# Official language image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/python/tags/
image: python:3.11.3

# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
pySROS_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pysros/cache"

# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
paths:
- .cache/pip
- venv/
- .cache/pysros/cache

stages:
- inventory
- backup

before_script:
- python -V # Print out python version for debugging
- pip install virtualenv
- virtualenv venv
- source venv/bin/activate
- mkdir -p $pySROS_CACHE_DIR
- mkdir -p ~/.pysros # Create pysros cache directory
- ln -sf $pySROS_CACHE_DIR ~/.pysros/cache # Create symbolic link to allow pySROS cache to be preserved
- git config --global user.email "$GITLAB_USER_EMAIL"
- git config --global user.name "$GITLAB_USER_ID"

inventory-job:
stage: inventory
script:
- git checkout "$CI_COMMIT_REF_NAME"
- pip install -r requirements.txt
- python inventory.py
- git add clab*
- >
if ! git diff-index --quiet HEAD; then
git commit -m "push back from pipeline"
git remote set-url --push origin "https://$TOKEN_NAME:$ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git"
git push --set-upstream origin $CI_COMMIT_BRANCH -o ci.skip
else
echo "No changes applied to the NEs"
fi
rules:
- changes:
- inventory.yaml
tags:
- comlab8

backup-job:
stage: backup
variables:
GIT_STRATEGY: clone
script:
- git checkout "$CI_COMMIT_REF_NAME"
- pip install -r requirements.txt
- python backup.py
- git add clab*
- >
if ! git diff-index --quiet HEAD; then
git commit -m "push back from pipeline"
git remote set-url --push origin "https://$TOKEN_NAME:$ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git"
git push --set-upstream origin $CI_COMMIT_BRANCH -o ci.skip
else
echo "No changes applied to the NEs"
fi
tags:
- comlab8
37 changes: 37 additions & 0 deletions GitLab_cicd_configBackup/inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import yaml
import sys
from pathlib import Path

def loadInventory(inventoryFile):
f = open(inventoryFile)
inv = yaml.safe_load(f)

return inv

def main():
"""
"""

inventory = loadInventory('inventory.yaml')

for entry in inventory['hosts']:

# Checking the local filesystem and create a folder for each
# NE if it does not yet exist

print("Creating sub-directory for each host if needed\n")
filepath = Path(entry+'/config.json')
filepath.parent.mkdir(parents=True, exist_ok=True)

if filepath.is_file():
print("File exist")
else:
print("File not exist")
with filepath.open("w", encoding ="utf-8") as f:
f.write('# Empty config.json file automatically created through pipeline')
f.close()


if __name__ == "__main__":
main()
19 changes: 19 additions & 0 deletions GitLab_cicd_configBackup/inventory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
hosts:
clab-nsp_topo_new1-r131:
ansible_host: 10.0.1.131
# clab-nsp_topo_new1-r132: # commented out as r132 is currently in classic mode
# ansible_host: 10.0.1.132
clab-nsp_topo_new1-r133:
ansible_host: 10.0.1.133
clab-nsp_topo_new1-r134:
ansible_host: 10.0.1.134
clab-nsp_topo_new1-r135:
ansible_host: 10.0.1.135
clab-nsp_topo_new1-r136:
ansible_host: 10.0.1.136
clab-nsp_topo_new1-r137:
ansible_host: 10.0.1.137
clab-nsp_topo_new1-r138:
ansible_host: 10.0.1.138


0 comments on commit 46f0245

Please sign in to comment.