Skip to content
This repository has been archived by the owner on Sep 10, 2020. It is now read-only.

Add Ansible automation for test database setup #72

Closed
wants to merge 2 commits into from

Conversation

redshiftzero
Copy link
Contributor

Resolves #56

Copy link
Contributor

@psivesely psivesely left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you weren't aware of this before, for re-testing this I would use ANSIBLE_ARGS="--tags=database -vvv" vagrant provision (or up).

Can you change the line when: fpsd_database_result|skipped to when: not fpsd_database_result|skipped? Couldn't make a comment on that line because it's either Conor or my own mistake, but it would be good to fix.


# Machine Learning
pandas
tqdm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lack of newline is weird. Don't think it will matter.

@@ -40,6 +40,12 @@
template: template0
register: fpsd_database_result

- name: Setup TABLEFUNC extension.
postgresql_ext: name=tablefunc db="{{ fpsd_database_psql_env.PGDATABASE }}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with the rest of the role, the options to postgresql_ext should each be on their own line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to mention it totally screws up syntax highlighting in my editor with this style.

- name: Setup TABLEFUNC extension.
postgresql_ext: name=tablefunc db="{{ fpsd_database_psql_env.PGDATABASE }}"
register: postgres_extension
always_run: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to always run this--the state option defaults to "present," and idempotency is something to strive for in Ansible.

postgresql_ext: name=tablefunc db="{{ fpsd_database_psql_env.PGDATABASE }}"
register: postgres_extension
always_run: true
changed_when: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is unnecessary unless after you remove the run_always line the task is still showing a changed status even when re-run. Also, we need to know if it changed, because if so, we should delete the testdb and re-create it based on the new template.

@@ -56,6 +62,11 @@
when: "'raw' not in schemas.stdout"
register: raw_schema_result

- name: Create the features schema.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be combined with the last task:

  - name: Create the raw and features schemata.
    command: psql -c 'CREATE SCHEMA {{ item }};'
    when: "'item' not in schemas.stdout"
    register: "{{ item }}_result"
    with_items:
      - raw
      - features

@@ -99,6 +110,12 @@
lc_ctype: en_US.UTF-8
template: "{{ fpsd_database_psql_env.PGDATABASE }}"

when: raw_schema_result|changed or raw_schema_tables_result|changed
- name: Setup TABLEFUNC extension.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This task shouldn't need to run. The TABLEFUNC extension should come along with the template.

always_run: true
changed_when: false

when: raw_schema_result|changed or raw_schema_tables_result|changed or features_schema_result|changed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reflect our desire to know if the presence of the tablefunc extension has changed, as well as our combining the schemata creation into one task, we would change this to:

  when: raw_schema_tables_result|changed or postgres_extension|skipped or schemata_results|skipped

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I've made this: when: raw_schema_tables_result|changed or postgres_extension|skipped or schemas|changed

@redshiftzero
Copy link
Contributor Author

Thanks for the comments @fowlslegs!! I've made each suggested change, provisioned the VM again, and executed the tests from the feature-generation branch to check the test database was working okay - everything looks good.

Copy link
Contributor

@psivesely psivesely left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For testing purposes, after initial provisioning you should always re-provision (ANSIBLE_ARGS="--tags=database -vvv" vagrant provision), and check for two things:

  • The play succeeds again.
  • changed=0 in the output (i.e., the play is idempotent).

That's how I caught the syntax mistake in the schemata creation task.

name: tablefunc
db: "{{ fpsd_database_psql_env.PGDATABASE }}"
register: postgres_extension
always_run: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should remove this line still.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

db: "{{ fpsd_database_psql_env.PGDATABASE }}"
register: postgres_extension
always_run: true
changed_when: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

register: raw_schema_result
- name: Create the raw and features schemata.
command: psql -c 'CREATE SCHEMA {{ item }};'
when: "'item' not in schemas.stdout"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use {{ item }} here. Unlike with your default vars or results registered earlier in the tasklist, which are populated to the local Python task execution environment, you do actually need to use interpolation for each item when looping over an iterable in your with: directives. Otherwise, this task looks for the literal 'item' in schemas.stdout, which will always return true, and then the task will run and fail on re-provisioning because it will try to re-create the schemas, returning a non-zero exit code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, fixed!

@redshiftzero
Copy link
Contributor Author

Ok thanks again for the feedback!! I made these changes, provisioned and re-provisioned and I do get changed=0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants