8000 Add Multi-AZ Aurora DB to CI store-integration-tests by ryanemerson · Pull Request #26970 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/actions/aurora-create-database/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Create Aurora Database
description: Create AWS Aurora Database

inputs:
name:
description: 'The name of the Aurora DB cluster to deploy'
required: true
region:
description: 'The AWS region used to host the Aurora DB'
required: true
password:
description: 'The master password of the Aurora DB cluster'
required: false

outputs:
endpoint:
description: 'The Endpoint URL for Aurora clients to connect to'
value: ${{ steps.create.outputs.endpoint }}

runs:
using: "composite"
steps:
- id: create
shell: bash
run: |
source ./aurora_create.sh
echo "endpoint=${AURORA_ENDPOINT}" >> $GITHUB_OUTPUT
working-directory: .github/scripts/aws/rds
env:
AURORA_CLUSTER: ${{ inputs.name }}
AURORA_PASSWORD: ${{ inputs.password }}
AURORA_REGION: ${{ inputs.region }}
21 changes: 21 additions & 0 deletions .github/actions/aurora-delete-database/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Delete Aurora Database
description: Delete AWS Aurora Database

inputs:
name:
description: 'The name of the Aurora DB cluster to delete'
required: true
region:
description: 'The AWS region hosting the Aurora DB'
required: true

runs:
using: "composite"
steps:
- id: delete
shell: bash
run: ./aurora_delete.sh
working-directory: .github/scripts/aws/rds
env:
AURORA_CLUSTER: ${{ inputs.name }}
AURORA_REGION: ${{ inputs.region }}
7 changes: 7 additions & 0 deletions .github/scripts/ansible/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Ansible
###########
*_inventory.yml
*.pem
ansible.log
files/
env.yml
8 changes: 8 additions & 0 deletions .github/scripts/ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[defaults]
#log_path = ./ansible.log
host_key_checking=False
transport = ssh
forks = 50

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o ForwardAgent=yes -o StrictHostKeyChecking=no -o IdentitiesOnly=yes
26 changes: 26 additions & 0 deletions .github/scripts/ansible/aws_ec2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
set -e
cd $(dirname "${BASH_SOURCE[0]}")

if [[ "$RUNNER_DEBUG" == "1" ]]; then
set -x
fi

OPERATION=$1
REGION=$2

case $OPERATION in
requirements)
ansible-galaxy collection install -r requirements.yml
pip3 install --user ansible boto3 botocore
;;
create|delete|start|stop)
if [ -f "env.yml" ]; then ANSIBLE_CUSTOM_VARS_ARG="-e @env.yml"; fi
CLUSTER_NAME=${CLUSTER_NAME:-"keycloak_$(whoami)"}
ansible-playbook aws_ec2.yml -v -e "region=$REGION" -e "operation=$OPERATION" -e "cluster_name=$CLUSTER_NAME" $ANSIBLE_CUSTOM_VARS_ARG "${@:3}"
;;
*)
echo "Invalid option!"
echo "Available operations: requirements, create, delete, start, stop."
;;
esac
3 changes: 3 additions & 0 deletions .github/scripts/ansible/aws_ec2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- hosts: localhost
connection: local
roles: [aws_ec2]
2 changes: 2 additions & 0 deletions .github/scripts/ansible/keycloak.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- hosts: keycloak
roles: [keycloak_ec2_installer]
15 changes: 15 additions & 0 deletions .github/scripts/ansible/keycloak_ec2_installer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -e
cd $(dirname "${BASH_SOURCE[0]}")

if [[ "$RUNNER_DEBUG" == "1" ]]; then
set -x
fi

REGION=$1
KEYCLOAK_SRC=$2

CLUSTER_NAME=${CLUSTER_NAME:-"keycloak_$(whoami)"}

ansible-playbook -i ${CLUSTER_NAME}_${REGION}_inventory.yml keycloak.yml \
-e "keycloak_src=\"${KEYCLOAK_SRC}\""
2 changes: 2 additions & 0 deletions .github/scripts/ansible/mvn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- hosts: keycloak
roles: [mvn_ec2_runner]
15 changes: 15 additions & 0 deletions .github/scripts/ansible/mvn_ec2_runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -e
cd $(dirname "${BASH_SOURCE[0]}")

if [[ "$RUNNER_DEBUG" == "1" ]]; then
set -x
fi

REGION=$1
MVN_PARAMS=${@:2}

CLUSTER_NAME=${CLUSTER_NAME:-"keycloak_$(whoami)"}

ansible-playbook -i ${CLUSTER_NAME}_${REGION}_inventory.yml mvn.yml \
-e "mvn_params=\"${MVN_PARAMS}\""
3 changes: 3 additions & 0 deletions .github/scripts/ansible/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
collections:
- name: amazon.aws
version: 6.0.0
91 changes: 91 additions & 0 deletions .github/scripts/ansible/roles/aws_ec2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Ansible Role `aws_ec2`

Ansible role for creating, deleting, stopping and starting AWS EC2 instances
for running keycloak tests.

## Prerequisities

Role requires Ansible Collection `amazon.aws` version `6.0.0` or higher.

Role assumes that user is authenticated to use AWS CLI, ie. that authentication
variables `AWS_ACCESS_KEY` and `AWS_SECRET_KEY` are set in the environment.


## Parameters
- `region`: AWS region for the resources to be created in.
- `cluster_name`: Unique name of the instance cluster within the region. Defaults to `keycloak_{{ cluster_identifier }}`.
- `cluster_identifier`: Identifier to distingish multiple clusters within the region. Defaults to `${USER}`.
- `cluster_size`: Number of EC2 instances to be created.
- `ami_name`: Name of the AMI image to be used for spawning instances.
- `instance_type`: [AWS instance type](https://aws.amazon.com/ec2/instance-types/).
- `instance_volume_size`: Size of instance storage device.
- `instance_device`: Path to Linux storage device.

For defaults see `defaults/main.yml`.


## Example Playbook

Example playbook `aws_ec2.yml`:
```
- hosts: localhost
connection: local
roles: [aws_ec2_client]
```

## Create Instances

Using the example playbook run:
```
ansible-playbook aws_ec2.yml -e region=<REGION> -e operation=create
```

Replace <REGION> with actual value, e.g. `us-west-1`.

Optionally you can override other parameters by `-e PARAMETER=VALUE` or `-e @PARAMS.yml`.

This operation will create the following 2 files:
- `{{ cluster_name }}_{{ region }}.pem` - private SSH key.
- `{{ cluster_name }}_{{ region }}_inventory.yml` - an Ansible host inventory file.

```
keycloak:
children:
"{{ cluster_name }}_{{ region }}":
vars:
ansible_user: ec2-user
ansible_become: yes
ansible_ssh_private_key_file: "{{ cluster_name }}_{{ region }}.pem"
hosts:
host-1-ip-address:
host-2-ip-address:
...
```

Notice that the created hosts will be included in Ansible group `keycloak`
and subgroup `{{ cluster_name }}_{{ region }}`.


## Stop and Start instances

Using the example playbook run:
```
ansible-playbook aws_ec2.yml -e region=<REGION> -e operation=stop
```

After the instances are stopped their public IP addresses will be de-allocated.

```
ansible-playbook aws_ec2.yml -e region=<REGION> -e operation=start
```

After the instances are started again the role will re-create the host inventory file with updated public IP addresses.


## Delete Instances
Using the example playbook run:
```
ansible-playbook aws_ec2.yml -e region=<REGION> -e operation=delete
```

This will remove created AWS resources and delete the host inventory file and private key.
13 changes: 13 additions & 0 deletions .github/scripts/ansible/roles/aws_ec2/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cluster_identifier: "{{ lookup('env', 'USER') }}"
cluster_name: "keycloak_{{ cluster_identifier }}"
cluster_size: 1

cidr_ip: "{{ control_host_ip.stdout }}/32"

ami_name: RHEL-8.8.0_HVM-20230503-x86_64-54-Hourly2-GP2

instance_type: t3.large
instance_volume_size: 20
instance_device: /dev/sda1

no_log_sensitive: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
- name: Get Ansible Control Host's public IP
shell: curl -ks --ipv4 https://ifconfig.me
register: control_host_ip
no_log: "{{ no_log_sensitive }}"

- debug: var=cidr_ip

- name: Create Security Group
amazon.aws.ec2_group:
state: present
region: '{{ region }}'
name: '{{ cluster_name }}'
description: '{{ cluster_name }}'
rules:
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: '{{cidr_ip}}'
register: group
no_log: "{{ no_log_sensitive }}"

- name: Create Key
amazon.aws.ec2_key:
state: present
region: '{{ region }}'
name: '{{ cluster_name }}'
register: key
no_log: "{{ no_log_sensitive }}"

- name: Save Private Key on Ansible Control Machine
when: key.changed
copy:
content: '{{ key.key.private_key }}'
dest: '{{ cluster_name }}_{{ region }}.pem'
mode: 0600
no_log: "{{ no_log_sensitive }}"

- name: Look up AMI '{{ ami_name }}'
amazon.aws.ec2_ami_info:
region: '{{ region}}'
filters:
name: '{{ ami_name }}'
register: ami_info

- name: Create {{ cluster_size }} EC2 Instances
amazon.aws.ec2_instance:
state: started
region: '{{ region }}'
name: "{{ cluster_name }}"
exact_count: "{{ cluster_size }}"
instance_type: '{{ instance_type }}'
image_id: '{{ ami_info.images[0].image_id }}'
key_name: '{{ cluster_name }}'
security_group: '{{ group.group_id }}'
network:
assign_public_ip: yes
volumes:
- device_name: '{{ instance_device }}'
ebs:
volume_size: '{{ instance_volume_size }}'
delete_on_termination: true
register: instances
no_log: "{{ no_log_sensitive }}"

- name: Create Inventory File
template:
src: inventory.yml.j2
dest: '{{ cluster_name }}_{{ region }}_inventory.yml'
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- name: 'Delete EC2 instances'
amazon.aws.ec2_instance:
state: absent
region: '{{ region }}'
filters:
"tag:Name": '{{ cluster_name }}*'

- name: 'Delete EC2 security group'
amazon.aws.ec2_group:
state: absent
region: '{{ region }}'
name: '{{ cluster_n 10BC8 ame }}'

- name: 'Delete Key'
amazon.aws.ec2_key:
state: absent
region: '{{ region }}'
name: '{{ cluster_name }}'

- name: 'Delete inventory, key, and log'
file:
state: absent
path: '{{ item }}'
with_items:
- '{{ cluster_name }}_{{ region }}_inventory.yml'
- '{{ cluster_name }}_{{ region }}.pem'
12 changes: 12 additions & 0 deletions .github/scripts/ansible/roles/aws_ec2/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- debug: var=cluster_identifier
- debug: var=region
- debug: var=cluster_name

- include_tasks: create-resources.yml
when: operation == "create"

- include_tasks: manage-instances.yml
when: operation == "start" or operation == "stop"

- include_tasks: delete-resources.yml
when: operation == "delete"
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Start or Stop Instances
- name: "{{ operation[0]|upper }}{{ operation[1:] }} Instances"
amazon.aws.ec2_instance:
state: '{{ "stopped" if operation == "stop" else "started" }}'
region: '{{ region }}'
filters:
"tag:Name": '{{ cluster_name }}*'
instance-state-name: ['running', 'stopped', 'stopping']
no_log: "{{ no_log_sensitive }}"

- when: operation == "start"
block:
# When starting instances via `ec2_instance` module sometimes the `public_ip_address` is missing in the result.
# Added additional `ec2_instance_info` step to work around the issue.
- name: Get Instance Information
amazon.aws.ec2_instance_info:
region: '{{ region }}'
filters:
"tag:Name": '{{ cluster_name }}*'
instance-state-name: ['running']
register: instances
no_log: "{{ no_log_sensitive }}"
- name: Recreate Inventory File
template:
src: inventory.yml.j2
dest: '{{ cluster_name }}_{{ region }}_inventory.yml'
Loading
0