-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages.go
210 lines (174 loc) · 6.79 KB
/
packages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// package api why do I have to put this in every file and then keep them all up to date, etc
package api
import (
"errors"
"net/http"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"gitlab.alpinelinux.org/alpine/go/repository"
)
// TODO
// * Check if org/arch/repo/etc are in known
// * Beef up path traversal protection
// TODO this is a transitional var, need to move everything to use pkgrepoapi.packagebasedir
var PackageBaseDirectory string
// PkgRepoAPI - a collection packages, repos, versions, etc
type PkgRepoAPI struct {
Repos map[string]Repo
// PackageBaseDirectory - the base directory where packages are organized/stored
PackageBaseDirectory string
}
// NewPkgRepo - called by main function to
func NewPkgRepo(dir string) *PkgRepoAPI {
p := &PkgRepoAPI{
Repos: make(map[string]Repo),
}
// PackageBaseDirectory - the base directory where packages are organized/stored
p.PackageBaseDirectory = "file://" + dir
PackageBaseDirectory = "file://" + dir
return p
}
// This function wraps sending of an error in the Error format, and
// handling the failure to marshal that.
func sendRepoError(ctx echo.Context, code int, message string) {
repoErr := Error{
Code: int32(code),
Message: message,
}
err := ctx.JSON(code, repoErr)
// return err
if err != nil {
log.Warn().Err(err).Msg("failed to return json error")
}
}
// ListRepos - list repos in an org
func (p *PkgRepoAPI) ListRepos(ctx echo.Context, org, distro, version string) error {
// log.Debug().Str("org", org).Msg("ListRepos request")
result, err := listRepos(org, distro, version)
if err != nil {
log.Error().Err(err).Msg("failed to listRepos")
}
if len(result) == 0 {
return ctx.JSON(http.StatusInternalServerError, Error{Code: http.StatusInternalServerError, Message: "org does not have any repos"})
}
return ctx.JSON(http.StatusOK, result)
}
// CreateRepo - Add a new package repo
func (p *PkgRepoAPI) CreateRepo(ctx echo.Context, org string) error {
// We expect a NewRepo object in the request body.
var newRepo NewRepo
err := ctx.Bind(&newRepo)
if err != nil {
log.Warn().Err(err).Str("org", org).Msg("failed to create repo")
sendRepoError(ctx, http.StatusBadRequest, "Invalid format for NewRepo")
}
// We now have a repo, let's add it to our "database".
// TODO
return ctx.JSON(http.StatusCreated, Repo{Name: newRepo.Name})
}
// FindRepoByName - return a repo info from org, distro, version, repo name
func (p *PkgRepoAPI) FindRepoByName(ctx echo.Context, org, distro, version, repo string) error {
repoInfo := getRepoInfo(org, distro, version, repo)
return ctx.JSON(http.StatusOK, []Repo{repoInfo})
}
// DeleteRepo - delete a repo TODO
// func (p *PkgRepoAPI) DeleteRepo(ctx echo.Context, org, distro, version, repo string) error {
// return ctx.NoContent(http.StatusNoContent)
// }
// GetHealthPing - return health status (for k8s)
func (p *PkgRepoAPI) GetHealthPing(ctx echo.Context) error {
return ctx.String(http.StatusOK, "pong")
}
// GetHealtReady - return ready status (for k8s)
func (p *PkgRepoAPI) GetHealthReady(ctx echo.Context) error {
return ctx.String(http.StatusOK, "ready")
}
// HeadHealthReady - return ready status (for k8s)
func (p *PkgRepoAPI) HeadHealthReady(ctx echo.Context) error {
return ctx.String(http.StatusOK, "ready")
}
// ListPackagesByRepo - list packages in an org's repo
func (p *PkgRepoAPI) ListPackagesByRepo(ctx echo.Context, org, distro, version, repo, arch string) error {
pkgs, err := listPackages(org, distro, version, repo, arch)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, Error{Message: "failed to get package list"})
}
return ctx.JSON(http.StatusOK, pkgs)
}
// CreatePackage - Create a package in a repo and regenerate the index
func (p *PkgRepoAPI) CreatePackage(ctx echo.Context, org, distro, ver, repo, arch string) error {
file, err := ctx.FormFile("package")
if err != nil {
log.Warn().Err(err).Msg("failed to get file from submitted data")
}
src, err := file.Open()
if err != nil {
log.Warn().Err(err).Msg("failed to open src file")
}
defer src.Close()
pkg, err := repository.ParsePackage(src)
if err != nil {
log.Error().Err(err).Msg("failed to parse package from uploaded file")
return ctx.JSON(http.StatusInternalServerError, errors.New("failed to parse upload"))
}
src.Close()
writeUploadedPkg(file, org, distro, ver, repo, arch)
// go generateAPKIndex(org, distro, ver, repo, arch)
return ctx.JSON(http.StatusOK, &Package{Name: pkg.Name, Version: &pkg.Version}) // TODO do we really pkgrel?
}
// CreatePackageIndex - regenerate the index for a repo
func (p *PkgRepoAPI) CreatePackageIndex(ctx echo.Context, org, distro, ver, repo, arch string) error {
// generateAPKIndex is meant to be run in a goroutine, so we don't actually get any return from the function
// for now, just blindly return true, but we should tidy this up later
generateAPKIndex(org, distro, ver, repo, arch)
status := &GenerateIndex{Status: true}
return ctx.JSON(http.StatusOK, status)
}
// ListDistros - return a list of the supported distros
func (p *PkgRepoAPI) ListDistros(ctx echo.Context, org string) error {
distros, err := listDistros(org)
if err != nil {
log.Error().Err(err).Msg("failed to listDistros")
return ctx.JSON(http.StatusInternalServerError, Error{Message: "failed to get a list of distros"})
}
return ctx.JSON(http.StatusOK, distros)
}
// ListOrganizations - return a list of the organizations
// TODO this should probably only be available to the server tokens
func (p *PkgRepoAPI) ListOrganizations(ctx echo.Context) error {
orgs := listOrgs()
return ctx.JSON(http.StatusOK, orgs)
}
// GetOrganization - get an org
func (p *PkgRepoAPI) GetOrganization(ctx echo.Context, org string) error {
ret := &Organization{}
if orgExists(org) {
// distros, err := listRepos(org, distro)
ret = &Organization{
Name: &org,
Distributions: nil,
}
}
return ctx.JSON(http.StatusOK, ret)
}
// ListVersions - list of versions in an org's repo
func (p *PkgRepoAPI) ListVersions(ctx echo.Context, org, distro string) error {
dvs, err := listVersions(org, distro)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, Error{Message: "failed to list distro versions"})
}
return ctx.JSON(http.StatusOK, dvs)
}
// GetOrgDistro - Return info about a distribution for an org
func (p *PkgRepoAPI) GetOrgDistro(ctx echo.Context, org, distro string) error {
log.Debug().Str("org", org).Str("distro", distro).Msg("GetOrgDistro")
return ctx.JSON(http.StatusOK, []Distribution{})
}
// ListArches - list architectures for org/distro/version/repo
func (p *PkgRepoAPI) ListArches(ctx echo.Context, org, distro, version, repo string) error {
arches, err := listArches(org, distro, version, repo)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, Error{Message: "failed to get arch list"})
}
return ctx.JSON(http.StatusOK, arches)
}