Skip to content

Commit 8e13a9c

Browse files
author
Amit Budhu
committed
Update License, and modified readme
0 parents  commit 8e13a9c

12 files changed

+290
-0
lines changed

Berksfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source "https://supermarket.chef.io"
2+
3+
metadata

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 0.1.0
2+
3+
Initial release of winservice

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
gem "test-kitchen"
4+
gem "kitchen-vagrant"

LICENSE

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Copyright (C) 2016 Amitraj Budhu
2+
3+
All rights reserved - Do Not Redistribute

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# winservice-cookbook
2+
3+
Chef LWRP for creating and deleting Windows Services
4+
5+
## Supported Platforms
6+
7+
Windows 2008R2
8+
Windows 2012
9+
Windows 2012R2
10+
11+
## Attributes
12+
13+
<table>
14+
<tr>
15+
<th>Name</th>
16+
<th>String</th>
17+
<th>Name of the Service</th>
18+
<th>nil</th>
19+
</tr>
20+
<tr>
21+
<td><tt>['winservice']['bacon']</tt></td>
22+
<td>Boolean</td>
23+
<td>whether to include bacon</td>
24+
<td><tt>true</tt></td>
25+
</tr>
26+
</table>
27+
28+
## Usage
29+
30+
win_service "Create Window Service" do
31+
name MyService
32+
bin_path C:\\My\\Awesome\\Path
33+
display_name MyAwesomeService
34+
run_as_user Foo\\Bar
35+
run_as_password MySuperSecretPassword
36+
action :create
37+
end
38+
39+
win_service "Delete Window Service" do
40+
name MyService
41+
action :delete
42+
end
43+
44+
### winservice::default
45+
46+
Include `winservice` in your node's `metadata.rb`:
47+
48+
49+
## License and Authors
50+
51+
Author:: Amitraj Budhu (<[email protected]>)

Thorfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# encoding: utf-8
2+
3+
require 'bundler'
4+
require 'bundler/setup'
5+
require 'berkshelf/thor'
6+
7+
begin
8+
require 'kitchen/thor_tasks'
9+
Kitchen::ThorTasks.new
10+
rescue LoadError
11+
puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI']
12+
end

Vagrantfile

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
5+
VAGRANTFILE_API_VERSION = '2'
6+
7+
Vagrant.require_version '>= 1.5.0'
8+
9+
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
10+
# All Vagrant configuration is done here. The most common configuration
11+
# options are documented and commented below. For a complete reference,
12+
# please see the online documentation at vagrantup.com.
13+
14+
config.vm.hostname = 'winservice-berkshelf'
15+
16+
# Set the version of chef to install using the vagrant-omnibus plugin
17+
# NOTE: You will need to install the vagrant-omnibus plugin:
18+
#
19+
# $ vagrant plugin install vagrant-omnibus
20+
#
21+
if Vagrant.has_plugin?("vagrant-omnibus")
22+
config.omnibus.chef_version = 'latest'
23+
end
24+
25+
# Every Vagrant virtual environment requires a box to build off of.
26+
# If this value is a shorthand to a box in Vagrant Cloud then
27+
# config.vm.box_url doesn't need to be specified.
28+
config.vm.box = 'chef/ubuntu-14.04'
29+
30+
31+
# Assign this VM to a host-only network IP, allowing you to access it
32+
# via the IP. Host-only networks can talk to the host machine as well as
33+
# any other machines on the same network, but cannot be accessed (through this
34+
# network interface) by any external networks.
35+
config.vm.network :private_network, type: 'dhcp'
36+
37+
# Create a forwarded port mapping which allows access to a specific port
38+
# within the machine from a port on the host machine. In the example below,
39+
# accessing "localhost:8080" will access port 80 on the guest machine.
40+
41+
# Share an additional folder to the guest VM. The first argument is
42+
# the path on the host to the actual folder. The second argument is
43+
# the path on the guest to mount the folder. And the optional third
44+
# argument is a set of non-required options.
45+
# config.vm.synced_folder "../data", "/vagrant_data"
46+
47+
# Provider-specific configuration so you can fine-tune various
48+
# backing providers for Vagrant. These expose provider-specific options.
49+
# Example for VirtualBox:
50+
#
51+
# config.vm.provider :virtualbox do |vb|
52+
# # Don't boot with headless mode
53+
# vb.gui = true
54+
#
55+
# # Use VBoxManage to customize the VM. For example to change memory:
56+
# vb.customize ["modifyvm", :id, "--memory", "1024"]
57+
# end
58+
#
59+
# View the documentation for the provider you're using for more
60+
# information on available options.
61+
62+
# The path to the Berksfile to use with Vagrant Berkshelf
63+
# config.berkshelf.berksfile_path = "./Berksfile"
64+
65+
# Enabling the Berkshelf plugin. To enable this globally, add this configuration
66+
# option to your ~/.vagrant.d/Vagrantfile file
67+
config.berkshelf.enabled = true
68+
69+
# An array of symbols representing groups of cookbook described in the Vagrantfile
70+
# to exclusively install and copy to Vagrant's shelf.
71+
# config.berkshelf.only = []
72+
73+
# An array of symbols representing groups of cookbook described in the Vagrantfile
74+
# to skip installing and copying to Vagrant's shelf.
75+
# config.berkshelf.except = []
76+
77+
config.vm.provision :chef_solo do |chef|
78+
chef.json = {
79+
mysql: {
80+
server_root_password: 'rootpass',
81+
server_debian_password: 'debpass',
82+
server_repl_password: 'replpass'
83+
}
84+
}
85+
86+
chef.run_list = [
87+
'recipe[winservice::default]'
88+
]
89+
end
90+
end

chefignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.kitchen

libraries/helpers.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module WinServiceCookbook
2+
module Helpers
3+
include Chef::DSL::IncludeRecipe
4+
require "win32/service"
5+
6+
def service_exists?(service_name)
7+
if Win32::Service.exists?(service_name)
8+
true
9+
else
10+
false
11+
end
12+
end
13+
14+
def create_sc_command(name, bin_path, start_type, display_name, run_as_user, run_as_password)
15+
if run_as_user == "LocalSystem"
16+
command = "\"#{name}\" binPath= \"#{bin_path}\" start= #{start_type} DisplayName= \"#{display_name}\" obj= \"#{run_as_user}\""
17+
else
18+
command = "\"#{name}\" binPath= \"#{bin_path}\" start= #{start_type} DisplayName= \"#{display_name}\" obj= \"#{run_as_user}\" password= \"#{run_as_password}\""
19+
end
20+
end
21+
22+
def stop_service(service_name)
23+
if Win32::Service.exists?(service_name) && Win32::Service.status(service_name)['current_state'] != "stopped"
24+
begin
25+
Win32::Service.stop(service_name)
26+
rescue Exception => e
27+
raise "Service #{service_name} could not be stopped. Try again later.\nFull Exception: #{e}"
28+
end
29+
end
30+
end
31+
32+
end
33+
end

libraries/provider_winservice.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'chef/provider/lwrp_base'
2+
require_relative 'helpers'
3+
4+
class Chef
5+
class Provider
6+
class WinService < Chef::Provider::LWRPBase
7+
include WinServiceCookbook::Helpers
8+
provides :win_service if defined?(provides)
9+
10+
use_inline_resources if defined?(use_inline_resources)
11+
12+
def whyrun_supported?
13+
true
14+
end
15+
16+
# Start main :install action
17+
action :create do
18+
if service_exists?(new_resource.name)
19+
Chef::Log::fatal("Service already exists by name:#{new_resource.name}")
20+
else
21+
invoke_command = create_sc_command(new_resource.name, new_resource.bin_path, new_resource.start_type, new_resource.display_name, new_resource.run_as_user, new_resource.run_as_password)
22+
23+
powershell_script "Creating Service: #{new_resource.name}" do
24+
code <<-EOH
25+
sc.exe create #{invoke_command}
26+
EOH
27+
action :run
28+
end
29+
30+
end
31+
32+
end
33+
34+
35+
action :delete do
36+
if service_exists?(new_resource.name)
37+
stop_service(new_resource.name)
38+
powershell_script "Removing Service: #{new_resource.name}" do
39+
code <<-EOH
40+
sc.exe delete \"#{new_resource.name}\"
41+
EOH
42+
end
43+
else
44+
Chef::Log::fatal("No Service by name of: #{new_resource.name} exists. Are you sure you have the right service name?")
45+
end
46+
end
47+
48+
49+
end
50+
end
51+
end

libraries/resource_winservice.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'chef/resource/lwrp_base'
2+
3+
class Chef
4+
class Resource
5+
class WinService < Chef::Resource::LWRPBase
6+
provides :win_service
7+
8+
self.resource_name = :win_service
9+
10+
actions :create,:delete
11+
default_action :create
12+
13+
attribute :name, kind_of: String, default: nil
14+
attribute :bin_path, kind_of: String, default: nil
15+
attribute :start_type, kind_of: String, default: "disabled"
16+
attribute :display_name, kind_of: String, default: nil
17+
attribute :run_as_user, kind_of: String, default: "LocalSystem"
18+
attribute :run_as_password, kind_of: String, default: nil
19+
20+
end
21+
end
22+
end
23+
=begin
24+
#sc.exe
25+
create
26+
\"#{node['order_management']['service_name']}\"
27+
binPath= #{node['order_management']['bin_path']}
28+
start= auto DisplayName= \"#{node['order_management']['service_name']}\"
29+
obj= \"#{node['order_management']['username']}\"
30+
password= \"#{node['order_management']['username']}\"
31+
32+
=end

metadata.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name 'winservice'
2+
maintainer 'Amitraj Budhu'
3+
maintainer_email '[email protected]'
4+
license 'All rights reserved'
5+
description 'Installs/Configures winservice'
6+
long_description 'Installs/Configures winservice'
7+
version '0.1.0'

0 commit comments

Comments
 (0)