Module: Pacman

Extended by:
Pacman
Included in:
Pacman
Defined in:
lib/nub/pacman.rb

Overview

Wrapper around system Arch Linux pacman

Instance Method Summary collapse

Instance Method Details

#init(path, sysroot, config, mirrors, arch: 'x86_64', env: nil) ⇒ Object

Configure pacman for the given root

Parameters:

  • path (String)

    is the path on the host for pacman

  • sysroot (String)

    path to the system root to use

  • config (String)

    config file path to use, note gets copied in

  • mirrors (Array)

    of mirror paths to use, mirror file name is expected to be the name of the repo e.g. archlinux.mirrorlist

  • arch (String) (defaults to: 'x86_64')

    capturing the pacman target architecture e.g. x86_64

  • env (Hash) (defaults to: nil)

    of environment variables to set for session



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
83
84
85
86
87
# File 'lib/nub/pacman.rb', line 42

def init(path, sysroot, config, mirrors, arch:'x86_64', env:nil)

  # All configs are on the sysroot except config and cache
  mirrors = [mirrors] if mirrors.is_a?(String)
  self.path = path
  self.arch = arch
  self.env = env
  self.sysroot = sysroot
  self.log = File.join(sysroot, 'var/log/pacman.log')
  self.db_path = File.join(self.sysroot, 'var/lib/pacman')
  self.gpg_path = File.join(self.sysroot, 'etc/pacman.d/gnupg')
  self.hooks_path = File.join(self.sysroot, 'etc/pacman.d/hooks')
  self.repos = mirrors.map{|x| File.basename(x, '.mirrorlist')}
  mirrors_path = File.join(self.sysroot, 'etc/pacman.d')
  self.mirrors = mirrors.map{|x| File.join(mirrors_path, File.basename(x))}

  # Config and cache are kept separate from the sysroot
  self.config = File.join(self.path, File.basename(config))
  self.cache_path = File.join(self.path, 'cache')

  # Validate incoming params
  Log.die("pacman config '#{config}' doesn't exist") unless File.exist?(config)

  # Copy in pacman files for use in target
  FileUtils.mkdir_p(self.db_path)
  FileUtils.mkdir_p(self.gpg_path)
  FileUtils.mkdir_p(self.hooks_path)
  FileUtils.mkdir_p(self.cache_path)
  FileUtils.cp(config, self.config, preserve: true)
  FileUtils.cp(mirrors, mirrors_path, preserve: true)

  # Update the given pacman config file to use the given path
  FileUtils.replace(self.config, /(Architecture = ).*/, "\\1#{self.arch}")
  FileUtils.replace(self.config, /#(DBPath\s+= ).*/, "\\1#{self.db_path}")
  FileUtils.replace(self.config, /#(CacheDir\s+= ).*/, "\\1#{self.cache_path}")
  FileUtils.replace(self.config, /#(LogFile\s+= ).*/, "\\1#{self.log}")
  FileUtils.replace(self.config, /#(GPGDir\s+= ).*/, "\\1#{self.gpg_path}")
  FileUtils.replace(self.config, /#(HookDir\s+= ).*/, "\\1#{self.hooks_path}")
  FileUtils.replace(self.config, /.*(\/.*mirrorlist).*/, "Include = #{mirrors_path}\\1")

  # Initialize pacman keyring
  if !File.exist?(File.join(self.gpg_path, 'trustdb.gpg'))
    Sys.exec("pacman-key --config #{self.config} --init")
    Sys.exec("pacman-key --config #{self.config} --populate #{repos * ' '}")
  end
end

#install(pkgs, flags: nil) ⇒ Object

Install the given packages

Parameters:

  • pkgs (Array)

    of packages to install

  • flags (Array) (defaults to: nil)

    of params to pass into pacman



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/nub/pacman.rb', line 99

def install(pkgs, flags:nil)
  if pkgs && pkgs.any?
    cmd = []

    if self.sysroot
      cmd += ["pacstrap", "-c", "-M", self.sysroot, '--config', self.config]
    else
      cmd += ["pacman", "-S"]
    end

    # Add user flags
    cmd += flags if flags

    # Add packages to install
    cmd += ['--needed', *pkgs]

    # Execute
    self.env ? Sys.exec(cmd, env:self.env) : Sys.exec(cmd)
  end
end

#remove_conflict(pkgs) ⇒ Object

Remove the given conflicting packages

Parameters:

  • pkgs (Array)

    of packages to remove



122
123
124
125
126
127
128
129
# File 'lib/nub/pacman.rb', line 122

def remove_conflict(pkgs)
  if pkgs && pkgs.any?
    cmd = "pacman -Rn"
    cmd += " -r #{self.sysroot}" if self.sysroot
    cmd += " -d -d --noconfirm #{pkgs * ' '} &>/dev/null || true"
    Sys.exec(cmd)
  end
end

#updateObject

Update the pacman database



90
91
92
93
94
# File 'lib/nub/pacman.rb', line 90

def update
  cmd = "pacman -Sy"
  cmd += " --config #{self.config}" if self.config
  Sys.exec(cmd)
end