コード例 #1
0
ファイル: file.go プロジェクト: camarox53/coreos-baremetal
func fetchFile(l *log.Logger, f types.File) ([]byte, error) {
	switch f.Contents.Source.Scheme {
	case "http":
		client := util.NewHttpClient(l)
		data, status, err := client.Get(f.Contents.Source.String())
		if err != nil {
			return nil, err
		}

		l.Debug("GET result: %s", http.StatusText(status))
		if status != http.StatusOK {
			return nil, ErrStatusBad
		}

		return data, nil
	case "data":
		url, err := dataurl.DecodeString(f.Contents.Source.String())
		if err != nil {
			return nil, err
		}

		return url.Data, nil
	default:
		return nil, ErrSchemeUnsupported
	}
}
コード例 #2
0
// FetchResource fetches a resource given a URL. The supported schemes are http, data, and oem.
func FetchResource(l *log.Logger, u url.URL) ([]byte, error) {
	switch u.Scheme {
	case "http", "https":
		client := NewHttpClient(l)
		data, status, err := client.Get(u.String())
		if err != nil {
			return nil, err
		}

		l.Debug("GET result: %s", http.StatusText(status))
		switch status {
		case http.StatusOK, http.StatusNoContent:
			return data, nil
		case http.StatusNotFound:
			return nil, ErrNotFound
		default:
			return nil, ErrFailed
		}

	case "data":
		url, err := dataurl.DecodeString(u.String())
		if err != nil {
			return nil, err
		}

		return url.Data, nil

	case "oem":
		path := filepath.Clean(u.Path)
		if !filepath.IsAbs(path) {
			l.Err("oem path is not absolute: %q", u.Path)
			return nil, ErrPathNotAbsolute
		}

		// check if present under oemDirPath, if so use it.
		absPath := filepath.Join(oemDirPath, path)
		data, err := ioutil.ReadFile(absPath)
		if os.IsNotExist(err) {
			l.Info("oem config not found in %q, trying %q",
				oemDirPath, oemMountPath)

			// try oemMountPath, requires mounting it.
			err := mountOEM(l)
			if err != nil {
				l.Err("failed to mount oem partition: %v", err)
				return nil, ErrFailed
			}

			absPath := filepath.Join(oemMountPath, path)
			data, err = ioutil.ReadFile(absPath)
			umountOEM(l)
		} else if err != nil {
			l.Err("failed to read oem config: %v", err)
			return nil, ErrFailed
		}

		return data, nil

	default:
		return nil, ErrSchemeUnsupported
	}
}