# Execute command on specific node (for versioning database on deployment)

**URL:** <https://discourse.chef.io/t/execute-command-on-specific-node-for-versioning-database-on-deployment/2219>\
**Category:** Chef Infra (archive)\
**Created:** [November 30, 2011, 5:42pm UTC](https://discourse.chef.io/t/execute-command-on-specific-node-for-versioning-database-on-deployment/2219 "2011-11-30T17:42:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Harvey](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kevin_harvey/32/755_2.png) [@Kevin\_Harvey](https://discourse.chef.io/u/Kevin_Harvey)\
**Post date:** [November 30, 2011, 5:42pm UTC](https://discourse.chef.io/t/execute-command-on-specific-node-for-versioning-database-on-deployment/2219/1 "2011-11-30T17:42:06Z")

</div>

Hello all, I’m trying to keep versioned copies of my database with each new  
revision of my Django code. I’m working from the Django Quickstart.

In django.rb I have:

…  
dbm = nil

# If we are the database master

if node.run\_list.roles.include?(app[“database\_master\_role”][0])  
dbm = node  
else

# Find the database master

```
results = search(:node, "role:#{app["database_master_role"][0]} AND

```

chef\_environment:#{node.chef\_environment}", nil, 0, 1)  
rows = results[0]  
if rows.length == 1  
dbm = rows[0]  
end  
end  
…

After this block I have the variable dbm, which should point to the node  
with the database\_master role, whether that’s the node I’m on or some other  
node. (Right?)

Is there a way I can use that variable to run commands just on that node  
from this recipe? I’d like to do a mysqldump, create a new database with  
the revision number, and load the dump into it in the  
before\_migrationcallback in django.rb. Thanks in advance!

–  
Kevin Harvey  
Web Developer  
[kcharvey@gmail.com](mailto:kcharvey@gmail.com)

---

<div class="post-metadata">

**Author:** ![Michael\_Schueler](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Michael\_Schueler](https://discourse.chef.io/u/Michael_Schueler)\
**Post date:** [December 1, 2011, 2:59am UTC](https://discourse.chef.io/t/execute-command-on-specific-node-for-versioning-database-on-deployment/2219/2 "2011-12-01T02:59:41Z")

</div>

Hi,

I don't think that nodes performing changes on other nodes is a good use  
case for chef, nor is it a good idea. This stuff should be in your  
database cookbook.

If you don't really care about that and just want to do it anyways there  
are many different ways to do this. I think that the simplest way would  
have the recipe pass dbm[:fqdn] to an external script to do this for you.  
Or.. you could just put the whole thing into an execute block in your  
recipe.

This kind of stuff really belongs in your orchestration or deploy scripts

How do you trigger a deploy right now? Issuing a chef run on your Django  
server?

On Wed, Nov 30, 2011 at 9:42 AM, Kevin Harvey [kcharvey@gmail.com](mailto:kcharvey@gmail.com) wrote:

> Hello all, I'm trying to keep versioned copies of my database with each  
> new revision of my Django code. I'm working from the Django Quickstart.
> 
> In django.rb I have:
> 
> ...  
> dbm = nil
> 
> # If we are the database master
> 
> if node.run\_list.roles.include?(app["database\_master\_role"][0])  
> dbm = node  
> else
> 
> # Find the database master
> 
> ```
> results = search(:node, "role:#{app["database_master_role"][0]} AND
> 
> ```
> 
> chef\_environment:#{node.chef\_environment}", nil, 0, 1)  
> rows = results[0]  
> if rows.length == 1  
> dbm = rows[0]  
> end  
> end  
> ...
> 
> After this block I have the variable dbm, which should point to the node  
> with the database\_master role, whether that's the node I'm on or some other  
> node. (Right?)
> 
> Is there a way I can use that variable to run commands just on that node  
> from this recipe? I'd like to do a mysqldump, create a new database with  
> the revision number, and load the dump into it in the before\_migrationcallback in django.rb. Thanks in advance!
> 
> --  
> Kevin Harvey  
> Web Developer  
> [kcharvey@gmail.com](mailto:kcharvey@gmail.com)

---

<div class="post-metadata">

**Author:** ![Kevin\_Harvey](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kevin_harvey/32/755_2.png) [@Kevin\_Harvey](https://discourse.chef.io/u/Kevin_Harvey)\
**Post date:** [December 5, 2011, 2:23pm UTC](https://discourse.chef.io/t/execute-command-on-specific-node-for-versioning-database-on-deployment/2219/3 "2011-12-05T14:23:23Z")

</div>

Michael, thanks for you response. I agree that this stuff should be in the  
database cookbook. Right now we're only deploying single instances, but  
we'll eventually be deploying as a multi-instance infrastructure so I want  
to get this right.

Would you say the code I've included above (from the application cookbook)  
is in the wrong place? To date our whole strategy has been based on the  
quick start, so I hope that it is best practice.

We'd eventually like to have a corresponding copy of the database for each  
revision in the revisions directory. That happens in django.rb, and it  
would seem to make sense to do database migration from the same place.

Yes, a deploy is triggered on the Django server. If the Git repositiory has  
changed it grabs the new code and creates a new revision.

So, when it's all said and done, what's the right way to do this?

--  
Kevin Harvey  
Web Developer  
[kcharvey@gmail.com](mailto:kcharvey@gmail.com)

On Wed, Nov 30, 2011 at 9:59 PM, Michael Schueler \<  
[michael.schueler@gmail.com](mailto:michael.schueler@gmail.com)\> wrote:

> Hi,
> 
> I don't think that nodes performing changes on other nodes is a good use  
> case for chef, nor is it a good idea. This stuff should be in your  
> database cookbook.
> 
> If you don't really care about that and just want to do it anyways there  
> are many different ways to do this. I think that the simplest way would  
> have the recipe pass dbm[:fqdn] to an external script to do this for you.  
> Or.. you could just put the whole thing into an execute block in your  
> recipe.
> 
> This kind of stuff really belongs in your orchestration or deploy scripts
> 
> How do you trigger a deploy right now? Issuing a chef run on your Django  
> server?
> 
> On Wed, Nov 30, 2011 at 9:42 AM, Kevin Harvey [kcharvey@gmail.com](mailto:kcharvey@gmail.com) wrote:
> 
> > Hello all, I'm trying to keep versioned copies of my database with each  
> > new revision of my Django code. I'm working from the Django Quickstart.
> > 
> > In django.rb I have:
> > 
> > ...  
> > dbm = nil
> > 
> > # If we are the database master
> > 
> > if node.run\_list.roles.include?(app["database\_master\_role"][0])  
> > dbm = node  
> > else
> > 
> > # Find the database master
> > 
> > ```
> > results = search(:node, "role:#{app["database_master_role"][0]} AND
> > 
> > ```
> > 
> > chef\_environment:#{node.chef\_environment}", nil, 0, 1)  
> > rows = results[0]  
> > if rows.length == 1  
> > dbm = rows[0]  
> > end  
> > end  
> > ...
> > 
> > After this block I have the variable dbm, which should point to the node  
> > with the database\_master role, whether that's the node I'm on or some other  
> > node. (Right?)
> > 
> > Is there a way I can use that variable to run commands just on that node  
> > from this recipe? I'd like to do a mysqldump, create a new database with  
> > the revision number, and load the dump into it in the before\_migrationcallback in django.rb. Thanks in advance!
> > 
> > --  
> > Kevin Harvey  
> > Web Developer  
> > [kcharvey@gmail.com](mailto:kcharvey@gmail.com)
