func (e EnvironmentExtractor) constructFinishFromEnvironment(exitCode int, exitStatus int) (FinishOutput, error) { podID := os.Getenv(pods.PodIDEnvVar) if podID == "" { return FinishOutput{}, util.Errorf("No %s env var set", pods.PodIDEnvVar) } launchableID := os.Getenv(pods.LaunchableIDEnvVar) if launchableID == "" { return FinishOutput{}, util.Errorf("No %s env var set", pods.LaunchableIDEnvVar) } entryPoint := os.Getenv(launch.EntryPointEnvVar) if entryPoint == "" { return FinishOutput{}, util.Errorf("No %s env var set", launch.EntryPointEnvVar) } // It's okay if this one is missing, most pods are "legacy" pods that have a blank unique key podUniqueKey := os.Getenv(pods.PodUniqueKeyEnvVar) return FinishOutput{ PodID: types.PodID(podID), LaunchableID: launch.LaunchableID(launchableID), EntryPoint: entryPoint, PodUniqueKey: types.PodUniqueKey(podUniqueKey), ExitCode: exitCode, ExitStatus: exitStatus, }, nil }
func main() { bin2pod.Version(version.VERSION) kingpin.MustParse(bin2pod.Parse(os.Args[1:])) res := result{} manifestBuilder := manifest.NewBuilder() manifestBuilder.SetID(podID()) stanza := launch.LaunchableStanza{} stanza.LaunchableType = "hoist" workingDir := activeDir() err := addManifestConfig(manifestBuilder) tarLocation, err := makeTar(workingDir) if err != nil { log.Fatalln(err.Error()) } res.TarPath = tarLocation if *location != "" { stanza.Location = *location res.FinalLocation = *location } else { stanza.Location = fmt.Sprintf("file://%v", tarLocation) res.FinalLocation = tarLocation } manifestBuilder.SetLaunchables(map[launch.LaunchableID]launch.LaunchableStanza{ launch.LaunchableID(podID()): stanza, }) if err != nil { log.Fatalln(err.Error()) } manifest := manifestBuilder.GetManifest() res.ManifestPath, err = writeManifest(workingDir, manifest) if err != nil { log.Fatalf("Couldn't write manifest: %s", err) } b, err := json.MarshalIndent(res, "", " ") if err != nil { log.Fatalf("Couldn't marshal output: %s", err) } _, err = os.Stdout.Write(b) if err != nil { log.Fatalf("Couldn't write to stdout: %s", err) } }
// Runs Scan() once on the passed Scanner and converts the result to a FinishOutput func scanRow(scanner Scanner) (FinishOutput, error) { var id int64 var date time.Time var podID, podUniqueKey, launchableID, entryPoint string var exitCode, exitStatus int err := scanner.Scan(&id, &date, &podID, &podUniqueKey, &launchableID, &entryPoint, &exitCode, &exitStatus) if err != nil { return FinishOutput{}, err } return FinishOutput{ ID: id, PodID: types.PodID(podID), LaunchableID: launch.LaunchableID(launchableID), EntryPoint: entryPoint, PodUniqueKey: types.PodUniqueKey(podUniqueKey), ExitCode: exitCode, ExitStatus: exitStatus, ExitTime: date, }, nil }
func TestWatchPodStatus(t *testing.T) { respCh := make(chan *podstore_protos.PodStatusResponse) stream := &WatchPodStatusStream{ FakeServerStream: testutil.NewFakeServerStream(context.Background()), ResponseCh: respCh, } podStatusStore, server := setupServerWithFakePodStatusStore() podUniqueKey := types.NewPodUUID() req := &podstore_protos.WatchPodStatusRequest{ StatusNamespace: kp.PreparerPodStatusNamespace.String(), PodUniqueKey: podUniqueKey.String(), // Set it 1 above last index so we wait for the key to exist. (The test status // store starts at 1234 for some reason) WaitIndex: 1235, } watchErrCh := make(chan error) defer close(watchErrCh) go func() { err := server.WatchPodStatus(req, stream) if err != nil { watchErrCh <- err } }() expectedTime := time.Now() expectedManifest := `id: "test_app"` expectedPodState := podstatus.PodLaunched expectedLaunchableID := launch.LaunchableID("nginx") expectedEntryPoint := "launch" expectedExitCode := 3 expectedExitStatus := 4 setStatusErrCh := make(chan error) defer close(setStatusErrCh) go func() { err := podStatusStore.Set(podUniqueKey, podstatus.PodStatus{ Manifest: expectedManifest, PodStatus: expectedPodState, ProcessStatuses: []podstatus.ProcessStatus{ { LaunchableID: expectedLaunchableID, EntryPoint: expectedEntryPoint, LastExit: &podstatus.ExitStatus{ ExitTime: expectedTime, ExitCode: expectedExitCode, ExitStatus: expectedExitStatus, }, }, }, }) if err != nil { setStatusErrCh <- err } }() select { case <-time.After(5 * time.Second): t.Fatal("Didn't receive value after 5 seconds") case err := <-setStatusErrCh: t.Fatalf("Error setting status to trigger watch: %s", err) case err := <-watchErrCh: t.Fatalf("Unexpected error watching for status: %s", err) case resp := <-respCh: if resp.Manifest != expectedManifest { t.Errorf("Manifest didn't match expected, wanted %q got %q", expectedManifest, resp.Manifest) } if resp.PodState != expectedPodState.String() { t.Errorf("PodState didn't match expcted, wanted %q got %q", expectedPodState, resp.PodState) } if len(resp.ProcessStatuses) != 1 { t.Fatalf("Expected 1 process status in pod status but got %d", len(resp.ProcessStatuses)) } processStatus := resp.ProcessStatuses[0] if processStatus.LaunchableId != expectedLaunchableID.String() { t.Errorf("Expected process status for launchable %q but found %q", expectedLaunchableID, processStatus.LaunchableId) } if processStatus.EntryPoint != expectedEntryPoint { t.Errorf("Expected process status for entry point %q but found %q", expectedEntryPoint, processStatus.EntryPoint) } if processStatus.LastExit == nil { t.Fatal("Expected exit information for process") } lastExit := processStatus.LastExit if lastExit.ExitTime != expectedTime.Unix() { t.Error("Exit time for process in status didn't match expected") } if lastExit.ExitCode != int64(expectedExitCode) { t.Errorf("Expected exit code %d but got %d", expectedExitCode, lastExit.ExitCode) } if lastExit.ExitStatus != int64(expectedExitStatus) { t.Errorf("Expected exit status %d but got %d", expectedExitStatus, lastExit.ExitStatus) } } }