Skip to content

[bugfix, cleanup] Finish moving plugin services to process compose #1150

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

Merged
merged 2 commits into from
Jun 15, 2023
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
4 changes: 2 additions & 2 deletions docs/app/docs/devbox_examples/languages/php.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Use `devbox services start|stop php-fpm` to start PHP-FPM in the background.
```bash
PHPFPM_PORT=8082
PHPFPM_ERROR_LOG_FILE={PROJECT_DIR}/.devbox/virtenv/php/php-fpm.log
PHPFPM_PID_FILE={PROJECT_DIR}/.devbox/virtenv/php/php-fpm.log
PHPFPM_PID_FILE={PROJECT_DIR}/.devbox/virtenv/php/php-fpm.pid
PHPRC={PROJECT_DIR}/devbox.d/php/php.ini
```

Expand All @@ -62,4 +62,4 @@ PHPRC={PROJECT_DIR}/devbox.d/php/php.ini
* {PROJECT_DIR}/devbox.d/php81/php-fpm.conf
* {PROJECT_DIR}/devbox.d/php81/php.ini

You can modify these file to configure PHP or your PHP-FPM server
You can modify these files to configure PHP or your PHP-FPM server
3 changes: 1 addition & 2 deletions internal/impl/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func (d *Devbox) Services() (services.Services, error) {
return nil, err
}

userSvcs := services.FromProcessComposeYaml(d.projectDir)
userSvcs := services.FromUserProcessCompose(d.projectDir)

svcSet := lo.Assign(pluginSvcs, userSvcs)
keys := make([]string, 0, len(svcSet))
Expand All @@ -514,7 +514,6 @@ func (d *Devbox) Services() (services.Services, error) {
}

return result, nil

}

func (d *Devbox) StartServices(ctx context.Context, serviceNames ...string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/plugin/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func getConfigIfAny(pkg *nix.Input, projectDir string) (*config, error) {
return nil, errors.WithStack(err)
}

// Try to find perfect match first
for _, file := range configFiles {
if file.IsDir() || strings.HasSuffix(file.Name(), ".go") {
continue
}
// Try to find perfect match first
content, err := plugins.BuiltIn.ReadFile(file.Name())
if err != nil {
return nil, errors.WithStack(err)
Expand Down
10 changes: 7 additions & 3 deletions internal/plugin/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,19 @@ func printReadme(cfg *config, w io.Writer, markdown bool) error {
}

func printServices(cfg *config, w io.Writer, markdown bool) error {
if len(cfg.Services) == 0 {
svcs, err := cfg.Services()
if err != nil {
return errors.WithStack(err)
}
if len(svcs) == 0 {
return nil
}
services := ""
for _, service := range cfg.Services {
for _, service := range svcs {
services += fmt.Sprintf("* %[1]s\n", service.Name)
}

_, err := fmt.Fprintf(
_, err = fmt.Fprintf(
w,
"%sServices:\n%s\nUse `devbox services start|stop [service]` to interact with services\n\n",
lo.Ternary(markdown, "### ", ""),
Expand Down
8 changes: 7 additions & 1 deletion internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ type config struct {
Packages []string `json:"packages"`
Env map[string]string `json:"env"`
Readme string `json:"readme"`
Services services.Services `json:"services"`

Shell struct {
// InitHook contains commands that will run at shell startup.
Expand All @@ -60,6 +59,13 @@ func (c *config) ProcessComposeYaml() (string, bool) {
return "", false
}

func (c *config) Services() (services.Services, error) {
if file, ok := c.ProcessComposeYaml(); ok {
return services.FromProcessCompose(file)
}
return nil, nil
}

func (m *Manager) Include(included string) error {
name, err := m.parseInclude(included)
if err != nil {
Expand Down
25 changes: 13 additions & 12 deletions internal/plugin/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
package plugin

import (
"fmt"
"os"

"go.jetpack.io/devbox/internal/nix"
"go.jetpack.io/devbox/internal/services"
)

// TODO: this should have PluginManager as receiver so we can build once with
// pkgs, includes, etc
func (m *Manager) GetServices(
pkgs []*nix.Input,
includes []string,
) (services.Services, error) {
svcs := services.Services{}
allSvcs := services.Services{}

allPkgs := append([]*nix.Input(nil), pkgs...)
for _, include := range includes {
Expand All @@ -34,15 +35,15 @@ func (m *Manager) GetServices(
continue
}

if file, ok := conf.ProcessComposeYaml(); ok {
svc := services.Service{
Name: conf.Name,
Env: conf.Env,
ProcessComposePath: file,
}
svcs[conf.Name] = svc
svcs, err := conf.Services()
if err != nil {
fmt.Fprintf(os.Stderr, "error reading services in plugin \"%s\", skipping", conf.Name)
continue
}
for name, svc := range svcs {
allSvcs[name] = svc
}

}
return svcs, nil

return allSvcs, nil
}
8 changes: 4 additions & 4 deletions internal/services/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import (
"go.jetpack.io/devbox/internal/cuecfg"
)

func FromProcessComposeYaml(projectDir string) Services {
// TODO need to handle if a filepath is passed in
func FromUserProcessCompose(projectDir string) Services {
processComposeYaml := lookupProcessCompose(projectDir, "")
if processComposeYaml == "" {
return nil
}
userSvcs, err := readProcessCompose(processComposeYaml)

userSvcs, err := FromProcessCompose(processComposeYaml)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading process-compose.yaml: %s, skipping", err)
return nil
}
return userSvcs
}

func readProcessCompose(path string) (Services, error) {
func FromProcessCompose(path string) (Services, error) {
processCompose := &types.Project{}
services := Services{}
err := errors.WithStack(cuecfg.ParseFile(path, processCompose))
Expand Down
4 changes: 1 addition & 3 deletions internal/services/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ func StartProcessManager(
}

for _, s := range availableServices {
if file, hasComposeYaml := s.ProcessComposeYaml(); hasComposeYaml {
flags = append(flags, "-f", file)
}
flags = append(flags, "-f", s.ProcessComposePath)
}

if processComposeBackground {
Expand Down
98 changes: 2 additions & 96 deletions internal/services/services.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,11 @@
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.

//lint:file-ignore U1000 Ignore unused function temporarily for debugging
package services

import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/a8m/envsubst"
"github.com/fatih/color"
"github.com/pkg/errors"

"go.jetpack.io/devbox/internal/envir"
)

type Services map[string]Service
type Services map[string]Service // name -> Service

type Service struct {
Name string `json:"name"`
Env map[string]string `json:"-"`
RawPort string `json:"port"`
Start string `json:"start"`
Stop string `json:"stop"`
Name string
ProcessComposePath string
}

// TODO: (john) Since moving to process-compose, our services no longer use the old `toggleServices` function. We'll need to clean a lot of this up in a later PR.

type serviceAction int

const (
startService serviceAction = iota
stopService
)

func printProxyURL(w io.Writer, services Services) error { // TODO: remove it?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callsite to this was accidentally removed in https://github.com/jetpack-io/devbox/pull/836/files (I looked it up because I was surprised this was unused)

The purpose of this endpoint is to print the URL the user can use to proxy to their devbox.sh service. I think it's an important part of making devbox cloud usable.

That said, currently only a single plugin declares a port, so we may want to just show the generic message and figure out how to show the port later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll address this in a separate PR.

if !envir.IsDevboxCloud() {
return nil
}

hostname, err := os.Hostname()
if err != nil {
return errors.WithStack(err)
}

printGeneric := false
for _, service := range services {
if port, _ := service.Port(); port != "" {
color.New(color.FgHiGreen).Fprintf(
w,
"To access %s on this vm use: %s-%s.svc.devbox.sh\n",
service.Name,
hostname,
port,
)
} else {
printGeneric = true
}
}

if printGeneric {
color.New(color.FgHiGreen).Fprintf(
w,
"To access other services on this vm use: %s-<port>.svc.devbox.sh\n",
hostname,
)
}
return nil
}

func (s *Service) Port() (string, error) {
if s.RawPort == "" {
return "", nil
}
return envsubst.String(s.RawPort)
}

func (s *Service) ProcessComposeYaml() (string, bool) {
return s.ProcessComposePath, true
}

func (s *Service) StartName() string {
return fmt.Sprintf("%s-service-start", s.Name)
}

func (s *Service) StopName() string {
return fmt.Sprintf("%s-service-stop", s.Name)
}

func (s *Services) UnmarshalJSON(b []byte) error {
var m map[string]Service
if err := json.Unmarshal(b, &m); err != nil {
return err
}
*s = make(Services)
for name, svc := range m {
svc.Name = name
(*s)[name] = svc
}
return nil
}
29 changes: 0 additions & 29 deletions internal/wrapnix/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ import (
"go.jetpack.io/devbox/internal/cmdutil"
"go.jetpack.io/devbox/internal/nix"
"go.jetpack.io/devbox/internal/plugin"
"go.jetpack.io/devbox/internal/services"
)

type devboxer interface {
NixBins(ctx context.Context) ([]string, error)
ShellEnvHash(ctx context.Context) (string, error)
ShellEnvHashKey() string
ProjectDir() string
Services() (services.Services, error)
}

//go:embed wrapper.sh.tmpl
Expand All @@ -39,11 +37,6 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {
return err
}

services, err := devbox.Services()
if err != nil {
return err
}

// Remove all old wrappers
_ = os.RemoveAll(filepath.Join(devbox.ProjectDir(), plugin.WrapperPath))

Expand All @@ -52,28 +45,6 @@ func CreateWrappers(ctx context.Context, devbox devboxer) error {
_ = os.MkdirAll(destPath, 0755)

bashPath := cmdutil.GetPathOrDefault("bash", "/bin/bash")
for _, service := range services {
if err = createWrapper(&createWrapperArgs{
devboxer: devbox,
BashPath: bashPath,
Command: service.Start,
Env: service.Env,
ShellEnvHash: shellEnvHash,
destPath: filepath.Join(destPath, service.StartName()),
}); err != nil {
return err
}
if err = createWrapper(&createWrapperArgs{
devboxer: devbox,
BashPath: bashPath,
Command: service.Stop,
Env: service.Env,
ShellEnvHash: shellEnvHash,
destPath: filepath.Join(destPath, service.StopName()),
}); err != nil {
return err
}
}

bins, err := devbox.NixBins(ctx)
if err != nil {
Expand Down
19 changes: 4 additions & 15 deletions plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,16 @@ Plugins are defined as JSON Template files, using the following schema:
"<key>": "<value>"
},
"create_files": {
"<destination>": "<source>",
"<destination>": "<source>"
},
"init_hook": [
"<bash commands>"
],
"services": {
"service_name": {
"start": "<start_command>",
"stop": "<stop_command>",
"port": <number>
}
}
]
}
```

A plugin can define services by adding a `process-compose.yaml` file in its `create_files` stanza. See process compose [docs](https://github.com/F1bonacc1/process-compose) or existing plugins in this directory for examples.

### Plugin Lifecycle

Plugins are activated whenever a developer runs `devbox shell`, runs a script with `devbox run`, or starts a service using `devbox services start|restart`. The lifecycle of a devbox shell with plugins goes in the following order.
Expand Down Expand Up @@ -122,12 +117,6 @@ You should use this to copy starter config files or templates needed to run the

A single `bash` command or list of `bash` commands that should run before the user's shell is initialized. This will run every time a shell is started, so you should avoid any resource heavy or long running processes in this step.

#### `services` *object*

A map of services that your plugin exposes to the user through `devbox services`. Services should have a `start` command and `stop` command defined so that Devbox can safely start and stop your service. You can optionally specify a `port` for Devbox to use along with automatic port forwarding in Devbox Cloud

For more details, see our [Services Documentation](https://www.jetpack.io/devbox/docs/guides/services/)

## Tips for Writing Plugins

* Only add plugins for packages that require configuration to work with Devbox.
Expand Down
9 changes: 1 addition & 8 deletions plugins/apacheHttpd.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apache",
"version": "0.0.1",
"version": "0.0.2",
"match": "^(apache|apacheHttpd)$",
"readme": "If you with to edit the config file, please copy it out of the .devbox directory.",
"env": {
Expand All @@ -14,12 +14,5 @@
"{{ .DevboxDir }}/httpd.conf": "apache/httpd.conf",
"{{ .DevboxDirRoot }}/web/index.html": "web/index.html",
"{{ .Virtenv }}/process-compose.yaml": "apache/process-compose.yaml"
},
"services": {
"apache": {
"port": "$HTTPD_PORT",
"start": "apachectl start -f $HTTPD_CONFDIR/httpd.conf",
"stop": "apachectl stop -f $HTTPD_CONFDIR/httpd.conf"
}
}
}
Loading