refractor(build): move os logic to its own module.

This commit is contained in:
Alexandre Pujol 2024-03-21 18:58:32 +00:00
parent 662dd1c6dc
commit e1d1d0be3d
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
9 changed files with 158 additions and 86 deletions

View file

@ -11,11 +11,11 @@ import (
"github.com/arduino/go-paths-helper"
"github.com/roddhjav/apparmor.d/pkg/logging"
oss "github.com/roddhjav/apparmor.d/pkg/os"
)
var (
overwrite bool = false
Distribution string
DistDir *paths.Path
Root *paths.Path
RootApparmord *paths.Path
@ -27,10 +27,8 @@ func init() {
Root = paths.New(".build")
FlagDir = DistDir.Join("flags")
RootApparmord = Root.Join("apparmor.d")
Distribution = getSupportedDistribution()
if Distribution == "ubuntu" {
os := NewOSRelease()
if os["VERSION_CODENAME"] == "noble" {
if oss.Distribution == "ubuntu" {
if oss.Release["VERSION_CODENAME"] == "noble" {
Builds = append(Builds, BuildABI3)
overwrite = true
}

View file

@ -8,6 +8,8 @@ import (
"os"
"os/exec"
"testing"
oss "github.com/roddhjav/apparmor.d/pkg/os"
)
func chdirGitRoot() {
@ -74,7 +76,7 @@ func Test_PreBuild(t *testing.T) {
chdirGitRoot()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Distribution = tt.dist
oss.Distribution = tt.dist
if tt.full {
Prepares = append(Prepares, SetFullSystemPolicy)
Builds = append(Builds, BuildFullSystemPolicy)

View file

@ -12,6 +12,7 @@ import (
"github.com/arduino/go-paths-helper"
"github.com/roddhjav/apparmor.d/pkg/logging"
oss "github.com/roddhjav/apparmor.d/pkg/os"
"github.com/roddhjav/apparmor.d/pkg/util"
)
@ -59,7 +60,7 @@ func Synchronise() ([]string, error) {
// Ignore profiles and files as defined in dists/ignore/
func Ignore() ([]string, error) {
res := []string{}
for _, name := range []string{"main.ignore", Distribution + ".ignore"} {
for _, name := range []string{"main.ignore", oss.Distribution + ".ignore"} {
path := DistDir.Join("ignore", name)
if !path.Exist() {
continue
@ -130,7 +131,7 @@ func Merge() ([]string, error) {
// Set the distribution specificities
func Configure() ([]string, error) {
res := []string{}
switch Distribution {
switch oss.Distribution {
case "arch", "opensuse":
case "ubuntu":
@ -152,7 +153,7 @@ func Configure() ([]string, error) {
}
default:
return []string{}, fmt.Errorf("%s is not a supported distribution", Distribution)
return []string{}, fmt.Errorf("%s is not a supported distribution", oss.Distribution)
}
return res, nil
@ -161,7 +162,7 @@ func Configure() ([]string, error) {
// Set flags on some profiles according to manifest defined in `dists/flags/`
func SetFlags() ([]string, error) {
res := []string{}
for _, name := range []string{"main.flags", Distribution + ".flags"} {
for _, name := range []string{"main.flags", oss.Distribution + ".flags"} {
path := FlagDir.Join(name)
if !path.Exist() {
continue

View file

@ -5,71 +5,11 @@
package prebuild
import (
"os"
"strings"
"github.com/arduino/go-paths-helper"
"golang.org/x/exp/slices"
)
var (
osReleaseFile = "/etc/os-release"
supportedDists = map[string][]string{
"arch": {},
"debian": {},
"ubuntu": {},
"opensuse": {"suse", "opensuse-tumbleweed"},
"whonix": {},
}
)
func NewOSRelease() map[string]string {
var lines []string
var err error
for _, name := range []string{osReleaseFile, "/usr/lib/os-release"} {
path := paths.New(name)
if path.Exist() {
lines, err = path.ReadFileAsLines()
if err != nil {
panic(err)
}
break
}
}
os := map[string]string{}
for _, line := range lines {
item := strings.Split(line, "=")
if len(item) == 2 {
os[item[0]] = strings.Trim(item[1], "\"")
}
}
return os
}
func getSupportedDistribution() string {
dist, present := os.LookupEnv("DISTRIBUTION")
if present {
return dist
}
os := NewOSRelease()
id := os["ID"]
if id == "ubuntu" {
return id
}
id_like := os["ID_LIKE"]
for main, based := range supportedDists {
if main == id || main == id_like {
return main
} else if slices.Contains(based, id) {
return main
} else if slices.Contains(based, id_like) {
return main
}
}
return id
}
func copyTo(src *paths.Path, dst *paths.Path) error {
files, err := src.ReadDirRecursiveFiltered(nil, paths.FilterOutDirectories(), paths.FilterOutNames("README.md"))
if err != nil {

View file

@ -1,184 +0,0 @@
// apparmor.d - Full set of apparmor profiles
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
// SPDX-License-Identifier: GPL-2.0-only
package prebuild
import (
"reflect"
"testing"
"github.com/arduino/go-paths-helper"
)
const (
Archlinux = `NAME="Arch Linux"
PRETTY_NAME="Arch Linux"
ID=arch
BUILD_ID=rolling
ANSI_COLOR="38;2;23;147;209"
HOME_URL="https://archlinux.org/"
DOCUMENTATION_URL="https://wiki.archlinux.org/"
SUPPORT_URL="https://bbs.archlinux.org/"
BUG_REPORT_URL="https://bugs.archlinux.org/"
PRIVACY_POLICY_URL="https://terms.archlinux.org/docs/privacy-policy/"
LOGO=archlinux-logo`
Ubuntu = `PRETTY_NAME="Ubuntu 22.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.2 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy`
Debian = `PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"`
OpenSUSETumbleweed = `ID="opensuse-tumbleweed"
ID_LIKE="opensuse suse"
VERSION_ID="20230404"
PRETTY_NAME="openSUSE Tumbleweed"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:tumbleweed:20230404"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
DOCUMENTATION_URL="https://en.opensuse.org/Portal:Tumbleweed"
LOGO="distributor-logo-Tumbleweed"`
ArcoLinux = `NAME=ArcoLinux
ID=arcolinux
ID_LIKE=arch
BUILD_ID=rolling
ANSI_COLOR="0;36"
HOME_URL="https://arcolinux.info/"
SUPPORT_URL="https://arcolinuxforum.com/"
BUG_REPORT_URL="https://github.com/arcolinux"
LOGO=arcolinux-hello`
Fedora = `NAME="Fedora Linux"
VERSION="37 (Workstation Edition)"
ID=fedora
VERSION_ID=37
VERSION_CODENAME=""
PLATFORM_ID="platform:f37"
PRETTY_NAME="Fedora Linux 37 (Workstation Edition)"
ANSI_COLOR="0;38;2;60;110;180"
LOGO=fedora-logo-icon`
)
func TestNewOSRelease(t *testing.T) {
tests := []struct {
name string
osRelease string
want map[string]string
}{
{
name: "Archlinux",
osRelease: Archlinux,
want: map[string]string{
"NAME": "Arch Linux",
"PRETTY_NAME": "Arch Linux",
"ID": "arch",
"BUILD_ID": "rolling",
"ANSI_COLOR": "38;2;23;147;209",
"HOME_URL": "https://archlinux.org/",
"DOCUMENTATION_URL": "https://wiki.archlinux.org/",
"SUPPORT_URL": "https://bbs.archlinux.org/",
"BUG_REPORT_URL": "https://bugs.archlinux.org/",
"PRIVACY_POLICY_URL": "https://terms.archlinux.org/docs/privacy-policy/",
"LOGO": "archlinux-logo",
},
},
{
name: "Ubuntu",
osRelease: Ubuntu,
want: map[string]string{
"PRETTY_NAME": "Ubuntu 22.04.2 LTS",
"NAME": "Ubuntu",
"VERSION_ID": "22.04",
"VERSION": "22.04.2 LTS (Jammy Jellyfish)",
"VERSION_CODENAME": "jammy",
"ID": "ubuntu",
"ID_LIKE": "debian",
"HOME_URL": "https://www.ubuntu.com/",
"SUPPORT_URL": "https://help.ubuntu.com/",
"BUG_REPORT_URL": "https://bugs.launchpad.net/ubuntu/",
"PRIVACY_POLICY_URL": "https://www.ubuntu.com/legal/terms-and-policies/privacy-policy",
"UBUNTU_CODENAME": "jammy",
},
},
}
osReleaseFile = "/tmp/os-release"
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := paths.New(osReleaseFile).WriteFile([]byte(tt.osRelease))
if err != nil {
return
}
if got := NewOSRelease(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewOSRelease() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getSupportedDistribution(t *testing.T) {
tests := []struct {
name string
osRelease string
want string
}{
{
name: "Archlinux",
osRelease: Archlinux,
want: "arch",
},
{
name: "Ubuntu",
osRelease: Ubuntu,
want: "ubuntu",
},
{
name: "Debian",
osRelease: Debian,
want: "debian",
},
{
name: "OpenSUSE Tumbleweed",
osRelease: OpenSUSETumbleweed,
want: "opensuse",
},
// {
// name: "Fedora",
// osRelease: Fedora,
// want: "fedora",
// },
}
osReleaseFile = "/tmp/os-release"
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := paths.New(osReleaseFile).WriteFile([]byte(tt.osRelease))
if err != nil {
return
}
got := getSupportedDistribution()
if got != tt.want {
t.Errorf("getSupportedDistribution() = %v, want %v", got, tt.want)
}
})
}
}