-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #473 from baytulakova/403
- Loading branch information
Showing
3 changed files
with
56 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"archive/tar" | ||
"bytes" | ||
"compress/gzip" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
|
@@ -80,22 +81,32 @@ func getSize(file string) (size int) { | |
} | ||
|
||
func Upload(w http.ResponseWriter, r *http.Request) { | ||
log.Info("Start uploading the file") | ||
if r.Method == "POST" { | ||
_, header, _ := r.FormFile("file") | ||
if e := db.IsFileExists(header.Filename); e == true { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte("Failed to upload apt with the same name")) | ||
return | ||
} | ||
md5, sha256, owner := upload.Handler(w, r) | ||
if len(md5) == 0 || len(sha256) == 0 { | ||
log.Info("Md5 or sha256 is empty. Failed to calculate the hash") | ||
return | ||
} | ||
log.Info(fmt.Sprintf("Starting to read deb package %v", header.Filename)) | ||
control, err := readDeb(header.Filename) | ||
if err != nil { | ||
log.Warn(err.Error()) | ||
log.Warn("Reading deb package finished with error: ", err.Error()) | ||
w.WriteHeader(http.StatusUnsupportedMediaType) | ||
w.Write([]byte(err.Error())) | ||
if db.Delete(owner, "apt", md5) == 0 { | ||
log.Info(fmt.Sprintf("Removed file %v", config.Storage.Path+md5)) | ||
os.Remove(config.Storage.Path + md5) | ||
} | ||
return | ||
} | ||
log.Info("Starting to read control file of deb package") | ||
meta := getControl(control) | ||
meta["Filename"] = header.Filename | ||
meta["Size"] = strconv.Itoa(getSize(config.Storage.Path + header.Filename)) | ||
|
@@ -108,14 +119,17 @@ func Upload(w http.ResponseWriter, r *http.Request) { | |
meta["tag"] = tags | ||
my_uuid, err := uuid.NewV4() | ||
if err != nil { | ||
log.Warn("Failed to generate UUID") | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
ID := my_uuid.String() | ||
db.AddTag(strings.Split(tags, ","), ID, "apt") | ||
log.Info(fmt.Sprintf("Writing deb package %v into database", header.Filename)) | ||
err = db.Write(owner, ID, header.Filename, meta) | ||
if err != nil { | ||
log.Warn("Failed writing record into database") | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
|
@@ -134,24 +148,28 @@ func Upload(w http.ResponseWriter, r *http.Request) { | |
|
||
func Download(w http.ResponseWriter, r *http.Request) { | ||
file := r.URL.Query().Get("hash") | ||
log.Info(fmt.Sprintf("Starting download the deb package %v", file)) | ||
if len(file) == 0 { | ||
file = strings.TrimPrefix(r.RequestURI, "/kurjun/rest/apt/") | ||
} | ||
size := getSize(config.Storage.Path + "Packages") | ||
if file == "Packages" && size == 0 { | ||
GenerateReleaseFile() | ||
} | ||
log.Info(fmt.Sprintf("Opening file %v", config.Storage.Path+file)) | ||
if f, err := os.Open(config.Storage.Path + file); err == nil && file != "" { | ||
defer f.Close() | ||
io.Copy(w, f) | ||
} else { | ||
log.Info(fmt.Sprintf("File %v not found", config.Storage.Path+file)) | ||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
} | ||
|
||
func Delete(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == "DELETE" { | ||
if hash := upload.Delete(w, r); len(hash) != 0 { | ||
log.Info(fmt.Sprintf("Removed file with hash %v", hash)) | ||
w.Write([]byte("Removed")) | ||
return | ||
} | ||
|
@@ -184,6 +202,7 @@ func List(w http.ResponseWriter, r *http.Request) { | |
} | ||
|
||
func renameOldDebFiles() { | ||
log.Info("Starting rename old deb files") | ||
list := db.SearchName("") | ||
for _, k := range list { | ||
if db.CheckRepo("", []string{"apt"}, k) == 0 { | ||
|
@@ -192,48 +211,69 @@ func renameOldDebFiles() { | |
item := download.FormatItem(db.Info(k), "apt") | ||
os.Rename(config.Storage.Path+item.Hash.Md5, config.Storage.Path+item.Name) | ||
} | ||
log.Info("Finished rename old deb packages") | ||
} | ||
|
||
func GenerateReleaseFile() { | ||
log.Warn("Starting GenerateReleaseFiles") | ||
cmd := exec.Command("bash", "-c", "dpkg-scanpackages . /dev/null | tee Packages | gzip > Packages.gz") | ||
cmd.Dir = config.Storage.Path | ||
log.Warn("config.Storage.Path: ", config.Storage.Path) | ||
log.Warn("Cmd.Dir: ", cmd.Dir) | ||
log.Warn("Running command to run dpkg-scanpackages and waiting for it to finish") | ||
err := cmd.Run() | ||
if err != nil { | ||
log.Fatal(err) | ||
log.Info("Can't run dpkg-scanpackages") | ||
} | ||
log.Warn(fmt.Sprintf("Command to run dpkg-scanpackages finished with error %v", err)) | ||
|
||
cmd = exec.Command("bash", "-c", "apt-ftparchive release . > Release") | ||
cmd.Dir = config.Storage.Path | ||
log.Warn("config.Storage.Path: ", config.Storage.Path) | ||
log.Warn("Cmd.Dir: ", cmd.Dir) | ||
log.Warn("Running command to run apt-ftparchive and waiting for it to finish") | ||
err = cmd.Run() | ||
if err != nil { | ||
log.Fatal(err) | ||
log.Info("Can't run apt-ftparchive") | ||
} | ||
log.Warn(fmt.Sprintf("Command to run apt-ftparchive finished with error %v", err)) | ||
|
||
cmd = exec.Command("bash", "-c", "gpg --batch --yes --armor -u [email protected] -abs -o Release.gpg Release") | ||
cmd.Dir = config.Storage.Path | ||
log.Warn("config.Storage.Path: ", config.Storage.Path) | ||
log.Warn("Cmd.Dir: ", cmd.Dir) | ||
log.Warn("Running command to sign Realease file and waiting for it to finish") | ||
err = cmd.Run() | ||
if err != nil { | ||
log.Fatal(err) | ||
log.Info("Can't sign Realease file") | ||
} | ||
log.Warn(fmt.Sprintf("Command to sign Realease file finished with error %v", err)) | ||
log.Warn("Finished GenerateReleaseFiles") | ||
} | ||
|
||
func Generate(w http.ResponseWriter, r *http.Request) { | ||
log.Info("Starting Generate") | ||
token := strings.ToLower(r.Header.Get("token")) | ||
owner := strings.ToLower(db.TokenOwner(token)) | ||
if len(token) == 0 || len(owner) == 0 { | ||
log.Warn("Not authorized user wanted to generate release file") | ||
w.WriteHeader(http.StatusUnauthorized) | ||
w.Write([]byte("Not authorized")) | ||
log.Warn(r.RemoteAddr + " - rejecting generate request") | ||
return | ||
} | ||
if owner != "subutai" && owner != "jenkins" { | ||
log.Warn("Not allowed user wanted to generate release file") | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte("Only allowed users can generate release file")) | ||
log.Warn(r.RemoteAddr + " - rejecting generate request") | ||
return | ||
} | ||
log.Info("Generating release file") | ||
GenerateReleaseFile() | ||
w.Write([]byte("New Packages file generated and Release file signed")) | ||
log.Info("New Packages file generated and Release file signed") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters