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









share|improve this question























  • 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














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









share|improve this question























  • 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












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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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















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












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)





share|improve this answer



























    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





    share|improve this answer




















      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
      );



      );













      draft saved

      draft discarded


















      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)





      share|improve this answer
























        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)





        share|improve this answer






















          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)





          share|improve this answer












          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)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 15:16









          mrswadge

          1,09611532




          1,09611532






















              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





              share|improve this answer
























                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





                share|improve this answer






















                  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





                  share|improve this answer












                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 5 at 0:34









                  MrRoth

                  1,543103250




                  1,543103250



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                      How do I collapse sections of code in Visual Studio Code for Windows?

                      ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ