Skip to content

Commit c4b1b80

Browse files
author
Massimiliano Giovagnoli
committed
feat: add support for amazon linux 2023
Signed-off-by: Massimiliano Giovagnoli <me@maxgio.it>
1 parent c128768 commit c4b1b80

File tree

5 files changed

+221
-0
lines changed

5 files changed

+221
-0
lines changed

.github/workflows/publish.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ jobs:
7070
version: "2"
7171
- name: Publish results to S3
7272
run: make publish/amazonlinux2022
73+
amazonlinux-2023:
74+
runs-on: ubuntu-latest
75+
steps:
76+
- uses: actions/checkout@v2
77+
with:
78+
fetch-depth: '0'
79+
- run: echo "STABLE=`echo $(git describe --tags --abbrev=0)`" >> $GITHUB_ENV
80+
- uses: actions/checkout@v2
81+
with:
82+
ref: ${{ env.STABLE }}
83+
- uses: actions/setup-go@v3
84+
with:
85+
go-version-file: 'go.mod'
86+
- name: Install AWS CLI
87+
id: install-aws-cli
88+
uses: unfor19/install-aws-cli-action@master
89+
with:
90+
version: "2"
91+
- name: Publish results to S3
92+
run: make publish/amazonlinux2023
7393
centos:
7494
runs-on: ubuntu-latest
7595
steps:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Available distributions:
4242
- *amazonlinux*
4343
- *amazonlinux2*
4444
- *amazonlinux2022*
45+
- *amazonlinux2023*
4546
- *centos*
4647
- *debian*
4748
- *ubuntu*

cmd/list_amazonlinux2023.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright © 2022 maxgio92 <me@maxgio.it>
3+
4+
Licensed under the Apache License, Version v2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"github.com/spf13/cobra"
20+
21+
"github.com/maxgio92/krawler/internal/format"
22+
v2023 "github.com/maxgio92/krawler/pkg/distro/amazonlinux/v2023"
23+
)
24+
25+
// amazonLinux2Cmd represents the centos command.
26+
var amazonLinux2023Cmd = &cobra.Command{
27+
Use: "amazonlinux2023",
28+
Short: "List Amazon Linux 2023 kernel releases",
29+
RunE: func(cmd *cobra.Command, args []string) error {
30+
kernelReleases, err := getKernelReleases(&v2023.AmazonLinux{}, RPMKernelHeadersPackageName)
31+
cobra.CheckErr(err)
32+
33+
if len(kernelReleases) > 0 {
34+
Output, err = format.Encode(Output, kernelReleases, format.Type(outputFormat))
35+
cobra.CheckErr(err)
36+
} else {
37+
//nolint:errcheck
38+
Output.WriteString("No releases found.\n")
39+
}
40+
41+
return nil
42+
},
43+
}
44+
45+
func init() {
46+
listCmd.AddCommand(amazonLinux2023Cmd)
47+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package v2023
2+
3+
import (
4+
"context"
5+
"io"
6+
"net/http"
7+
"net/url"
8+
"strings"
9+
10+
"github.com/maxgio92/krawler/pkg/distro"
11+
common "github.com/maxgio92/krawler/pkg/distro/amazonlinux"
12+
packages "github.com/maxgio92/krawler/pkg/packages"
13+
"github.com/maxgio92/krawler/pkg/packages/rpm"
14+
)
15+
16+
type AmazonLinux struct {
17+
common.AmazonLinux
18+
}
19+
20+
func (a *AmazonLinux) Configure(config distro.Config) error {
21+
return a.ConfigureCommon(DefaultConfig, config)
22+
}
23+
24+
// SearchPackages scrapes each mirror, for each distro version, for each repository,
25+
// for each architecture, and returns slice of Package and optionally an error.
26+
func (a *AmazonLinux) SearchPackages(options packages.SearchOptions) ([]packages.Package, error) {
27+
a.Config.Output.Logger = options.Log()
28+
29+
// Build distribution version-specific mirror root URLs.
30+
perVersionMirrorURLs, err := a.BuildMirrorURLs(a.Config.Mirrors, a.Config.Versions)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
// Build available repository URLs based on provided configuration,
36+
// for each distribution version.
37+
repositoriesURLrefs, err := common.BuildRepositoriesURLs(perVersionMirrorURLs, a.Config.Repositories)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
// Dereference repository URLs.
43+
repositoryURLs, err := a.dereferenceRepositoryURLs(repositoriesURLrefs, a.Config.Archs)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
// Get RPM packages from each repository.
49+
rss := []string{}
50+
for _, ru := range repositoryURLs {
51+
rss = append(rss, ru.String())
52+
}
53+
54+
searchOptions := rpm.NewSearchOptions(&options, a.Config.Archs, rss)
55+
rpmPackages, err := rpm.SearchPackages(searchOptions)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
return rpmPackages, nil
61+
}
62+
63+
func (a *AmazonLinux) dereferenceRepositoryURLs(repoURLs []*url.URL, archs []packages.Architecture) ([]*url.URL, error) {
64+
var urls []*url.URL
65+
66+
for _, ar := range archs {
67+
for _, v := range repoURLs {
68+
r, err := a.dereferenceRepositoryURL(v, ar)
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
if r != nil {
74+
urls = append(urls, r)
75+
}
76+
}
77+
}
78+
79+
return urls, nil
80+
}
81+
82+
func (a *AmazonLinux) dereferenceRepositoryURL(src *url.URL, arch packages.Architecture) (*url.URL, error) {
83+
var dest *url.URL
84+
85+
mirrorListURL, err := url.JoinPath(src.String(), string(arch), "mirror.list")
86+
if err != nil {
87+
return nil, err
88+
}
89+
90+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, mirrorListURL, nil)
91+
if err != nil {
92+
return nil, err
93+
}
94+
95+
resp, err := http.DefaultClient.Do(req)
96+
if err != nil {
97+
return nil, err
98+
}
99+
defer resp.Body.Close()
100+
101+
if resp.StatusCode != http.StatusOK {
102+
a.Config.Output.Logger.Error("Amazon Linux v2023 repository URL not valid to be dereferenced")
103+
//nolint:nilnil
104+
return nil, nil
105+
}
106+
107+
if resp.Body == nil {
108+
a.Config.Output.Logger.Error("empty response from Amazon Linux v2023 repository reference URL")
109+
//nolint:nilnil
110+
return nil, nil
111+
}
112+
113+
b, err := io.ReadAll(resp.Body)
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
// Get first repository URL available, no matter what the geolocation.
119+
s := strings.Split(string(b), "\n")[0]
120+
121+
dest, err = url.Parse(s)
122+
if err != nil {
123+
return nil, err
124+
}
125+
126+
return dest, nil
127+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package v2023
2+
3+
import (
4+
"github.com/maxgio92/krawler/pkg/distro"
5+
"github.com/maxgio92/krawler/pkg/packages"
6+
)
7+
8+
// DefaultConfig is the default configuration for scrape Amazon Linux (RPM) packages.
9+
// As of now URI templating depends on distro's viper.AllSettings() data.
10+
var DefaultConfig = distro.Config{
11+
Mirrors: []packages.Mirror{
12+
{
13+
Name: "AL2023",
14+
URL: "https://cdn.amazonlinux.com/al2023/core/mirrors/",
15+
},
16+
},
17+
Repositories: []packages.Repository{
18+
{Name: "", URI: "latest"},
19+
},
20+
Archs: []packages.Architecture{
21+
"aarch64",
22+
"x86_64",
23+
"ppc64le",
24+
},
25+
Versions: []distro.Version{""},
26+
}

0 commit comments

Comments
 (0)