Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/gitpodRun.go
2498 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"encoding/base64"
9
"io"
10
"os"
11
"syscall"
12
13
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
14
"github.com/spf13/cobra"
15
)
16
17
// This command takes a base64 stream, saves it to disk and executes it. We use this in
18
// agent-smith to upload/run the sentinel.
19
// We don't want this explanation to end up in the binary.
20
21
const delimiter = "@"
22
23
// gitpodRunCmd represents the gitpodRun command
24
var gitpodRunCmd = &cobra.Command{
25
Use: "__gitpod_run [<pathToExecutable>]",
26
Short: "Used by Gitpod to ensure smooth operation",
27
Hidden: true,
28
Args: cobra.MaximumNArgs(1),
29
RunE: func(cmd *cobra.Command, args []string) error {
30
// ignore trace
31
utils.TrackCommandUsageEvent.Command = nil
32
33
var tmpfile *os.File
34
var err error
35
if len(args) == 1 {
36
tmpfile, err = os.OpenFile(args[0], os.O_WRONLY|os.O_CREATE, 0700)
37
} else {
38
tmpfile, err = os.CreateTemp("", "gp")
39
}
40
if err != nil {
41
_ = os.Remove(tmpfile.Name())
42
return err
43
}
44
45
decoder := base64.NewDecoder(base64.RawStdEncoding, &delimitingReader{os.Stdin, false})
46
_, err = io.Copy(tmpfile, decoder)
47
if err != nil {
48
_ = os.Remove(tmpfile.Name())
49
return err
50
}
51
tmpfile.Close()
52
53
err = os.Chmod(tmpfile.Name(), 0700)
54
if err != nil {
55
_ = os.Remove(tmpfile.Name())
56
return err
57
}
58
err = syscall.Exec(tmpfile.Name(), []string{"gpr", "serve"}, []string{})
59
if err != nil {
60
_ = os.Remove(tmpfile.Name())
61
return err
62
}
63
return nil
64
},
65
}
66
67
type delimitingReader struct {
68
io.Reader
69
EOF bool
70
}
71
72
func (p *delimitingReader) Read(buf []byte) (int, error) {
73
if p.EOF {
74
return 0, io.EOF
75
}
76
77
n, err := p.Reader.Read(buf)
78
if err != nil {
79
return n, err
80
}
81
82
for i := 0; i < n; i++ {
83
if buf[i] == delimiter[0] {
84
p.EOF = true
85
return i, io.EOF
86
}
87
}
88
89
return n, err
90
}
91
92
func init() {
93
rootCmd.AddCommand(gitpodRunCmd)
94
}
95
96