diff --git a/connection.py b/connection.py new file mode 100755 index 0000000..dfe1247 --- /dev/null +++ b/connection.py @@ -0,0 +1,31 @@ + +#!/usr/bin/python3 + +import ftplib +import json +import requests + +urlFTPServer="10.0.12.114" +urlManager='http://10.0.12.114:80' + +ftp = ftplib.FTP(urlFTPServer,'ftp-user','ftp-password') + +count=0 +result={} + +ftp.cwd('tests') + +for dir in ftp.nlst(): + ftp.cwd(dir) + + if 'test_description.yaml' in ftp.nlst(): + count+=1 + result[dir]='test-description.yaml' + + ftp.cwd('..') + +result["Total Tests"] = count + +x=requests.post(urlManager, result) + +print(result) \ No newline at end of file diff --git a/ftpTestApi/Dockerfile b/ftpTestApi/Dockerfile new file mode 100644 index 0000000..7f59edd --- /dev/null +++ b/ftpTestApi/Dockerfile @@ -0,0 +1,19 @@ +# Pulling Alpine image +FROM python:3.10-alpine + +RUN ln -sf python3 /usr/bin/python + +RUN pip3 install "requests==2.28.0" + +#Copy the connection file +COPY ./connection.py /app/connection.py + +#Create cron job +COPY ./cronfile /app/mycron +RUN crontab /app/mycron + +#Create process to ensure the container keeps working +RUN touch /tmp/out.log + + +CMD crond && tail -f /tmp/out.log \ No newline at end of file diff --git a/ftpTestApi/connection.py b/ftpTestApi/connection.py new file mode 100755 index 0000000..f3de212 --- /dev/null +++ b/ftpTestApi/connection.py @@ -0,0 +1,77 @@ + +#!/usr/bin/python3 + +import ftplib +import json +from textwrap import indent +import requests +import os + +# FTP File Reading Class + +class Reader: + def __init__(self): + self.data = "" + + def __call__(self, s): + self.data += str(s) + + +#Reading Environment Variables + +ftpIP = os.environ["FTPIP"] +ftpUser = os.environ["FTPUser"] +ftpPassword = os.environ["FTPPassword"] +ftpDir = os.environ["FTPDir"] + +cicdManagerIP = os.environ["CI_CD_Manager_IP"] +cicdManagerUser = os.environ["CI_CD_Manager_User"] +cicdManagerPassword = os.environ["CI_CD_Manager_Password"] + + + +#Establish Connection to server +ftp = ftplib.FTP(ftpIP, ftpUser, ftpPassword) + +count = 0 +result = {} + +ftp.cwd(ftpDir) + +#Read and retrieve all test descriptions +for dir in ftp.nlst(): + + ftp.cwd(dir) + + if 'test_description.yaml' in ftp.nlst(): + count += 1 + + r = Reader() + ftp.retrbinary('RETR test_description.yaml', r) + result[dir] = r.data + + ftp.cwd('..') + +result["Total Tests"] = count + +try: + headers = { + 'accept': 'application/json', + } + + json_data = { + 'username': cicdManagerUser, + 'password': cicdManagerPassword, + } + + response = requests.post('http://'+cicdManagerIP+'/users/login', headers=headers, json=json_data) + + if response.status_code == 200: + + # Endpoint to later send the + # x = requests.post('http://'+cicdManagerIP +'/...', result) + print("Total Tests: "+ json.dumps(result["Total Tests"], indent=1)) + + +except ValueError: + print(ValueError) \ No newline at end of file diff --git a/ftpTestApi/cronfile b/ftpTestApi/cronfile new file mode 100644 index 0000000..8b1bd27 --- /dev/null +++ b/ftpTestApi/cronfile @@ -0,0 +1 @@ +* * * * * python3 /app/connection.py \ No newline at end of file diff --git a/ftpTestApi/envfile b/ftpTestApi/envfile new file mode 100644 index 0000000..af2787f --- /dev/null +++ b/ftpTestApi/envfile @@ -0,0 +1,8 @@ +FTPIP=10.0.12.114 +FTPUser=ftp-user +FTPPassword=ftp-password +FTPDir=tests + +CI_CD_Manager_IP=10.0.12.114:8000 +CI_CD_Manager_User=admin +CI_CD_Manager_Password=admin