-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
306 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
aws_autoscaling_event_pattern: | ||
source: | ||
- aws.autoscaling | ||
detail-type: | ||
- "EC2 Instance Launch Successful" | ||
- "EC2 Instance-terminate Lifecycle Action" | ||
- "EC2 Instance Terminate Successful" | ||
|
||
# Those we don't handle for now | ||
# XXX(Szpadel): Why we cannot handle those? | ||
# - "EC2 Instance-launch Lifecycle Action" | ||
# - "EC2 Instance Terminate Successful" | ||
# - "EC2 Instance Launch Unsuccessful" | ||
# - "EC2 Instance Terminate Unsuccessful" | ||
|
||
|
||
aws_autoscaling_triggers_list: [] | ||
# - id: | ||
# arn: | ||
|
||
aws_autoscaling_lambda_functions_list: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
- name: Setup autoscaling event trigger | ||
cloudwatchevent_rule: | ||
name: "trigger-autoscaling-lambda-{{ mageops_app_name }}" | ||
region: "{{ aws_region }}" | ||
state: present | ||
event_pattern: "{{ aws_autoscaling_event_pattern | to_json }}" | ||
targets: "{{ aws_autoscaling_triggers_list }}" | ||
|
||
register: aws_autoscaling_event | ||
|
||
- name: Allow autoscaling event handler lambda to be executed by CloudWatch Events | ||
lambda_policy: | ||
state: present | ||
region: "{{ aws_region }}" | ||
function_name: "{{ item }}" | ||
action: "lambda:InvokeFunction" | ||
principal: "events.amazonaws.com" | ||
statement_id: "AWSEvents-handleAutoscalingEvent-{{ mageops_app_name }}" | ||
source_arn: "{{ aws_autoscaling_event.rule.arn }}" | ||
with_items: "{{ aws_autoscaling_lambda_functions_list }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
roles/cs.aws-iam/templates/handle_node_coordinator_lambda.policy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"ec2:DescribeInstances", | ||
"ec2:DeleteTags", | ||
"ec2:CreateTags" | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
aws_lambda_node_coordinator_package_url: "https://github.com/mageops/aws-lambda-node-coordinator/releases/download/v0.1.4/lambda.zip" | ||
aws_lambda_node_coordinator_package_path: "{{ mageops_ansible_temp_dir }}/node_coordinator_lambda_v0.1.4.zip" | ||
aws_lambda_node_coordinator_runtime: nodejs12.x | ||
aws_lambda_handle_node_coordinator_autoscaling_event_name: "handleNodeCoordinatorAutoscalingEvent-{{ mageops_app_name }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dependencies: | ||
- role: cs.ansible-plugins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
- name: Download lambda deploy packages | ||
get_url: | ||
url: "{{ aws_lambda_node_coordinator_package_url }}" | ||
dest: "{{ aws_lambda_node_coordinator_package_path }}" | ||
|
||
- name: Register lambda handler for coordinating nodes | ||
lambda: | ||
name: "{{ aws_lambda_handle_node_coordinator_autoscaling_event_name }}" | ||
state: present | ||
zip_file: "{{ aws_lambda_node_coordinator_package_path }}" | ||
runtime: "{{ aws_lambda_node_coordinator_runtime }}" | ||
role: "arn:aws:iam::{{ aws_account_id }}:role/{{ aws_iam_role_node_coordinator_lambda_execution }}" | ||
handler: index.handler | ||
region: "{{ aws_region }}" | ||
timeout: 15 | ||
environment_variables: | ||
ENVIRONMENT: "{{ mageops_environment }}" | ||
PROJECT: "{{ mageops_project }}" | ||
register: handle_autoscaling_event_lambda | ||
|
||
- name: Register autoscaling trigger | ||
set_fact: | ||
coordinator_autoscaling_trigger: | ||
id: "{{ aws_lambda_handle_node_coordinator_autoscaling_event_name }}-target" | ||
arn: "{{ handle_autoscaling_event_lambda.configuration.function_arn }}" | ||
|
||
- name: Append trigger to list | ||
set_fact: | ||
aws_autoscaling_triggers_list: "{{ aws_autoscaling_triggers_list + [coordinator_autoscaling_trigger] }}" | ||
aws_autoscaling_lambda_functions_list: "{{ aws_autoscaling_lambda_functions_list + [aws_lambda_handle_node_coordinator_autoscaling_event_name] }}" | ||
|
||
- name: Require cs.aws-autoscaling-triggers finalizer | ||
set_fact: | ||
finalize_roles: "{{ finalize_roles + ['cs.aws-autoscaling-triggers'] }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Should this service be enabled | ||
aws_magento_cron_enabled: no | ||
|
||
aws_magento_cron_files_install: | ||
- { src: "aws-cron-service.sh", dest: "/usr/local/libexec/aws-cron-service.sh", mode: "0755" } | ||
|
||
aws_magento_cron_files_install_templated: | ||
- { src: "magento-cron-aws.service.j2", dest: "/etc/systemd/system/magento-cron-aws.service" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
|
||
get_current_instance_id() { | ||
curl -Lsf http://instance-data/latest/meta-data/instance-id | ||
} | ||
|
||
get_current_region() { | ||
curl -Lsf http://instance-data/latest/meta-data/placement/availability-zone | sed 's/[a-z]$//' | ||
} | ||
|
||
get_ec2_tag_value() { | ||
local region=$1 | ||
local instance_id=$2 | ||
local tag_name=$3 | ||
|
||
aws ec2 describe-tags --region "${region}" --filters "Name=resource-id,Values=${instance_id}" "Name=key,Values=${tag_name}" | jq -r '.Tags[].Value' | ||
} | ||
|
||
is_tag_exists() { | ||
local tag_name=$1 | ||
local tag_value=$2 | ||
|
||
local instance_id | ||
local found_value | ||
local region | ||
region="$(get_current_region)" | ||
instance_id="$(get_current_instance_id)" | ||
found_value="$(get_ec2_tag_value "$region" "$instance_id" "$tag_name")" | ||
|
||
if [ "$tag_value" = "$found_value" ];then | ||
return 0 | ||
fi | ||
return 1 | ||
} | ||
|
||
trigger_cron() { | ||
cd "$MAGENTO_ROOT_DIR" | ||
php "$MAGENTO_ROOT_DIR/bin/magento" cron:run | ||
} | ||
|
||
main() { | ||
if [ -z "$MAGENTO_ROOT_DIR" ];then | ||
echo "MAGENTO_ROOT_DIR env is not set!" | ||
exit 1 | ||
fi | ||
while sleep 60;do | ||
if is_tag_exists "Cron" "yes";then | ||
trigger_cron & | ||
fi | ||
done | ||
} | ||
|
||
main "$@" |
Oops, something went wrong.