Skip to content

Commit

Permalink
Added lxc? virtualization check
Browse files Browse the repository at this point in the history
  • Loading branch information
nvwls authored and sethvargo committed Jul 22, 2014
1 parent 28f4b19 commit edeb957
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,17 @@ http_request 'http://...' do
end
```

### Virtualization
- `lxc?`

#### Examples
```ruby
service 'ntpd' do
action [:enable, :start]
not_if { lxc? }
end
```

### Filters
- `compile_time` - accepts a block of resources to run at compile time
- `before` - insert resource in the collection before the given resource
Expand Down
1 change: 1 addition & 0 deletions lib/chef/sugar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Sugar
require_relative 'sugar/shell'
require_relative 'sugar/vagrant'
require_relative 'sugar/version'
require_relative 'sugar/virtualization'
end
end

Expand Down
41 changes: 41 additions & 0 deletions lib/chef/sugar/virtualization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Copyright 2014, Joseph J. Nuspl Jr. <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

class Chef
module Sugar
module Virtualization
extend self

#
# Determine if the current node is running in a linux container.
#
# @param [Chef::Node] node
#
# @return [Boolean]
# true if the machine is currently running in a container, false
# otherwise
#
def lxc?(node)
node['virtualization'] && node['virtualization']['system'] == 'lxc'
end
end

module DSL
# @see Chef::Sugar::Virtualization#lxc?
def lxc?; Chef::Sugar::Virtualization.lxc?(node); end
end
end
end
22 changes: 22 additions & 0 deletions spec/unit/chef/sugar/virtualization_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

describe Chef::Sugar::Virtualization do
it_behaves_like 'a chef sugar'

describe '#lxc?' do
it 'returns true when the machine is a linux contianer' do
node = { 'virtualization' => { 'system' => 'lxc' } }
expect(described_class.lxc?(node)).to be_truthy
end

it 'returns false when the virtual machine is not lxc' do
node = { 'virtualization' => { 'system' => 'vbox' } }
expect(described_class.lxc?(node)).to be_falsey
end

it 'returns false when the machine is not virtual' do
node = {}
expect(described_class.lxc?(node)).to be_falsey
end
end
end

0 comments on commit edeb957

Please sign in to comment.