Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use docker image name that is used to run the starter container #53

Merged
merged 1 commit into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const (
cgroupDockerMarker = ":/docker/"
)

type containerInfo struct {
Name string
ImageName string
}

// isRunningInDocker checks if the process is running in a docker container.
func isRunningInDocker() bool {
if os.Getenv("RUNNING_IN_DOCKER") != "true" {
Expand All @@ -46,8 +51,8 @@ func isRunningInDocker() bool {
return true
}

// findDockerContainerName find the name (or if not possible the ID) of the container that is used to run this process.
func findDockerContainerName(dockerEndpoint string) (string, error) {
// findDockerContainerInfo find information (name or if not possible the ID, image-name) of the container that is used to run this process.
func findDockerContainerInfo(dockerEndpoint string) (containerInfo, error) {
findID := func() (string, error) {
raw, err := ioutil.ReadFile("/proc/self/cgroup")
if err != nil {
Expand All @@ -68,21 +73,30 @@ func findDockerContainerName(dockerEndpoint string) (string, error) {

id, err := findID()
if err != nil {
return "", maskAny(err)
return containerInfo{}, maskAny(err)
}
info := containerInfo{Name: id}

// Find name for container with ID.
client, err := docker.NewClient(dockerEndpoint)
if err != nil {
return id, nil // fallback to ID
return info, nil // fallback to ID
}
container, err := client.InspectContainer(id)
if err != nil {
return id, nil // fallback to ID
return info, nil // fallback to ID
}

if name := container.Name; name != "" {
return name, nil
info.Name = name
}
return id, nil // fallback to ID
info.ImageName = container.Image

// Try to inspect image for more naming info
image, err := client.InspectImage(container.Image)
if err == nil && len(image.RepoTags) > 0 {
info.ImageName = image.RepoTags[0]
}

return info, nil
}
23 changes: 17 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ import (
// Configuration data with defaults:

const (
projectName = "arangodb"
defaultDockerGCDelay = time.Minute * 10
projectName = "arangodb"
defaultDockerGCDelay = time.Minute * 10
defaultDockerStarterImage = "arangodb/arangodb-starter"
)

var (
Expand Down Expand Up @@ -85,6 +86,7 @@ var (
sslCAFile string
dockerEndpoint string
dockerImage string
dockerStarterImage = defaultDockerStarterImage
dockerUser string
dockerContainerName string
dockerGCDelay time.Duration
Expand Down Expand Up @@ -277,12 +279,20 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
}

// Auto detect docker container ID (if needed)
if isRunningInDocker() && dockerContainerName == "" {
id, err := findDockerContainerName(dockerEndpoint)
if isRunningInDocker() {
info, err := findDockerContainerInfo(dockerEndpoint)
if err != nil {
log.Fatalf("Cannot find docker container name. Please specify using --dockerContainer=...")
if dockerContainerName == "" {
log.Fatalf("Cannot find docker container name. Please specify using --dockerContainer=...")
}
} else {
if dockerContainerName == "" {
dockerContainerName = info.Name
}
if info.ImageName != "" {
dockerStarterImage = info.ImageName
}
}
dockerContainerName = id
}

// Some plausibility checks:
Expand Down Expand Up @@ -389,6 +399,7 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
DockerContainerName: dockerContainerName,
DockerEndpoint: dockerEndpoint,
DockerImage: dockerImage,
DockerStarterImage: dockerStarterImage,
DockerUser: dockerUser,
DockerGCDelay: dockerGCDelay,
DockerNetworkMode: dockerNetworkMode,
Expand Down
1 change: 1 addition & 0 deletions service/arangodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Config struct {
DockerContainerName string // Name of the container running this process
DockerEndpoint string // Where to reach the docker daemon
DockerImage string // Name of Arangodb docker image
DockerStarterImage string
DockerUser string
DockerGCDelay time.Duration
DockerNetworkMode string
Expand Down
2 changes: 1 addition & 1 deletion service/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (s *Service) showSlaveStartCommands(runner Runner) {
if s.announcePort != s.MasterPort {
port = strconv.Itoa(s.announcePort)
}
fmt.Println(runner.CreateStartArangodbCommand(s.DataDir, index, s.OwnAddress, port))
fmt.Println(runner.CreateStartArangodbCommand(s.DataDir, index, s.OwnAddress, port, s.DockerStarterImage))
fmt.Println()
}
}
Expand Down
2 changes: 1 addition & 1 deletion service/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Runner interface {
Start(command string, args []string, volumes []Volume, ports []int, containerName, serverDir string) (Process, error)

// Create a command that a user should use to start a slave arangodb instance.
CreateStartArangodbCommand(myDataDir string, index int, masterIP string, masterPort string) string
CreateStartArangodbCommand(myDataDir string, index int, masterIP, masterPort, starterImageName string) string

// Cleanup after all processes are dead and have been cleaned themselves
Cleanup() error
Expand Down
4 changes: 2 additions & 2 deletions service/runner_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (r *dockerRunner) pullImage(image string) error {
return nil
}

func (r *dockerRunner) CreateStartArangodbCommand(myDataDir string, index int, masterIP string, masterPort string) string {
func (r *dockerRunner) CreateStartArangodbCommand(myDataDir string, index int, masterIP, masterPort, starterImageName string) string {
addr := masterIP
hostPort := DefaultMasterPort + (portOffsetIncrement * (index - 1))
if masterPort != "" {
Expand All @@ -284,7 +284,7 @@ func (r *dockerRunner) CreateStartArangodbCommand(myDataDir string, index int, m
lines := []string{
fmt.Sprintf("docker volume create arangodb%d &&", index),
fmt.Sprintf("docker run -it --name=adb%d --rm %s -v arangodb%d:/data", index, netArgs, index),
fmt.Sprintf("-v /var/run/docker.sock:/var/run/docker.sock arangodb/arangodb-starter"),
fmt.Sprintf("-v /var/run/docker.sock:/var/run/docker.sock %s", starterImageName),
fmt.Sprintf("--starter.address=%s --starter.join=%s", masterIP, addr),
}
return strings.Join(lines, " \\\n ")
Expand Down
2 changes: 1 addition & 1 deletion service/runner_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (r *processRunner) Start(command string, args []string, volumes []Volume, p
return &process{log: r.log, p: c.Process, isChild: true}, nil
}

func (r *processRunner) CreateStartArangodbCommand(myDataDir string, index int, masterIP string, masterPort string) string {
func (r *processRunner) CreateStartArangodbCommand(myDataDir string, index int, masterIP, masterPort, starterImageName string) string {
if masterIP == "" {
masterIP = "127.0.0.1"
}
Expand Down