Skip to content

Commit

Permalink
Fixed linux uid issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnudeep authored and Mirage20 committed Jun 27, 2019
1 parent deb24b1 commit 811388c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
39 changes: 30 additions & 9 deletions components/cli/pkg/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -106,24 +107,21 @@ func RunBuild(tag string, fileName string) {
cmdDockerPs := exec.Command("docker", "ps", "--filter",
"label=ballerina-runtime="+version.BuildVersion(),
"--filter", "label=currentDir="+currentDir, "--filter", "status=running", "--format", "{{.ID}}")
out, err := cmdDockerPs.Output()
containerId, err := cmdDockerPs.Output()
if err != nil {
spinner.Stop(false)
util.ExitWithErrorMessage("Error in retrieving cellery cli docker instance status ", err)
util.ExitWithErrorMessage("Error in retrieving cellery cli docker instance status", err)
}

if string(out) == "" {
if string(containerId) == "" {
cmdDockerRun := exec.Command("docker", "run", "-d",
"-l", "ballerina-runtime="+version.BuildVersion(),
"-l", "current.dir="+currentDir,
"--mount", "type=bind,source="+currentDir+",target=/home/cellery/src",
"--mount", "type=bind,source="+util.UserHomeDir()+string(os.PathSeparator)+".ballerina,target=/home/cellery/.ballerina",
"--mount", "type=bind,source="+util.UserHomeDir()+string(os.PathSeparator)+".cellery,target=/home/cellery/.cellery",
"--mount", "type=bind,source="+util.UserHomeDir()+string(os.PathSeparator)+".kube,target=/home/cellery/.kube",
"wso2cellery/ballerina-runtime:"+version.BuildVersion(), "sleep", "600",
)
util.UserHomeDir()

stderrReader, err := cmdDockerRun.StderrPipe()
if err != nil {
spinner.Stop(false)
Expand Down Expand Up @@ -159,7 +157,7 @@ func RunBuild(tag string, fileName string) {
go func() {
for {
if stdoutScanner.Scan() {
out = []byte(stdoutScanner.Text())
containerId = []byte(stdoutScanner.Text())
break
}
}
Expand All @@ -168,13 +166,36 @@ func RunBuild(tag string, fileName string) {
err = cmdDockerRun.Wait()
if err != nil {
spinner.Stop(false)
util.ExitWithErrorMessage("Docker Run Error %s\n", err)
util.ExitWithErrorMessage("Error while running ballerina-runtime docker image", err)
}
time.Sleep(5 * time.Second)
}

cliUser, err := user.Current()
if err != nil {
spinner.Stop(false)
util.ExitWithErrorMessage("Error while retrieving the current user", err)
}

if cliUser.Uid != constants.CELLERY_DOCKER_CLI_USER_ID {
cmdUserExist := exec.Command("docker", "exec", strings.TrimSpace(string(containerId)),
"id", "-u", cliUser.Username)
_, errUserExist := cmdUserExist.Output()
if errUserExist != nil {
cmdUserAdd := exec.Command("docker", "exec", strings.TrimSpace(string(containerId)), "useradd", "-m",
"-d", "/home/cellery", "--uid", cliUser.Uid, cliUser.Username)

_, errUserAdd := cmdUserAdd.Output()
if errUserAdd != nil {
spinner.Stop(false)
util.ExitWithErrorMessage("Error in adding Cellery execution user", errUserAdd)
}
}
}

balFileName := filepath.Base(tempBuildFileName)
cmd = exec.Command("docker", "exec", "-w", "/home/cellery/src", "-u", "1000",
strings.TrimSpace(string(out)), constants.DOCKER_CLI_BALLERINA_EXECUTABLE_PATH, "run", "target/"+balFileName, "build", string(iName), "{}")
strings.TrimSpace(string(containerId)), constants.DOCKER_CLI_BALLERINA_EXECUTABLE_PATH, "run", "target/"+balFileName, "build", string(iName), "{}")
}
execError := ""
stderrReader, _ := cmd.StderrPipe()
Expand Down
37 changes: 31 additions & 6 deletions components/cli/pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -850,12 +852,12 @@ func startCellInstance(imageDir string, instanceName string, runningNode *depend
cmdDockerPs := exec.Command("docker", "ps", "--filter", "label=ballerina-runtime="+version.BuildVersion(),
"--filter", "label=currentDir="+currentDir, "--filter", "status=running", "--format", "{{.ID}}")

out, err := cmdDockerPs.Output()
containerId, err := cmdDockerPs.Output()
if err != nil {
util.ExitWithErrorMessage("Docker Run Error", err)
}

if string(out) == "" {
if string(containerId) == "" {

cmdDockerRun := exec.Command("docker", "run", "-d", "-l", "ballerina-runtime="+version.BuildVersion(),
"--mount", "type=bind,source="+currentDir+",target=/home/cellery/src",
Expand All @@ -865,13 +867,36 @@ func startCellInstance(imageDir string, instanceName string, runningNode *depend
"wso2cellery/ballerina-runtime:"+version.BuildVersion(), "sleep", "600",
)

out, err = cmdDockerRun.Output()
containerId, err = cmdDockerRun.Output()
if err != nil {
util.ExitWithErrorMessage("Docker Run Error %s\n", err)
util.ExitWithErrorMessage("Docker Run Error", err)
}
time.Sleep(5 * time.Second)
}

cliUser, err := user.Current()
if err != nil {
util.ExitWithErrorMessage("Error while retrieving the current user", err)
}

exeUid := constants.CELLERY_DOCKER_CLI_USER_ID

if cliUser.Uid != constants.CELLERY_DOCKER_CLI_USER_ID && runtime.GOOS == "linux" {
cmdUserExist := exec.Command("docker", "exec", strings.TrimSpace(string(containerId)),
"id", "-u", cliUser.Username)
_, errUserExist := cmdUserExist.Output()
if errUserExist != nil {
cmdUserAdd := exec.Command("docker", "exec", strings.TrimSpace(string(containerId)), "useradd", "-m",
"-d", "/home/cellery", "--uid", cliUser.Uid, cliUser.Username)

_, errUserAdd := cmdUserAdd.Output()
if errUserAdd != nil {
util.ExitWithErrorMessage("Error in adding Cellery execution user", errUserAdd)
}
}
exeUid = cliUser.Uid
}

cmdArgs = append(cmdArgs, "-e", constants.CELLERY_IMAGE_DIR_ENV_VAR+"="+imageDir)

re := regexp.MustCompile(`^.*cellery-cell-image`)
Expand All @@ -895,8 +920,8 @@ func startCellInstance(imageDir string, instanceName string, runningNode *depend
cmd.Args = append(cmd.Args, "-e", envVar.Key+"="+envVar.Value)
}
}
cmd.Args = append(cmd.Args, "-w", "/home/cellery/src", "-u", "1000",
strings.TrimSpace(string(out)), constants.DOCKER_CLI_BALLERINA_EXECUTABLE_PATH, "run", tempRunFileName, "run",
cmd.Args = append(cmd.Args, "-w", "/home/cellery/src", "-u", exeUid,
strings.TrimSpace(string(containerId)), constants.DOCKER_CLI_BALLERINA_EXECUTABLE_PATH, "run", tempRunFileName, "run",
string(iName), string(dependenciesJson))
}
defer os.Remove(imageDir)
Expand Down
2 changes: 2 additions & 0 deletions components/cli/pkg/constants/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,5 @@ const CELLERY_SQL_PASSWORD = "cellery-sql-user"

const INGRESS_MODE_NODE_PORT = "Node port [kubeadm, minikube]"
const INGRESS_MODE_LOAD_BALANCER = "Load balancer [gcp, docker for desktop]"

const CELLERY_DOCKER_CLI_USER_ID = "1000"

0 comments on commit 811388c

Please sign in to comment.