diff --git a/apt/apt.go b/apt/apt.go index 1eeb7f9..1ac77b4 100644 --- a/apt/apt.go +++ b/apt/apt.go @@ -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,6 +148,7 @@ 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/") } @@ -141,10 +156,12 @@ func Download(w http.ResponseWriter, r *http.Request) { 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) } } @@ -152,6 +169,7 @@ func Download(w http.ResponseWriter, r *http.Request) { 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 subutai-release@subutai.io -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") } diff --git a/db/db.go b/db/db.go index a0020d6..9640e21 100644 --- a/db/db.go +++ b/db/db.go @@ -3,6 +3,7 @@ package db import ( "crypto/sha256" "fmt" + "io/ioutil" "os" "path/filepath" "strconv" @@ -663,6 +664,18 @@ func OwnerFilesByRepo(owner string, repo string) (list []string) { return } +func IsFileExists(filename string) bool { + exist := false + files, _ := ioutil.ReadDir(config.Storage.Path) + for _, file := range files { + if file.Name() == filename { + exist = true + return exist + } + } + return exist +} + func PrintBucketName(buckets []string) (path string) { path = "tx" for i := range buckets { diff --git a/upload/upload.go b/upload/upload.go index 22e1bee..67afa85 100644 --- a/upload/upload.go +++ b/upload/upload.go @@ -95,8 +95,8 @@ func Handler(w http.ResponseWriter, r *http.Request) (md5sum, sha256sum, owner s return } if repo[3] != "apt" { - log.Debug(fmt.Sprintf("repo[3] is not apt. Renaming %+v to %+v", config.Storage.Path + header.Filename, config.Storage.Path + md5sum)) - os.Rename(config.Storage.Path + header.Filename, config.Storage.Path + md5sum) + log.Debug(fmt.Sprintf("repo[3] is not apt. Renaming %+v to %+v", config.Storage.Path+header.Filename, config.Storage.Path+md5sum)) + os.Rename(config.Storage.Path+header.Filename, config.Storage.Path+md5sum) } log.Info("File received: " + header.Filename + "(" + md5sum + ")") return md5sum, sha256sum, owner