summaryrefslogtreecommitdiff
path: root/lib/dslkeywords/package.rb
blob: 9e4324ff0fd861db123865c70259a706c95e9159 (plain)
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
require 'erb'
require 'fileutils'

require_relative 'resource'

module RCM
  class DNFPackageManager
    # Raised when a dnf subcommand exits with a non-zero status or when
    # the dnf binary cannot be found.
    class CommandFailed < StandardError; end

    def installed?(pkg) = false

    def install(pkg)
      return if installed?(pkg)

      run_dnf!('install', pkg)
    end

    def update(pkg)
      run_dnf!('update', pkg)
    end

    def remove(pkg)
      return unless installed?(pkg)

      run_dnf!('remove', pkg)
    end

    private

    # Execute dnf <subcommand> -y <pkg> using separate arguments (no shell
    # interpolation). Raises CommandFailed when dnf exits non-zero or is
    # not found ($? is nil when the binary cannot be exec'd).
    def run_dnf!(subcommand, pkg)
      result = system('dnf', subcommand, '-y', pkg)
      return if result

      exit_code = $?&.exitstatus || '?'
      raise CommandFailed, "dnf #{subcommand} #{pkg} failed (exit #{exit_code})"
    end
  end

  # Managing packages
  class Package < Resource
    attr_reader :path

    class UnsupportedOS < StandardError; end

    def initialize(name)
      super(name)
      # Use ::File to avoid resolving to RCM::File once file.rb is loaded.
      raise UnsupportedOS, 'OS is not supported' unless ::File.file?('/etc/fedora-release')

      @manager = DNFPackageManager.new

      @name = name
    end

    def packages(*pks)
      raise 'Not yet implemented'
    end

    def evaluate!
      nil unless super
    end
  end

  # Add file keyword to the DSL
  class DSL
    def package(name, &block)
      return unless @conds_met

      f = Package.new(name)
      f.packages(f.instance_eval(&block))
      self << f
      f
    end
  end
end