git integration. renamed pipeline.groovy to jenkinsfile

This commit is contained in:
dima 2024-10-20 13:37:39 +03:00
parent cf04997289
commit e5f1ffb21d
4 changed files with 103 additions and 63 deletions

95
Jenkinsfile vendored Normal file
View File

@ -0,0 +1,95 @@
pipeline {
agent any
environment {
SSH_ROOT_PASSWORD = credentials('ssh_root_password') /
PSQL_PASSWORD = credentials('PSQL_pass')
ANSIBLE_HOST_KEY_CHECKING = 'False'
parameters {
string(name: 'DB_SERVER_IP', defaultValue: '192.168.0.71', description: 'IP-адрес сервера')
string(name: 'SSH_USER', defaultValue: 'root', description: 'Имя пользователя для SSH подключения')
string(name: 'DB_USER', defaultValue: 'postgres', description: 'Имя пользователя базы данных')
string(name: 'DB_NAME', defaultValue: 'mydb', description: 'Имя базы данных')
}
stages {
stage('Clone repository') {
steps {
git branch: 'main', url: 'http://192.168.0.70:3000/coursework/courseworkrep.git'
}
}
stage('Install PostgreSQL') {
steps {
script {
def tempDir = sh(script: 'mktemp -d', returnStdout: true).trim()
def hostsFile = "${tempDir}/hosts.ini"
echo "Temporary directory created: ${tempDir}"
writeFile file: hostsFile, text: """
[postgres_servers]
${params.DB_SERVER_IP} ansible_user=${params.SSH_USER} ansible_password=${SSH_ROOT_PASSWORD} ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
"""
sh "cat ${hostsFile}"
sh """
ansible-playbook -i ${hostsFile} install_postgresql.yml \
-e postgres_user=${params.DB_USER} \
-e postgres_password=${PSQL_PASSWORD} \
-e postgres_db=${params.DB_NAME}
"""
sh "rm -rf ${tempDir}"
echo "Temporary directory ${tempDir} has been removed"
}
}
}
stage('Setup PostgreSQL Backup with Cron') {
steps {
script {
def tempDir = sh(script: 'mktemp -d', returnStdout: true).trim()
def hostsFile = "${tempDir}/hosts.ini"
echo "Temporary directory created: ${tempDir}"
writeFile file: hostsFile, text: """
[postgres_servers]
${params.DB_SERVER_IP} ansible_user=${params.SSH_USER} ansible_password=${SSH_ROOT_PASSWORD} ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
"""
sh "cat ${hostsFile}"
sh """
ansible-playbook -i ${hostsFile} backup_postgresql.yml \
-e postgres_user=${params.DB_USER} \
-e postgres_password=${PSQL_PASSWORD} \
-e postgres_db=${params.DB_NAME} \
-e backup_dir=/path/to/backup
"""
sh "rm -rf ${tempDir}"
echo "Temporary directory ${tempDir} has been removed"
}
}
}
}
post {
always {
echo "Pipeline completed"
}
failure {
echo "Pipeline failed"
}
success {
echo "Pipeline succeeded"
}
}
}

View File

@ -1,13 +1,13 @@
--- ---
- name: Backup PostgreSQL and set up cron job - name: Backup PostgreSQL and create cron
hosts: postgres_servers hosts: postgres_servers
become: true become: true
vars: vars:
backup_dir: "/var/backups/postgresql" backup_dir: "{{ backup_dir }}"
tasks: tasks:
- name: Ensure backup directory exists - name: Create backup directory
file: file:
path: "{{ backup_dir }}" path: "{{ backup_dir }}"
state: directory state: directory
@ -15,7 +15,7 @@
group: postgres group: postgres
mode: '0755' mode: '0755'
- name: Perform PostgreSQL database backup - name: PostgreSQL database backup
become_user: postgres become_user: postgres
command: > command: >
pg_dump -U {{ postgres_user }} pg_dump -U {{ postgres_user }}
@ -25,7 +25,7 @@
environment: environment:
PGPASSWORD: "{{ postgres_password }}" PGPASSWORD: "{{ postgres_password }}"
- name: Create cron job for hourly PostgreSQL backup - name: Create cron backup script
cron: cron:
name: "PostgreSQL hourly backup" name: "PostgreSQL hourly backup"
user: postgres user: postgres

View File

@ -71,7 +71,7 @@
state: present state: present
- name: Create a table for storing names and phone numbers - name: Create a table
community.postgresql.postgresql_query: community.postgresql.postgresql_query:
db: "{{ postgres_db }}" db: "{{ postgres_db }}"
query: > query: >
@ -83,11 +83,11 @@
login_user: "{{ postgres_user }}" login_user: "{{ postgres_user }}"
login_password: "{{ postgres_password }}" login_password: "{{ postgres_password }}"
- name: Открыть порт PostgreSQL 5432 в файрволе - name: open firewall 5432 port
command: firewall-cmd --add-port=5432/tcp --permanent command: firewall-cmd --add-port=5432/tcp --permanent
become: yes become: yes
- name: Перезагрузить файрвол для применения изменений - name: Restart Firewall
command: firewall-cmd --reload command: firewall-cmd --reload
become: yes become: yes

View File

@ -1,55 +0,0 @@
pipeline {
agent any
parameters {
string(name: 'TARGET_IP', defaultValue: '192.168.0.72', description: 'IP адрес машины для установки PostgreSQL')
string(name: 'DB_USER', defaultValue: 'myuser', description: 'Имя пользователя для базы данных PostgreSQL')
string(name: 'DB_NAME', defaultValue: 'mydatabase', description: 'Имя базы данных PostgreSQL')
string(name: 'BACKUP_DIR', defaultValue: '/var/backups/postgresql', description: 'Путь для сохранения резервных копий')
}
environment {
ANSIBLE_HOST_KEY_CHECKING = 'False'
}
stages {
stage('Install PostgreSQL') {
steps {
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) {
script {
writeFile file: '/ansiblesql/hosts.ini', text: """
[postgres_servers]
${params.TARGET_IP} ansible_user=${SSH_USER} ansible_ssh_pass=${POSTGRES_PASSWORD} ansible_connection=ssh
"""
sh """
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/install_postgresql.yml \\
-e postgres_user=${params.DB_USER} \\
-e postgres_password=${POSTGRES_PASSWORD} \\
-e postgres_db=${params.DB_NAME}
"""
}
}
}
}
stage('Setup PostgreSQL Backup with Cron') {
steps {
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) {
script {
sh """
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/backup_postgresql.yml \\
-e postgres_user=${params.DB_USER} \\
-e postgres_password=${POSTGRES_PASSWORD} \\
-e postgres_db=${params.DB_NAME} \\
-e backup_dir=${params.BACKUP_DIR}
"""
}
}
}
}
}
}