Skip to content

Commit

Permalink
Add stopped projects to ddev list, fixes ddev#642
Browse files Browse the repository at this point in the history
  • Loading branch information
rfay committed May 22, 2019
1 parent 48217c2 commit 94d24c1
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/ddev/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ running 'ddev stop <projectname>.`,
}

// Do not show any describe output if we can't find the project.
if project.SiteStatus() == ddevapp.SiteNotFound {
if project.SiteStatus() == ddevapp.SiteStopped {
util.Failed("no project found. have you run 'ddev start'?")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/ddev/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var DdevExecCmd = &cobra.Command{
util.Failed("Failed to exec command: %v", err)
}

if strings.Contains(app.SiteStatus(), ddevapp.SiteNotFound) {
if strings.Contains(app.SiteStatus(), ddevapp.SiteStopped) {
util.Failed("Project is not currently running. Try 'ddev start'.")
}

Expand Down
14 changes: 11 additions & 3 deletions cmd/ddev/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,33 @@ import (
"github.com/spf13/cobra"
)

// continuous, if set, makes list continuously output
var continuous bool

// all, if set, shows non-running projects in addition to running/paused
var all bool

// DdevListCmd represents the list command
var DdevListCmd = &cobra.Command{
Use: "list",
Short: "List projects",
Long: `List projects.`,
Run: func(cmd *cobra.Command, args []string) {
for {
apps := ddevapp.GetActiveProjects()
apps, err := ddevapp.GetProjects()
if err != nil {
util.Failed("failed getting GetProjects: %v", err)
}
appDescs := make([]map[string]interface{}, 0)

if len(apps) < 1 {
output.UserOut.WithField("raw", appDescs).Println("There are no active ddev projects.")
output.UserOut.WithField("raw", appDescs).Println("No ddev projects were found.")
} else {
table := ddevapp.CreateAppTable()
for _, app := range apps {
desc, err := app.Describe()
if err != nil {
util.Failed("Failed to describe project %s: %v", app.GetName(), err)
util.Error("Failed to describe project %s: %v", app.GetName(), err)
}
appDescs = append(appDescs, desc)
ddevapp.RenderAppRow(table, desc)
Expand All @@ -46,6 +53,7 @@ var DdevListCmd = &cobra.Command{
}

func init() {
DdevListCmd.Flags().BoolVarP(&all, "all", "a", false, "If set, all projects will be displayed, even if not running.")
DdevListCmd.Flags().BoolVarP(&continuous, "continuous", "", false, "If set, project information will be emitted once per second")
RootCmd.AddCommand(DdevListCmd)
}
2 changes: 1 addition & 1 deletion cmd/ddev/cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var DdevLogsCmd = &cobra.Command{
util.Failed("Failed to retrieve logs: %v", err)
}

if strings.Contains(app.SiteStatus(), ddevapp.SiteNotFound) {
if strings.Contains(app.SiteStatus(), ddevapp.SiteStopped) {
util.Failed("Project is not currently running. Try 'ddev start'.")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/ddev/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var DdevSSHCmd = &cobra.Command{
util.Failed("Failed to ssh: %v", err)
}

if strings.Contains(app.SiteStatus(), ddevapp.SiteNotFound) {
if strings.Contains(app.SiteStatus(), ddevapp.SiteStopped) {
util.Failed("Project is not currently running. Try 'ddev start'.")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/ddev/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ To snapshot the database on stop, use "ddev stop --snapshot"; A snapshot is auto

// Iterate through the list of projects built above, removing each one.
for _, project := range projects {
if project.SiteStatus() == ddevapp.SiteNotFound {
if project.SiteStatus() == ddevapp.SiteStopped {
util.Warning("Project %s is not currently running. Try 'ddev start'.", project.GetName())
}

Expand Down
13 changes: 9 additions & 4 deletions pkg/ddevapp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (app *DdevApp) WriteConfig() error {

// We now want to reserve the port we're writing for HostDBPort and HostWebserverPort and so they don't
// accidentally get used for other projects.
err := app.CheckAndReserveHostPorts()
err := app.UpdateGlobalProjectList()
if err != nil {
return err
}
Expand Down Expand Up @@ -261,9 +261,9 @@ RUN echo "Built from ` + app.DBImage + `" >/var/tmp/built-from.txt
return nil
}

// CheckAndReserveHostPorts checks that configured host ports are not already
// UpdateGlobalProjectList checks that configured host ports are not already
// reserved by another project.
func (app *DdevApp) CheckAndReserveHostPorts() error {
func (app *DdevApp) UpdateGlobalProjectList() error {
portsToReserve := []string{}
if app.HostDBPort != "" {
portsToReserve = append(portsToReserve, app.HostDBPort)
Expand All @@ -281,7 +281,12 @@ func (app *DdevApp) CheckAndReserveHostPorts() error {
return err
}
}
err := globalconfig.ReservePorts(app.Name, portsToReserve)
// TODO: Make sure there isn't already a conflicting project
err := globalconfig.SetProjectAppRoot(app.Name, app.AppRoot)
if err != nil {
return err
}
err = globalconfig.ReservePorts(app.Name, portsToReserve)

return err
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ const SiteRunning = "running"
// SiteStarting
const SiteStarting = "starting"

// SiteNotFound defines the string used to denote a site where the containers were not found/do not exist.
const SiteNotFound = "not found"
// SiteStopped defines the string used to denote a site where the containers were not found/do not exist, but the project is there.
const SiteStopped = "stopped"

// SiteDirMissing defines the string used to denote when a site is missing its application directory.
const SiteDirMissing = "app directory missing"
Expand Down Expand Up @@ -443,7 +443,7 @@ func (app *DdevApp) SiteStatus() string {
return ""
}
if container == nil {
statuses[service] = SiteNotFound
statuses[service] = SiteStopped
} else {
status, _ := dockerutil.GetContainerHealth(container)

Expand Down Expand Up @@ -681,7 +681,8 @@ func (app *DdevApp) Start() error {
}

// Make sure that any ports allocated are available.
err = app.CheckAndReserveHostPorts()
// and of course add to global project list as well
err = app.UpdateGlobalProjectList()
if err != nil {
return err
}
Expand Down Expand Up @@ -1048,7 +1049,7 @@ func (app *DdevApp) DockerEnv() {
func (app *DdevApp) Pause() error {
app.DockerEnv()

if app.SiteStatus() == SiteNotFound {
if app.SiteStatus() == SiteStopped {
return fmt.Errorf("no project to stop")
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/ddevapp/ddevapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestMain(m *testing.M) {
log.Fatalf("TestMain shutdown: app.Init() failed on site %s in dir %s, err=%v", TestSites[i].Name, TestSites[i].Dir, err)
}

if app.SiteStatus() != ddevapp.SiteNotFound {
if app.SiteStatus() != ddevapp.SiteStopped {
err = app.Stop(true, false)
if err != nil {
log.Fatalf("TestMain shutdown: app.Stop() failed on site %s, err=%v", TestSites[i].Name, err)
Expand Down Expand Up @@ -1802,7 +1802,7 @@ func TestGetAppsEmpty(t *testing.T) {
err := app.Init(site.Dir)
assert.NoError(err)

if app.SiteStatus() != ddevapp.SiteNotFound {
if app.SiteStatus() != ddevapp.SiteStopped {
err = app.Stop(true, false)
assert.NoError(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ddevapp/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func RenderRouterStatus() string {
badRouter := "\nThe router is not yet healthy. Your projects may not be accessible.\nIf it doesn't become healthy try running 'ddev start' on a project to recreate it."

switch status {
case SiteNotFound:
case SiteStopped:
renderedStatus = color.RedString(status) + badRouter
case "healthy":
renderedStatus = color.CyanString(status)
Expand All @@ -143,7 +143,7 @@ func GetRouterStatus() (string, string) {
container, err := dockerutil.FindContainerByLabels(label)

if err != nil || container == nil {
status = SiteNotFound
status = SiteStopped
} else {
status, logOutput = dockerutil.GetContainerHealth(container)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/ddevapp/ssh_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func RenderSSHAuthStatus() string {
var renderedStatus string

switch status {
case SiteNotFound:
case SiteStopped:
renderedStatus = color.RedString(status)
case "healthy":
renderedStatus = color.CyanString(status)
Expand All @@ -159,10 +159,10 @@ func GetSSHAuthStatus() string {

if err != nil {
util.Error("Failed to execute FindContainerByLabels(%v): %v", label, err)
return SiteNotFound
return SiteStopped
}
if container == nil {
return SiteNotFound
return SiteStopped
}
health, _ := dockerutil.GetContainerHealth(container)
return health
Expand Down
21 changes: 20 additions & 1 deletion pkg/ddevapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ddevapp

import (
"fmt"
"github.com/drud/ddev/pkg/globalconfig"
"github.com/drud/ddev/pkg/nodeps"
"path"
"path/filepath"
Expand Down Expand Up @@ -82,7 +83,7 @@ func RenderAppRow(table *uitable.Table, row map[string]interface{}) {
switch {
case strings.Contains(status, SitePaused):
status = color.YellowString(status)
case strings.Contains(status, SiteNotFound):
case strings.Contains(status, SiteStopped):
status = color.RedString(status)
case strings.Contains(status, SiteDirMissing):
status = color.RedString(status)
Expand Down Expand Up @@ -316,3 +317,21 @@ func CheckForMissingProjectFiles(project *DdevApp) error {

return nil
}

// GetProjects returns projects that are listed
// in globalconfig projectlist but don't show up from docker.
func GetProjects() ([]*DdevApp, error) {
apps := []*DdevApp{}
// TODO: This should use a get method instead of directly accessing
for _, info := range globalconfig.DdevGlobalConfig.ProjectList {
// Skip if AppRoot hasn't been placed in globalconfig
if info.AppRoot != "" {
activeApp, err := NewApp(info.AppRoot, true, ProviderDefault)
if err != nil {
return nil, err
}
apps = append(apps, activeApp)
}
}
return apps, nil
}
14 changes: 14 additions & 0 deletions pkg/globalconfig/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
}

type ProjectInfo struct {
AppRoot string `yaml:"approot"`
UsedHostPorts []string `yaml:"used_host_ports,omitempty,flow"`
}

Expand Down Expand Up @@ -243,6 +244,19 @@ func ReservePorts(projectName string, ports []string) error {
return err
}

// SetProjectAppRoot() sets the approot in the ProjectInfo of global config
func SetProjectAppRoot(projectName string, appRoot string) error {
// If the project doesn't exist, add it.
_, ok := DdevGlobalConfig.ProjectList[projectName]
if !ok {
DdevGlobalConfig.ProjectList[projectName] = &ProjectInfo{}
}
DdevGlobalConfig.ProjectList[projectName].AppRoot = appRoot
// TODO: Aren't we going to end up doing this more than we need to?
err := WriteGlobalConfig(DdevGlobalConfig)
return err
}

// RemoveProjectInfo() removes the ProjectInfo line for a project
func RemoveProjectInfo(projectName string) error {
_, ok := DdevGlobalConfig.ProjectList[projectName]
Expand Down

0 comments on commit 94d24c1

Please sign in to comment.