Skip to content

Commit

Permalink
Add pid file creation for okteto up command. (#860)
Browse files Browse the repository at this point in the history
* Add pid file creation for okteto up command.

Signed-off-by: adhaamehab <adhaamehab.me@gmail.com>
  • Loading branch information
adhaamehab committed May 5, 2020
1 parent 4e32f8a commit cc079a7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ import (
"net/url"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/docker/docker/pkg/term"
"github.com/okteto/okteto/cmd/utils"
"github.com/okteto/okteto/pkg/analytics"
buildCMD "github.com/okteto/okteto/pkg/cmd/build"
"github.com/okteto/okteto/pkg/config"
"github.com/okteto/okteto/pkg/errors"
k8Client "github.com/okteto/okteto/pkg/k8s/client"
"github.com/okteto/okteto/pkg/k8s/deployments"
Expand Down Expand Up @@ -162,6 +165,7 @@ func Up() *cobra.Command {

//RunUp starts the up sequence
func RunUp(dev *model.Dev, autoDeploy, build, forcePull, resetSyncthing bool) error {

up := &UpContext{
Dev: dev,
Exit: make(chan error, 1),
Expand Down Expand Up @@ -229,6 +233,13 @@ func (up *UpContext) Activate(autoDeploy, build, resetSyncthing bool) {
up.Dev.Namespace = namespace
}

if err := createPIDFile(up.Dev.Namespace, up.Dev.Name); err != nil {
log.Infof("failed to create pid file for %s - %s", up.Dev.Namespace, up.Dev.Name)
up.Exit <- fmt.Errorf("couldn't create pid file for %s - %s", up.Dev.Namespace, up.Dev.Name)
return
}
defer cleanPIDFile(up.Dev.Namespace, up.Dev.Name)

up.Namespace, err = namespaces.Get(up.Dev.Namespace, up.Client)
if err != nil {
log.Infof("failed to get namespace %s: %s", up.Dev.Namespace, err)
Expand Down Expand Up @@ -892,3 +903,25 @@ func printDisplayContext(dev *model.Dev) {
}
fmt.Println()
}

// createPIDFile creates a PID file to track Up state and existence
func createPIDFile(ns, dpName string) error {
filePath := filepath.Join(config.GetDeploymentHome(ns, dpName), "okteto.pid")
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("Unable to create PID file at %s", filePath)
}
defer file.Close()
if _, err := file.WriteString(strconv.Itoa(os.Getpid())); err != nil {
return fmt.Errorf("Unable to write to PID file at %s", filePath)
}
return nil
}

// cleanPIDFile deletes PID file after Up finishes
func cleanPIDFile(ns, dpName string) {
filePath := filepath.Join(config.GetDeploymentHome(ns, dpName), "okteto.pid")
if err := os.Remove(filePath); err != nil && !os.IsNotExist(err) {
log.Infof("Unable to delete PID file at %s", filePath)
}
}
32 changes: 32 additions & 0 deletions cmd/up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"

"github.com/okteto/okteto/pkg/config"
"github.com/okteto/okteto/pkg/errors"
"github.com/okteto/okteto/pkg/model"
)
Expand Down Expand Up @@ -101,3 +106,30 @@ func Test_printDisplayContext(t *testing.T) {
}

}

func TestCreatePIDFile(t *testing.T) {
deploymentName := "deployment"
namespace := "namespace"
if err := createPIDFile(namespace, deploymentName); err != nil {
t.Fatal("unable to create pid file")
}

filePath := filepath.Join(config.GetDeploymentHome(namespace, deploymentName), "okteto.pid")
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Fatal("didn't create pid file")
}

filePID, err := ioutil.ReadFile(filePath)
if err != nil {
t.Fatal("pid file is corrupted")
}
if string(filePID) != strconv.Itoa(os.Getpid()) {
t.Fatal("pid file content is invalid")
}

cleanPIDFile(namespace, deploymentName)
if _, err := os.Create(filePath); os.IsExist(err) {
t.Fatal("didn't delete pid file")
}

}

0 comments on commit cc079a7

Please sign in to comment.