forked from Yelp/dockersh
/
dockersh.go
65 lines (60 loc) · 2.14 KB
/
dockersh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"github.com/docker/libcontainer/user"
"os"
"os/signal"
"strings"
"syscall"
)
func main() {
if os.Args[0] == "/init" {
fmt.Fprintf(os.Stdout, "started dockersh persistent container\n")
// Wait for terminating signal
sc := make(chan os.Signal, 2)
signal.Notify(sc, syscall.SIGTERM, syscall.SIGINT)
<-sc
os.Exit(0)
} else {
os.Exit(realMain())
}
}
func tmplConfigVar(template string, v *configInterpolation) string {
shell := "/bin/bash"
return strings.Replace(strings.Replace(strings.Replace(template, "%h", v.Home, -1), "%u", v.User, -1), "%s", shell, -1)
}
func realMain() int {
username, homedir, uid, gid, err := getCurrentUser()
if err != nil {
fmt.Fprintf(os.Stderr, "could not get current user: %v", err)
return 1
}
config, err := loadAllConfig(username, homedir)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not load config: %v\n", err)
return 1
}
configInterpolations := configInterpolation{homedir, username}
realUsername := tmplConfigVar(config.ContainerUsername, &configInterpolations)
realHomedirTo := tmplConfigVar(config.MountHomeTo, &configInterpolations)
realHomedirFrom := tmplConfigVar(config.MountHomeFrom, &configInterpolations)
realImageName := tmplConfigVar(config.ImageName, &configInterpolations)
realShell := tmplConfigVar(config.Shell, &configInterpolations)
realUserCwd := tmplConfigVar(config.UserCwd, &configInterpolations)
realContainerName := tmplConfigVar(config.ContainerName, &configInterpolations)
_, err = dockerpid(realContainerName)
if err != nil {
_, err = dockerstart(realUsername, realHomedirFrom, realHomedirTo, realContainerName, realImageName, config.DockerSocket, config.MountHome, config.MountTmp, config.MountDockerSocket, config.Entrypoint, config.Cmd, config.DockerOpt)
if err != nil {
fmt.Fprintf(os.Stderr, "could not start container: %s\n", err)
return 1
}
}
_, _, groups, _, err := user.GetUserGroupSupplementaryHome(username, 65536, 65536, "/")
err = nsenterexec(realContainerName, uid, gid, groups, realUserCwd, realShell)
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting shell in new container: %v\n", err)
return 1
}
return 0
}