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

Creates GH Actions section for sample data #13095

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

filipefurtad0
Copy link
Contributor

What? Why?

Introduces a section on GH-Actions to provide the possibility to manage creation of sample data.

Mockup:

image

Marked in yellow: section on branch/tag to be removed. Not sure how to do this.

What should we test?

  • Visit GH-Actions section.
  • Notice there is a new section called "Deploy Sample Data"
  • Select it; Click Run Workflow (right side of the screen)
  • It should open a menu, similar to the "Deploy to Staging"
  • One should be able to select a staging server (dropdown)
  • One should be able to and type in sample data file name
  • Clicking Run Workflow should run the script, and:
    • make a backup/baseline of the DB
    • create sample data according to the given input

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

We'll need to add this to the testers handbook.

@filipefurtad0 filipefurtad0 self-assigned this Jan 23, 2025
@filipefurtad0
Copy link
Contributor Author

It's a bit difficult to know whether this will work, as the changes will only be visible after merging. At least I'm not aware of any way to test this, in advance.

Also, I'm not sure the script section will do what it should. Some questions:

@filipefurtad0 filipefurtad0 marked this pull request as draft January 23, 2025 23:07
@filipefurtad0 filipefurtad0 marked this pull request as ready for review February 3, 2025 19:13
mkdir -p ~/backup/
pg_dump -h localhost -U ofn_user openfoodnetwork | gzip > ~/backup/staging-`date +%Y%m%d%H%M%S`-before-reset.sql.gz
bundle exec rake db:reset ofn:sample_data
sudo systemctl stop puma sidekiq
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this should be starting puma/sidekiq again :

Suggested change
sudo systemctl stop puma sidekiq
sudo systemctl start puma sidekiq

@@ -35,5 +35,5 @@ jobs:
sudo systemctl stop puma sidekiq
mkdir -p ~/backup/
pg_dump -h localhost -U ofn_user openfoodnetwork | gzip > ~/backup/staging-`date +%Y%m%d%H%M%S`-before-reset.sql.gz
bundle exec rake db:reset ofn:sample_data
bundle exec rake db:reset ofn:${{ secrets.sample_data_name }}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not sure I am following this, as far as I understand we only have ofn:sample_data task for sample data ?
What are the expected inputs here ?

Copy link
Member

Choose a reason for hiding this comment

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

I can imagine it might be useful to provide individual scripts for each sample data factory (eg users, enterprises, fees). But that hasn't been done yet, so I guess Filipe was just testing to see what's possible. We can't test it until it's merged so I'm happy to leave this in.

It's worth noting the sample data factories depend on each other (eg sample enterprises are managed by the sample users), so if you execute enterprises before users, the script would probably fail. But as long as the full sample data script is executed first, that should never be a problem.

Copy link
Member

Choose a reason for hiding this comment

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

  • do we really need to reset the DB? Or can we run the sample_data script on top of existing data? It would be best to keep existing data.

It looks like the sample_data script can run on top of existing data. If there is a matching record (eg user with same email, or enterprise with same name), the record will be overwritten with the sample data (enterprise data shown here for example: https://github.com/openfoodfoundation/openfoodnetwork/blob/master/lib/tasks/sample_data/enterprise_factory.rb#L24)

So we can do this without a reset:

Suggested change
bundle exec rake db:reset ofn:${{ secrets.sample_data_name }}
bundle exec rake ofn:${{ secrets.sample_data_name }}

Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Use the new script. Of course, to restore the db would require another GH Action, which you could make after this one succeeds :D

@@ -35,5 +35,5 @@ jobs:
sudo systemctl stop puma sidekiq
mkdir -p ~/backup/
pg_dump -h localhost -U ofn_user openfoodnetwork | gzip > ~/backup/staging-`date +%Y%m%d%H%M%S`-before-reset.sql.gz
bundle exec rake db:reset ofn:sample_data
bundle exec rake db:reset ofn:${{ secrets.sample_data_name }}
Copy link
Member

Choose a reason for hiding this comment

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

I can imagine it might be useful to provide individual scripts for each sample data factory (eg users, enterprises, fees). But that hasn't been done yet, so I guess Filipe was just testing to see what's possible. We can't test it until it's merged so I'm happy to leave this in.

It's worth noting the sample data factories depend on each other (eg sample enterprises are managed by the sample users), so if you execute enterprises before users, the script would probably fail. But as long as the full sample data script is executed first, that should never be a problem.

@@ -35,5 +35,5 @@ jobs:
sudo systemctl stop puma sidekiq
mkdir -p ~/backup/
pg_dump -h localhost -U ofn_user openfoodnetwork | gzip > ~/backup/staging-`date +%Y%m%d%H%M%S`-before-reset.sql.gz
bundle exec rake db:reset ofn:sample_data
bundle exec rake db:reset ofn:${{ secrets.sample_data_name }}
Copy link
Member

Choose a reason for hiding this comment

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

  • do we really need to reset the DB? Or can we run the sample_data script on top of existing data? It would be best to keep existing data.

It looks like the sample_data script can run on top of existing data. If there is a matching record (eg user with same email, or enterprise with same name), the record will be overwritten with the sample data (enterprise data shown here for example: https://github.com/openfoodfoundation/openfoodnetwork/blob/master/lib/tasks/sample_data/enterprise_factory.rb#L24)

So we can do this without a reset:

Suggested change
bundle exec rake db:reset ofn:${{ secrets.sample_data_name }}
bundle exec rake ofn:${{ secrets.sample_data_name }}

Comment on lines +7 to +19
inputs:
server:
description: "Staging Server"
type: choice
required: true
options:
- staging.openfoodnetwork.org.uk
- staging.openfoodnetwork.org.au
- staging.coopcircuits.fr
sample_data_name:
description: "Sample Data Name"
type: string
required: true
Copy link
Member

Choose a reason for hiding this comment

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

This is really nice to have some input for an action.

Comment on lines +32 to +36
- name: Run sample data script
if: success()
run: |
sudo systemctl stop puma sidekiq
~/apps/openfoodnetwork/current/script/ci/save-staging-baseline.sh
Copy link
Member

Choose a reason for hiding this comment

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

I'm sorry but this doesn't work.

This script is executed on the Github Actions machine. It's not executed on one of the staging servers. In theory, we could execute commands via ssh on the staging servers but at the moment they don't allow that. We would need to authorize an SSH key stored in Github to perform these commands. And I'm not sure we want that.

Copy link
Member

Choose a reason for hiding this comment

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

😅 eep I missed that! We do currently execute commands remotely with ansible when deploying to staging, so it wouldn't be too much of stretch to do the same here.

What we need is for this script to be saved on the server. Then it's pretty trivial to log in and run it. eg:

ssh [email protected]
script/load_sample_data

If we have that, then setting up this remote trigger doesn't seem worth the effort. But if it makes it easier for non-technical testers (ie Rachel) then perhaps it is worth the effort. Maybe we need to discuss at delivery-circle.

Copy link
Contributor Author

@filipefurtad0 filipefurtad0 Feb 4, 2025

Choose a reason for hiding this comment

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

This script is executed on the Github Actions machine

Uff, I obviously I missed that as well. Thanks for pointing that out @mkllnk.

What we need is for this script to be saved on the server. Then it's pretty trivial to log in and run it.

Ok, I'll have a second look at this @dacook . I'm not sure though I fully grasp the complexity here - I'm looking out for this Slack thread, and then proceed. Thanks for your help and reviews.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry I wasn't very prescriptive. I'm not sure if Maikel has concerns about this approach.
I suggest the next step is to create a new script in ofn-install. I had a look.. it's not easy to explain.. I'll try it..

Copy link
Member

Choose a reason for hiding this comment

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

Maikel what do you think of this? openfoodfoundation/ofn-install#994

Copy link
Member

Choose a reason for hiding this comment

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

Yes, all possible. I was just cautious because it's not quick and easy. But if there's enough appetite for this feature then sure we can do it.

@filipefurtad0
Copy link
Contributor Author

Adding the blocked label till we figure out the best way forward. Discussion ongoing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: In Progress ⚙
Development

Successfully merging this pull request may close these issues.

GH-Actions option to create sample data on staging servers
4 participants