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

Potential Exit Code Issue #3

Closed
stack72 opened this issue Apr 12, 2013 · 9 comments
Closed

Potential Exit Code Issue #3

stack72 opened this issue Apr 12, 2013 · 9 comments

Comments

@stack72
Copy link

stack72 commented Apr 12, 2013

Hi Josh

I was trying to use this module today and wrote the following in my init.pp

exec { 'Import ServerManager Module':
command => 'Import-Module ServerManager; Add-WindowsFeature BitLocker',
unless => 'Import-Module ServerManager; if((Get-WindowsFeature BitLocker).Installed -eq $true) {exit 1}',
provider => powershell,
}

The issue here is that my machine doesn't have BitLocker installed on it. So the unless should not return exit 1, which would mean that the command itself should run but it never runs.

Do I need to add anything else here? If I can get this module working, then I can basically script the entire installation of a Windows 2008 and 2012 server.

Thoughts here?

Paul

@joshcooper
Copy link
Contributor

Hi Paul, the unless logic makes my head hurt sometimes. The way it works is that command is executed if unless returns something other than 0.

Since BitLocker is not installed, the unless command returns 0, so the command is never executed. You'll want to flip your logic around, or use an onlyif parameter instead which is the opposite of unless, as in only execute command if onlyif returns 0.

If that resolves the issue, can you close this?

@stack72
Copy link
Author

stack72 commented Apr 12, 2013

Hey

Thanks for responding so fast, I have tried the following now:

exec { 'Import ServerManager Module':
command => 'Import-Module ServerManager; Add-WindowsFeature BitLocker',
onlyif => 'Import-Module ServerManager; if((Get-WindowsFeature BitLocker).Installed -eq $true) {exit 1}',
provider => powershell,
}

Which, to my understanding, means that I should run the command only if BitLocker is not currently installed. If this logic is now correct, then it still doesnt work i'm afraid

Paul

@stack72
Copy link
Author

stack72 commented Apr 15, 2013

Hi Josh

I have manged to get something working here. I have the following init.pp file

class test_windows_install {
    $powershell_cmd = "c:\\Windows\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe -executionpolicy     remotesigned"
    exec { 'telnet-client':
        command => "$powershell_cmd -Command \"Import-Module ServerManager; Add-WindowsFeature Telnet-    Client\"",
        path => $::path,
        onlyif => "$powershell_cmd -Command \"Import-Module ServerManager; if((Get-WindowsFeature Telnet-    Client).Installed -eq $false) {exit 0} else { exit 1 }\"",
        logoutput => true,
    }
}

I wasn't able to get your puppet provider to do an Import-Module as it wasnt able to find the system modules location and I had to import the x64 version of the module. So I went with an exec and a sysnative location.

The trouble here is that this configuration is applied every time. It seems to skip the onlyif clause. Is there any specific things I need to do in order to make the onlyif work?

Paul

@joshcooper
Copy link
Contributor

I wasn't able to get your puppet provider to do an Import-Module as it wasnt able to find the system modules location and I had to import the x64 version of the module.

That's surprising, because I explicitly check for and use the sysnative version (x64) if it's available. It could be related to me not specifying -executionpolicy remotesigned? I'd like to make the powershell options user-configurable.

So I went with an exec and a sysnative location. The trouble here is that this configuration is applied every time.

Try running puppet agent --test --debug and see which powershell commands are being executed, and make sure they are returning the exit codes you expect.

$false) {exit 0} else { exit 1 }""

I noticed your onlyif parameter contains a $false, which puppet will try to interpolate (since you're in a double quoted string). Since you need to interpolate $powershell_cmd, you'll have to escape the dollar, e.g. \$false

@stack72
Copy link
Author

stack72 commented May 23, 2013

hey Josh Thanks for coming back to me, I have been able to implement the following solution now:

#/modules/manage_windows_feature/manifests/init.pp

define manage_windows_feature($feature_name = $title) {
  $powershell_cmd = 'powershell.exe'
  $exec_policy = '-executionpolicy remotesigned'
  exec { "install-feature-${feature_name}" :
    command   => "${powershell_cmd} ${exec_policy} -Command \"Import-Module ServerManager; Add-WindowsFeature ${feature_name}\"",
    path      => "C:\\Windows\\sysnative\\WindowsPowershell\\v1.0;${::path}",
    onlyif    => "${powershell_cmd} ${exec_policy} -Command \"Import-Module ServerManager; if((Get-WindowsFeature ${feature_name}).Installed) { exit 1 }\"",
    logoutput => true,
  }
}

#/modules/windowsfeatures_dotnet_3_5/manifests/init.pp

class windowsfeatures_dotnet_3_5 {
  manage_windows_feature { 'NET-HTTP-Activation': }
  manage_windows_feature { 'NET-Win-CFAC': }
  manage_windows_feature { 'NET-Non-HTTP-Activ': }
  manage_windows_feature { 'AS-MSMQ-Activation': }
}

This allows me to easily abstract the exec command away from the user of the system. I reckon we should be able to get the powershell provider working. I think this is something I would like to explore with you further. I'm hoping you will be at puppetconf? I will be there delivering a talk on puppet and windows so id love to be able to talk more about windows automation there

Paul

@joshcooper
Copy link
Contributor

@stack72 I will definitely be at puppetconf, and awesome to hear you're giving on talk about puppet and windows. If you want to bounce ideas or whatever, let me know, I'd be happy to help. We are planning on an informal hacking on puppet day (or days), so it'd be cool to hack on windows stuff.

About windows features, I assume you've seen our dism module? It would be cool to add support for servermanager. From what I understand servermanager handles dependencies, but dism doesn't?

@ferventcoder
Copy link
Contributor

So where is this at? Does this work now?

@joshcooper
Copy link
Contributor

@stack72 I've submitted a PR #8 for comments, and using that was able to shorten your example to:

define manage_windows_feature($feature_name = $title) {
  exec { "install-feature-${feature_name}" :
    command   => "Import-Module ServerManager; Add-WindowsFeature ${feature_name}",
    onlyif    => "Import-Module ServerManager; if((Get-WindowsFeature ${feature_name}).Installed) { exit 1 }",
    logoutput => true,
    provider  => powershell,
  }
}

Comments welcome!

@joshcooper
Copy link
Contributor

@stack72 It sounds like you have this working, and I recently merged contributions from @rob, such as setting the default -executionpolicy bypass. I'm going to mark this issue as resolved, but if you run into problems, let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants