func uploadBits(server, appGuid string, zipFile *os.File, bloblets []bloblet.Bloblet, presentFiles []resources.AppFileResource) { apiUrl := fmt.Sprintf("http://%s/v2/apps/%s/bits", server, appGuid) fileutils.TempFile("requests", func(requestFile *os.File, err error) { cliutil.Check(err) // json.Marshal represents a nil value as "null" instead of an empty slice "[]" if presentFiles == nil { presentFiles = []resources.AppFileResource{} } presentFilesJSON, err := json.Marshal(presentFiles) cliutil.Check(err) boundary, err := writeUploadBody(zipFile, requestFile, bloblets, presentFilesJSON) cliutil.Check(err) var request *Request request, err = newRequestForFile("PUT", apiUrl, requestFile) cliutil.Check(err) contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary) request.HttpReq.Header.Set("Content-Type", contentType) response := &resources.Resource{} _, err = performPollingRequestForJSONResponse(server, request, response, DefaultAppUploadBitsTimeout) cliutil.Check(err) }) }
func (b *bloblet) Write(w io.Writer) { fileutils.TempDir("bloblet-zipdir", func(zipDir string, err error) { cliutil.Check(err) zipPath := filepath.Join(zipDir, BlobletFileName) cliutil.Check(b.Compress(zipPath)) cliutil.Check(fileutils.CopyPathToWriter(zipPath, w)) }) }
func gwDoRequestAndHandlerError(request *Request) (rawResponse *http.Response, err error) { rawResponse, err = gwDoRequest(request.HttpReq) cliutil.Check(err) if rawResponse.StatusCode > 299 { jsonBytes, _ := ioutil.ReadAll(rawResponse.Body) rawResponse.Body.Close() rawResponse.Body = ioutil.NopCloser(bytes.NewBuffer(jsonBytes)) cliutil.Check(fmt.Errorf("gwDoRequestAndHandlerError: %d %s", rawResponse.StatusCode, string(jsonBytes))) } return }
func ProcessResponse(response []byte) []IntegrityFields { responseFieldsColl := []IntegrityFields{} err := json.Unmarshal(response, &responseFieldsColl) cliutil.Check(err) // fmt.Printf("Returned SHA1s/Sizes: %#v\n", responseFieldsColl) return responseFieldsColl }
func doScan(appDir string, processAppFiles func(appDir string, condensate bloblet.Condensate, affs []models.AppFileFields)) { log.Println("Scanning application files and computing SHA1s") affs, condensate, err := AppFilesInDir(appDir) cliutil.Check(err) log.Println("Scanned application files and computed SHA1s") processAppFiles(appDir, condensate, affs) }
func Scan(path string, processAppFiles func(appDir string, condensate bloblet.Condensate, affs []models.AppFileFields)) { zipper := app_files.ApplicationZipper{} if zipper.IsZipFile(path) { fileutils.TempDir("unzipped-app", func(tmpDir string, err error) { cliutil.Check(err) log.Println("Unzipping application") cliutil.Check(zipper.Unzip(path, tmpDir)) log.Println("Unzipped application") doScan(tmpDir, processAppFiles) log.Println("Deleting unzipped application") }) } else { doScan(path, processAppFiles) } }
func ResMatchRequest(server string, ifs []IntegrityFields) *http.Request { url := fmt.Sprintf("http://%s/v2/resource_match", server) body := integrityFieldsJSONReader(ifs) request, err := http.NewRequest("PUT", url, body) cliutil.Check(err) request.Header.Set("accept", "application/json") request.Header.Set("content-type", "application/json") request.Header.Set("User-Agent", "bloblets client v0.1 / "+runtime.GOOS) return request }
func main() { if len(os.Args) != 3 || len(os.Args) > 1 && os.Args[1] == "help" { fmt.Println("Usage: client host:port application-path") os.Exit(1) } log.Println("Push started") client := &http.Client{} server := os.Args[1] scanner.Scan(os.Args[2], func(appDir string, condensate bloblet.Condensate, appFiles []models.AppFileFields) { log.Println("Resource matching started") request := resmatch.ResMatchRequest(server, integrityFields(appFiles)) log.Println("Resource matching sending request") response := cliutil.Converse(client, request) log.Println("Resource matching received response") presentIffs := resmatch.ProcessResponse(response) log.Println("Resource matching response demarshalled") presentFiles := intersectAppFilesIntegrityFields(appFiles, presentIffs) log.Println("Resource matching complete") appFilesToUpload := make([]models.AppFileFields, len(appFiles)) copy(appFilesToUpload, appFiles) for _, file := range presentFiles { appFilesToUpload = deleteAppFile(appFilesToUpload, file.Path) } // Populate file modes var err error presentFiles, err = PopulateFileMode(appDir, presentFiles) cliutil.Check(err) hasFileToUpload := len(appFilesToUpload) > 0 fileutils.TempDir("upload-dir", func(uploadDir string, err error) { cliutil.Check(err) var blobletsToUpload []bloblet.Bloblet blobletsToUpload, appFilesToUpload = condensate.Bloblets(appFilesToUpload) cliutil.Check(app_files.ApplicationFiles{}.CopyFiles(appFilesToUpload, appDir, uploadDir)) fileutils.TempFile("uploads", func(zipFile *os.File, err error) { if hasFileToUpload { log.Println("Zipping application files") cliutil.Check(zipWithBetterErrors(uploadDir, zipFile)) log.Println("Zipped application files") } log.Println("Uploading application") appbits.UploadApp(server, "test-guid", zipFile, blobletsToUpload, presentFiles) log.Println("Uploaded application") log.Println("Deleting uploads file") }) log.Println("Deleting upload directory") }) }) log.Println("Push completed") }
func integrityFieldsJSONReader(ifs []IntegrityFields) io.Reader { integrityFieldsJson, err := json.Marshal(ifs) cliutil.Check(err) return bytes.NewReader(integrityFieldsJson) }