Creating a new tag for updating the master os - master-update 604/head
authorAdam Kraitman <akraitma@redhat.com>
Sun, 31 Jan 2021 18:46:07 +0000 (20:46 +0200)
committerAdam Kraitman <akraitma@redhat.com>
Sun, 31 Jan 2021 18:46:07 +0000 (20:46 +0200)
And also doing changes to the rook-recovery role

Signed-off-by: Adam Kraitman <akraitma@redhat.com>
roles/rook-recovery/tasks/main.yml
roles/rook-recovery/tasks/master-update.yml [new file with mode: 0644]
roles/rook-recovery/vars/main.yml

index 5b1dc37f5ee534c454067927c15bbc85c7a74762..84a0b066311529218bc86b73352125023f5d7d33 100644 (file)
@@ -5,17 +5,21 @@
     tags:
       - always
 
+  - import_tasks: master-update.yml
+    tags:
+      - master-update
+
   - name: Launch instance
     ec2:
       aws_access_key: "{{ aws_access_key }}"
       aws_secret_key: "{{ aws_secret_key }}"
       key_name: "{{ keypair }}"
       group: "{{ security_group }}"
-      instance_type: "{{ instance_type }}"
+      instance_type: "{{ master_instance_type }}"
       image: "{{ image }}"
       wait: true
       region: "{{ region }}"
-      vpc_subnet_id: "{{ vpc_subnet_id }}" 
+      vpc_subnet_id: "{{ vpc_subnet_id }}"
       assign_public_ip: yes
       instance_tags: "{{ aws_tags }}"
       wait: yes
       label: "{{ item.id }} - {{ item.public_ip }}"
 
   - name: Remove jenkins docker old container
-    command: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ubuntu@"{{ item.public_ip }}" sudo docker rm jenkins
+    command: ssh -i "{{ rook_key }}" ubuntu@"{{ item.public_ip }}" sudo docker rm jenkins
     with_items: '{{ ec2_instances.instances }}'
     loop_control:
       label: "{{ item.id }} - {{ item.public_ip }}"
 
   - name: Start jenkins container
-    command: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ubuntu@"{{ item.public_ip }}" sudo docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -v /mnt/jenkins/jenkins:/var/jenkins_home "{{ jenkins_image }}" 
+    command: ssh -i "{{ rook_key }}" ubuntu@"{{ item.public_ip }}" sudo docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -v /mnt/jenkins/jenkins:/var/jenkins_home "{{ jenkins_master_image }}"
     with_items: '{{ ec2_instances.instances }}'
     loop_control:
       label: "{{ item.id }} - {{ item.public_ip }}"
 
-  - name: The instance was succssfuly started  
+  - name: The instance was succssfuly started
     debug:
       msg:
-      - "The Rook Jenkins is up and running the instance is named: {{ aws_tags.Name }}-{{ '%02d' | format(item.0 + 1) }}" 
+      - "The Rook Jenkins is up and running the instance is named: {{ aws_tags.Name }}-{{ '%02d' | format(item.0 + 1) }}"
       - "In order to make him avalible to public network you will need to add him to the load balancing target group"
     with_indexed_items: "{{ ec2_instances.instances }}"
     loop_control:
diff --git a/roles/rook-recovery/tasks/master-update.yml b/roles/rook-recovery/tasks/master-update.yml
new file mode 100644 (file)
index 0000000..c49fd1a
--- /dev/null
@@ -0,0 +1,56 @@
+---
+  - name: Gather facts
+    ec2_instance_facts:
+      filters:
+        "tag:Name": "{{ master_name }}"
+        instance-state-name: running
+    register: master_metadata
+
+  - name: create date
+    shell: "date +%d-%m-%y"
+    register: date
+
+  - name: Take a image of the master
+    ec2_ami:
+      aws_access_key: "{{ aws_access_key }}"
+      aws_secret_key: "{{ aws_secret_key }}"
+      instance_id: "{{ master_metadata.instances[0].instance_id }}"
+      no_reboot: yes
+      wait: yes
+      name: "{{ master_name }}-{{ date.stdout }}"
+      tags:
+        Name: "{{ master_name }}-{{ date.stdout }}"
+
+  - name: Update apt cache
+    command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" sudo apt-get update
+  - name: Update packages
+    command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" sudo apt-get upgrade -y
+
+  - name: Check if system requires reboot
+    command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" [ -f /var/run/reboot-required ]; echo $?
+    register: reboot
+
+  - name: Reboot if required
+    command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" sudo reboot
+    ignore_errors: yes
+    when: reboot.stdout == '0'
+
+  - name: Wait for SSH to come up
+    wait_for: host={{ master_metadata.instances[0].public_dns_name }} port=22 delay=60 timeout=320 state=started
+    when: reboot.stdout == '0'
+
+  - name: Check if old container exist
+    command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" docker ps -a | grep -i jenkins | wc -l
+    register: container
+    when: reboot.stdout == '0'
+
+  - name: Remove jenkins old container if exist
+    command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" sudo docker rm jenkins
+    when:
+      - container.stdout == '1'
+      - reboot.stdout == '0'
+
+  - name: Start jenkins container
+    command: ssh -i "{{ rook_key }}" ubuntu@"{{ master_metadata.instances[0].public_dns_name }}" sudo docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -v /mnt/jenkins/jenkins:/var/jenkins_home "{{ jenkins_master_image }}"
+    when: reboot.stdout == '0'
index b67ac6d536bb719a6e8786014ee2e9f38a1e4196..f3ce2ade5f5f37e6806b2f2752e66b2c10ecba2d 100644 (file)
@@ -1,7 +1,7 @@
 ---
-jenkins_image: jenkins/jenkins:2.190.1
+jenkins_master_image: jenkins/jenkins:2.235.5
 keypair: root-jenkins-new-key
-instance_type: m4.large
+master_instance_type: m4.large
 security_group: rook-jenkins-group
 image: ami-09c003703cafa9056
 region: us-east-1
@@ -10,3 +10,5 @@ instance_name: Recovery-Rook-Jenkins
 aws_tags:
   Name: "{{ instance_name }}"
   Application: "Jenkins"
+master_name: Prod-Jenkins
+rook_key: "{{ secrets_path | mandatory }}/rook_key.yml"