Skip to content

Commit

Permalink
Enhance code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolman-Freecss committed Oct 6, 2024
1 parent 33bd192 commit 5e231aa
Show file tree
Hide file tree
Showing 20 changed files with 419 additions and 289 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-devops.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
run: sudo apt-get update && sudo apt-get install docker-compose -y

- name: Run Docker Compose
working-directory: src/local
working-directory: src/env.local
run: docker-compose up -d

- name: Wait for services to be ready
Expand All @@ -48,5 +48,5 @@ jobs:
- name: Tear down Docker Compose
if: always()
working-directory: src/local
working-directory: src/env.local
run: docker-compose down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
# Ignore jenkins_plugins folder except .gitkeep
**/jenkins_plugins/*
!**/jenkins_plugins/.gitkeep
!**/jenkins_plugins/plugins.yaml

# Ignore .env/local.env files
.env/local.env
# Ignore .env/env.local.env files
.env/env.local

# Ignore RSA temp files
*_rsa*
12 changes: 3 additions & 9 deletions .run/Execute_build.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
<env name="JENKINS_URL" value="http://localhost:8080" />
<env name="JENKINS_USER" value="admin" />
<env name="JENKINS_PASS" value="admin" />
<env name="GITHUB_CREDENTIALS_ID" value="github-credentials" />
<env name="GITHUB_USER" value="Kolman_Freecss" />
<env name="PAT_JENKINS" value="" />
<env name="EXECUTION_ENVIRONMENT" value="local" />
<env name="ENV" value="local" />
</envs>
<option name="SDK_HOME" value="" />
<option name="SDK_NAME" value="Python 3.10 (KF_EnvOrchestatorTool)" />
<option name="WORKING_DIRECTORY" value="D:\Data\ProjectsData\Programming\Projects\Web\_Devops\KF_EnvOrchestatorTool\src\local\python" />
<option name="WORKING_DIRECTORY" value="D:\Data\ProjectsData\Programming\Projects\Web\_Devops\KF_EnvOrchestatorTool\src\local\main" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/local/python/build.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/local/main/build.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
Expand Down
10 changes: 3 additions & 7 deletions .run/Execute_init_jenkins.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
<env name="JENKINS_URL" value="http://localhost:8080" />
<env name="JENKINS_USER" value="admin" />
<env name="JENKINS_PASS" value="admin" />
<env name="PAT_JENKINS" value="" />
<env name="JENKINS_CREDENTIALS_ID" value="github-credentials" />
<env name="ENV" value="local" />
</envs>
<option name="SDK_HOME" value="" />
<option name="SDK_NAME" value="Python 3.10 (KF_EnvOrchestatorTool)" />
<option name="WORKING_DIRECTORY" value="D:\Data\ProjectsData\Programming\Projects\Web\_Devops\KF_EnvOrchestatorTool\src\local\python" />
<option name="WORKING_DIRECTORY" value="D:\Data\ProjectsData\Programming\Projects\Web\_Devops\KF_EnvOrchestatorTool\src\local\main" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/local/python/init_jenkins.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/local/main/init_jenkins.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ docker push kolmanfreecss/jenkins-git
- Use `dos2unix` to convert the scripts to Unix format.
- ```bash
dos2unix YOUR_SCRIPT.sh
dos2unix YOUR_SCRIPT.helpers
```
## AWS
Expand Down
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Things to improve

- [ ] CLI interface to add configuration, tasks, etc.
- [ ] Healthcheck not working properly in dockercompose
- [ ] Healthcheck not working properly in dockercompose
- [ ] Fix Github Actions Tests
24 changes: 18 additions & 6 deletions src/local/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Use the Jenkins base image
FROM kolmanfreecss/jenkins-git:latest as jenkins-git
FROM kolmanfreecss/jenkins-git:0.2.0-SNAPSHOT as jenkins-git

RUN echo "INIT Dockerfile"

# Switch to the root user to install packages
USER root


# ---------- PYTHON DEPS ----------
# Install necessary dependencies
RUN apt-get update && apt-get install -y python3-pip python3-venv

Expand All @@ -18,29 +20,39 @@ COPY requirements.txt /app/
# Install Python dependencies in the virtual environment
RUN /app/venv/bin/pip install --no-cache-dir -r /app/requirements.txt

# ----------------------------------------


# Copy shell scripts and Python scripts to the container
#COPY shellscript/start_py_jenkins.sh /app/start_py_jenkins.sh
#COPY python/build.py /app/build.py
#COPY shellscript/start_py_jenkins.helpers /app/start_py_jenkins.helpers
#COPY main/build.py /app/build.py

# ---------- JENKINS DEPS ----------

COPY jenkins.yaml /var/jenkins_home/casc.yaml
ENV CASC_JENKINS_CONFIG=/var/jenkins_home/casc.yaml

# Copy the shell script that installs dependencies
COPY jenkins_install_deps.sh /usr/local/bin/jenkins_install_deps.sh

# Ensure the shell scripts are executable
RUN chmod +x /usr/local/bin/jenkins_install_deps.sh
RUN chmod +x /usr/env.local/bin/jenkins_install_deps.helpers

# Set the virtual environment as the default for Python
ENV PATH="/app/venv/bin:$PATH"

RUN echo "Before executing the shell script"

# Execute the dependency installation script
RUN /usr/local/bin/jenkins_install_deps.sh
RUN /usr/env.local/bin/jenkins_install_deps.helpers

# ----------------------------------------

RUN echo "After executing the shell script and before executing the Python script"

RUN ls -l /app

# Execute the copied shell script
#RUN /app/build.sh
#RUN /app/build.helpers

RUN echo "After executing the Python script"
9 changes: 5 additions & 4 deletions src/local/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
build:
context: . # Path where the Dockerfile is located
dockerfile: Dockerfile # Name of the Dockerfile
image: kolmanfreecss/jenkins-git:latest
container_name: kf-jenkins
user: root
privileged: true
Expand All @@ -17,7 +18,7 @@ services:
- jenkins_home:/var/jenkins_home # Volume for Jenkins data
- ./init-scripts:/var/jenkins_init_scripts # We mount the init scripts
- ./jenkins_plugins:/var/jenkins_home/plugins # We mount the plugins
entrypoint: ["/bin/bash", "-c", "/usr/bin/tini -- /usr/local/bin/jenkins.sh && dockerd > /var/log/dockerd.log 2>&1 && tail -f /dev/null"]
entrypoint: ["/bin/bash", "-c", "/usr/bin/tini -- /usr/env.local/bin/jenkins.helpers && dockerd > /var/log/dockerd.log 2>&1 && tail -f /dev/null"]
healthcheck:
test: ["CMD-SHELL", "curl -sS http://localhost:8080/login || exit 1"]
interval: 30s
Expand All @@ -30,7 +31,7 @@ services:
image: python:3.10
container_name: kf-jenkins-init
volumes:
- ./python:/usr/src/app
- ./main:/usr/src/app
- ./.data:/usr/src/app/data
- ./requirements.txt:/usr/src/app/requirements.txt
working_dir: /usr/src/app
Expand All @@ -42,7 +43,7 @@ services:
- GITHUB_USER=Kolman_Freecss
# TODO: This is a temporary PAT, it should be replaced by a secret (in prod environment) or not synch with the repository
- PAT_JENKINS=
- EXECUTION_ENVIRONMENT=docker-local
- EXECUTION_ENVIRONMENT=docker-env.local
command: >
bash -c "pip install -r requirements.txt && python init_jenkins.py && python build.py"
depends_on:
Expand All @@ -51,4 +52,4 @@ services:

volumes:
jenkins_home:
driver: local
driver: env.local
2 changes: 1 addition & 1 deletion src/local/init-scripts/init.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

# Execute the python script
# Execute the main script
python3 /app/build.py

8 changes: 8 additions & 0 deletions src/local/jenkins_plugins/plugins.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
jenkins:
systemMessage: "Jenkins configured by JCasC"
plugins:
required:
- git
- workflow-aggregator
- pipeline
- docker
54 changes: 54 additions & 0 deletions src/local/main/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os

import src.local.main.config as config_module
import src.local.main.services as services
from src.local.main.services import CredentialsType

try:
print(f"JENKINS INFO -> Jenkins URL: {config_module.get(config_module.ConfigKeys.JENKINS_URL)}, Username: {config_module.get(config_module.ConfigKeys.JENKINS_USER)}, API Token: {config_module.get(config_module.ConfigKeys.JENKINS_PASS)}")

# Jenkins version
user = services.jenkins_service.get_whoami()
version = services.jenkins_service.get_version()
print('JENKINS INFO -> Hello %s from Jenkins %s' % (user['fullName'], version))

# Print all files in the current directory
print(os.listdir('.'))

# Configured on docker-compose (check volumes)
if config_module.ENV == 'env.local':
path_job = '../.data/Jenkinsfile_spring.xml'
else:
path_job = './data/Jenkinsfile_spring.xml'

# List all content dir
if config_module.ENV != 'env.local':
try:
content = os.listdir('../')
print(content)
except Exception as e:
print(e)

# Read XML job
job_config = open(path_job).read()

# Create credentials
services.build_credentials(CredentialsType.USER)

job_name = "spring-pipeline"

try:
if services.jenkins_service.job_exists(job_name):
services.jenkins_service.delete_job(job_name)
print(f'Job {job_name} already exists and was deleted')
else:
print(f'Job {job_name} does not exist')
services.jenkins_service.create_job(job_name, job_config)
print('Job created successfully')
except Exception as e:
print(e)

services.jenkins_service.build_job(job_name)
print('Job execute successfully')
except Exception as e:
print("Error: ", e)
43 changes: 43 additions & 0 deletions src/local/main/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
from dotenv import dotenv_values

from enum import Enum

class ConfigKeys(str, Enum):
JENKINS_URL = 'JENKINS_URL'
JENKINS_USER = 'JENKINS_USER'
JENKINS_PASS = 'JENKINS_PASS'
PAT_JENKINS = 'PAT_JENKINS'
JENKINS_CREDENTIALS_ID = 'JENKINS_CREDENTIALS_ID'


# Global variables
config = {}
ENV = os.getenv('ENV') # Provided by Github Actions or Environment Variables
if ENV == 'prod':
env_files_path = '<cloud-config>'
else:
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..'))
env_files_path = os.path.join(project_root, '.env', f'env.{ENV}')
ENV = 'local'

# TODO - This is a hack to get around the fact that the sensitive data is not injected through the gitlab pipeline
config = dotenv_values(env_files_path)

if ENV == 'prod':
# Jenkins connection details
jenkins_url = os.getenv(ConfigKeys.JENKINS_URL.value)
jenkins_username = os.getenv(ConfigKeys.JENKINS_USER.value)
jenkins_password = os.getenv(ConfigKeys.JENKINS_PASS.value) # Get it from Jenkins > User > Configure
jenkins_pat_token = os.getenv(ConfigKeys.PAT_JENKINS.value)
jenkins_credentials_id = os.getenv(ConfigKeys.JENKINS_CREDENTIALS_ID.value)

# Storing values in the config dictionary
config[ConfigKeys.JENKINS_URL.value] = jenkins_url
config[ConfigKeys.JENKINS_USER.value] = jenkins_username
config[ConfigKeys.JENKINS_PASS.value] = jenkins_password
config[ConfigKeys.PAT_JENKINS.value] = jenkins_pat_token
config[ConfigKeys.JENKINS_CREDENTIALS_ID.value] = jenkins_credentials_id

def get(key: str) -> str:
return config.get(key)
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 5e231aa

Please sign in to comment.