ariva
ariva

Reputation: 11

ruby function adding pkg exclusion if not already exists in each repo defined in repo file

I am trying to create ruby function (within chef cookbook) adding package exclusion string to each repo defined in /etc/yum.repos.d/repo.repo. In example, for pkg zabbix* it search each repo and add 'exclude=zabbix*' if not already added, behind the last repo config line.

What i was try so far looks like this:

def self.exclude_pkg(repo_file, exclude)
  File.open(repo_file, 'r+') do |file|
    lines = file.readlines
    sections = lines.each_with_index.group_by { |line, _| line.strip.start_with?('[') }

    sections.each do |_found, section|
      next unless section.none? { |row, _index| row.strip == "exclude=#{exclude}" }
      puts 'section: ' + section.to_s
      insert_row = section.reverse_each.find { |row, _index| !row.empty? }
      section.insert(insert_row + 1, "exclude=#{exclude}\n")
    end

    file.seek(0)
    file.write(sections.flat_map { |_, section_rows| section_rows })
  end
end

in this example i want to find all epel repos and exclude all zabbix packages, so i call the function like this:

to_exclude = 'zabbix*'
files = Dir.glob('/etc/yum.repos.d/*epel*repo')
files.each do |repo_file|
  exclude_pkg(repo_file, to_exclude)
end

But so far i receive error:

TypeError
no implicit conversion of Integer into Array

in

 10:        sections.each do |_found, section|
 11:          next unless section.none? { |row, _index| row.strip == "exclude=#{exclude}" }
 12:          puts 'section: ' + section.to_s
 13:          insert_row = section.reverse_each.find { |row, _index| !row.empty? }
 14>>         section.insert(insert_row + 1, "exclude=#{exclude}\n")
 15:        end
 16:  
 17:        file.seek(0)
 18:        file.write(sections.flat_map { |_, section_rows| section_rows })
 19:      end
 20:    end
 21:  end

how such a function should be constructed so that:

Example input file (exclusion added only to the last section):

[ol9_baseos_latest]
name=Oracle Linux 9 BaseOS Latest ($basearch)
baseurl=https://yum$ociregion.$ocidomain/repo/OracleLinux/OL9/baseos/latest/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1

[ol9_appstream]
name=Oracle Linux 9 Application Stream Packages ($basearch)
baseurl=https://yum$ociregion.$ocidomain/repo/OracleLinux/OL9/appstream/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1
exclude=zabbix*

Example output file:

[ol9_baseos_latest]
name=Oracle Linux 9 BaseOS Latest ($basearch)
baseurl=https://yum$ociregion.$ocidomain/repo/OracleLinux/OL9/baseos/latest/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1
exclude=zabbix*

[ol9_appstream]
name=Oracle Linux 9 Application Stream Packages ($basearch)
baseurl=https://yum$ociregion.$ocidomain/repo/OracleLinux/OL9/appstream/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1
exclude=zabbix*

Thank you very much for your invaluable help!

Upvotes: 0

Views: 42

Answers (0)

Related Questions