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

Preserve user-data across run #8752

Draft
wants to merge 5 commits into
base: development
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ virtualenv/
.DS_Store
my_*_vars.yml
user-data.yaml
*-user-data.yaml
user-info.yaml
*-user-info.yaml
.vscode/settings.json
__pycache__
*.pyc
Expand Down
18 changes: 6 additions & 12 deletions ansible/action_plugins/agnosticd_user_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,30 @@ def run(self, tmp=None, task_vars=None):
result['user'] = user

try:
output_dir = self._templar.template(
task_vars.get('output_dir',
task_vars['hostvars'].get('localhost',{}).get('output_dir',
task_vars.get('playbook_dir', '.')
)
)
)

action = self._templar.template('{{ ACTION | default(hostvars.localhost.ACTION) | default("provision") }}')
output_dir = self._templar.template('{{ output_dir | default(hostvars.localhost.output_dir) | default(playbook_dir) | default(".") }}')
# Attempt to make output_dir if not exists
try:
os.makedirs(output_dir)
except OSError:
pass

if not user and msg != None:
fh = open(os.path.join(output_dir, 'user-info.yaml'), 'a')
fh = open(os.path.join(output_dir, f'{action}-user-info.yaml'), 'a')
if isinstance(msg, list):
for m in msg:
fh.write('- ' + json.dumps(m) + "\n")
else:
fh.write('- ' + json.dumps(msg) + "\n")
fh.close()
if not user and body != None:
fh = open(os.path.join(output_dir, 'user-body.yaml'), 'a')
fh = open(os.path.join(output_dir, f'{action}-user-body.yaml'), 'a')
fh.write('- ' + json.dumps(body) + "\n")
fh.close()
if data or user:
user_data = None
try:
fh = open(os.path.join(output_dir, 'user-data.yaml'), 'r')
fh = open(os.path.join(output_dir, f'{action}-user-data.yaml'), 'r')
user_data = yaml.safe_load(fh)
fh.close()
except FileNotFoundError:
Expand Down Expand Up @@ -149,7 +143,7 @@ def run(self, tmp=None, task_vars=None):
else:
user_data.update(data)

fh = open(os.path.join(output_dir, 'user-data.yaml'), 'w')
fh = open(os.path.join(output_dir, f'{action}-user-data.yaml'), 'w')
yaml.safe_dump(user_data, stream=fh, explicit_start=True)
fh.close()
result['failed'] = False
Expand Down
17 changes: 12 additions & 5 deletions ansible/cloud_providers/ec2_infrastructure_deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@
include_role:
name: infra-images

# If infra-aws-capacity-reservation role was not executed, run it
- when: >-
agnosticd_aws_capacity_reservation_results is not defined or
agnosticd_aws_capacity_reservation_results.reservations | default({}) | length == 0
name: Run infra-aws-capacity-reservation
- name: Run infra-aws-capacity-reservation
include_role:
name: infra-aws-capacity-reservation
vars:
ACTION: provision

- when: >-
agnosticd_aws_capacity_reservation_results.reservations | default({}) | length > 0
block:
- name: Empty the agnosticd_images and run the detection again
set_fact:
agnosticd_images: {}

- name: Run infra-images again to use the proper region selected by the reservations
include_role:
name: infra-images

- name: Run infra-aws-open-environment Role
include_role:
name: infra-aws-open-environment
Expand Down
6 changes: 3 additions & 3 deletions ansible/completion_callback.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
- agnosticd_callback_url != ''
- agnosticd_callback_token != ''
vars:
user_body_yaml: "{{ output_dir ~ '/user-body.yaml' }}"
user_data_yaml: "{{ output_dir ~ '/user-data.yaml' }}"
user_info_yaml: "{{ output_dir ~ '/user-info.yaml' }}"
user_body_yaml: "{{ [ output_dir, ACTION ~ '-user-body.yaml' ] | path_join }}"
user_data_yaml: "{{ [ output_dir, ACTION ~ '-user-data.yaml' ] | path_join }}"
user_info_yaml: "{{ [ output_dir, ACTION ~ '-user-info.yaml' ] | path_join }}"
uri:
url: "{{ agnosticd_callback_url }}"
method: POST
Expand Down
4 changes: 2 additions & 2 deletions ansible/configs/just-a-bunch-of-nodes/default_vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ instances:
- key: "ostype"
value: "linux"
- key: "instance_filter"
value: "{{ env_type }}-{{ email }}"
value: "{{ env_type }}-{{ email | default(requester_username, true) | default('unknownuser', true) }}"
volumes:
- name: '/dev/sda1'
size: 20
Expand All @@ -73,7 +73,7 @@ instances:
- key: "ostype"
value: "linux"
- key: "instance_filter"
value: "{{ env_type }}-{{ email }}"
value: "{{ env_type }}-{{ email | default(requester_username, true) | default('unknownuser', true) }}"
security_groups:
- DefaultSG

Expand Down
2 changes: 1 addition & 1 deletion ansible/configs/just-a-bunch-of-nodes/default_vars_ec2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ansible_user: ec2-user
remote_user: ec2-user

# The region to be used, if not specified by -e in the command line
aws_region: us-east-1
#aws_region: us-east-1

node_instance_image: RHEL8-default
bastion_instance_image: RHEL8-default
Expand Down
14 changes: 14 additions & 0 deletions ansible/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
############ Step 006 Destroy Workshop using workshop_prefix
################################################################################
################################################################################
- name: Step 0000 Set Action
hosts: localhost
connection: local
gather_facts: false
tags:
- must
- step0000
- setup_runtime
become: false
tasks:
- name: Set ACTION to destroy
when: ACTION is undefined
set_fact:
ACTION: destroy

- import_playbook: setup_runtime.yml

Expand Down
4 changes: 2 additions & 2 deletions ansible/lookup_plugins/agnosticd_user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
from ansible.plugins.lookup import LookupBase

class LookupModule(LookupBase):
def run(self, terms, user=None, **kwargs):
def run(self, terms, action='provision', user=None, **kwargs):
self.set_options(direct=kwargs)
ret = []

output_dir = self._templar.template('{{ output_dir | default(playbook_dir) | default(".") }}')
user_data = {}
try:
fh = open(os.path.join(output_dir, 'user-data.yaml'), 'r')
fh = open(os.path.join(output_dir, f'{action}-user-data.yaml'), 'r')
user_data = yaml.safe_load(fh)
fh.close()
except FileNotFoundError:
Expand Down
14 changes: 14 additions & 0 deletions ansible/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
############ Step 0000 Setup Runtime
################################################################################
################################################################################
- name: Step 0000 Set Action
hosts: localhost
connection: local
gather_facts: false
tags:
- must
- step0000
- setup_runtime
become: false
tasks:
- name: Set ACTION to provision
when: ACTION is undefined
set_fact:
ACTION: provision

- import_playbook: setup_runtime.yml
tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
set_fact:
aws_region: "{{ agnosticd_aws_capacity_reservation_results.region }}"


- name: Ensure aws_region is not passed as extra vars and different
assert:
that: aws_region == agnosticd_aws_capacity_reservation_results.region
Expand All @@ -26,3 +27,14 @@
when: agnosticd_aws_capacity_reservation_results.single_availability_zone | default('') != ''
set_fact:
aws_availability_zone: "{{ agnosticd_aws_capacity_reservation_results.single_availability_zone }}"

- name: Save region in user-data
agnosticd_user_info:
data:
aws_region: "{{ agnosticd_aws_capacity_reservation_results.region }}"

- name: Save AZ in user-data
when: agnosticd_aws_capacity_reservation_results.single_availability_zone | default('') != ''
agnosticd_user_info:
data:
aws_availability_zone: "{{ agnosticd_aws_capacity_reservation_results.single_availability_zone }}"
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@
- name: Set fallback_regions (for deletion in the right region)
set_fact:
fallback_regions: "{{ agnosticd_aws_capacity_reservation_regions }}"

- name: Retrieve the region from user-data.yaml
when: aws_region | default('') == ''
set_fact:
aws_region: "{{ lookup('agnosticd_user_data', 'aws_region') }}"
7 changes: 6 additions & 1 deletion ansible/roles-infra/infra-images/tasks/ec2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@
loop_control:
label: "{{ item.key }}"
debug:
msg: "{{ item.key }} - {{ item.value.name }} - {{ item.value.image_id }} - {{ item.value.platform_details }}"
msg: >-
{{ item.key }} -
{{ item.value.name }} -
{{ item.value.image_id }} -
{{ item.value.platform_details }} -
{{ aws_region_final | default(aws_region) | default('us-east-1') }}
loop: "{{ agnosticd_images | dict2items }}"
19 changes: 18 additions & 1 deletion ansible/setup_runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,29 @@
include_role:
name: agnosticd_restore_output_dir

- name: Touch file provision-user-data.yaml and provision-user-info.yaml
file:
state: touch
path: "{{ [ output_dir, 'provision-' ~ item ] | path_join }}"
loop:
- user-info.yaml
- user-data.yaml

- name: Create empty user-info.yaml and user-data.yaml in output dir
when: not agnosticd_preserve_user_data | default(false) | bool
copy:
content: |
---
dest: "{{ output_dir }}/{{ item }}"
dest: "{{ [ output_dir, ACTION ~ '-' ~ item ] | path_join }}"
loop:
- user-info.yaml
- user-data.yaml

- name: Create symlink user-data.yaml -> provision-user-data.yaml
file:
src: "{{ [ output_dir, 'provision-' ~ item ] | path_join }}"
dest: "{{ [ output_dir, item ] | path_join }}"
state: link
loop:
- user-info.yaml
- user-data.yaml
Expand Down
14 changes: 14 additions & 0 deletions ansible/update.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
---
# Entry point for update action.
- name: Step 0000 Set Action
hosts: localhost
connection: local
gather_facts: false
tags:
- must
- step0000
- setup_runtime
become: false
tasks:
- name: Set ACTION to update
when: ACTION is undefined
set_fact:
ACTION: update

- name: Setup AgnosticD Runtime
import_playbook: setup_runtime.yml
Expand Down
4 changes: 2 additions & 2 deletions docs/User_Info.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ To set data to be consumed by other automation, such as the Babylon Events UI/Bo
openshift_api_url: "{{ _openshift_api_url }}"
---------------------------------------------------------------

Data values are saved as a dictionary to `user-data.yaml` in the output directory.
Data values are saved as a dictionary to `provision-user-data.yaml` in the output directory.

To use this data through bookbag Asciidoc deployed by the Babylon Events UI we recommend setting AsciiDoc macros:

Expand Down Expand Up @@ -117,4 +117,4 @@ User `data` dictionary values are combined with any values previously set for th
If the same key is set twice for a user then the previous value is replaced.
Environment data, set by calls to `agnosticd_user_info` without `user` will not be exposed to users.

All user data and messages is collected in `user-data.yaml` with user data stored as a dictionary under the key "users".
All user data and messages is collected in `ACTION-user-data.yaml` with user data stored as a dictionary under the key "users".
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ Which is a file also on `agnosticd/ansible directory`, not on our own config dir
- agnosticd_callback_url != ''
- agnosticd_callback_token != ''
vars:
user_data_yaml: "{{ output_dir ~ '/user-data.yaml' }}"
user_info_yaml: "{{ output_dir ~ '/user-info.yaml' }}"
user_data_yaml: "{{ [ output_dir, ACTION ~ '-user-data.yaml' ] | path_join }}"
user_info_yaml: "{{ [ output_dir, ACTION ~ '-user-info.yaml' ] | path_join }}"
uri:
url: "{{ agnosticd_callback_url }}"
method: POST
Expand Down
Loading