Skip to content

Commit

Permalink
Generate SSH Windows & Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolman-Freecss committed Oct 5, 2024
1 parent 6ee10b0 commit 33bd192
Showing 5 changed files with 117 additions and 20 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -13,4 +13,7 @@
!**/jenkins_plugins/.gitkeep

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

# Ignore RSA temp files
*_rsa*
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -15,6 +15,10 @@ This tool is used to serve an automated environment in local or cloud platform t
- [Configuration](#configuration)
- [Dependencies](#dependencies)
- [Tech stacks CI/CD](#tech-stacks-ci/cd)
- [Troubleshoting](#troubleshoting)
- [Jenkins](#jenkins)
- [Scripts](#scripts)
- [AWS](#aws)

# Systems

@@ -173,15 +177,12 @@ docker push kolmanfreecss/jenkins-git

# Troubleshoting

## Jenkins
- Script to install Jenkins not working properly.
- Alternative Solution: Connect through SSH to the EC2 instance and install Jenkins
manually. (https://mirrors.jenkins.io/redhat-stable/)
- After that connect to the IPv4 Public EC2 instance with HTTP protocol and port 8080.
- Example: http://YOUR_EC2_PUBLIC_IP:8080
- Check SSH key permissions to connect to EC2 instance.
- `chmod 400 my-ssh-key.pem`
- Remove permissions to other group users or another users because AWS won't let you connect to the EC2 instance if
the permissions are too permissive.
- Check EC2 system log from AWS section to see if Jenkins is running properly or installed.
- BIG Problems installing plugins https://community.jenkins.io/t/issue-while-upgrading-plugins-on-latest-jenkins/9846
- It seems that halifax has blocked the ISP, so we need to install the plugins manually or use another ISP in order
@@ -206,6 +207,19 @@ docker push kolmanfreecss/jenkins-git
- ```bash
aws ec2 get-console-output --instance-id YOUR_INSTANCE_ID --output text
```
## Scripts
- Use `dos2unix` to convert the scripts to Unix format.
- ```bash
dos2unix YOUR_SCRIPT.sh
```
## AWS
- Check SSH key permissions to connect to EC2 instance.
- `chmod 400 my-ssh-key.pem`
- Remove permissions to other group users or another users because AWS won't let you connect to the EC2 instance if
the permissions are too permissive.
---
51 changes: 40 additions & 11 deletions src/local/python/init_jenkins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import platform
import subprocess

import requests
@@ -14,17 +15,45 @@

# ------------------------------- Methods -------------------------------

def create_ssh_key():
# Generate an SSH key pair
print("Generating an SSH key pair...")
ssh_keygen = subprocess.run(['ssh-keygen', '-t', 'rsa', '-N', '', '-f', 'jenkins_rsa'], check=True)
print("SSH key pair generated successfully.")
def create_ssh_key() -> str:
"""
Generate an SSH key pair if it does not exist.
:return: The private key as a string. None if the private key is not found.
"""
gen_private_key = None
ssh_dir = os.path.expanduser('~/.ssh')
ssh_key_path = os.path.join(ssh_dir, 'id_rsa')

# Read the public key
with open('jenkins_rsa.pub', 'r') as public_key_file:
public_key = public_key_file.read()
if not os.path.exists(ssh_dir):
os.makedirs(ssh_dir)
print(f"Directory {ssh_dir} created.")

return public_key
# Generate an SSH key pair
if os.path.exists(ssh_key_path) and os.path.getsize(ssh_key_path) > 0:
print(f"PY -> SSH key pair already exists at {ssh_key_path}.")
else:
print("Generating an SSH key pair...")
script_path = './sh/gen_ssh.sh'
# Check the OS and run the corresponding script
if platform.system() == 'Windows':
script_path = os.path.abspath('./sh/gen_ssh.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)
try:
with open(ssh_key_path, 'r') as private_key_file:
gen_private_key = private_key_file.read()
print(f"Private key: {gen_private_key}")
except FileNotFoundError:
print(f"Private key not found at {ssh_key_path}.")
return ""
print("SSH key pair generated successfully.")

return gen_private_key

# ------------------------------- END Methods -------------------------------

@@ -33,7 +62,7 @@ def create_ssh_key():
print(f"JENKINS INFO -> Jenkins URL: {jenkins_url}, Username: {username}, API Token: {api_token}")

# ----------- Generate SSH key pair -----------
create_ssh_key()
private_key = create_ssh_key() # Get the private key from the SSH key pair to connect Jenkins node via SSH to the agent (machine defined)

# Connect to the Jenkins server
jenkins_service = jenkins.Jenkins(jenkins_url, username, api_token)
@@ -104,7 +133,7 @@ def create_ssh_key():
params = {
'port': '22',
'username': username,
'credentialsId': jenkins_credentials_id,
'credentialsId': api_token, # private_key
'host': 'host.docker.internal' # Is the host where jenkins docker is running
}
print("Creating node with parameters")
31 changes: 31 additions & 0 deletions src/local/python/sh/gen_ssh.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@echo off
setlocal

echo Start...

REM Detect if we're on Windows and set the SSH key path
set "SSH_KEY_PATH=%USERPROFILE%\.ssh\id_rsa"

REM Show the path where the SSH key will be generated
echo Path to generate SSH key: %SSH_KEY_PATH%

REM Check if the SSH key already exists
if exist "%SSH_KEY_PATH%" (
echo SSH key already exists at %SSH_KEY_PATH%
) else (
REM Create the .ssh directory if it doesn't exist
if not exist "%USERPROFILE%\.ssh" (
mkdir "%USERPROFILE%\.ssh"
echo Directory created: %USERPROFILE%\.ssh
) else (
echo Directory already exists: %USERPROFILE%\.ssh
)

REM Generate an SSH key without a passphrase (-N "") and without interaction (-q)
ssh-keygen -t rsa -b 4096 -f "%SSH_KEY_PATH%" -N "" -q

REM Show success message
echo SSH key generated at %SSH_KEY_PATH%
)

endlocal
28 changes: 24 additions & 4 deletions src/local/python/sh/gen_ssh.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
#!/bin/bash

# Path where the SSH key will be stored (you can change this if needed)
SSH_KEY_PATH="$HOME/.ssh/id_rsa"
# Detect Windows using Git Bash or WSL
if command -v cmd.exe &> /dev/null; then
# If you're in Windows, adjust $HOME to a valid Windows path
if [ -n "$USERPROFILE" ]; then
SSH_KEY_PATH="$USERPROFILE/.ssh/id_rsa"
else
SSH_KEY_PATH="C:/Users/$(whoami)/.ssh/id_rsa"
fi
else
# If you're in a Unix-like environment, don't change anything
SSH_KEY_PATH="$HOME/.ssh/id_rsa"
fi

echo "SH -> Path to generate SSH key: $SSH_KEY_PATH"

# Check if the SSH key already exists
if [ -f "$SSH_KEY_PATH" ]; then
echo "SSH key already exists at $SSH_KEY_PATH"
echo "SH -> SSH key already exists at $SSH_KEY_PATH"
else
DIR_PATH="$(dirname "$SSH_KEY_PATH")"
echo "Trying to create directory: $DIR_PATH"

# Create the ~/.ssh directory if it doesn't exist
mkdir -p "$HOME/.ssh"
if [ ! -d "$DIR_PATH" ]; then
mkdir -p "$DIR_PATH" || { echo "Failed to create directory"; exit 1; }
echo "Directory created: $DIR_PATH"
else
echo "Directory already exists: $DIR_PATH"
fi

# Generate an SSH key without a passphrase (-N "") and without interaction (-q)
ssh-keygen -t rsa -b 4096 -f "$SSH_KEY_PATH" -N "" -q

0 comments on commit 33bd192

Please sign in to comment.