Commit 72f02b2a authored by Christopher Huhn's avatar Christopher Huhn
Browse files

Rework plugin and collect architecture and source package names of installed packages

parent d81cd4cf
#!/usr/bin/ruby
# -*- coding: iso-8859-15 -*-
#
# plugin to gather dpkg-related information
# Cookbook Name:: sys
# File:: files/default/ohai_plugins/dpkg.rb
#
# $Id$
# Copyright 2013-2022 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
#
# Copyright 2013 GSI Helmholtzzentrum für Schwerionenforschung GmbH <hpc@gsi.de>
# Authors:
# Christopher Huhn <c.huhn@gsi.de>
# Bastian Neuburger <b.neuburger@gsi.de>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# plugin to gather dpkg-related information
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'json'
require 'mixlib/shellout'
# New 'header' for Ohai plugins
Ohai.plugin(:Dpkg) do
# Are there any attributes this plugin depends on?
depends 'platform_family'
depends 'platform_version'
......@@ -32,46 +36,54 @@ Ohai.plugin(:Dpkg) do
# Which attribute name space will this plugin take care of?
provides 'debian'
# Start populating the attribute name space this plugin is
# responsible for.
# You can differentiate between platforms for data collection, e.g.
# collect_data(:windows) and then have a separate
# collect_data(:linux). If you don't want to distinguish, simply use
# one collect_data(:default).
collect_data(:linux) do
# read a list of installed packages:
# dpkg-query can be told to produce arbitrary output
# inspired by https://github.com/demonccc/chef-repo/blob/master/plugins/ohai/linux/dpkg.rb
# but instead of eval'ing the output, we produce JSON and parse it,
# which is much more secure as it prevents us from running arbitrary
# Ruby code via a forged dpkg-query ...
def package_data
dpkg_query = 'dpkg-query -W -f=\'"${Package}": {' \
'"version": "${Version}",' \
'"status": "${Status}",' \
'"arch": "${Architecture}",' \
'"src_pkg": "${source:Package}"' \
'}\n\''
dpkg_query_so = shell_out(dpkg_query)
if dpkg_query_so.error?
Ohai::Log.error "dpkg-query failed: #{dpkg_query_so}"
return
end
JSON.parse("{#{dpkg_query_so.stdout.split("\n").join(',')}}")
end
# Start populating the attribute name space this plugin is responsible for.
collect_data(:linux) do
# Only within the collect_data methods you can access the
# attributes you declared with 'depends' above
if platform_family.eql?('debian')
# read a list of installed packages:
# dpkg-query can be told to produce arbitrary output
# inspired by https://github.com/demonccc/chef-repo/blob/master/plugins/ohai/linux/dpkg.rb
# but instead of eval'ing the output, we produce JSON and parse it, which is much more secure
# as it prevents us from running arbitrary Ruby code via a forged dpkg-query ...
debian Mash.new
debian['packages'] = JSON.parse('{' + `dpkg-query -W -f='"${Package}": {"version":"${Version}", "status":"${Status}"}\n'`.split("\n").join(',') + '}')
# figure out the debian architecture (not neccessarily equal to node.kernel.machine!)
debian["architecture"] = `dpkg --print-architecture`.chomp
# list of enabled multiarch architectures (eg. i386 on amd64):
# no multiarch before Wheezy
if platform_version.to_i > 6 || platform_version =~ /\/sid$/
debian["foreign_architectures"] = `dpkg --print-foreign-architectures`.split("\n")
end
unless platform_family.eql?('debian')
# Print a warning that this plugin is probably not usefule if
# platform_family != debian
Ohai::Log.info "Not a debian derivative, #{__FILE__} only collects " \
"data for nodes with platform_family.eq? 'debian'."
return
end
# this is already provided by the LSB plugin:
debian["codename"] = `lsb_release -cs`.chomp
debian Mash.new
else
debian['packages'] = package_data
# Print a warning that this plugin is probably not usefule if
# platform_family != debian
Ohai::Log.warn("Not a debian derivative, #{__FILE__} only collects data for nodes with platform_family.eq? 'debian'.")
# figure out the debian architecture
# (differs from node['kernel']['machine']!)
debian["architecture"] = shell_out('dpkg --print-architecture').stdout.chomp
# list of enabled multiarch architectures (eg. i386 on amd64):
# no multiarch before Wheezy
if platform_version.to_i > 6 || platform_version =~ %r{/sid$}
debian["foreign_architectures"] =
shell_out('dpkg --print-foreign-architectures').split("\n")
end
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment