only_if and not_if guard


#1

Hello All,

I’m writing a recipe where I’m trying to create a database in MSSQL. I’ve
written and execute resource, which is working fine. Now I want it to be
executed conditionally, if database exist do not execute the resource.
Irrespective of the condition the resource is getting executed every time.

The resource I’m using is given below.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if { "sqlcmd -U user -P secret -S server -b -Q “select Count (*) from
master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Am I doing anything wrong here?

Thanks
Ravi


#2

Hi Ravi,

From my understanding:

  • you pass a block to not_if when you are running ruby code
  • You pass a string to not_if to execute from the shell

See here http://docs.opscode.com/resource_common.html#guards

It looks to me like you want to execute from the shell. So you could try
leave out the block and see what happens.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if "sqlcmd -U user -P secret -S server -b -Q “select Count (*)
from master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Thanks,

Brian

On 12/12/2013 12:26, “Ravindra” ravindra.chandrakar@gmail.com wrote:

Hello All,

I’m writing a recipe where I’m trying to create a database in MSSQL. I’ve
written and execute resource, which is working fine. Now I want it to be
executed conditionally, if database exist do not execute the resource.
Irrespective of the condition the resource is getting executed every time.

The resource I’m using is given below.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if { "sqlcmd -U user -P secret -S server -b -Q “select Count (*)
from
master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Am I doing anything wrong here?

Thanks
Ravi


#3

Thanks Brain.

My final goal is to create database only if database does not exist,
otherwise create it.

When you said “leave out the block”, do you want to say remove the
not_if? Could you please help me understanding this?

Thanks,
Ravi

On 12/12/13, Brian Fletcher brian.fletcher@workday.com wrote:

Hi Ravi,

From my understanding:

  • you pass a block to not_if when you are running ruby code
  • You pass a string to not_if to execute from the shell

See here http://docs.opscode.com/resource_common.html#guards

It looks to me like you want to execute from the shell. So you could try
leave out the block and see what happens.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if "sqlcmd -U user -P secret -S server -b -Q “select Count (*)
from master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Thanks,

Brian

On 12/12/2013 12:26, “Ravindra” ravindra.chandrakar@gmail.com wrote:

Hello All,

I’m writing a recipe where I’m trying to create a database in MSSQL. I’ve
written and execute resource, which is working fine. Now I want it to be
executed conditionally, if database exist do not execute the resource.
Irrespective of the condition the resource is getting executed every time.

The resource I’m using is given below.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if { "sqlcmd -U user -P secret -S server -b -Q “select Count (*)
from
master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Am I doing anything wrong here?

Thanks
Ravi


#4

Removing the block in this case means removing the curly braces { }.


~~ StormeRider ~~

“Every world needs its heroes […] They inspire us to be better than we
are. And they protect from the darkness that’s just around the corner.”

(from Smallville Season 6x1: “Zod”)

On why I hate the phrase “that’s so lame”… http://bit.ly/Ps3uSS

On Thu, Dec 12, 2013 at 5:12 AM, Ravindra Chandrakar <
ravindra.chandrakar@gmail.com> wrote:

Thanks Brain.

My final goal is to create database only if database does not exist,
otherwise create it.

When you said “leave out the block”, do you want to say remove the
not_if? Could you please help me understanding this?

Thanks,
Ravi

On 12/12/13, Brian Fletcher brian.fletcher@workday.com wrote:

Hi Ravi,

From my understanding:

  • you pass a block to not_if when you are running ruby code
  • You pass a string to not_if to execute from the shell

See here http://docs.opscode.com/resource_common.html#guards

It looks to me like you want to execute from the shell. So you could try
leave out the block and see what happens.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if "sqlcmd -U user -P secret -S server -b -Q “select Count (*)
from master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Thanks,

Brian

On 12/12/2013 12:26, “Ravindra” ravindra.chandrakar@gmail.com wrote:

Hello All,

I’m writing a recipe where I’m trying to create a database in MSSQL. I’ve
written and execute resource, which is working fine. Now I want it to be
executed conditionally, if database exist do not execute the resource.
Irrespective of the condition the resource is getting executed every
time.

The resource I’m using is given below.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if { "sqlcmd -U user -P secret -S server -b -Q “select Count (*)
from
master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Am I doing anything wrong here?

Thanks
Ravi


#5

Thanks Morgan for clarifying.

I’m not using curly braces. My code is enclosed within double quotes ".

Thanks,
Ravi

On 12/12/13, Morgan Blackthorne stormerider@gmail.com wrote:

Removing the block in this case means removing the curly braces { }.


~~ StormeRider ~~

“Every world needs its heroes […] They inspire us to be better than we
are. And they protect from the darkness that’s just around the corner.”

(from Smallville Season 6x1: “Zod”)

On why I hate the phrase “that’s so lame”… http://bit.ly/Ps3uSS

On Thu, Dec 12, 2013 at 5:12 AM, Ravindra Chandrakar <
ravindra.chandrakar@gmail.com> wrote:

Thanks Brain.

My final goal is to create database only if database does not exist,
otherwise create it.

When you said “leave out the block”, do you want to say remove the
not_if? Could you please help me understanding this?

Thanks,
Ravi

On 12/12/13, Brian Fletcher brian.fletcher@workday.com wrote:

Hi Ravi,

From my understanding:

  • you pass a block to not_if when you are running ruby code
  • You pass a string to not_if to execute from the shell

See here http://docs.opscode.com/resource_common.html#guards

It looks to me like you want to execute from the shell. So you could
try
leave out the block and see what happens.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if "sqlcmd -U user -P secret -S server -b -Q “select Count (*)
from master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Thanks,

Brian

On 12/12/2013 12:26, “Ravindra” ravindra.chandrakar@gmail.com wrote:

Hello All,

I’m writing a recipe where I’m trying to create a database in MSSQL.
I’ve
written and execute resource, which is working fine. Now I want it to
be
executed conditionally, if database exist do not execute the resource.
Irrespective of the condition the resource is getting executed every
time.

The resource I’m using is given below.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if { "sqlcmd -U user -P secret -S server -b -Q “select Count
(*)
from
master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Am I doing anything wrong here?

Thanks
Ravi


#6

In your first email, you were.


~~ StormeRider ~~

“Every world needs its heroes […] They inspire us to be better than we
are. And they protect from the darkness that’s just around the corner.”

(from Smallville Season 6x1: “Zod”)

On why I hate the phrase “that’s so lame”… http://bit.ly/Ps3uSS

On Thu, Dec 12, 2013 at 5:41 AM, Ravindra Chandrakar <
ravindra.chandrakar@gmail.com> wrote:

Thanks Morgan for clarifying.

I’m not using curly braces. My code is enclosed within double quotes ".

Thanks,
Ravi

On 12/12/13, Morgan Blackthorne stormerider@gmail.com wrote:

Removing the block in this case means removing the curly braces { }.


~~ StormeRider ~~

“Every world needs its heroes […] They inspire us to be better than we
are. And they protect from the darkness that’s just around the corner.”

(from Smallville Season 6x1: “Zod”)

On why I hate the phrase “that’s so lame”… http://bit.ly/Ps3uSS

On Thu, Dec 12, 2013 at 5:12 AM, Ravindra Chandrakar <
ravindra.chandrakar@gmail.com> wrote:

Thanks Brain.

My final goal is to create database only if database does not exist,
otherwise create it.

When you said “leave out the block”, do you want to say remove the
not_if? Could you please help me understanding this?

Thanks,
Ravi

On 12/12/13, Brian Fletcher brian.fletcher@workday.com wrote:

Hi Ravi,

From my understanding:

  • you pass a block to not_if when you are running ruby code
  • You pass a string to not_if to execute from the shell

See here http://docs.opscode.com/resource_common.html#guards

It looks to me like you want to execute from the shell. So you could
try
leave out the block and see what happens.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if "sqlcmd -U user -P secret -S server -b -Q "select Count
(*)

from master.dbo.sysdatabases WHERE name = ‘myDB’""
end

Thanks,

Brian

On 12/12/2013 12:26, “Ravindra” ravindra.chandrakar@gmail.com
wrote:

Hello All,

I’m writing a recipe where I’m trying to create a database in MSSQL.
I’ve
written and execute resource, which is working fine. Now I want it to
be
executed conditionally, if database exist do not execute the resource.
Irrespective of the condition the resource is getting executed every
time.

The resource I’m using is given below.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if { "sqlcmd -U user -P secret -S server -b -Q “select Count
(*)
from
master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Am I doing anything wrong here?

Thanks
Ravi


#7

Ohh, That was typo. I’m really sorry for that.

However I got it working. I’ve changed the condition as below,
sqlcmd -U user -P secret -S server -b -Q “select * from
master.dbo.sysdatabases” | grep myDB.

Thanks all for the support.

Thanks,
Ravi

On 12/12/13, Morgan Blackthorne stormerider@gmail.com wrote:

In your first email, you were.


~~ StormeRider ~~

“Every world needs its heroes […] They inspire us to be better than we
are. And they protect from the darkness that’s just around the corner.”

(from Smallville Season 6x1: “Zod”)

On why I hate the phrase “that’s so lame”… http://bit.ly/Ps3uSS

On Thu, Dec 12, 2013 at 5:41 AM, Ravindra Chandrakar <
ravindra.chandrakar@gmail.com> wrote:

Thanks Morgan for clarifying.

I’m not using curly braces. My code is enclosed within double quotes ".

Thanks,
Ravi

On 12/12/13, Morgan Blackthorne stormerider@gmail.com wrote:

Removing the block in this case means removing the curly braces { }.


~~ StormeRider ~~

“Every world needs its heroes […] They inspire us to be better than
we
are. And they protect from the darkness that’s just around the corner.”

(from Smallville Season 6x1: “Zod”)

On why I hate the phrase “that’s so lame”… http://bit.ly/Ps3uSS

On Thu, Dec 12, 2013 at 5:12 AM, Ravindra Chandrakar <
ravindra.chandrakar@gmail.com> wrote:

Thanks Brain.

My final goal is to create database only if database does not exist,
otherwise create it.

When you said “leave out the block”, do you want to say remove the
not_if? Could you please help me understanding this?

Thanks,
Ravi

On 12/12/13, Brian Fletcher brian.fletcher@workday.com wrote:

Hi Ravi,

From my understanding:

  • you pass a block to not_if when you are running ruby code
  • You pass a string to not_if to execute from the shell

See here http://docs.opscode.com/resource_common.html#guards

It looks to me like you want to execute from the shell. So you could
try
leave out the block and see what happens.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o
C”\Create_db.log"
not_if "sqlcmd -U user -P secret -S server -b -Q "select Count
(*)

from master.dbo.sysdatabases WHERE name = ‘myDB’""
end

Thanks,

Brian

On 12/12/2013 12:26, “Ravindra” ravindra.chandrakar@gmail.com
wrote:

Hello All,

I’m writing a recipe where I’m trying to create a database in MSSQL.
I’ve
written and execute resource, which is working fine. Now I want it
to
be
executed conditionally, if database exist do not execute the
resource.
Irrespective of the condition the resource is getting executed every
time.

The resource I’m using is given below.

execute “Create_DB” do
command “sqlcmd -S server -i C:\createDB.sql -o C”\Create_db.log"
not_if { "sqlcmd -U user -P secret -S server -b -Q “select Count
(*)
from
master.dbo.sysdatabases WHERE name = ‘myDB’”"
end

Am I doing anything wrong here?

Thanks
Ravi