Determining the Operating System in a Chef Library
up vote
0
down vote
favorite
There is no access to os.windows? in a chef library, whereas in a recipe it is available. I thought it might be possible to get it by adding a require 'os' line to my library to make it available.
require 'os'
module Project
module Helper
# ...
def serviceExists?(service_name)
if os.windows?
puts 'Windows detected'
# ... etc ...
else
raise 'Unimplemented..'
end
end
# ...
end
end
This didn't work. Instead I received an error:
LoadError
---------
cannot load such file -- os
Is it possible to access the os variable or determine the operating system in a library without passing os as a parameter to the method? I was hoping to handle the os complexities in the library to keep the recipe cleaner.
I'm calling the code via the following not_if statement.
batch "Install #service_name service" do
extend Project::Helper
cwd install_home
code <<-EOH
@echo off
call "installSvc.cmd"
EOH
not_if serviceExists?(service_name)
end
chef
add a comment |
up vote
0
down vote
favorite
There is no access to os.windows? in a chef library, whereas in a recipe it is available. I thought it might be possible to get it by adding a require 'os' line to my library to make it available.
require 'os'
module Project
module Helper
# ...
def serviceExists?(service_name)
if os.windows?
puts 'Windows detected'
# ... etc ...
else
raise 'Unimplemented..'
end
end
# ...
end
end
This didn't work. Instead I received an error:
LoadError
---------
cannot load such file -- os
Is it possible to access the os variable or determine the operating system in a library without passing os as a parameter to the method? I was hoping to handle the os complexities in the library to keep the recipe cleaner.
I'm calling the code via the following not_if statement.
batch "Install #service_name service" do
extend Project::Helper
cwd install_home
code <<-EOH
@echo off
call "installSvc.cmd"
EOH
not_if serviceExists?(service_name)
end
chef
I thought about doing something like stackoverflow.com/a/20341462/1247302, but it feels dirty.
– mrswadge
Nov 9 at 13:51
Tried mixing the module into the Recipe DSL viaChef::Recipe.send(:include, Project::Helper)stackoverflow.com/a/22081109/1247302, but I got errors thatosdoesn't exist as a method.undefined methodos' for Chef::Resource::Batch` (i'm calling from anot_ifin the recipe)
– mrswadge
Nov 9 at 14:26
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
There is no access to os.windows? in a chef library, whereas in a recipe it is available. I thought it might be possible to get it by adding a require 'os' line to my library to make it available.
require 'os'
module Project
module Helper
# ...
def serviceExists?(service_name)
if os.windows?
puts 'Windows detected'
# ... etc ...
else
raise 'Unimplemented..'
end
end
# ...
end
end
This didn't work. Instead I received an error:
LoadError
---------
cannot load such file -- os
Is it possible to access the os variable or determine the operating system in a library without passing os as a parameter to the method? I was hoping to handle the os complexities in the library to keep the recipe cleaner.
I'm calling the code via the following not_if statement.
batch "Install #service_name service" do
extend Project::Helper
cwd install_home
code <<-EOH
@echo off
call "installSvc.cmd"
EOH
not_if serviceExists?(service_name)
end
chef
There is no access to os.windows? in a chef library, whereas in a recipe it is available. I thought it might be possible to get it by adding a require 'os' line to my library to make it available.
require 'os'
module Project
module Helper
# ...
def serviceExists?(service_name)
if os.windows?
puts 'Windows detected'
# ... etc ...
else
raise 'Unimplemented..'
end
end
# ...
end
end
This didn't work. Instead I received an error:
LoadError
---------
cannot load such file -- os
Is it possible to access the os variable or determine the operating system in a library without passing os as a parameter to the method? I was hoping to handle the os complexities in the library to keep the recipe cleaner.
I'm calling the code via the following not_if statement.
batch "Install #service_name service" do
extend Project::Helper
cwd install_home
code <<-EOH
@echo off
call "installSvc.cmd"
EOH
not_if serviceExists?(service_name)
end
chef
chef
edited Nov 9 at 14:32
asked Nov 9 at 13:43
mrswadge
1,09611532
1,09611532
I thought about doing something like stackoverflow.com/a/20341462/1247302, but it feels dirty.
– mrswadge
Nov 9 at 13:51
Tried mixing the module into the Recipe DSL viaChef::Recipe.send(:include, Project::Helper)stackoverflow.com/a/22081109/1247302, but I got errors thatosdoesn't exist as a method.undefined methodos' for Chef::Resource::Batch` (i'm calling from anot_ifin the recipe)
– mrswadge
Nov 9 at 14:26
add a comment |
I thought about doing something like stackoverflow.com/a/20341462/1247302, but it feels dirty.
– mrswadge
Nov 9 at 13:51
Tried mixing the module into the Recipe DSL viaChef::Recipe.send(:include, Project::Helper)stackoverflow.com/a/22081109/1247302, but I got errors thatosdoesn't exist as a method.undefined methodos' for Chef::Resource::Batch` (i'm calling from anot_ifin the recipe)
– mrswadge
Nov 9 at 14:26
I thought about doing something like stackoverflow.com/a/20341462/1247302, but it feels dirty.
– mrswadge
Nov 9 at 13:51
I thought about doing something like stackoverflow.com/a/20341462/1247302, but it feels dirty.
– mrswadge
Nov 9 at 13:51
Tried mixing the module into the Recipe DSL via
Chef::Recipe.send(:include, Project::Helper) stackoverflow.com/a/22081109/1247302, but I got errors that os doesn't exist as a method. undefined method os' for Chef::Resource::Batch` (i'm calling from a not_if in the recipe)– mrswadge
Nov 9 at 14:26
Tried mixing the module into the Recipe DSL via
Chef::Recipe.send(:include, Project::Helper) stackoverflow.com/a/22081109/1247302, but I got errors that os doesn't exist as a method. undefined method os' for Chef::Resource::Batch` (i'm calling from a not_if in the recipe)– mrswadge
Nov 9 at 14:26
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
I changed approach slightly instead of trying to access the os object directly, I instead used the node object via a 'mixin'.
require 'win32/service'
module Project
module Helper
def serviceExists?(service_name)
if node['os'].include?('windows')
Win32::Service.exists?(service_name)
else
raise 'Checking for services under this platform is not implemented yet.'
end
end
end
end
Chef::Recipe.send(:include, Project::Helper)
Chef::Resource.send(:include, Project::Helper)
add a comment |
up vote
0
down vote
use ohai...
install it using gem_package resource and then leverage ohai:
require 'ohai'
ohai = Ohai::System.new
ohai.all_plugins
os = ohai[:kernel][:os].downcase
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226882%2fdetermining-the-operating-system-in-a-chef-library%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I changed approach slightly instead of trying to access the os object directly, I instead used the node object via a 'mixin'.
require 'win32/service'
module Project
module Helper
def serviceExists?(service_name)
if node['os'].include?('windows')
Win32::Service.exists?(service_name)
else
raise 'Checking for services under this platform is not implemented yet.'
end
end
end
end
Chef::Recipe.send(:include, Project::Helper)
Chef::Resource.send(:include, Project::Helper)
add a comment |
up vote
0
down vote
accepted
I changed approach slightly instead of trying to access the os object directly, I instead used the node object via a 'mixin'.
require 'win32/service'
module Project
module Helper
def serviceExists?(service_name)
if node['os'].include?('windows')
Win32::Service.exists?(service_name)
else
raise 'Checking for services under this platform is not implemented yet.'
end
end
end
end
Chef::Recipe.send(:include, Project::Helper)
Chef::Resource.send(:include, Project::Helper)
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I changed approach slightly instead of trying to access the os object directly, I instead used the node object via a 'mixin'.
require 'win32/service'
module Project
module Helper
def serviceExists?(service_name)
if node['os'].include?('windows')
Win32::Service.exists?(service_name)
else
raise 'Checking for services under this platform is not implemented yet.'
end
end
end
end
Chef::Recipe.send(:include, Project::Helper)
Chef::Resource.send(:include, Project::Helper)
I changed approach slightly instead of trying to access the os object directly, I instead used the node object via a 'mixin'.
require 'win32/service'
module Project
module Helper
def serviceExists?(service_name)
if node['os'].include?('windows')
Win32::Service.exists?(service_name)
else
raise 'Checking for services under this platform is not implemented yet.'
end
end
end
end
Chef::Recipe.send(:include, Project::Helper)
Chef::Resource.send(:include, Project::Helper)
answered Nov 9 at 15:16
mrswadge
1,09611532
1,09611532
add a comment |
add a comment |
up vote
0
down vote
use ohai...
install it using gem_package resource and then leverage ohai:
require 'ohai'
ohai = Ohai::System.new
ohai.all_plugins
os = ohai[:kernel][:os].downcase
add a comment |
up vote
0
down vote
use ohai...
install it using gem_package resource and then leverage ohai:
require 'ohai'
ohai = Ohai::System.new
ohai.all_plugins
os = ohai[:kernel][:os].downcase
add a comment |
up vote
0
down vote
up vote
0
down vote
use ohai...
install it using gem_package resource and then leverage ohai:
require 'ohai'
ohai = Ohai::System.new
ohai.all_plugins
os = ohai[:kernel][:os].downcase
use ohai...
install it using gem_package resource and then leverage ohai:
require 'ohai'
ohai = Ohai::System.new
ohai.all_plugins
os = ohai[:kernel][:os].downcase
answered Dec 5 at 0:34
MrRoth
1,543103250
1,543103250
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226882%2fdetermining-the-operating-system-in-a-chef-library%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I thought about doing something like stackoverflow.com/a/20341462/1247302, but it feels dirty.
– mrswadge
Nov 9 at 13:51
Tried mixing the module into the Recipe DSL via
Chef::Recipe.send(:include, Project::Helper)stackoverflow.com/a/22081109/1247302, but I got errors thatosdoesn't exist as a method.undefined methodos' for Chef::Resource::Batch` (i'm calling from anot_ifin the recipe)– mrswadge
Nov 9 at 14:26