Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python script for FTP connection and Dockerfile #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#!/usr/bin/python3

import ftplib
import json
import requests

urlFTPServer="10.0.12.114"
Dariod9 marked this conversation as resolved.
Show resolved Hide resolved
urlManager='http://10.0.12.114:80'

ftp = ftplib.FTP(urlFTPServer,'ftp-user','ftp-password')
Dariod9 marked this conversation as resolved.
Show resolved Hide resolved

count=0
result={}

ftp.cwd('tests')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'tests' should be a variable that we can easily change. Shouldn't be hardcoded


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agents will have to authenticate themselves on the CI/CD Manager.
We will have to deal with these authentication mechanisms. Authenticate the Agent, check if the token is expired, etc...


print(result)
19 changes: 19 additions & 0 deletions ftpTestApi/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
77 changes: 77 additions & 0 deletions ftpTestApi/connection.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions ftpTestApi/cronfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* * * * * python3 /app/connection.py
8 changes: 8 additions & 0 deletions ftpTestApi/envfile
Original file line number Diff line number Diff line change
@@ -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