Skip to content

Commit 2ce06cb

Browse files
committed
Attempt to address Issue chef-cookbooks#34
1 parent ef62e94 commit 2ce06cb

File tree

5 files changed

+103
-15
lines changed

5 files changed

+103
-15
lines changed

README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# supermarket-omnibus-cookbook
44

5-
This cookbook installs the [Chef Supermarket](https://github.com/opscode/supermarket) server using the [omnibus-supermarket](https://github.com/opscode/omnibus-supermarket) packages from PackageCloud.
5+
This cookbook installs the [Chef Supermarket](https://github.com/opscode/supermarket) server using the [omnibus-supermarket](https://github.com/opscode/omnibus-supermarket) packages from PackageCloud.
66
This cookbook also renders supermarket.json file which is used for managing configuration of Supermarket.
77

88
# Usage
@@ -18,18 +18,26 @@ default['supermarket_omnibus']['chef_oauth2_secret'] = 'a49402219627cfa6318d58b1
1818
default['supermarket_omnibus']['chef_oauth2_verify_ssl'] = false
1919
```
2020

21-
If you wish to specify a package version, a repository or a source, you can do that now:
21+
# Install from the repository of nightly packages
2222
```ruby
23-
default['supermarket_omnibus']['package_version'] = '1.2.3'
24-
25-
# install from the repository of nightly packages
2623
default['supermarket_omnibus']['package_repo'] = 'chef-current'
24+
```
2725

26+
```ruby
2827
# OR, specify a Supermarket package explicitly from a location of your choosing
2928
default['supermarket_omnibus']['package_url'] = 'http://bit.ly/98K8eH'
3029
```
3130

32-
If you wish to specify additional settings, you can pass them via the `default['supermarket_omnibus']['config']` attribute.
31+
# Enable easy upgrades of your Supermarket installation (disabled by default)
32+
```ruby
33+
# set the following in a wrapper cookbook
34+
default['supermarket_omnibus']['upgrades_enabled'] = true # enables upgrade action
35+
default['supermarket_omnibus']['reconfig_after_upgrades'] = true # run `supermarket-ctl reconfigure` after upgrades
36+
default['supermarket_omnibus']['restart_after_upgrades'] = true # run `supermarket-ctl restart` after upgrades
37+
default['supermarket_omnibus']['package_version'] = '2.3.3' # pin to a desired semantic version; upgrade will occurr if necessary
38+
```
39+
40+
If you wish to specify additional settings, you can pass them via the `default['supermarket_omnibus']['config']` attribute.
3341
Example: for custom SSL certificates define the following `config` attributes:
3442

3543
```ruby

attributes/default.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
21
default['supermarket_omnibus']['chef_server_url'] = nil
32
default['supermarket_omnibus']['chef_oauth2_app_id'] = nil
43
default['supermarket_omnibus']['chef_oauth2_secret'] = nil
54
default['supermarket_omnibus']['chef_oauth2_verify_ssl'] = false
65
default['supermarket_omnibus']['config'] = {}
6+
default['supermarket_omnibus']['upgrades_enabled'] = false
7+
default['supermarket_omnibus']['reconfig_after_upgrades'] = false
8+
default['supermarket_omnibus']['restart_after_upgrades'] = false
79

810
default['supermarket_omnibus']['package_url'] = nil
911
default['supermarket_omnibus']['package_version'] = :latest

libraries/supermarket_server.rb

+67-8
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44
class Chef
55
class Resource
6-
# Missing top-level class documentation comment
6+
# Missing top-level class documentation comment.
77
class SupermarketServer < Chef::Resource
8+
attr_accessor :exists, :supermarket_version
9+
810
def initialize(name, run_context = nil)
911
super
1012
@resource_name = :supermarket_server
1113
@provider = Chef::Provider::SupermarketServer
12-
@action = :create
13-
@allowed_actions = [:create]
14+
@allowed_actions = [:create, :upgrade, :reconfigure]
15+
@default_action = :create
1416
end
1517

1618
def chef_server_url(arg = nil)
@@ -29,6 +31,18 @@ def chef_oauth2_verify_ssl(arg = nil)
2931
set_or_return(:chef_oauth2_verify_ssl, arg, kind_of: [TrueClass, FalseClass], required: true)
3032
end
3133

34+
def reconfig_after_upgrades(arg = nil)
35+
set_or_return(:reconfig_after_upgrades, arg, kind_of: [TrueClass, FalseClass], required: true)
36+
end
37+
38+
def restart_after_upgrades(arg = nil)
39+
set_or_return(:restart_after_upgrades, arg, kind_of: [TrueClass, FalseClass], required: true)
40+
end
41+
42+
def supermarket_version(arg = nil)
43+
set_or_return(:supermarket_version, arg, kind_of: [String, Symbol], required: true)
44+
end
45+
3246
def config(arg = nil)
3347
set_or_return(:config, arg, kind_of: [Hash])
3448
end
@@ -38,18 +52,57 @@ def config(arg = nil)
3852

3953
class Chef
4054
class Provider
41-
# Missing top-level class documentation comment
55+
# Missing top-level class documentation comment.
4256
class SupermarketServer < Chef::Provider::LWRPBase
57+
SUPERMARKET_VERSION_FILE = '/opt/supermarket/version-manifest.json'.freeze
58+
SUPERMARKET_CONFIG = '/etc/supermarket/supermarket.json'.freeze
59+
4360
use_inline_resources if defined?(use_inline_resources)
4461

4562
def whyrun_supported?
4663
true
4764
end
4865

66+
def load_current_resource
67+
@current_resource = Chef::Resource::SupermarketServer.new(@new_resource.name)
68+
load_version
69+
Chef::Log.info("Current version: #{@current_resource.supermarket_version}")
70+
Chef::Log.info("Requested version: #{@new_resource.supermarket_version}")
71+
@current_resource
72+
end
73+
74+
def load_version
75+
require 'json'
76+
version_manifest = JSON.parse(::File.read(SUPERMARKET_VERSION_FILE))
77+
@current_resource.supermarket_version = version_manifest['software']['supermarket']['described_version']
78+
@current_resource.exists = ::File.exist?(SUPERMARKET_CONFIG) && ::File.exist?(SUPERMARKET_VERSION_FILE)
79+
rescue
80+
@current_resource.exists = false
81+
@current_resource.supermarket_version = nil
82+
end
83+
84+
def action_reconfigure
85+
execute 'reconfigure_supermarket_instance' do
86+
command 'sudo supermarket-ctl reconfigure'
87+
only_if { new_resource.reconfig_after_upgrades }
88+
end
89+
90+
execute 'restart_supermarket_instance' do
91+
command 'sudo supermarket-ctl restart'
92+
only_if { new_resource.restart_after_upgrades }
93+
end
94+
end
95+
96+
def can_upgrade?(vnow, vnext)
97+
return true if vnow.nil? && vnext == :latest
98+
Gem::Version.new(vnext) > Gem::Version.new(vnow)
99+
rescue
100+
Chef::Log.warn("Cannot upgrade. Please set `node['supermarket_omnibus']['package_version']` to a semantic version.")
101+
false
102+
end
103+
49104
def supermarket_config
50105
{
51-
'chef_server_url' => new_resource.chef_server_url,
52-
'chef_oauth2_app_id' => new_resource.chef_oauth2_app_id,
53106
'chef_oauth2_secret' => new_resource.chef_oauth2_secret,
54107
'chef_oauth2_verify_ssl' => new_resource.chef_oauth2_verify_ssl
55108
}
@@ -59,7 +112,13 @@ def merged_supermarket_config
59112
new_resource.config.merge(supermarket_config)
60113
end
61114

62-
action :create do
115+
def action_upgrade
116+
return unless can_upgrade?(@current_resource.supermarket_version, new_resource.supermarket_version)
117+
action_create
118+
action_reconfigure if @current_resource.exists
119+
end
120+
121+
def action_create
63122
hostsfile_entry node['ipaddress'] do
64123
hostname node['hostname']
65124
not_if "grep #{node['hostname']} /etc/hosts"
@@ -107,7 +166,7 @@ def merged_supermarket_config
107166
package_source cache_path
108167
else
109168
Chef::Log.info "Using CHEF's public repository #{node['supermarket_omnibus']['package_repo']}"
110-
version node['supermarket_omnibus']['package_version']
169+
version new_resource.supermarket_version
111170
end
112171
end
113172
end

recipes/default.rb

+6
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@
1414
chef_oauth2_secret node['supermarket_omnibus']['chef_oauth2_secret']
1515
chef_oauth2_verify_ssl node['supermarket_omnibus']['chef_oauth2_verify_ssl']
1616
config node['supermarket_omnibus']['config'].to_hash
17+
reconfig_after_upgrades node['supermarket_omnibus']['reconfig_after_upgrades']
18+
restart_after_upgrades node['supermarket_omnibus']['restart_after_upgrades']
19+
supermarket_version node['supermarket_omnibus']['package_version']
20+
action :create
1721
end
22+
23+
include_recipe 'supermarket-omnibus-cookbook::upgrades'

recipes/upgrades.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Cookbook Name:: supermarket-omnibus-cookbook
2+
## Recipe:: upgrade
3+
##
4+
## Copyright (c) 2014 The Authors, All Rights Reserved.
5+
#
6+
7+
unless node['supermarket_omnibus']['upgrades_enabled']
8+
Chef::Log.warn('The attribute `node["supermarket_omnibus"]["upgrades_enabled"]` is not set to true.')
9+
Chef::Log.warn('Will not attempt upgrades on this node.')
10+
return
11+
end
12+
13+
resources(supermarket_server: 'supermarket').action :upgrade

0 commit comments

Comments
 (0)