Created
April 4, 2014 06:34
-
-
Save crrlcx/9969276 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright (c) 2012 ZenRobotics Ltd. All rights reserved. | |
| # $modules/$module/lib/puppet/type/mapped_drive.rb | |
| Puppet::Type.newtype(:mapped_drive) do | |
| ensurable | |
| newparam(:local, :namevar => true) do | |
| desc 'Drive letter to map.' | |
| end | |
| newparam(:remote) do | |
| desc 'Remote UNC path to map drive letter to.' | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright (c) 2012 ZenRobotics Ltd. All rights reserved. | |
| # $modules/$module/lib/puppet/provider/mapped_drive/net.rb | |
| Puppet::Type.type(:mapped_drive).provide(:net) do | |
| defaultfor :osfamily => :windows | |
| confine :osfamily => :windows | |
| commands :net => 'net' | |
| def exists? | |
| parse | |
| if @mapped.key? resource[:local] and @mapped[resource[:local]] == resource[:remote] | |
| true | |
| else | |
| false | |
| end | |
| end | |
| def create | |
| parse | |
| if @mapped.key? resource[:local] | |
| unmap! resource[:local] | |
| # FIXME: needs delay or something, otherwise the next call seems to fail | |
| end | |
| map! resource[:local], resource[:remote] | |
| end | |
| def destroy | |
| unmap! resource[:local] | |
| end | |
| private | |
| def parse | |
| output = IO.popen('net use').readlines | |
| @mapped = {} | |
| for line in output do | |
| if match = /^(\w+)\s+(\w:)\s+([\w\.\\]+)\s+.*$/.match(line) | |
| status = match[1] | |
| local = match[2] | |
| remote = match[3] | |
| @mapped[local] = remote | |
| end | |
| end | |
| end | |
| def unmap!(local) | |
| output = IO.popen("net use #{local} /delete") | |
| end | |
| def map!(local, remote) | |
| output = IO.popen("net use #{local} #{remote}") | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment