Skip to content

Commit

Permalink
GitOps update & Start SSH on Agent
Browse files Browse the repository at this point in the history
Fix GitOps
  • Loading branch information
Kolman-Freecss committed Oct 16, 2024
1 parent 3ca2ea0 commit f951496
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 88 deletions.
42 changes: 11 additions & 31 deletions .github/workflows/test-devops.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,19 @@ on:
- dev

jobs:

test-docker:
check-image:
runs-on: ubuntu-latest

services:
docker:
image: docker:19.03.12
options: --privileged # To execute Docker on Github Actions
ports:
- 8080:8080
volumes:
- /var/run/docker.sock:/var/run/docker.sock

steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Set up Docker Compose
run: sudo apt-get update && sudo apt-get install docker-compose -y
- name: Check out code
uses: actions/checkout@v2

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

- name: Wait for services to be ready
run: sleep 10

- name: Run Tests
- name: Check if Docker image exists
run: |
# Here we run the test commands, e.g. to check that the services are running
curl --fail http://localhost:8080 || exit 1 # Basic test to check if Jenkins is running
- name: Tear down Docker Compose
if: always()
working-directory: src/local
run: docker-compose down
# Attempt to pull the Docker image
if docker pull kolmanfreecss/jenkins-git:0.2.0-SNAPSHOT; then
echo "Docker image kolmanfreecss/jenkins-git:0.2.0-SNAPSHOT exists on Docker Hub."
else
echo "Docker image kolmanfreecss/jenkins-git:0.2.0-SNAPSHOT does not exist on Docker Hub."
exit 1
fi
11 changes: 0 additions & 11 deletions src/local/main/helpers/start_jenkins_agent.sh

This file was deleted.

17 changes: 17 additions & 0 deletions src/local/main/helpers/start_ssh_jenkins_agent.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@echo off
REM Start the sshd service
powershell -Command "Start-Service sshd"

REM Check if the sshd service is running
powershell -Command "Get-Service sshd | Select-Object Status"

REM Save the service status to a variable for logging
for /f "tokens=2 delims= " %%a in ('powershell -Command "Get-Service sshd | Select-Object -ExpandProperty Status"') do set status=%%a

REM Log the service status to a file
echo The sshd service is in status: %status% >> sshd_service_log.txt

REM Display the status on the screen
echo The sshd service is in status: %status%

pause
13 changes: 13 additions & 0 deletions src/local/main/helpers/start_ssh_jenkins_agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Start the sshd service
sudo systemctl start sshd

# Check if the sshd service is running
service_status=$(systemctl is-active sshd)

# Log the service status to a file
echo "The sshd service is in status: $service_status" >> sshd_service_log.txt

# Display the status on the screen
echo "The sshd service is in status: $service_status"
60 changes: 14 additions & 46 deletions src/local/main/init_jenkins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import platform
import subprocess

import jenkins

import config as config_module
Expand Down Expand Up @@ -25,47 +28,6 @@ def fetch():
node_description = 'Node configured with Docker'
remote_fs = '/var/jenkins_home'
labels = 'docker'
num_executors = 2

# Script to install Docker on the node
install_docker_script = '''#!/bin/bash
if ! [ -x "$(command -v docker)" ]; then
echo "Docker is not installed. Proceeding to install Docker..."
curl -fsSL https://get.docker.com -o get-docker.helpers
helpers get-docker.helpers
sudo usermod -aG docker jenkins # Add the Jenkins user to the Docker group
echo "Docker installed successfully."
else
echo "Docker is already installed."
fi
'''

# Node configuration with the Docker installation script
# node_config_xml = f'''<?xml version="1.0" encoding="UTF-8"?>
# <slave>
# <name>{node_name}</name>
# <description>{node_description}</description>
# <remoteFS>{remote_fs}</remoteFS>
# <numExecutors>{num_executors}</numExecutors>
# <mode>EXCLUSIVE</mode>
# <retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
# <launcher class="hudson.slaves.CommandLauncher">
# <command>{install_docker_script}</command>
# </launcher>
# <label>{labels}</label>
# <nodeProperties/>
# </slave>'''
node_config_xml = f'''<?xml version="1.0" encoding="UTF-8"?>
<slave>
<name>{node_name}</name>
<description>{node_description}</description>
<remoteFS>{remote_fs}</remoteFS>
<numExecutors>{num_executors}</numExecutors>
<mode>EXCLUSIVE</mode>
<retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
<label>{labels}</label>
<nodeProperties/>
</slave>'''

# Create the node in Jenkins
try:
Expand Down Expand Up @@ -102,11 +64,17 @@ def fetch():

print(f"Node '{node_name}' created successfully with Docker installation.")

# TODO: Init SSH Client on agent host
start_jenkins_agent = './start_jenkins_agent.helpers'
print(f'Now it will start the Jenkins agent with the following command: {start_jenkins_agent}')
# subprocess.run(['bash', start_jenkins_agent], check=True)
# print('Jenkins agent started successfully.')
start_ssh_jenkins_agent = './helpers/start_ssh_jenkins_agent.sh'
print(f'Now it will start the Jenkins agent with the following command: {start_ssh_jenkins_agent}')
# Check the OS and run the corresponding script
if platform.system() == 'Windows':
start_ssh_jenkins_agent = './helpers/start_ssh_jenkins_agent.bat'
print(f"Running the batch script: {start_ssh_jenkins_agent}")
# Execute the batch script for Windows
subprocess.run([start_ssh_jenkins_agent], check=True, shell=True)
else:
print(f"Running the shell script: {start_ssh_jenkins_agent}")
subprocess.run(['bash', start_ssh_jenkins_agent], check=True)

except jenkins.JenkinsException as e:
print(f"Error creating node: {e}")
Expand Down

0 comments on commit f951496

Please sign in to comment.