コード例 #1
0
ファイル: buildpack_bits.go プロジェクト: fujitsu-cf/cli
func (repo CloudControllerBuildpackBitsRepository) performMultiPartUpload(url string, fieldName string, fileName string, body io.Reader) error {
	var capturedErr error

	fileutils.TempFile("requests", func(requestFile *os.File, err error) {
		if err != nil {
			capturedErr = err
			return
		}

		writer := multipart.NewWriter(requestFile)
		part, err := writer.CreateFormFile(fieldName, fileName)

		if err != nil {
			_ = writer.Close()
			capturedErr = err
			return
		}

		_, err = io.Copy(part, body)
		if err != nil {
			capturedErr = fmt.Errorf("%s: %s", T("Error creating upload"), err.Error())
			return
		}

		err = writer.Close()
		if err != nil {
			capturedErr = err
			return
		}

		var request *net.Request
		request, err = repo.gateway.NewRequestForFile("PUT", url, repo.config.AccessToken(), requestFile)
		if err != nil {
			capturedErr = err
			return
		}

		contentType := fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary())
		request.HTTPReq.Header.Set("Content-Type", contentType)

		_, err = repo.gateway.PerformRequest(request)
		if err != nil {
			capturedErr = err
		}
	})

	return capturedErr
}
コード例 #2
0
ファイル: buildpack_bits.go プロジェクト: fujitsu-cf/cli
func (repo CloudControllerBuildpackBitsRepository) downloadBuildpack(url string, cb func(*os.File, error)) {
	fileutils.TempFile("buildpack-download", func(tempfile *os.File, err error) {
		if err != nil {
			cb(nil, err)
			return
		}

		var certPool *x509.CertPool
		if len(repo.TrustedCerts) > 0 {
			certPool = x509.NewCertPool()
			for _, tlsCert := range repo.TrustedCerts {
				cert, _ := x509.ParseCertificate(tlsCert.Certificate[0])
				certPool.AddCert(cert)
			}
		}

		client := &http.Client{
			Transport: &http.Transport{
				Dial:            (&gonet.Dialer{Timeout: 5 * time.Second}).Dial,
				TLSClientConfig: &tls.Config{RootCAs: certPool},
				Proxy:           http.ProxyFromEnvironment,
			},
		}

		response, err := client.Get(url)
		if err != nil {
			cb(nil, err)
			return
		}
		defer response.Body.Close()

		_, err = io.Copy(tempfile, response.Body)
		if err != nil {
			cb(nil, err)
			return
		}

		_, err = tempfile.Seek(0, 0)
		if err != nil {
			cb(nil, err)
			return
		}

		cb(tempfile, nil)
	})
}
コード例 #3
0
ファイル: application_bits.go プロジェクト: fujitsu-cf/cli
func (repo CloudControllerApplicationBitsRepository) UploadBits(appGUID string, zipFile *os.File, presentFiles []resources.AppFileResource) (apiErr error) {
	apiURL := fmt.Sprintf("/v2/apps/%s/bits", appGUID)
	fileutils.TempFile("requests", func(requestFile *os.File, err error) {
		if err != nil {
			apiErr = fmt.Errorf("%s: %s", T("Error creating tmp file: {{.Err}}", map[string]interface{}{"Err": err}), err.Error())
			return
		}

		// json.Marshal represents a nil value as "null" instead of an empty slice "[]"
		if presentFiles == nil {
			presentFiles = []resources.AppFileResource{}
		}

		presentFilesJSON, err := json.Marshal(presentFiles)
		if err != nil {
			apiErr = fmt.Errorf("%s: %s", T("Error marshaling JSON"), err.Error())
			return
		}

		boundary, err := repo.writeUploadBody(zipFile, requestFile, presentFilesJSON)
		if err != nil {
			apiErr = fmt.Errorf("%s: %s", T("Error writing to tmp file: {{.Err}}", map[string]interface{}{"Err": err}), err.Error())
			return
		}

		var request *net.Request
		request, apiErr = repo.gateway.NewRequestForFile("PUT", repo.config.APIEndpoint()+apiURL, repo.config.AccessToken(), requestFile)
		if apiErr != nil {
			return
		}

		contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
		request.HTTPReq.Header.Set("Content-Type", contentType)

		response := &resources.Resource{}
		_, apiErr = repo.gateway.PerformPollingRequestForJSONResponse(repo.config.APIEndpoint(), request, response, DefaultAppUploadBitsTimeout)
		if apiErr != nil {
			return
		}
	})

	return
}
コード例 #4
0
ファイル: curl_test.go プロジェクト: fujitsu-cf/cli
		Expect(curlRepo.Method).To(Equal(""))
		Expect(curlRepo.Path).To(Equal("/foo"))
		Expect(ui.Outputs()).To(ContainSubstrings([]string{"response for get"}))
		Expect(ui.Outputs()).ToNot(ContainSubstrings(
			[]string{"FAILED"},
			[]string{"Content-Size:1024"},
		))
	})

	Context("when the --output flag is provided", func() {
		It("saves the body of the response to the given filepath if it exists", func() {
			fileutils.TempFile("poor-mans-pipe", func(tempFile *os.File, err error) {
				Expect(err).ToNot(HaveOccurred())
				curlRepo.ResponseBody = "hai"

				runCurlWithInputs([]string{"--output", tempFile.Name(), "/foo"})
				contents, err := ioutil.ReadAll(tempFile)
				Expect(err).ToNot(HaveOccurred())
				Expect(string(contents)).To(Equal("hai"))
			})
		})

		It("saves the body of the response to the given filepath if it doesn't exists", func() {
			fileutils.TempDir("poor-mans-dir", func(tmpDir string, err error) {
				Expect(err).ToNot(HaveOccurred())
				curlRepo.ResponseBody = "hai"

				filePath := filepath.Join(tmpDir, "subdir1", "banana.txt")
				runCurlWithInputs([]string{"--output", filePath, "/foo"})

				file, err := os.Open(filePath)
コード例 #5
0
		logger := NewLogger(buffer, true, "", "false")

		logger.Print("Hello World")

		Expect(buffer).To(gbytes.Say("Hello World"))

		_, err := os.Open("false")
		Expect(err).To(HaveOccurred())
	})

	It("returns a logger that writes to STDOUT and a file when verbose is set and CF_TRACE is a path", func() {
		fileutils.TempFile("trace_test", func(file *os.File, err error) {
			logger := NewLogger(buffer, true, file.Name(), "")

			logger.Print("Hello World")

			Expect(buffer).To(gbytes.Say("Hello World"))

			fileContents, _ := ioutil.ReadAll(file)
			Expect(fileContents).To(ContainSubstring("Hello World"))
		})
	})

	It("returns a logger that writes to STDOUT and a file when verbose is set and config.trace is a path", func() {
		fileutils.TempFile("trace_test", func(file *os.File, err error) {
			logger := NewLogger(buffer, true, "", file.Name())

			logger.Print("Hello World")

			Expect(buffer).To(gbytes.Say("Hello World"))

			fileContents, _ := ioutil.ReadAll(file)
コード例 #6
0
ファイル: push.go プロジェクト: cloudfoundry/v3-cli-plugin
func Push(cliConnection plugin.CliConnection, args []string) {
	appDir := "."
	buildpack := "null"
	dockerImage := ""

	fc := flags.New()
	fc.NewStringFlag("filepath", "p", "path to app dir or zip to upload")
	fc.NewStringFlag("buildpack", "b", "the buildpack to use")
	fc.NewStringFlag("docker-image", "di", "the docker image to use")
	fc.Parse(args...)
	if fc.IsSet("p") {
		appDir = fc.String("p")
	}
	if fc.IsSet("b") {
		buildpack = fmt.Sprintf(`"%s"`, fc.String("b"))
	}
	if fc.IsSet("di") {
		dockerImage = fmt.Sprintf(`"%s"`, fc.String("di"))
	}

	mySpace, _ := cliConnection.GetCurrentSpace()

	lifecycle := ""
	if dockerImage != "" {
		lifecycle = `"lifecycle": { "type": "docker", "data": {} }`
	} else {
		lifecycle = fmt.Sprintf(`"lifecycle": { "type": "buildpack", "data": { "buildpack": %s } }`, buildpack)
	}

	//create the app
	rawOutput, err := cliConnection.CliCommandWithoutTerminalOutput("curl", "/v3/apps", "-X", "POST", "-d",
		fmt.Sprintf(`{"name":"%s", "relationships": { "space": {"guid":"%s"}}, %s}`, fc.Args()[1], mySpace.Guid, lifecycle))
	FreakOut(err)
	output := strings.Join(rawOutput, "")
	app := V3AppModel{}
	err = json.Unmarshal([]byte(output), &app)
	FreakOut(err)
	if app.Error_Code != "" {
		FreakOut(errors.New("Error creating v3 app: " + app.Error_Code))
	}
	time.Sleep(2 * time.Second) // wait for app to settle before kicking off the log streamer
	go Logs(cliConnection, args)
	time.Sleep(2 * time.Second) // b/c sharing the cliConnection makes things break

	//create package
	pack := V3PackageModel{}
	if dockerImage != "" {
		request := fmt.Sprintf(`{"type": "docker", "data": {"image": %s}}`, dockerImage)
		rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/apps/%s/packages", app.Guid), "-X", "POST", "-d", request)
		FreakOut(err)
		output = strings.Join(rawOutput, "")

		err = json.Unmarshal([]byte(output), &pack)
		if err != nil {
			FreakOut(errors.New("Error creating v3 app package: " + app.Error_Code))
		}
	} else {
		//create the empty package to upload the app bits to
		rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/apps/%s/packages", app.Guid), "-X", "POST", "-d", "{\"type\": \"bits\"}")
		FreakOut(err)
		output = strings.Join(rawOutput, "")

		err = json.Unmarshal([]byte(output), &pack)
		if err != nil {
			FreakOut(errors.New("Error creating v3 app package: " + app.Error_Code))
		}

		token, err := cliConnection.AccessToken()
		FreakOut(err)
		api, apiErr := cliConnection.ApiEndpoint()
		FreakOut(apiErr)
		apiString := fmt.Sprintf("%s", api)
		if strings.Index(apiString, "s") == 4 {
			apiString = apiString[:4] + apiString[5:]
		}

		//gather files
		zipper := appfiles.ApplicationZipper{}
		fileutils.TempFile("uploads", func(zipFile *os.File, err error) {
			zipper.Zip(appDir, zipFile)
			_, upload := exec.Command("curl", fmt.Sprintf("%s/v3/packages/%s/upload", apiString, pack.Guid), "-F", fmt.Sprintf("bits=@%s", zipFile.Name()), "-H", fmt.Sprintf("Authorization: %s", token), "-H", "Expect:").Output()
			FreakOut(upload)
		})

		//waiting for cc to pour bits into blobstore
		Poll(cliConnection, fmt.Sprintf("/v3/packages/%s", pack.Guid), "READY", 5*time.Minute, "Package failed to upload")
	}

	rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/packages/%s/droplets", pack.Guid), "-X", "POST", "-d", "{}")
	FreakOut(err)
	output = strings.Join(rawOutput, "")
	droplet := V3DropletModel{}
	err = json.Unmarshal([]byte(output), &droplet)
	if err != nil {
		FreakOut(errors.New("error marshaling the v3 droplet: " + err.Error()))
	}
	//wait for the droplet to be ready
	Poll(cliConnection, fmt.Sprintf("/v3/droplets/%s", droplet.Guid), "STAGED", 10*time.Minute, "Droplet failed to stage")

	//assign droplet to the app
	rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/apps/%s/droplets/current", app.Guid), "-X", "PUT", "-d", fmt.Sprintf("{\"droplet_guid\":\"%s\"}", droplet.Guid))
	FreakOut(err)
	output = strings.Join(rawOutput, "")

	//pick the first available shared domain, get the guid
	space, _ := cliConnection.GetCurrentSpace()
	nextUrl := "/v2/shared_domains"
	allDomains := DomainsModel{}
	for nextUrl != "" {
		rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", nextUrl)
		FreakOut(err)
		output = strings.Join(rawOutput, "")
		tmp := DomainsModel{}
		err = json.Unmarshal([]byte(output), &tmp)
		FreakOut(err)
		allDomains.Resources = append(allDomains.Resources, tmp.Resources...)

		if tmp.NextUrl != "" {
			nextUrl = tmp.NextUrl
		} else {
			nextUrl = ""
		}
	}
	domainGuid := allDomains.Resources[0].Metadata.Guid
	rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", "v2/routes", "-X", "POST", "-d", fmt.Sprintf(`{"host":"%s","domain_guid":"%s","space_guid":"%s"}`, fc.Args()[1], domainGuid, space.Guid))
	output = strings.Join(rawOutput, "")

	var routeGuid string
	if strings.Contains(output, "CF-RouteHostTaken") {
		rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("v2/routes?q=host:%s;domain_guid:%s", fc.Args()[1], domainGuid))
		output = strings.Join(rawOutput, "")
		routes := RoutesModel{}
		err = json.Unmarshal([]byte(output), &routes)
		routeGuid = routes.Routes[0].Metadata.Guid
	} else {
		route := RouteModel{}
		err = json.Unmarshal([]byte(output), &route)
		if err != nil {
			FreakOut(errors.New("error unmarshaling the route: " + err.Error()))
		}
		routeGuid = route.Metadata.Guid
	}

	FreakOut(err)
	route := RouteModel{}
	err = json.Unmarshal([]byte(output), &route)
	if err != nil {
		FreakOut(errors.New("error unmarshaling the route: " + err.Error()))
	}

	//map the route to the app
	rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", "/v3/route_mappings", "-X", "POST", "-d", fmt.Sprintf(`{"relationships": { "route": { "guid": "%s" }, "app": { "guid": "%s" } }`, routeGuid, app.Guid))
	FreakOut(err)
	output = strings.Join(rawOutput, "")

	//start the app
	rawOutput, err = cliConnection.CliCommandWithoutTerminalOutput("curl", fmt.Sprintf("/v3/apps/%s/start", app.Guid), "-X", "PUT")
	FreakOut(err)
	output = strings.Join(rawOutput, "")

	fmt.Println("Done pushing! Checkout your processes using 'cf apps'")
}
コード例 #7
0
	. "github.com/onsi/gomega"
)

var _ = Describe("Flag Content Helpers", func() {
	Describe("GetContentsFromOptionalFlagValue", func() {
		It("returns an empty byte slice when given an empty string", func() {
			bs, err := util.GetContentsFromOptionalFlagValue("")
			Expect(err).NotTo(HaveOccurred())
			Expect(bs).To(Equal([]byte{}))
		})

		It("returns bytes when given a file name prefixed with @", func() {
			fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) {
				fileData := `{"foo": "bar"}`
				tmpFile.WriteString(fileData)

				bs, err := util.GetContentsFromOptionalFlagValue("@" + tmpFile.Name())
				Expect(err).NotTo(HaveOccurred())
				Expect(bs).To(Equal([]byte(fileData)))
			})
		})

		It("returns bytes when given a file name not prefixed with @", func() {
			fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) {
				fileData := `{"foo": "bar"}`
				tmpFile.WriteString(fileData)

				bs, err := util.GetContentsFromOptionalFlagValue(tmpFile.Name())
				Expect(err).NotTo(HaveOccurred())
				Expect(bs).To(Equal([]byte(fileData)))
			})
		})