From e5f1ffb21d718f0904436b6ebbfa475c7ebf666e Mon Sep 17 00:00:00 2001 From: dima Date: Sun, 20 Oct 2024 13:37:39 +0300 Subject: [PATCH] git integration. renamed pipeline.groovy to jenkinsfile --- Jenkinsfile | 95 ++++++++++++++++++++++++++++++++++++++++++ backup_postgresql.yml | 10 ++--- install_postgresql.yml | 6 +-- pipeline.groovy | 55 ------------------------ 4 files changed, 103 insertions(+), 63 deletions(-) create mode 100644 Jenkinsfile delete mode 100644 pipeline.groovy diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..d9f6a00 --- /dev/null +++ b/Jenkinsfile @@ -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" + } + } +} diff --git a/backup_postgresql.yml b/backup_postgresql.yml index dc87313..8eaf780 100644 --- a/backup_postgresql.yml +++ b/backup_postgresql.yml @@ -1,13 +1,13 @@ --- - - name: Backup PostgreSQL and set up cron job + - name: Backup PostgreSQL and create cron hosts: postgres_servers become: true vars: - backup_dir: "/var/backups/postgresql" + backup_dir: "{{ backup_dir }}" tasks: - - name: Ensure backup directory exists + - name: Create backup directory file: path: "{{ backup_dir }}" state: directory @@ -15,7 +15,7 @@ group: postgres mode: '0755' - - name: Perform PostgreSQL database backup + - name: PostgreSQL database backup become_user: postgres command: > pg_dump -U {{ postgres_user }} @@ -25,7 +25,7 @@ environment: PGPASSWORD: "{{ postgres_password }}" - - name: Create cron job for hourly PostgreSQL backup + - name: Create cron backup script cron: name: "PostgreSQL hourly backup" user: postgres diff --git a/install_postgresql.yml b/install_postgresql.yml index 96f986e..3ad2356 100644 --- a/install_postgresql.yml +++ b/install_postgresql.yml @@ -71,7 +71,7 @@ state: present - - name: Create a table for storing names and phone numbers + - name: Create a table community.postgresql.postgresql_query: db: "{{ postgres_db }}" query: > @@ -83,11 +83,11 @@ login_user: "{{ postgres_user }}" login_password: "{{ postgres_password }}" - - name: Открыть порт PostgreSQL 5432 в файрволе + - name: open firewall 5432 port command: firewall-cmd --add-port=5432/tcp --permanent become: yes - - name: Перезагрузить файрвол для применения изменений + - name: Restart Firewall command: firewall-cmd --reload become: yes diff --git a/pipeline.groovy b/pipeline.groovy deleted file mode 100644 index a31e4ba..0000000 --- a/pipeline.groovy +++ /dev/null @@ -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} - """ - } - } - } - } - } -}