Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions modules/net_share/Modulefile → Modulefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name 'simondean-net_share'
version '0.1.2'
summary 'Windows network share module'
description 'Module for configuring Windows network file shares. Uses the \'net share\' command. '
project_page 'https://github.com/simondean/puppet-net_share'
license 'Apache License, Version 2.0'
author 'Simon Dean'
source 'https://github.com/simondean/puppet-net_share'
name 'simondean-net_share'
version '0.1.2'
summary 'Windows network share module'
description 'Module for configuring Windows network file shares. Uses the \'net share\' command. '
project_page 'https://github.com/simondean/puppet-net_share'
license 'Apache License, Version 2.0'
author 'Simon Dean'
source 'https://github.com/simondean/puppet-net_share'
Original file line number Diff line number Diff line change
@@ -1,169 +1,169 @@
Puppet::Type.type(:net_share).provide(:net_share) do
desc "Windows network share"
confine :operatingsystem => :windows
defaultfor :operatingsystem => :windows
commands :net => File.join(ENV['SystemRoot'] || 'c:/windows', 'system32/net.exe')
mk_resource_methods
# Match resources to providers where the resource name matches the provider name
def self.prefetch(resources)
resources.each do |name, resource|
provider = new(query(name))
if provider
resource.provider = provider
else
resource.provider = new(:ensure => :absent)
end
end
end
def initialize(*args)
super
# Make a duplicate of the properties so we can compare them during a flush
@initial_properties = @property_hash.dup
end
def exists?
@property_hash[:ensure] != :absent
end
def create
@property_hash[:ensure] = :present
end
def destroy
@property_hash[:ensure] = :absent
end
def flush
if @property_hash[:ensure] != :absent
if @initial_properties[:ensure] != :absent
info "deleting and recreating net_share '#{name}'"
execute_delete
end
execute_create
else
execute_delete
end
@property_hash.clear
@initial_properties.clear
end
private
def self.query(name)
cmd = [command(:net), 'share', name]
output = execute(cmd, { :failonfail => false })
properties = {}
properties[:name] = name
if $?.exitstatus == 2
properties[:ensure] = :absent
return properties
elsif $?.exitstatus != 0
raise ExecutionFailure, "Execution of '#{cmd.join(' ')}' returned #{$?.exitstatus}: #{output}"
end
properties[:ensure] = :present
output.each do |line|
break if line.rstrip.length == 0
last_name = name
name = line[0..17].rstrip
value = line[18..-1].rstrip
if name.length == 0
name = last_name
end
case name
when 'Path'
properties[:path] = value
when 'Remark'
properties[:remark] = value
when 'Maximum users'
if value == 'No limit'
properties[:maximumusers] = :unlimited
else
properties[:maximumusers] = value.to_i
end
when 'Caching'
case value
when 'Caching disabled'
value = :none
when 'Manual caching of documents'
value = :manual
when 'Automatic caching of documents'
value = :documents
when 'Automatic caching of programs and documents'
value = :programs
else
raise Puppet::Error, "Unrecognised Caching value '#{value}'"
end
properties[:cache] = value
when 'Permission'
properties[:permissions] ||= []
user, access = value.split(',', 2)
permission = "#{user.strip},#{access.strip.downcase}"
properties[:permissions] << permission
end
end
properties
end
def execute_create
net(*(['share', "#{resource[:name]}=#{resource[:path]}"] + get_property_args()))
end
def execute_delete
net('share', resource[:name], '/delete')
end
def get_property_args()
args = []
self.class.resource_type.validproperties.each do |name|
if name != :ensure
value = @resource.should(name)
unless value.nil?
case name
when :path
when :remark
args << "/remark:#{value}"
when :maximumusers
if value == :unlimited
args << "/unlimited"
else
args << "/users:#{value}"
end
when :cache
args << "/cache:#{value}"
when :permissions
value.each do |user_permissions|
args << "/grant:#{user_permissions}"
end
else
raise Puppet::Error, "Unrecognised property '#{name}'"
end
end
@property_hash[name] = value
end
end
args
end
end
Puppet::Type.type(:net_share).provide(:net_share) do
desc "Windows network share"

confine :operatingsystem => :windows
defaultfor :operatingsystem => :windows

commands :net => File.join(ENV['SystemRoot'] || 'c:/windows', 'system32/net.exe')

mk_resource_methods

# Match resources to providers where the resource name matches the provider name
def self.prefetch(resources)
resources.each do |name, resource|
provider = new(query(name))

if provider
resource.provider = provider
else
resource.provider = new(:ensure => :absent)
end
end
end

def initialize(*args)
super

# Make a duplicate of the properties so we can compare them during a flush
@initial_properties = @property_hash.dup
end

def exists?
@property_hash[:ensure] != :absent
end

def create
@property_hash[:ensure] = :present
end

def destroy
@property_hash[:ensure] = :absent
end

def flush
if @property_hash[:ensure] != :absent
if @initial_properties[:ensure] != :absent
info "deleting and recreating net_share '#{name}'"
execute_delete
end

execute_create
else
execute_delete
end

@property_hash.clear
@initial_properties.clear
end

private
def self.query(name)
cmd = [command(:net), 'share', name]
output = execute(cmd, { :failonfail => false })

properties = {}
properties[:name] = name

if $?.exitstatus == 2
properties[:ensure] = :absent
return properties
elsif $?.exitstatus != 0
raise ExecutionFailure, "Execution of '#{cmd.join(' ')}' returned #{$?.exitstatus}: #{output}"
end

properties[:ensure] = :present

output.each_line do |line|
break if line.rstrip.length == 0

last_name = name
name = line[0..17].rstrip
value = line[18..-1].rstrip

if name.length == 0
name = last_name
end

case name
when 'Path'
properties[:path] = value
when 'Remark'
properties[:remark] = value
when 'Maximum users'
if value == 'No limit'
properties[:maximumusers] = :unlimited
else
properties[:maximumusers] = value.to_i
end
when 'Caching'
case value
when 'Caching disabled'
value = :none
when 'Manual caching of documents'
value = :manual
when 'Automatic caching of documents'
value = :documents
when 'Automatic caching of programs and documents'
value = :programs
else
raise Puppet::Error, "Unrecognised Caching value '#{value}'"
end

properties[:cache] = value
when 'Permission'
properties[:permissions] ||= []

user, access = value.split(',', 2)
permission = "#{user.strip},#{access.strip.downcase}"

properties[:permissions] << permission
end
end

properties
end

def execute_create
net(*(['share', "#{resource[:name]}=#{resource[:path]}"] + get_property_args()))
end

def execute_delete
net('share', resource[:name], '/delete')
end

def get_property_args()
args = []

self.class.resource_type.validproperties.each do |name|
if name != :ensure
value = @resource.should(name)

unless value.nil?
case name
when :path
when :remark
args << "/remark:#{value}"
when :maximumusers
if value == :unlimited
args << "/unlimited"
else
args << "/users:#{value}"
end
when :cache
args << "/cache:#{value}"
when :permissions
value.each do |user_permissions|
args << "/grant:#{user_permissions}"
end
else
raise Puppet::Error, "Unrecognised property '#{name}'"
end
end

@property_hash[name] = value
end
end

args
end
end
Loading