File: opkg.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (82 lines) | stat: -rw-r--r-- 1,990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require_relative '../../../puppet/provider/package'

Puppet::Type.type(:package).provide :opkg, :source => :opkg, :parent => Puppet::Provider::Package do
  desc "Opkg packaging support. Common on OpenWrt and OpenEmbedded platforms"

  commands :opkg => "opkg"

  confine     :operatingsystem => :openwrt
  defaultfor  :operatingsystem => :openwrt

  def self.instances
    packages = []
    execpipe("#{command(:opkg)} list-installed") do |process|
      regex = %r{^(\S+) - (\S+)}
      fields = [:name, :ensure]
      hash = {}

      process.each_line { |line|
        match = regex.match(line)
        if match
          fields.zip(match.captures) { |field,value| hash[field] = value }
          hash[:provider] = self.name
          packages << new(hash)
          hash = {}
        else
          warning(_("Failed to match line %{line}") % { line: line })
        end
      }
    end
    packages
  rescue Puppet::ExecutionFailure
    return nil
  end

  def latest
    output = opkg( "list", @resource[:name])
    matches = /^(\S+) - (\S+)/.match(output).captures
    matches[1]
  end

  def install
    # OpenWrt package lists are ephemeral, make sure we have at least
    # some entries in the list directory for opkg to use
    opkg('update') if package_lists.size <= 2

    if @resource[:source]
      opkg( '--force-overwrite', 'install', @resource[:source] )
    else
      opkg( '--force-overwrite', 'install', @resource[:name] )
    end
  end

  def uninstall
    opkg( 'remove', @resource[:name] )
  end

  def update
    self.install
  end

  def query
    # list out our specific package
    output = opkg( 'list-installed', @resource[:name] )
    if output =~ /^(\S+) - (\S+)/
      return { :ensure => $2 }
    end
    nil
  rescue Puppet::ExecutionFailure
    return {
      :ensure => :purged,
      :status => 'missing',
      :name => @resource[:name],
      :error => 'ok',
    }
  end

  private

  def package_lists
    Dir.entries('/var/opkg-lists/')
  end
end