Skip to content

Commit

Permalink
Test 1
Browse files Browse the repository at this point in the history
  • Loading branch information
YisDav committed Nov 12, 2024
1 parent ed5c197 commit ff41ae2
Showing 1 changed file with 51 additions and 29 deletions.
80 changes: 51 additions & 29 deletions .github/workflows/distributed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ on:
env:
COMPOSITE_NAME: "linux-SUBNAME-amd64"
ALLOCATOR_PATH: "/tmp/allocator_instance"
INSTANCE_NAMES: "wi1 wi2 wi3 dashboard manager worker"
PRIVATE_KEY_PATH: "/tmp/allocator_instance/id_rsa"
permissions:
id-token: write # JWT
contents: read # actions/checkout
Expand All @@ -44,7 +46,7 @@ jobs:
if [ "${{ inputs.SYSTEMS }}" != "null" && "${{ inputs.SYSTEMS }}" != "" ]; then
SYSTEMS_LIST=${{ inputs.SYSTEMS }}
else
SYSTEMS_LIST='["CentOS_8", "AmazonLinux_2", "Ubuntu_22", "RHEL8"]'
SYSTEMS_LIST='["Ubuntu_22"]'
fi
echo "SYSTEMS_JSON=$(echo $SYSTEMS_LIST | jq -c '.')" >> $GITHUB_OUTPUT
echo "Systems JSON: $SYSTEMS_JSON"
Expand All @@ -57,6 +59,27 @@ jobs:
fi
echo "VERBOSITY=$VERBOSITY" >> $GITHUB_OUTPUT
echo "Verbosity level: $VERBOSITY"
- name: Configure AWS credentials (assume role)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
role-session-name:
aws-region: us-east-1
- name: Create common SSH key pair in the ALLOCATOR_PATH
run: |
mkdir -p $ALLOCATOR_PATH
key_name=gha_ansible_key_$(date +%s)
private_key_file=$ALLOCATOR_PATH/${key_name}
aws ec2 create-key-pair --key-name ${key_name} > key_pair_output.json
jq -r '.KeyMaterial' key_pair_output.json > ${private_key_file}
# Set PRIVATE_KEY_PATH
echo "PRIVATE_KEY_PATH=${private_key_file}" >> $GITHUB_ENV
- name: Upload artifact (ALLOCATOR_PATH directory)
uses: actions/upload-artifact@v4
with:
name: allocator-instance-ssh-${{ github.run_id }}
path: ${{ env.ALLOCATOR_PATH }}
outputs:
SYSTEMS_JSON: ${{ steps.compute-outputs.outputs.SYSTEMS_JSON }}
VERBOSITY: ${{ steps.compute-outputs.outputs.VERBOSITY }}
Expand All @@ -82,6 +105,11 @@ jobs:
ref: ${{ inputs.AUTOMATION_REFERENCE }}
token: ${{ secrets.GH_CLONE_TOKEN }}
path: wazuh-automation/
- name: Download the (SSH) artifact
uses: actions/download-artifact@v4
with:
name: allocator-instance-ssh-${{ github.run_id }}
path: ${{ env.ALLOCATOR_PATH }}
- name: Install python and create virtual environment
run: |
sudo apt-get update
Expand Down Expand Up @@ -135,10 +163,9 @@ jobs:
- name: Allocate cluster of EC2 instances
id: allocator_instance
run: |
instance_names=("wi1" "wi2" "wi3", "dashboard", "manager", "worker")
instance_names=($INSTANCE_NAMES)
inventory_file="$ALLOCATOR_PATH/inventory_all"
mkdir -p $ALLOCATOR_PATH
ssh_key_path=${{ env.PRIVATE_KEY_PATH }}
for i in ${!instance_names[@]}; do
instance_name=${instance_names[$i]}
Expand All @@ -149,6 +176,7 @@ jobs:
--composite-name ${{ env.COMPOSITE_NAME }} \
--working-dir $ALLOCATOR_PATH --track-output $ALLOCATOR_PATH/track_${instance_name}.yml \
--inventory-output $ALLOCATOR_PATH/inventory_${instance_name}.yml \
--ssh-key "$ssh_key_path" \
--instance-name gha_ansible_${{ matrix.system }}_${instance_name}_${{ github.run_id }} --label-team devops --label-termination-date 1d
instance_id=$(grep '^identifier' $ALLOCATOR_PATH/track_${instance_name}.yml | awk '{print $2}')
Expand All @@ -175,24 +203,25 @@ jobs:
elif [[ $i -eq 5 ]]; then
echo "worker ansible_host=$ansible_host private_ip=$private_ip" >> $inventory_file
fi
if [[ $i -eq ${#instance_names[@]}-1 ]]; then
echo "[wi_cluster]" >> $inventory_file
echo "wi1" >> $inventory_file
echo "wi2" >> $inventory_file
echo "wi3" >> $inventory_file
echo "[all:vars]" >> $inventory_file
echo "ansible_ssh_user=$ansible_user" >> $inventory_file
echo "ansible_port=$ansible_port" >> $inventory_file
echo "ansible_ssh_private_key_file=$ansible_ssh_private_key_file" >> $inventory_file
echo "ansible_ssh_common_args='$ansible_ssh_common_args'" >> $inventory_file
echo "ansible_ssh_extra_args='-o StrictHostKeyChecking=no'" >> $inventory_file
fi
) &
done
# Wait for all provisioning tasks to complete
wait
echo "" >> $inventory_file # blank line to separate groups
echo "[wi_cluster]" >> $inventory_file
echo "wi1" >> $inventory_file
echo "wi2" >> $inventory_file
echo "wi3" >> $inventory_file
echo "" >> $inventory_file
echo "[all:vars]" >> $inventory_file
echo "ansible_ssh_user=$ansible_user" >> $inventory_file
echo "ansible_port=$ansible_port" >> $inventory_file
echo "ansible_ssh_private_key_file=$ALLOCATOR_PATH/id_rsa" >> $inventory_file
echo "ansible_ssh_common_args='$ansible_ssh_common_args'" >> $inventory_file
echo "ansible_ssh_extra_args='-o StrictHostKeyChecking=no'" >> $inventory_file
- name: Save ALLOCATOR_PATH directory as artifact
uses: actions/upload-artifact@v4
with:
Expand All @@ -202,16 +231,13 @@ jobs:
uses: actions/checkout@v4
- name: Ansible Playbook run Wazuh Distributed Production Ready
run: |
# ansible-playbook ./.github/playbooks/aio-wazuh.yml \
ansible-playbook playbooks/wazuh-production-ready.yml -b -K \
-i $ALLOCATOR_PATH/inventory_all \
-l all \
${{ needs.setup-runner.outputs.VERBOSITY }}
stop-runner:
name: Stop allocated EC2 instance
needs:
# - start-runner # required to get output from the start-runner job
# - install-aio-single-instance # required to wait when the main job is done
- setup-runner
- start-runner
runs-on: ubuntu-latest
Expand Down Expand Up @@ -250,18 +276,14 @@ jobs:
path: ${{ env.ALLOCATOR_PATH }}
- name: Delete allocated EC2 instance
run: |
# python3 wazuh-automation/deployability/modules/allocation/main.py \
# --action delete
# --track-output $ALLOCATOR_PATH/track_${instance_name}.yml
instance_names=($INSTANCE_NAMES)
for i in ${!instance_names[@]}; do
instance_name=${instance_names[$i]}
# Delete instance in parallel
track_file="$ALLOCATOR_PATH/track_${instance_name}.yml"
echo "Deleting instance: $instance_name using track file $track_file"
(
# Delete instance
python3 wazuh-automation/deployability/modules/allocation/main.py \
--action delete --provider aws --size large \
--composite-name ${{ env.COMPOSITE_NAME }} \
--working-dir $ALLOCATOR_PATH --track-output $ALLOCATOR_PATH/track_${instance_name}.yml \
--inventory-output $ALLOCATOR_PATH/inventory_${instance_name}.yml \
--instance-name gha_ansible_${{ matrix.system }}_${instance_name} _${{ github.run_id }} --label-team devops --label-termination-date 1d
--action delete --provider aws --track-output $track_file
) &
done

0 comments on commit ff41ae2

Please sign in to comment.