Help - Creating a "First Run Only" Resource


#1

I am still learning to write cookbooks and need to Create a “First Run Only” Resource. I am looking at the Wiki Creating a “First Run Only” Resource
(http://wiki.opscode.com/pages/viewpage.action?pageId=15728818) and am having some difficulties interpreting the example. Does anyone have a cookbook with a complete example?

Basically, I have a recipe (config_disk.rb) that needs to do the following only once.

------Begin snippet —
bash “Configure_Disk” do
user "root"
cwd "/tmp"
code <<-EOH

Partition sda (Name and Data node)

/sbin/fdisk /dev/sda << EOF
n
p

w
EOF
/sbin/partprobe /dev/sda
#######Partition sdb (Name and Data node)
/sbin/fdisk /dev/sdb << EOF
n
p
1

w
EOF
EOH
end
----End of snippet-----

Thanks
Randy
OCLC Inc.


#2

You want to figure out what command you can run to verify the partition table is set correctly, and add it as a not_if to the resource.

Adam


Opscode, Inc.
Adam Jacob, Chief Product Officer
T: (206) 619-7151 E: adam@opscode.com

On Nov 7, 2011, at 9:47 AM, Van Fossan,Randy wrote:

I am still learning to write cookbooks and need to Create a “First Run Only” Resource. I am looking at the Wiki Creating a “First Run Only” Resource
(http://wiki.opscode.com/pages/viewpage.action?pageId=15728818) and am having some difficulties interpreting the example. Does anyone have a cookbook with a complete example?

Basically, I have a recipe (config_disk.rb) that needs to do the following only once.

------Begin snippet —
bash “Configure_Disk” do
user "root"
cwd "/tmp"
code <<-EOH

Partition sda (Name and Data node)

/sbin/fdisk /dev/sda << EOF
n
p

w
EOF
/sbin/partprobe /dev/sda
#######Partition sdb (Name and Data node)
/sbin/fdisk /dev/sdb << EOF
n
p
1

w
EOF
EOH
end
----End of snippet-----

Thanks
Randy
OCLC Inc.


#3

Hi Adam,

No, I really need the First Run Only so that if a disk fails and is
replaced, it doesn’t automatically fdisk it. I need the First Run Only
Resource. Also, there is quite a bit more code to go in the snippet as
well.

-----Original Message-----
From: Adam Jacob [mailto:adam@opscode.com]
Sent: Monday, November 07, 2011 12:57 PM
To: chef@lists.opscode.com
Subject: [chef] Re: Help - Creating a “First Run Only” Resource

You want to figure out what command you can run to verify the partition
table is set correctly, and add it as a not_if to the resource.

Adam


Opscode, Inc.
Adam Jacob, Chief Product Officer
T: (206) 619-7151 E: adam@opscode.com

On Nov 7, 2011, at 9:47 AM, Van Fossan,Randy wrote:

I am still learning to write cookbooks and need to Create a “First Run
Only” Resource. I am looking at the Wiki Creating a "First Run Only"
Resource
(http://wiki.opscode.com/pages/viewpage.action?pageId=15728818) and am
having some difficulties interpreting the example. Does anyone have a
cookbook with a complete example?

Basically, I have a recipe (config_disk.rb) that needs to do the
following only once.

------Begin snippet —
bash “Configure_Disk” do
user "root"
cwd "/tmp"
code <<-EOH

Partition sda (Name and Data node) /sbin/fdisk /dev/sda << EOF n

p

w
EOF
/sbin/partprobe /dev/sda
#######Partition sdb (Name and Data node)
/sbin/fdisk /dev/sdb << EOF
n
p
1

w
EOF
EOH
end
----End of snippet-----

Thanks
Randy
OCLC Inc.


#4

What exactly you do not understand ? First boot recipe uses attribute on node to signal if it was already executed on that node.

On Nov 7, 2011, at 21:47, Van Fossan,Randy wrote:

I am still learning to write cookbooks and need to Create a “First Run Only” Resource. I am looking at the Wiki Creating a “First Run Only” Resource
(http://wiki.opscode.com/pages/viewpage.action?pageId=15728818) and am having some difficulties interpreting the example. Does anyone have a cookbook with a complete example?

Basically, I have a recipe (config_disk.rb) that needs to do the following only once.

------Begin snippet —
bash “Configure_Disk” do
user "root"
cwd "/tmp"
code <<-EOH

Partition sda (Name and Data node)

/sbin/fdisk /dev/sda << EOF
n
p

w
EOF
/sbin/partprobe /dev/sda
#######Partition sdb (Name and Data node)
/sbin/fdisk /dev/sdb << EOF
n
p
1

w
EOF
EOH
end
----End of snippet-----

Thanks
Randy
OCLC Inc.


#5

In the examples, are they to go into a resource or a recipe? I just
don’t have enough to build on…

Example from Chef Wiki------
execute “some_command” do
command "command to run once"
notifies :create, “ruby_block[some_command_run_flag]”, :immediately
not_if { node.attribute?(“some_command_complete”) }
end

ruby_block “some_command_run_flag” do
block do
node.set[‘some_command_complete’] = true
node.save
end
action :nothing
end

-----Original Message-----
From: Maxim Kulkin [mailto:maxim.kulkin@gmail.com]
Sent: Monday, November 07, 2011 2:37 PM
To: chef@lists.opscode.com
Subject: [chef] Re: Help - Creating a “First Run Only” Resource

What exactly you do not understand ? First boot recipe uses attribute on
node to signal if it was already executed on that node.

On Nov 7, 2011, at 21:47, Van Fossan,Randy wrote:

I am still learning to write cookbooks and need to Create a “First Run
Only” Resource. I am looking at the Wiki Creating a "First Run Only"
Resource
(http://wiki.opscode.com/pages/viewpage.action?pageId=15728818) and am
having some difficulties interpreting the example. Does anyone have a
cookbook with a complete example?

Basically, I have a recipe (config_disk.rb) that needs to do the
following only once.

------Begin snippet —
bash “Configure_Disk” do
user "root"
cwd "/tmp"
code <<-EOH

Partition sda (Name and Data node) /sbin/fdisk /dev/sda << EOF n

p

w
EOF
/sbin/partprobe /dev/sda
#######Partition sdb (Name and Data node)
/sbin/fdisk /dev/sdb << EOF
n
p
1

w
EOF
EOH
end
----End of snippet-----

Thanks
Randy
OCLC Inc.


#6

One good optional would be to have the recipe remove itself from the runlist; if you remove it at the end of the recipe and save the node, that’s going to be pretty reliable.

Look at the application::rails recipe, IIRC it removes the migration recipe towards the end.

On Nov 7, 2011, at 6:47 PM, “Van Fossan,Randy” vanfossr@oclc.org wrote:

I am still learning to write cookbooks and need to Create a “First Run Only” Resource. I am looking at the Wiki Creating a “First Run Only” Resource
(http://wiki.opscode.com/pages/viewpage.action?pageId=15728818) and am having some difficulties interpreting the example. Does anyone have a cookbook with a complete example?

Basically, I have a recipe (config_disk.rb) that needs to do the following only once.

------Begin snippet —
bash “Configure_Disk” do
user "root"
cwd "/tmp"
code <<-EOH

Partition sda (Name and Data node)

/sbin/fdisk /dev/sda << EOF
n
p

w
EOF
/sbin/partprobe /dev/sda
#######Partition sdb (Name and Data node)
/sbin/fdisk /dev/sdb << EOF
n
p
1

w
EOF
EOH
end
----End of snippet-----

Thanks
Randy
OCLC Inc.


#7

What do you mean by “resource” ? You can just put this text into your recipe and that’s it.

On Nov 7, 2011, at 23:44, Van Fossan,Randy wrote:

In the examples, are they to go into a resource or a recipe? I just
don’t have enough to build on…

Example from Chef Wiki------
execute “some_command” do
command "command to run once"
notifies :create, “ruby_block[some_command_run_flag]”, :immediately
not_if { node.attribute?(“some_command_complete”) }
end

ruby_block “some_command_run_flag” do
block do
node.set[‘some_command_complete’] = true
node.save
end
action :nothing
end

-----Original Message-----
From: Maxim Kulkin [mailto:maxim.kulkin@gmail.com]
Sent: Monday, November 07, 2011 2:37 PM
To: chef@lists.opscode.com
Subject: [chef] Re: Help - Creating a “First Run Only” Resource

What exactly you do not understand ? First boot recipe uses attribute on
node to signal if it was already executed on that node.

On Nov 7, 2011, at 21:47, Van Fossan,Randy wrote:

I am still learning to write cookbooks and need to Create a “First Run
Only” Resource. I am looking at the Wiki Creating a "First Run Only"
Resource
(http://wiki.opscode.com/pages/viewpage.action?pageId=15728818) and am
having some difficulties interpreting the example. Does anyone have a
cookbook with a complete example?

Basically, I have a recipe (config_disk.rb) that needs to do the
following only once.

------Begin snippet —
bash “Configure_Disk” do
user "root"
cwd "/tmp"
code <<-EOH

Partition sda (Name and Data node) /sbin/fdisk /dev/sda << EOF n

p

w
EOF
/sbin/partprobe /dev/sda
#######Partition sdb (Name and Data node)
/sbin/fdisk /dev/sdb << EOF
n
p
1

w
EOF
EOH
end
----End of snippet-----

Thanks
Randy
OCLC Inc.


#8

I was thinking that parts of this code needed to be a resource provider
under cookbookname/provider…

-----Original Message-----
From: Maxim Kulkin [mailto:maxim.kulkin@gmail.com]
Sent: Monday, November 07, 2011 3:12 PM
To: chef@lists.opscode.com
Subject: [chef] Re: Help - Creating a “First Run Only” Resource

What do you mean by “resource” ? You can just put this text into your
recipe and that’s it.

On Nov 7, 2011, at 23:44, Van Fossan,Randy wrote:

In the examples, are they to go into a resource or a recipe? I just
don’t have enough to build on…

Example from Chef Wiki------
execute “some_command” do
command "command to run once"
notifies :create, “ruby_block[some_command_run_flag]”, :immediately
not_if { node.attribute?(“some_command_complete”) } end

ruby_block “some_command_run_flag” do
block do
node.set[‘some_command_complete’] = true node.save end action :nothing

end

-----Original Message-----
From: Maxim Kulkin [mailto:maxim.kulkin@gmail.com]
Sent: Monday, November 07, 2011 2:37 PM
To: chef@lists.opscode.com
Subject: [chef] Re: Help - Creating a “First Run Only” Resource

What exactly you do not understand ? First boot recipe uses attribute
on node to signal if it was already executed on that node.

On Nov 7, 2011, at 21:47, Van Fossan,Randy wrote:

I am still learning to write cookbooks and need to Create a “First
Run
Only” Resource. I am looking at the Wiki Creating a "First Run Only"
Resource
(http://wiki.opscode.com/pages/viewpage.action?pageId=15728818) and
am
having some difficulties interpreting the example. Does anyone have a

cookbook with a complete example?

Basically, I have a recipe (config_disk.rb) that needs to do the
following only once.

------Begin snippet —
bash “Configure_Disk” do
user "root"
cwd "/tmp"
code <<-EOH

Partition sda (Name and Data node) /sbin/fdisk /dev/sda << EOF n

p

w
EOF
/sbin/partprobe /dev/sda
#######Partition sdb (Name and Data node) /sbin/fdisk /dev/sdb << EOF

n p
1

w
EOF
EOH
end
----End of snippet-----

Thanks
Randy
OCLC Inc.


#9

It should look like this. Let’s say you have a cookbook called “partition” and a recipe called “default.rb”.

partition/attributes/default.rb

node.default[:partitioned] = false

partition/recipes/default.rb

if node[:partitioned] == false
… put all your recipe stuff that you want to run one time right here …

node.normal[:partitioned] = true
node.save
end

That will ensure that, if there has been no error in partitioning, you won’t ever try again.

Adam


Opscode, Inc.
Adam Jacob, Chief Product Officer
T: (206) 619-7151 E: adam@opscode.com

On Nov 7, 2011, at 12:15 PM, Van Fossan,Randy wrote:

I was thinking that parts of this code needed to be a resource provider
under cookbookname/provider…

-----Original Message-----
From: Maxim Kulkin [mailto:maxim.kulkin@gmail.com]
Sent: Monday, November 07, 2011 3:12 PM
To: chef@lists.opscode.com
Subject: [chef] Re: Help - Creating a “First Run Only” Resource

What do you mean by “resource” ? You can just put this text into your
recipe and that’s it.

On Nov 7, 2011, at 23:44, Van Fossan,Randy wrote:

In the examples, are they to go into a resource or a recipe? I just
don’t have enough to build on…

Example from Chef Wiki------
execute “some_command” do
command "command to run once"
notifies :create, “ruby_block[some_command_run_flag]”, :immediately
not_if { node.attribute?(“some_command_complete”) } end

ruby_block “some_command_run_flag” do
block do
node.set[‘some_command_complete’] = true node.save end action :nothing

end

-----Original Message-----
From: Maxim Kulkin [mailto:maxim.kulkin@gmail.com]
Sent: Monday, November 07, 2011 2:37 PM
To: chef@lists.opscode.com
Subject: [chef] Re: Help - Creating a “First Run Only” Resource

What exactly you do not understand ? First boot recipe uses attribute
on node to signal if it was already executed on that node.

On Nov 7, 2011, at 21:47, Van Fossan,Randy wrote:

I am still learning to write cookbooks and need to Create a “First
Run
Only” Resource. I am looking at the Wiki Creating a "First Run Only"
Resource
(http://wiki.opscode.com/pages/viewpage.action?pageId=15728818) and
am
having some difficulties interpreting the example. Does anyone have a

cookbook with a complete example?

Basically, I have a recipe (config_disk.rb) that needs to do the
following only once.

------Begin snippet —
bash “Configure_Disk” do
user "root"
cwd "/tmp"
code <<-EOH

Partition sda (Name and Data node) /sbin/fdisk /dev/sda << EOF n

p

w
EOF
/sbin/partprobe /dev/sda
#######Partition sdb (Name and Data node) /sbin/fdisk /dev/sdb << EOF

n p
1

w
EOF
EOH
end
----End of snippet-----

Thanks
Randy
OCLC Inc.