From 6196c8a43d3cc06384421d4f06942fabeffdd058 Mon Sep 17 00:00:00 2001 From: dariod9 Date: Wed, 29 Jun 2022 17:59:30 +0100 Subject: [PATCH 1/3] Python script for FTP connection and Dockerfile --- Dockerfile | 23 +++++++++++++++++++++++ connection.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 Dockerfile create mode 100755 connection.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0db812a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +# Dockerfile to create image with cron services +FROM ubuntu:latest +MAINTAINER baeldung.com + +# Add the script to the Docker Image +ADD connection.py /root/connection.py + +# Give execution rights on the cron scripts +RUN chmod 0644 /root/connection.py + +#Install Cron +RUN apt-get update +RUN apt-get -y install cron +RUN apt-get install -y python3 +RUN apt-get install -y pip +RUN pip3 install requests + +# Add the cron job +RUN crontab -l | { cat; echo "* * * * * /bin/python3 /root/connection.py"; } | crontab - +RUN cron + +# Run the command on container startup +CMD cron \ No newline at end of file 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 From 7efca9fe0b490934f4ac5b322045d2507fe8839c Mon Sep 17 00:00:00 2001 From: dariod9 Date: Fri, 1 Jul 2022 00:32:59 +0100 Subject: [PATCH 2/3] Dockerfile with alpine and external env variables --- Dockerfile | 23 ----------------- ftpTestApi/Dockerfile | 19 ++++++++++++++ ftpTestApi/connection.py | 56 ++++++++++++++++++++++++++++++++++++++++ ftpTestApi/cronfile | 1 + ftpTestApi/envfile | 5 ++++ 5 files changed, 81 insertions(+), 23 deletions(-) delete mode 100644 Dockerfile create mode 100644 ftpTestApi/Dockerfile create mode 100755 ftpTestApi/connection.py create mode 100644 ftpTestApi/cronfile create mode 100644 ftpTestApi/envfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 0db812a..0000000 --- a/Dockerfile +++ /dev/null @@ -1,23 +0,0 @@ -# Dockerfile to create image with cron services -FROM ubuntu:latest -MAINTAINER baeldung.com - -# Add the script to the Docker Image -ADD connection.py /root/connection.py - -# Give execution rights on the cron scripts -RUN chmod 0644 /root/connection.py - -#Install Cron -RUN apt-get update -RUN apt-get -y install cron -RUN apt-get install -y python3 -RUN apt-get install -y pip -RUN pip3 install requests - -# Add the cron job -RUN crontab -l | { cat; echo "* * * * * /bin/python3 /root/connection.py"; } | crontab - -RUN cron - -# Run the command on container startup -CMD cron \ 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..ce7e2e4 --- /dev/null +++ b/ftpTestApi/connection.py @@ -0,0 +1,56 @@ + +#!/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"] + +managerIP = os.environ["ManagerIP"] + + +#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] = 'Test found for '+dir + + ftp.cwd('..') + +result["Total Tests"] = count + +x = requests.post('http://'+managerIP, result) + +print(json.dumps(result, indent=2)) 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..176860c --- /dev/null +++ b/ftpTestApi/envfile @@ -0,0 +1,5 @@ +FTPIP=10.0.12.114 +FTPUser=ftp-user +FTPPassword=ftp-password +FTPDir=tests +ManagerIP=10.0.12.114:80 \ No newline at end of file From 72b740ab96464f9e789e57b6d22d5ec0bba5e131 Mon Sep 17 00:00:00 2001 From: Dario Matos Date: Fri, 1 Jul 2022 16:32:54 +0100 Subject: [PATCH 3/3] Script with CICD Manager Authentication (only login, credentials via envfile, no token verification) and ready to POST test descriptions --- ftpTestApi/connection.py | 45 +++++++++++++++++++++++++++++----------- ftpTestApi/envfile | 5 ++++- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/ftpTestApi/connection.py b/ftpTestApi/connection.py index ce7e2e4..f3de212 100755 --- a/ftpTestApi/connection.py +++ b/ftpTestApi/connection.py @@ -7,14 +7,14 @@ import requests import os -#FTP File Reading Class +# FTP File Reading Class -# class Reader: -# def __init__(self): -# self.data = "" +class Reader: + def __init__(self): + self.data = "" -# def __call__(self, s): -# self.data += str(s) + def __call__(self, s): + self.data += str(s) #Reading Environment Variables @@ -24,7 +24,10 @@ ftpPassword = os.environ["FTPPassword"] ftpDir = os.environ["FTPDir"] -managerIP = os.environ["ManagerIP"] +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 @@ -43,14 +46,32 @@ if 'test_description.yaml' in ftp.nlst(): count += 1 - # r = Reader() - # ftp.retrbinary('RETR test_description.yaml', r) - result[dir] = 'Test found for '+dir + r = Reader() + ftp.retrbinary('RETR test_description.yaml', r) + result[dir] = r.data ftp.cwd('..') result["Total Tests"] = count -x = requests.post('http://'+managerIP, result) +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)) + -print(json.dumps(result, indent=2)) +except ValueError: + print(ValueError) \ No newline at end of file diff --git a/ftpTestApi/envfile b/ftpTestApi/envfile index 176860c..af2787f 100644 --- a/ftpTestApi/envfile +++ b/ftpTestApi/envfile @@ -2,4 +2,7 @@ FTPIP=10.0.12.114 FTPUser=ftp-user FTPPassword=ftp-password FTPDir=tests -ManagerIP=10.0.12.114:80 \ No newline at end of file + +CI_CD_Manager_IP=10.0.12.114:8000 +CI_CD_Manager_User=admin +CI_CD_Manager_Password=admin