Hello everyone,

I haven't seen a Puppet module to create a windows service so I've hacked 
something together as a Defined Type instead of a Parameterized Class 
(mainly because I want to call it for multiple services) and I would 
appreciate feedback and suggestions for improvements.  I'm having a few 
issues where it wants to install/setup the service multiple times as well 
as trying to start the service and wondering if there's a better way. 
 Please excuse type-o's and such, I wrote it pretty fast.

define windows::service(
  $service_zip_file         = undef,  # Zip file of service exe, DLLs and 
config file to use - will be moving to Chocolatey
  $service_name           = undef,  
  $service_description  = undef,
  $service_base_path   = undef, # Had issues with 'paths'
  $service_full_path      = undef,
  $service_binary          = undef,
 ) {

  file { "${service_zip_file}":
    path    => "${service_base_path}/${service_zip_file}",
    source  => "puppet:///modules/our_services/${service_zip_file}",
  }

  acl { "${service_name}-acl":
      target           => "${service_base_path}",
      target_type  => 'file',
      purge           => 'false',
      permissions => [
        { identity       => 'Administrator', 
            rights          => ['full'], 
            perm_type  => 'allow', 
            child_types => 'all', 
            affects        => 'all' 
        },
        { identity => 'Users', 
            rights => ['read','execute'], 
            perm_type=> 'allow', 
            child_types => 'all', 
            affects => 'all' 
        },
      ],
      owner                                  => 'Administrators', 
#Creator_Owner specific, doesn't manage unless specified 
      group                                   => 'Users',         
 #Creator_Group specific, doesn't manage unless specified
      inherit_parent_permissions => 'true',
      require                                 => 
File["${service_name}-${service_ver}"],
    }

  windows::unzip { "${service_base_path}${service_zip_file}":
    destination => "${service_full_path}",
    creates       => "${service_full_path}${service_binary}",
    require        => File["${service_zip_file}"],
  }
 
  Exec { path => $::path }
  $service_exists = "powershell get-service -name ${service_name}"
  $service_config = "start= auto binPath= 
\"${service_full_path}${service_binary}\""
  # ->
  exec { "create_service-${service_name}":
     command => "sc create ${service_name} ${service_config}",
     unless => $service_exists,
   }
   ->
  exec { "update_service-${service_name}":
    command => "sc config ${service_name} ${service_config}",
    onlyif => $service_exists,
  }
  ->
  exec { "configure_recovery-${service_name}":
    command => "sc failure ${service_name} reset= 0 actions= 
restart/10000/restart/10000/restart/10000",
  }
  ->
  exec { "disable_recovery-${service_name}":
    command => "sc failureflag ${service_name} 0",
  }
  ->
  exec { "stop_service-${service_name}":
    command => "powershell stop-process -name $process_name",
    returns => [0, 1],
  }
  ->
  exec { "enable_recovery-${service_name}":
    command => "sc failureflag ${service_name} 1",
  }
  ->
 # I may want to manage this somewhere else, because it keeps tring to 
start the service. 
 # I shouldn't be too hard to determine if a service is running or not
  exec { "start_service-${service_name}":
    command => "sc start ${service_name}",
  }
}
##############################################
## Calling this at the node for 2 services
##############################################
$svcName1             = 'OurService1'
$svcDisplayName1 = 'Our New Service1'
$svcPath1               = 'C:\\Program Files\\ourService1\\'
$svcFullPath1         = 'C:\\Program Files\\ourService1\\1.3\\'
$svcBin1                 = 'ourservice1.exe'
$svcZip1                 = 'ourservice1-1.3.zip'

$svcName1             = 'OurService2'
$svcDisplayName1 = 'Our New Service2'
$svcPath1               = 'C:\\Program Files\\ourService2\\'
$svcFullPath1         = 'C:\\Program Files\\ourService2\\1.3\\'
$svcBin1                 = 'ourservice2.exe'
$svcZip1                 = 'ourservice2-1.3.zip'

windows::service { "${$svcName1}":
     service_zip_file         => "${$svcZip1}",
     service_name           => "${$svcName1}",
     service_description   => "${svcDisplayName1}",
     service_base_path   => "${svcPath1}",
     service_full_path      => "${svcFullPath1}",
     service_binary          => "${svcBin1}",
   }

windows::service { "${$svcName2}":
     service_zip_file         => "${$svcZip2}",
     service_name           => "${$svcName2}",
     service_description   => "${svcDisplayName2}",
     service_base_path   => "${svcPath2}",
     service_full_path      => "${svcFullPath2}",
     service_binary          => "${svcBin2}",
   }

Thanks everyone for your time and brain-cycles.
--Jim



-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/c79b63a6-2c26-45f9-b0b4-cd2dfc6190dc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to