Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

role/deploy_flask_app - change required parameter key pair with path to ssh key file #109

Merged
merged 51 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
725e7ad
Fix integration test issue
abikouo Dec 19, 2023
35bc474
adding issue number
abikouo Dec 19, 2023
d15c275
Retrieve RDS instance info
abikouo Dec 19, 2023
83bdced
minor updates
abikouo Dec 19, 2023
7719bb8
setting file permission
abikouo Dec 20, 2023
30228f8
Fix issue with delete.yaml
abikouo Dec 20, 2023
9d11e47
try to ping the workers
abikouo Dec 20, 2023
4e5b860
try to ping the workers
abikouo Dec 20, 2023
fff9401
commit with aws alias
abikouo Dec 20, 2023
f6d536f
add credentials
abikouo Dec 20, 2023
fed9b8e
add path to ssh private key
abikouo Dec 20, 2023
0b40c4e
skip load balancer for now
abikouo Dec 20, 2023
eb7c6f3
update subnet value when creating load balancer
abikouo Dec 20, 2023
0e09fe5
fix inventory template issue
abikouo Dec 20, 2023
2552c00
fix inventory
abikouo Dec 20, 2023
1228fac
fix inventory
abikouo Dec 20, 2023
3d8f72c
full integration
abikouo Dec 21, 2023
12a48c1
install host packages via user_data when creating instances
abikouo Dec 21, 2023
7a16851
add IAM role to install package into bastion
abikouo Dec 21, 2023
584a227
minor updates
abikouo Dec 22, 2023
8af0f2c
update playbooks
abikouo Dec 22, 2023
94c1457
minor updates
abikouo Jan 8, 2024
91f82fb
Replace ansible.builtin.shell by ansible.builtin.command
abikouo Jan 8, 2024
4ad3c54
fix: playbook webapp - delete resources
abikouo Jan 8, 2024
37805f4
Remove dependency with aws_setup_credentials
abikouo Jan 9, 2024
b45004c
Remove unused playbook
abikouo Jan 9, 2024
0957321
add retries when trying to connect to ec2 instances
abikouo Jan 9, 2024
136d136
minor updates
abikouo Jan 9, 2024
8731e94
Remove all conditional checks
abikouo Jan 9, 2024
68588a1
update wait_timeout while creating load balancer
abikouo Jan 10, 2024
e7056b1
temporary commit
abikouo Jan 10, 2024
6dac13b
using roles
abikouo Jan 17, 2024
b38b83e
add missing paremeters
abikouo Jan 17, 2024
bd0a907
Update RDS result
abikouo Jan 18, 2024
b760acc
Revert update of tests/integration/targets/setup_rsa_keys
abikouo Jan 18, 2024
b32f8fc
Support storage of key pair into S3 bucket - Needed for running on AW…
abikouo Jan 18, 2024
b1e01ad
Fix ansible-lint
abikouo Jan 18, 2024
dd2eec0
Conditionnal delete for sshkey_file
abikouo Jan 19, 2024
985b23c
Remove confusing variable deploy_flask_app_region
abikouo Jan 19, 2024
3f13bc5
fix linters
abikouo Jan 19, 2024
eba57a0
minor update for code review
abikouo Jan 26, 2024
49187b8
Update 20231219-deploy_flask_app-update-arguments-spec.yml
abikouo Jan 31, 2024
0c620fc
minor code review updates
abikouo Jan 31, 2024
d6ea987
add missing deletion steps
abikouo Jan 31, 2024
4c4e498
Wait for the RDS instance to be available
abikouo Jan 31, 2024
a9813d0
RDS result update
abikouo Jan 31, 2024
f89c274
delete additional key pairs
abikouo Feb 1, 2024
dbb3c18
Fixing issue with webapp_ha_aurora playbook
abikouo Feb 6, 2024
ca71f7e
conditional delete for global cluster
abikouo Feb 6, 2024
960b9eb
fix minor issue
abikouo Feb 6, 2024
4de99e5
Update playbooks/webapp/vars/main.yaml
abikouo Feb 9, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
breaking_changes:
- >-
roles/deploy_flask_app - Add parameter ``deploy_flask_app_bastion_ssh_private_key`` to define
the path to the ssh private key file to use to connect to the bastion host (https://github.com/redhat-cop/cloud.aws_ops/issues/103).
abikouo marked this conversation as resolved.
Show resolved Hide resolved
- >-
roles/deploy_flask_app - The following parameters no longer required have been removed
``deploy_flask_app_bastion_host_required_packages``, ``deploy_flask_app_local_registry_port``,
``deploy_flask_app_local_registry_pwd``, ``deploy_flask_app_local_registry_user``,
``deploy_flask_app_git_repository`` (https://github.com/redhat-cop/cloud.aws_ops/issues/103).
14 changes: 0 additions & 14 deletions playbooks/webapp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ To delete the webapp:
* **deploy_flask_app_bastion_host_name** (str): Name for the EC2 instance. Default: `"{{ resource_prefix }}-bastion"`
* **bastion_host_type** (str): Instance type for the EC2 instance. Default: `t2.xlarge`
* **deploy_flask_app_bastion_host_username** (str): Username for the bastion host SSH user. Default: `fedora`
* **deploy_flask_app_bastion_host_required_packages** (list, elements str): Packages to be installed on the bastion host. Default:
```yaml
- python3
- python-virtualenv
- sshpass
- git
- podman
- httpd-tools
- ansible
```

### Networking

Expand Down Expand Up @@ -142,12 +132,8 @@ To delete the webapp:

### Webapp

* **deploy_flask_app_git_repository** (str): Git repository for the webapp. Default: `https://github.com/abikouo/webapp_pyflask_demo.git`
* **deploy_flask_app_number_of_workers** (int): Number of worker instances to create. Default: `2`
* **deploy_flask_app_workers_instance_type** (str): EC2 instance type for workers. Default: `t2.xlarge`
* **deploy_flask_app_local_registry_user** (str): Username for local Podman registry. Default: `ansible`
* **deploy_flask_app_local_registry_pwd** (str): Password for local Podman registry. Default: `testing123`
* **deploy_flask_app_local_registry_port** (int): Port for the local Podman registery. Default: `"{{ app_listening_port }}"`
* **deploy_flask_app_config** (dict, elements dict): Configuration values for the webapp, passed as corresponding env variables FLASK_APP, FLASK_ENV, ADMIN_USER, and ADMIN_PASSWORD when the app is deployed. Default:
```yaml
app_dir: /app/pyapp
Expand Down
13 changes: 13 additions & 0 deletions playbooks/webapp/files/ec2-trust-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
70 changes: 0 additions & 70 deletions playbooks/webapp/files/run_app.yaml

This file was deleted.

26 changes: 17 additions & 9 deletions playbooks/webapp/migrate_webapp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,24 @@
rds_snapshot_arn: "{{ result.db_snapshot_arn }}"
region: "{{ dest_region }}"

- name: Deploy app
- name: Create workers and deploy application
when: operation == "create"
ansible.builtin.import_role:
name: cloud.aws_ops.deploy_flask_app
vars:
deploy_flask_app_private_subnet_id: "{{ private_subnet.subnet.id }}"
deploy_flask_app_vpc_id: "{{ vpc.vpc.id }}"
deploy_flask_app_vm_info: "{{ vm_result }}"
deploy_flask_app_rds_info: "{{ rds_result }}"
deploy_flask_app_region: "{{ dest_region }}"
module_defaults:
group/aws:
aws_access_key: "{{ aws_access_key | default(omit) }}"
aws_secret_key: "{{ aws_secret_key | default(omit) }}"
security_token: "{{ security_token | default(omit) }}"
region: "{{ dest_region }}"
block:
- name: Deploy app
ansible.builtin.import_role:
name: cloud.aws_ops.deploy_flask_app
vars:
deploy_flask_app_bastion_ssh_private_key: "{{ ssh_key_file_path }}"
deploy_flask_app_private_subnet_id: "{{ private_subnet.subnet.id }}"
deploy_flask_app_vpc_id: "{{ vpc.vpc.id }}"
deploy_flask_app_vm_info: "{{ vm_result }}"
deploy_flask_app_rds_info: "{{ rds_result }}"

- name: Delete RDS snapshots from different regions
amazon.aws.rds_instance_snapshot:
Expand Down
41 changes: 16 additions & 25 deletions playbooks/webapp/tasks/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -202,45 +202,36 @@
db_instance_identifier: "{{ rds_identifier }}"
register: rds_result

- name: Set 'sshkey_file' variable
ansible.builtin.set_fact:
sshkey_file: ~/private-key-{{ deploy_flask_app_sshkey_pair_name }}-{{ region | default(aws_region) }}

- name: Create key pair to connect to the VM
amazon.aws.ec2_key:
name: "{{ deploy_flask_app_sshkey_pair_name }}"
register: rsa_key

- name: Save private key into file
ansible.builtin.copy:
content: "{{ rsa_key.key.private_key }}"
dest: "{{ sshkey_file }}"
mode: 0400
when: rsa_key is changed

- name: Check if the vm exists
amazon.aws.ec2_instance_info:
filters:
instance-type: "{{ bastion_host_type }}"
key-name: "{{ deploy_flask_app_sshkey_pair_name }}"
vpc-id: "{{ vpc.vpc.id }}"
instance-state-name: running
register: vm_result
- name: Create key pair to connect to the virtual machine
ansible.builtin.include_tasks: manage_keypair.yaml

- name: Ensure IAM instance role exists
amazon.aws.iam_role:
name: "{{ ec2_iam_role_name }}"
assume_role_policy_document: "{{ lookup('file', 'ec2-trust-policy.json') }}"
state: present
create_instance_profile: true
wait: true
register: role_output

- name: Create a virtual machine
when: vm_result.instances | length == 0
amazon.aws.ec2_instance:
name: "{{ deploy_flask_app_bastion_host_name }}"
instance_type: "{{ bastion_host_type }}"
image_id: "{{ images.images.0.image_id }}"
key_name: "{{ deploy_flask_app_sshkey_pair_name }}"
subnet_id: "{{ subnet.subnet.id }}"
ebs_optimized: true
instance_role: "{{ role_output.iam_role.role_name }}"
network:
assign_public_ip: true
groups:
- "{{ secgroup.group_id }}"
security_groups:
- "{{ secgroup.group_id }}"
user_data: |
#!/bin/bash
yum install -y python3 python-virtualenv sshpass netcat ansible
wait: true
state: started
register: vm_result
116 changes: 56 additions & 60 deletions playbooks/webapp/tasks/delete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
region: "{{ region | default(aws_region) }}"

block:
- name: Delete S3 bucket
amazon.aws.s3_bucket:
name: "{{ bucket_name }}"
state: absent
force: true

- name: Get vpc information
amazon.aws.ec2_vpc_net_info:
Expand All @@ -22,54 +27,37 @@
ansible.builtin.set_fact:
vpc_id: "{{ vpc.vpcs.0.vpc_id }}"

- name: Get bastion instance info
# Delete Load balancer
- name: List Load balancer(s) from VPC
community.aws.elb_classic_lb_info:
register: load_balancers

abikouo marked this conversation as resolved.
Show resolved Hide resolved
- name: Set fact for list of load balancers to delete
ansible.builtin.set_fact:
load_balancers_to_delete: "{{ load_balancers.elbs | selectattr('vpc_id', 'equalto', vpc_id) | map(attribute='load_balancer_name') | list }}"

- name: Delete load balancer(s)
amazon.aws.elb_classic_lb:
name: "{{ item }}"
wait: true
state: absent
with_items: "{{ load_balancers_to_delete }}"

# Delete EC2 instances
- name: Get EC2 instance info
amazon.aws.ec2_instance_info:
filters:
instance-type: "{{ bastion_host_type }}"
key-name: "{{ deploy_flask_app_sshkey_pair_name }}"
vpc-id: "{{ vpc_id }}"
instance-state-name: running
register: bastion

- name: Delete EC2 instances with dependant Resources
when: bastion.instances | length == 1
block:
- name: Set 'instance_host_name' variable
ansible.builtin.set_fact:
instance_host_name: "{{ bastion.instances.0.public_dns_name | split('.') | first }}"

- name: Delete workers key pair
amazon.aws.ec2_key:
name: "{{ instance_host_name }}-key"
state: absent

- name: Delete load balancer
amazon.aws.elb_classic_lb:
name: "{{ instance_host_name }}-lb"
wait: true
state: absent

- name: List workers
amazon.aws.ec2_instance_info:
filters:
tag:Name: "{{ instance_host_name }}-workers"
instance-state-name: running
register: running

- name: Delete workers
when: running.instances | length != 0
amazon.aws.ec2_instance:
instance_ids: "{{ running.instances | map(attribute='instance_id') | list }}"
wait: true
state: terminated

- name: Delete bastion host
amazon.aws.ec2_instance:
instance_ids:
- "{{ bastion.instances.0.instance_id }}"
wait: true
state: terminated
register: ec2_instances

- name: Delete ec2 instances from VPC
amazon.aws.ec2_instance:
instance_ids: "{{ ec2_instances.instances | map(attribute='instance_id') | list }}"
wait: true
state: terminated
when: ec2_instances.instances | length > 0

# Delete RDS instance
- name: Delete RDS instance
amazon.aws.rds_instance:
state: absent
Expand All @@ -87,19 +75,7 @@
name: "{{ rds_subnet_group_name }}"
state: absent

- name: List Security group from VPC
amazon.aws.ec2_security_group_info:
filters:
vpc-id: "{{ vpc_id }}"
tag:prefix: "{{ resource_prefix }}"
register: secgroups

- name: Delete security groups
amazon.aws.ec2_security_group:
state: absent
group_id: "{{ item }}"
with_items: "{{ secgroups.security_groups | map(attribute='group_id') | list }}"

# Delete VPC route table
- name: List routes table from VPC
amazon.aws.ec2_vpc_route_table_info:
filters:
Expand All @@ -115,6 +91,7 @@
state: absent
with_items: "{{ route_table.route_tables | map(attribute='id') | list }}"

# Delete NAT Gateway
- name: Get NAT gateway
amazon.aws.ec2_vpc_nat_gateway_info:
filters:
Expand All @@ -128,20 +105,39 @@
wait: true
with_items: "{{ nat_gw.result | map(attribute='nat_gateway_id') | list }}"

# Delete Internet gateway
- name: Delete internet gateway
amazon.aws.ec2_vpc_igw:
vpc_id: "{{ vpc_id }}"
state: absent

# Delete Subnets
- name: List Subnets from VPC
amazon.aws.ec2_vpc_subnet_info:
filters:
vpc-id: "{{ vpc_id }}"
register: vpc_subnets

- name: Delete subnets
amazon.aws.ec2_vpc_subnet:
cidr: "{{ item }}"
state: absent
vpc_id: "{{ vpc_id }}"
with_items: "{{ subnet_cidr }}"
with_items: "{{ vpc_subnets.subnets | map(attribute='cidr_block') | list }}"

# Delete Security groups
- name: List Security group from VPC
amazon.aws.ec2_security_group_info:
filters:
vpc-id: "{{ vpc_id }}"
register: secgroups

- name: Delete security groups
amazon.aws.ec2_security_group:
state: absent
group_id: "{{ item }}"
with_items: "{{ secgroups.security_groups | rejectattr('group_name', 'equalto', 'default') | map(attribute='group_id') | list }}"

# As ec2_vpc_route_table can't delete route table, the vpc still has dependencies and cannot be deleted.
# You need to do it delete it manually using either the console or the cli.
- name: Delete VPC
amazon.aws.ec2_vpc_net:
name: "{{ vpc_name }}"
Expand Down
Loading
Loading