Class: Knife::Pkg::PackageController
- Inherits:
-
Object
- Object
- Knife::Pkg::PackageController
show all
- Defined in:
- lib/knife-pkg/controllers/package_controller.rb
Constant Summary
collapse
- ONE_DAY_IN_SECS =
86500
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.available_updates(node, session, opts = {}) ⇒ Object
-
.init_controller(node, session, opts) ⇒ Object
-
.list_available_updates(updates) ⇒ Object
-
.platform_family_by_local_ohai(session, opts) ⇒ Object
-
.ui ⇒ Object
-
.update!(node, session, packages, opts) ⇒ Object
Connects to the node, updates packages (defined with param ‘packages`) without confirmation, all other available updates with confirmation.
Instance Method Summary
collapse
Constructor Details
#initialize(node, session, opts = {}) ⇒ PackageController
Returns a new instance of PackageController.
31
32
33
34
35
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 31
def initialize(node, session, opts = {})
@node = node
@session = session
@options = opts
end
|
Instance Attribute Details
#node ⇒ Object
Returns the value of attribute node.
26
27
28
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 26
def node
@node
end
|
#options ⇒ Object
Returns the value of attribute options.
28
29
30
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 28
def options
@options
end
|
#session ⇒ Object
Returns the value of attribute session.
27
28
29
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 27
def session
@session
end
|
#ui ⇒ Object
Returns the value of attribute ui.
29
30
31
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 29
def ui
@ui
end
|
Class Method Details
.available_updates(node, session, opts = {}) ⇒ Object
88
89
90
91
92
93
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 88
def available_updates(node, session, opts = {})
ctrl = self.init_controller(node, session, opts)
ctrl.try_update_pkg_cache
updates = ctrl.available_updates
list_available_updates(ctrl.update_info(updates))
end
|
.init_controller(node, session, opts) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 95
def init_controller(node, session, opts)
platform_family = node[:platform_family] || self.platform_family_by_local_ohai(session, opts)
ctrl_name = PlatformFamily.map_to_pkg_ctrl(platform_family)
raise NotImplementedError, "I'm sorry, but #{node[:platform_family]} is not supported!" if ctrl_name == 'unknown'
Chef::Log.debug("Platform Family #{platform_family} detected, using #{ctrl_name}")
require File.join(File.dirname(__FILE__), ctrl_name)
ctrl = Object.const_get('Knife').const_get('Pkg').const_get("#{ctrl_name.capitalize}PackageController").new(node, session, opts)
ctrl.ui = self.ui
ctrl
end
|
.list_available_updates(updates) ⇒ Object
40
41
42
43
44
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 40
def list_available_updates(updates)
updates.each do |update|
ui.info(ui.color("\t" + update.to_s, :yellow))
end
end
|
107
108
109
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 107
def platform_family_by_local_ohai(session, opts)
ShellCommand.exec("ohai platform_family| grep \\\"", session).stdout.strip.gsub(/\"/,'')
end
|
.ui ⇒ Object
46
47
48
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 46
def ui
@ui ||= Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {})
end
|
.update!(node, session, packages, opts) ⇒ Object
Connects to the node, updates packages (defined with param ‘packages`) without confirmation, all other available updates with confirmation
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 59
def update!(node, session, packages, opts)
ctrl = self.init_controller(node, session, opts)
auto_updates = packages.map { |u| Package.new(u) }
updates_for_dialog = Array.new
ctrl.try_update_pkg_cache
available_updates = ctrl.available_updates
if opts[:yes]
auto_updates = available_updates
end
available_updates.each do |avail|
if auto_updates.select { |p| p.name == avail.name }.count == 0
updates_for_dialog << avail
else
ui.info("\tUpdating #{avail.to_s}")
ctrl.update_package_verbose!(avail)
end
end
ctrl.update_dialog(updates_for_dialog)
end
|
Instance Method Details
#available_updates ⇒ Object
returns an ‘Array` of all available updates
141
142
143
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 141
def available_updates
raise NotImplementedError
end
|
#dry_run_supported? ⇒ Boolean
update the package cache e.g apt-get update
117
118
119
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 117
def dry_run_supported?
false
end
|
#exec(cmd) ⇒ Object
159
160
161
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 159
def exec(cmd)
ShellCommand.exec(cmd, @session)
end
|
#installed_version(package) ⇒ Object
returns the version string of the installed package
131
132
133
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 131
def installed_version(package)
raise NotImplementedError
end
|
#last_pkg_cache_update ⇒ Object
returns the ‘Time` of the last package cache update
126
127
128
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 126
def last_pkg_cache_update
raise NotImplementedError
end
|
#max_pkg_cache_age ⇒ Object
163
164
165
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 163
def max_pkg_cache_age
options[:max_pkg_cache_age] || ONE_DAY_IN_SECS
end
|
#sudo ⇒ Object
155
156
157
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 155
def sudo
@options[:sudo] ? 'sudo ' : ''
end
|
#try_update_pkg_cache ⇒ Object
186
187
188
189
190
191
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 186
def try_update_pkg_cache
if Time.now - last_pkg_cache_update > max_pkg_cache_age
@ui.info("Updating package cache...")
update_pkg_cache
end
end
|
#update_dialog(packages) ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 193
def update_dialog(packages)
return if packages.count == 0
ui.info("\tThe following updates are available:")
PackageController.list_available_updates(update_info(packages))
if UserDecision.yes?("\tDo you want to update all packages? [y|n]: ")
ui.info("\tupdating...")
packages.each do |p|
update_package_verbose!(p)
end
ui.info("\tall packages updated!")
else
packages.each do |package|
if UserDecision.yes?("\tDo you want to update #{package}? [y|n]: ")
result = update_package_verbose!(package)
ui.info("\t#{package} updated!")
end
end
end
end
|
#update_info(packages) ⇒ Object
167
168
169
170
171
172
173
174
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 167
def update_info(packages)
result = []
packages.each do |pkg|
installed_version = installed_version(pkg)
result << "#{pkg.name} (new: #{pkg.version} | installed: #{installed_version})"
end
result
end
|
#update_package!(package) ⇒ Object
updates a package should only execute a ‘dry-run’ if @options is set returns a ShellCommandResult
148
149
150
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 148
def update_package!(package)
raise NotImplementedError
end
|
#update_package_verbose!(package) ⇒ Object
176
177
178
179
180
181
182
183
184
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 176
def update_package_verbose!(package)
raise NotImplementedError, "\"dry run\" isn't supported for this platform! (maybe a bug)" if @options[:dry_run] && !dry_run_supported?
result = update_package!(package)
if @options[:dry_run] || @options[:verbose]
ui.info(result.stdout)
ui.error(result.stderr) unless result.stderr.empty?
end
end
|
#update_pkg_cache ⇒ Object
121
122
123
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 121
def update_pkg_cache
raise NotImplementedError
end
|
#update_version(package) ⇒ Object
returns the version string of the available update for a package
136
137
138
|
# File 'lib/knife-pkg/controllers/package_controller.rb', line 136
def update_version(package)
raise NotImplementedError
end
|