Skip to content

Commit

Permalink
Start Automatically ssh server
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolman-Freecss committed Oct 21, 2024
1 parent ce3eddd commit 4aeaf21
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ This tool is used to serve an automated environment in local or cloud platform t

### Configure Jenkins

Config Jenkins on your local machine:

- Initial password located at `/var/jenkins_home/secrets/initialAdminPassword`

1. Execute `docker-compsoe.yml` from `.docker/local` folder.
2. Go to `localhost:8080` and follow the instructions to configure Jenkins. (Create an initialAdminPassword)
3. Install Git plugin for Jenkins. (This is necessary to trigger pipelines with SCM option enabled)

Config Jenkins on your local machine:

- Initial password located at `/var/jenkins_home/secrets/initialAdminPassword`

### Configure Environment Variables to execute main.py

1. Create a `local.env` file at `.env` folder.
Expand All @@ -59,10 +59,12 @@ JENKINS_PASS=<YOUR_JENKINS_PASSWORD>
ACCESS_TOKEN=<YOUR_GITHUB_ACCESS_TOKEN>
```

Note: ACCESS_TOKEN is necessary to pull the repositories configured in the pipelines from Github.

### Configure SSH

- Install OpenSSH Server on your local machine.
- Start the service.
- (Optional: main.py automatically will start the OpenSSH Server if its installed) Start the service.

```bash
# Windows
Expand Down
29 changes: 29 additions & 0 deletions src/local/main/helpers/start_ssh_server.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@echo off
:: Check for Administrator privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
echo Requesting administrator privileges...
powershell Start-Process "%~0" -Verb RunAs
exit /b
)

:: Check if SSHD service exists
sc query sshd >nul 2>&1
if %errorlevel% neq 0 (
echo The SSHD service is not installed on this system.
exit /b
)

:: Check if SSHD is running
sc query sshd | findstr /I "RUNNING"
if %errorlevel% equ 0 (
echo SSHD service is already running.
) else (
echo Starting SSHD service...
net start sshd
if %errorlevel% equ 0 (
echo SSHD service started successfully.
) else (
echo Failed to start SSHD service.
)
)
31 changes: 31 additions & 0 deletions src/local/main/helpers/start_ssh_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# Function to check if the script is being run as root
check_root() {
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root. Please try again with sudo or as root."
exit 1
fi
}

# Check if running as root
check_root

# Check if the SSHD service is installed
if ! systemctl list-units --type=service --all | grep -q 'sshd.service'; then
echo "The SSHD service is not installed on this system."
exit 1
fi

# Check if SSHD is running
if systemctl is-active --quiet sshd; then
echo "SSHD service is already running."
else
echo "Starting SSHD service..."
systemctl start sshd
if [ $? -eq 0 ]; then
echo "SSHD service started successfully."
else
echo "Failed to start SSHD service."
fi
fi
3 changes: 3 additions & 0 deletions src/local/main/init_jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def fetch():
services.get_ssh() # Get the private key from the SSH key pair to connect Jenkins node via SSH to the agent (machine defined)
services.build_credentials(CredentialsType.SSH)

# ----------- Start SSH Server -----------
services.start_ssh_server()

# Jenkins version
user = services.jenkins_service.get_whoami()
version = services.jenkins_service.get_version()
Expand Down
25 changes: 21 additions & 4 deletions src/local/main/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def build_ssh_credentials(force: bool = False) -> any:
print(f'BUILD SSH CREDENTIALS:: XML Document: ***** ') # {credentials}')
return credentials


def build_credentials(credential_type: CredentialsType, force: bool = False) -> any:
"""
Create credentials in Jenkins based on the type specified
Expand Down Expand Up @@ -151,8 +150,6 @@ def get_ssh(force: bool = False) -> str:
print(f"No SSH key found at {ssh_key_path}. Generating a new key.")
return create_ssh(force)



def create_ssh(force: bool = False) -> str:
"""
Generate an SSH key pair if it does not exist.
Expand Down Expand Up @@ -199,4 +196,24 @@ def create_ssh(force: bool = False) -> str:
return private_key
except FileNotFoundError:
print(f"Private key not found at {ssh_key_path}.")
return ""
return ""

def start_ssh_server():
"""
Start the SSH server
"""
print("Starting SSH server...")
script_path = 'helpers/start_ssh_server.sh'

# Check the OS and run the corresponding script
if platform.system() == 'Windows':
script_path = os.path.abspath('helpers/start_ssh_server.bat')
print(f"Running the batch script: {script_path}")
# Execute the batch script for Windows
subprocess.run([script_path], check=True, shell=True)
else:
print(f"Running the shell script: {script_path}")
# Execute the shell script for Unix-like environments
subprocess.run(['bash', script_path], check=True)

print("SSH server started successfully.")

0 comments on commit 4aeaf21

Please sign in to comment.