Exemplo n.º 1
0
Arquivo: lib.go Projeto: appc/acpush
// Upload performs the upload of the ACI and signature specified in the
// Uploader struct.
func (u Uploader) Upload() error {
	acifile, err := os.Open(u.Acipath)
	if err != nil {
		return err
	}
	defer acifile.Close()

	ascfile, err := os.Open(u.Ascpath)
	if err != nil {
		return err
	}
	defer ascfile.Close()

	manifest, err := aci.ManifestFromImage(acifile)
	if err != nil {
		return err
	}
	app, err := discovery.NewAppFromString(u.Uri)
	if err != nil {
		return err
	}

	if _, ok := app.Labels[archLabelName]; !ok {
		arch, ok := manifest.Labels.Get(archLabelName)
		if !ok {
			return fmt.Errorf("manifest is missing label: %q", archLabelName)
		}
		app.Labels[archLabelName] = arch
	}

	if _, ok := app.Labels[osLabelName]; !ok {
		os, ok := manifest.Labels.Get(osLabelName)
		if !ok {
			return fmt.Errorf("manifest is missing label: %q", osLabelName)
		}
		app.Labels[osLabelName] = os
	}

	if _, ok := app.Labels[extLabelName]; !ok {
		app.Labels[extLabelName] = strings.Trim(schema.ACIExtension, ".")
	}

	// Just to make sure that we start reading from the front of the file in
	// case aci.ManifestFromImage changed the cursor into the file.
	_, err = acifile.Seek(0, 0)
	if err != nil {
		return err
	}

	manblob, err := manifest.MarshalJSON()
	if err != nil {
		return err
	}

	initurl, err := u.getInitiationURL(app)
	if err != nil {
		return err
	}

	initDeets, err := u.initiateUpload(initurl)
	if err != nil {
		return err
	}

	type partToUpload struct {
		label string
		url   string
		r     io.Reader
		draw  bool
	}

	for _, part := range []partToUpload{
		partToUpload{"manifest", initDeets.ManifestURL, bytes.NewReader(manblob), false},
		partToUpload{"signature", initDeets.SignatureURL, ascfile, true},
		partToUpload{"ACI", initDeets.ACIURL, acifile, true},
	} {
		err = u.uploadPart(part.url, part.r, part.draw, part.label)
		if err != nil {
			reason := fmt.Errorf("error uploading %s: %v", part.label, err)
			reportErr := u.reportFailure(initDeets.CompletedURL, reason.Error())
			if reportErr != nil {
				return fmt.Errorf("error uploading %s and error reporting failure: %v, %v", part.label, err, reportErr)
			}
			return reason
		}
	}

	err = u.reportSuccess(initDeets.CompletedURL)
	if err != nil {
		return err
	}

	return nil
}
Exemplo n.º 2
0
// Perform the upload of the ACI and signature specified in the Uploader struct.
func (u Uploader) Upload() error {
	app, err := discovery.NewAppFromString(u.Uri)
	if err != nil {
		return err
	}

	if _, ok := app.Labels["arch"]; !ok {
		app.Labels["arch"] = "amd64"
	}

	if _, ok := app.Labels["os"]; !ok {
		app.Labels["os"] = "linux"
	}

	if _, ok := app.Labels["ext"]; !ok {
		app.Labels["ext"] = "aci"
	}

	acifile, err := os.Open(u.Acipath)
	if err != nil {
		return err
	}
	defer acifile.Close()

	ascfile, err := os.Open(u.Ascpath)
	if err != nil {
		return err
	}
	defer ascfile.Close()

	manifest, err := aci.ManifestFromImage(acifile)
	if err != nil {
		return err
	}

	// Just to make sure that we start reading from the front of the file in
	// case aci.ManifestFromImage changed the cursor into the file.
	_, err = acifile.Seek(0, 0)
	if err != nil {
		return err
	}

	manblob, err := manifest.MarshalJSON()
	if err != nil {
		return err
	}

	initurl, err := u.getInitiationURL(app)
	if err != nil {
		return err
	}

	initDeets, err := u.initiateUpload(initurl)
	if err != nil {
		return err
	}

	err = u.uploadPart(initDeets.ManifestURL, bytes.NewReader(manblob), false, nil)
	if err != nil {
		reason := fmt.Sprintf("error uploading manifest: %v", err)
		reportErr := u.reportFailure(initDeets.CompletedURL, reason)
		if reportErr != nil {
			return fmt.Errorf("error uploading manifest and error reporting failure: %v, %v", err, reportErr)
		}
		return err
	}

	label := "signature"
	err = u.uploadPart(initDeets.SignatureURL, ascfile, true, &label)
	if err != nil {
		reason := fmt.Sprintf("error uploading signature: %v", err)
		reportErr := u.reportFailure(initDeets.CompletedURL, reason)
		if reportErr != nil {
			return fmt.Errorf("error uploading manifest and error reporting failure: %v, %v", err, reportErr)
		}
		return err
	}

	label = "ACI"
	err = u.uploadPart(initDeets.ACIURL, acifile, true, &label)
	if err != nil {
		reason := fmt.Sprintf("error uploading aci: %v", err)
		reportErr := u.reportFailure(initDeets.CompletedURL, reason)
		if reportErr != nil {
			return fmt.Errorf("error uploading manifest and error reporting failure: %v, %v", err, reportErr)
		}
		return err
	}

	err = u.reportSuccess(initDeets.CompletedURL)
	if err != nil {
		return err
	}

	return nil
}