Magick
Magick

Reputation: 5112

Openshift application lifecycle events - application creation event?

I'm trying to hook into an application created event in OpenShift - if such an event exists.

The reason being, I would like to have a command run (ideally in a new pod), for creating a database schema. It doesnt make sense to have this in the application image, as I only need this run once - when the application is created. I have looked into pod lifecycle hooks (https://docs.openshift.com/enterprise/3.1/dev_guide/deployments.html#pod-based-lifecycle-hook) however these events happen everytime there is a new deployment. So this also is too often for my use case.

Is there a way to have an image run just once when an Openshift application is created?

Upvotes: 0

Views: 428

Answers (1)

luciddreamz
luciddreamz

Reputation: 2093

You're on the right track in the comments here. In the OpenShift v2 days the same scenario existed with lifecycle hooks.

For our WordPress Quickstart in OpenShift v2, for instance, we would check to see if the database was created yet on every new deployment. If not, we initialized an empty database with the same name as the app (in this case letting WordPress create the schema afterwards, but it's the same idea needed here): OpenShift v2 WordPress deploy action hook

In OpenShift v3, there are a few ways to implement a similar lifecycle hook, but the common pattern we're using in our templates now is to leverage the ability to execute a new pod to run database setup steps just prior to the deployment phase: OpenShift v3 CakePHP pre deploy lifecycle hook

Following this pattern, you would add your code to generate your database schema in a file like the v3 CakePHP migrate-database.sh in your source repo and execute the script with a pre deploy lifecycle hook (via execNewPod), checking first to see if the database/schema (select * from someknowntable limit 1) exists before loading the schema.

Upvotes: 1

Related Questions