nub 0.0.76 → 0.0.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nub/fileutils.rb +10 -24
  3. data/lib/nub/pacman.rb +60 -38
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa43f1a038ebba265bc6b032dcb802cd5870e9b1cc703fe46fb5a96f6240d71a
4
- data.tar.gz: 5f6201fb958875f52935cb9b0257831671d3dd3db21f2ccbcb49356fdd1cf8d8
3
+ metadata.gz: 3efbedee8e45692f1b9cf2039541b2196ad9d9e511916964bae27f1777c9a796
4
+ data.tar.gz: 7928aabd0d289dd414473f7aa18c2c50e2b3f9626efb557dc801a2bdef5468a4
5
5
  SHA512:
6
- metadata.gz: da19249c6af9fac56ac2d8c7ec4c6a525792bbca53fe1b63b8dc5e47c0439e525b5e918ddfdc82fb820579e708dc4ed12602722491638f4be3681701e9d75019
7
- data.tar.gz: 902fbbaae0773ca9cd7a36b6d3575d9fba4b602ea85e3ae6789c9a599d4fc30cc5fb273bd18d7f7f71b9503027315a037199be38f0b60fc4700dd749f019c64f
6
+ metadata.gz: b768e4e55ac03d9619cbcf64c5429e045c3dc9e8975825b8aaf3352843ac2effa06a6d0866ee7c8157e60ec0b29aa594358145b623b07c308fbdedda57a9a085
7
+ data.tar.gz: 89fb4422d0d78aa692a096401468508ebdbf3abd611ebff5fdfc3c6cc504bf046cfc4ed69ec703af6aba503b34203e01ef75ac87d463b64f5f28d32cee697923
data/lib/nub/fileutils.rb CHANGED
@@ -119,38 +119,24 @@ module FileUtils
119
119
  def self.insert(file, values, regex:nil, offset:1)
120
120
  return false if not values or values.empty?
121
121
 
122
- changed = false
123
122
  values = [values] if values.is_a?(String)
124
123
  FileUtils.touch(file) if not File.exist?(file)
125
124
 
126
- begin
127
- data = nil
128
- File.open(file, 'r+') do |f|
129
- data = f.read
130
- lines = data.split("\n")
125
+ changed = self.update(file){|data|
126
+ lines = data.split("\n")
131
127
 
132
- # Match regex for insert location
133
- regex = Regexp.new(regex) if regex.is_a?(String)
134
- i = regex ? lines.index{|x| x =~ regex} : lines.size
135
- return false if not i
128
+ # Match regex for insert location
129
+ regex = Regexp.new(regex) if regex.is_a?(String)
130
+ if i = regex ? lines.index{|x| x =~ regex} : lines.size
136
131
  i += offset if regex and offset
137
132
 
138
133
  # Insert at offset
139
- values.each{|x|
140
- lines.insert(i, x) and i += 1
141
- changed = true
142
- }
143
-
144
- # Truncate then write out new content
145
- f.seek(0)
146
- f.truncate(0)
147
- f.puts(lines)
134
+ values.each{|x| lines.insert(i, x) and i += 1}
135
+
136
+ # Change data inline
137
+ data.gsub!(data, lines * "\n")
148
138
  end
149
- rescue
150
- # Revert back to the original incase of failure
151
- File.open(file, 'w'){|f| f << data} if data
152
- raise
153
- end
139
+ }
154
140
 
155
141
  return changed
156
142
  end
data/lib/nub/pacman.rb CHANGED
@@ -23,71 +23,93 @@ require 'fileutils'
23
23
  require_relative 'log'
24
24
  require_relative 'sys'
25
25
  require_relative 'module'
26
+ require_relative 'fileutils'
26
27
 
27
28
  # Wrapper around system Arch Linux pacman
28
29
  module Pacman
29
30
  extend self
30
- mattr_accessor(:path, :config, :sysroot)
31
+ mattr_accessor(:path, :config, :sysroot, :mirrors, :repos, :arch, :env)
31
32
 
32
33
  # Configure pacman for the given root
33
34
  # @param path [String] path where all pacman artifacts will be (i.e. logs, cache etc...)
34
35
  # @param config [String] config file path to use, note gets copied in
36
+ # @param mirrors [Array] of mirror paths to use, mirror file name is expected to be the
37
+ # name of the repo e.g. archlinux.mirrorlist
38
+ # @param arch [String] capturing the pacman target architecture e.g. x86_64
35
39
  # @param sysroot [String] path to the system root to use
36
- def init(path, config, sysroot)
40
+ # @param env [Hash] of environment variables to set for session
41
+ def init(path, config, mirrors, arch:'x86_64', sysroot:nil, env:nil)
42
+ mirrors = [mirrors] if mirrors.is_a?(String)
37
43
  self.path = path
44
+ self.arch = arch
38
45
  self.sysroot = sysroot
39
46
  self.config = File.join(path, File.basename(config))
47
+ self.repos = mirrors.map{|x| File.basename(x, '.mirrorlist')}
48
+ self.mirrors = mirrors.map{|x| File.join(path, File.basename(x))}
40
49
 
41
50
  # Validate incoming params
42
- Log.die("pacman path '#{path}' doesn't exist") unless Dir.exist?(path)
43
- Log.die("pacman sysroot '#{sysroot}' doesn't exist") unless Dir.exist?(sysroot)
44
- Log.die("pacman config file '#{config}' doesn't exist") unless File.exist?(config)
51
+ Log.die("pacman config '#{config}' doesn't exist") unless File.exist?(config)
45
52
 
46
- # Update the given pacman config file to use the given path
53
+ # Copy in pacman files for use in target
47
54
  FileUtils.rm_rf(File.join(path, '.'))
55
+ FileUtils.mkdir_p(File.join(self.path, 'db'))
56
+ FileUtils.mkdir_p(self.sysroot) if self.sysroot && !Dir.exist?(self.sysroot)
48
57
  FileUtils.cp(config, path, preserve: true)
49
-
50
- Sys.exec("cp -a #{@pacman_src_mirrors} #{@pacman_path}")
51
- Fedit.replace(@pacman_conf, /(Architecture = ).*/, "\\1#{@vars.arch}")
52
- # Leave DBPath set as /var/lib/pacman and copy out sync
53
- Fedit.replace(@pacman_conf, /#(CacheDir\s+= ).*/, "\\1#{File.join(@pacman_path, 'cache')}")
54
- Fedit.replace(@pacman_conf, /#(LogFile\s+= ).*/, "\\1#{File.join(@pacman_path, 'pacman.log')}")
55
- Fedit.replace(@pacman_conf, /#(GPGDir\s+= ).*/, "\\1#{File.join(@pacman_path, 'gnupg')}")
56
- Fedit.replace(@pacman_conf, /#(HookDir\s+= ).*/, "\\1#{File.join(@pacman_path, 'hooks')}")
57
- Fedit.replace(@pacman_conf, /.*(\/.*mirrorlist).*/, "Include = #{@pacman_path}\\1")
58
+ FileUtils.cp(mirrors, path, preserve: true)
58
59
 
59
- repos = Dir[File.join(@pacman_path, "*.mirrorlist")].map{|x| File.basename(x, '.mirrorlist')}
60
- Sys.exec("pacman-key --config #{@pacman_conf} --init")
61
- Sys.exec("pacman-key --config #{@pacman_conf} --populate #{repos * ' '}")
60
+ # Update the given pacman config file to use the given path
61
+ FileUtils.replace(self.config, /(Architecture = ).*/, "\\1#{self.arch}")
62
+ FileUtils.replace(self.config, /#(DBPath\s+= ).*/, "\\1#{File.join(self.path, 'db')}")
63
+ FileUtils.replace(self.config, /#(CacheDir\s+= ).*/, "\\1#{File.join(self.path, 'cache')}")
64
+ FileUtils.replace(self.config, /#(LogFile\s+= ).*/, "\\1#{File.join(self.path, 'pacman.log')}")
65
+ FileUtils.replace(self.config, /#(GPGDir\s+= ).*/, "\\1#{File.join(self.path, 'gnupg')}")
66
+ FileUtils.replace(self.config, /#(HookDir\s+= ).*/, "\\1#{File.join(self.path, 'hooks')}")
67
+ FileUtils.replace(self.config, /.*(\/.*mirrorlist).*/, "Include = #{self.path}\\1")
62
68
 
63
69
  # Initialize pacman keyring
64
- #Sys.exec("pacman-key --config #{self.config} --init")
65
- #Sys.exec("pacman-key --config #{self.config} --populate #{repos * ' '}")
70
+ Sys.exec("pacman-key --config #{self.config} --init")
71
+ Sys.exec("pacman-key --config #{self.config} --populate #{repos * ' '}")
66
72
  end
67
73
 
68
74
  # Update the pacman database
69
75
  def update
70
- success = false
71
- while not success
72
- begin
73
- cmd = "pacman -Sy"
74
- cmd += " --sysroot #{self.sysroot}" if self.sysroot
75
- Sys.exec(cmd)
76
- success = true
77
- rescue Exception => e
78
- puts(e.message)
79
- end
80
- end
76
+ cmd = "pacman -Sy"
77
+ cmd += " --config #{self.config}" if self.config
78
+ Sys.exec(cmd)
81
79
  end
82
80
 
83
81
  # Install the given packages
84
- # @param pkgs [Array] List of packages to install
85
- def install()
86
- puts("Pacstrap package installs for '#{deployment}'...".colorize(:cyan))
87
- cmd = ['pacstrap', '-GMc', deployment_work, '--config', @pacman_conf, '--needed', *apps[:reg]]
88
- cmd += ['--ignore', apps[:ignore] * ','] if apps[:ignore].any?
89
- !puts("Error: Failed to install packages correctly".colorize(:red)) and
90
- exit unless Sys.exec(cmd, env:@proxyenv)
82
+ # @param pkgs [Array] of packages to install
83
+ # @param ignore [Array] of packages to ignore
84
+ def install(pkgs, ignore:nil)
85
+ cmd = []
86
+
87
+ if self.sysroot
88
+ cmd += ["pacstrap", "-GMc", self.sysroot, '--config', self.config]
89
+ else
90
+ cmd += ["pacman", "-S"]
91
+ end
92
+
93
+ # Ignore any packages called out
94
+ ignore = [ignore] if ignore.is_a?(String)
95
+ cmd += ["--ignore", "#{ignore * ','}"] if ignore && ignore.any?
96
+
97
+ # Add packages to install
98
+ cmd += ['--needed', *pkgs]
99
+
100
+ # Execute if there are any packages given
101
+ if pkgs.any?
102
+ self.env ? Sys.exec(cmd, env:self.env) : Sys.exec(cmd)
103
+ end
104
+ end
105
+
106
+ # Remove the given conflicting packages
107
+ # @param pkgs [Array] of packages to remove
108
+ def remove_conflict(pkgs)
109
+ cmd = "pacman -Rn"
110
+ cmd += " -r #{self.sysroot}" if self.sysroot
111
+ cmd += " -d -d --noconfirm #{pkgs * ' '} &>/dev/null || true"
112
+ Sys.exec(cmd)
91
113
  end
92
114
  end
93
115
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.76
4
+ version: 0.0.81
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Crummett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-12 00:00:00.000000000 Z
11
+ date: 2018-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize