Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
switch to generic service action
Browse files Browse the repository at this point in the history
Signed-off-by: Jason McCallister <jason@craftcms.com>
  • Loading branch information
jasonmccallister committed Jul 23, 2020
1 parent 1bb3682 commit e3eecb5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
32 changes: 28 additions & 4 deletions internal/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package api

import (
"context"
"errors"
"log"
"os"
"strconv"

"github.com/craftcms/nitro/validate"
)
Expand All @@ -13,6 +15,28 @@ type NitrodService struct {
logger *log.Logger
}

func (s *NitrodService) PhpIniSettings(ctx context.Context, request *ChangePhpIniSettingRequest) (*ServiceResponse, error) {
// TODO add validation to the value based on the setting
switch request.GetSetting() {
case PhpIniSetting_MAX_EXECUTION_TIME:
_, err := strconv.Atoi(request.GetValue())
if err != nil {
return nil, errors.New("max_execution_time must be a valid integer")
}
}

// nitro php iniset max_execution_time val
// sed -i "/aaa=/c\aaa=xxx" your_file_here
_, err := s.command.Run("sed", []string{"-i", "s|max_execution_time|max_execution_time = " + request.GetValue() + "|g", "/etc/php/" + request.GetVersion() + "/fpm/php.ini"})
if err != nil {
return nil, err
}
// todo edit the cli
// todo restart the php-fpm service

return &ServiceResponse{Message: "successfully changed the ini setting for max_execution_time to 300"}, nil
}

func (s *NitrodService) PhpFpmService(ctx context.Context, request *PhpFpmServiceRequest) (*ServiceResponse, error) {
// validate the request
if err := validate.PHPVersion(request.GetVersion()); err != nil {
Expand All @@ -23,10 +47,10 @@ func (s *NitrodService) PhpFpmService(ctx context.Context, request *PhpFpmServic
var action string
var message string
switch request.GetAction() {
case PhpFpmServiceRequest_START:
case ServiceAction_START:
message = "started"
action = "start"
case PhpFpmServiceRequest_STOP:
case ServiceAction_STOP:
message = "stopped"
action = "stop"
default:
Expand All @@ -48,10 +72,10 @@ func (s *NitrodService) NginxService(ctx context.Context, request *NginxServiceR
var action string
var message string
switch request.GetAction() {
case NginxServiceRequest_START:
case ServiceAction_START:
message = "started"
action = "start"
case NginxServiceRequest_STOP:
case ServiceAction_STOP:
message = "stopped"
action = "stop"
default:
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var nginxRestartCommand = &cobra.Command{
ip := nitro.IP(machine, runner)
c := client.NewClient(ip, "50051")

resp, err := c.NginxService(cmd.Context(), &api.NginxServiceRequest{Action: api.NginxServiceRequest_RESTART})
resp, err := c.NginxService(cmd.Context(), &api.NginxServiceRequest{Action: api.ServiceAction_RESTART})
if err != nil {
return err
}
Expand All @@ -47,7 +47,7 @@ var nginxStartCommand = &cobra.Command{
ip := nitro.IP(machine, runner)
c := client.NewClient(ip, "50051")

resp, err := c.NginxService(cmd.Context(), &api.NginxServiceRequest{Action: api.NginxServiceRequest_START})
resp, err := c.NginxService(cmd.Context(), &api.NginxServiceRequest{Action: api.ServiceAction_START})
if err != nil {
return err
}
Expand All @@ -67,7 +67,7 @@ var nginxStopCommand = &cobra.Command{
ip := nitro.IP(machine, runner)
c := client.NewClient(ip, "50051")

resp, err := c.NginxService(cmd.Context(), &api.NginxServiceRequest{Action: api.NginxServiceRequest_STOP})
resp, err := c.NginxService(cmd.Context(), &api.NginxServiceRequest{Action: api.ServiceAction_STOP})
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var phpRestartCommand = &cobra.Command{
c := client.NewClient(ip, "50051")
php := config.GetString("php", flagPhpVersion)

resp, err := c.PhpFpmService(cmd.Context(), &api.PhpFpmServiceRequest{Version: php, Action: api.PhpFpmServiceRequest_RESTART})
resp, err := c.PhpFpmService(cmd.Context(), &api.PhpFpmServiceRequest{Version: php, Action: api.ServiceAction_RESTART})
if err != nil {
return err
}
Expand All @@ -50,7 +50,7 @@ var phpStartCommand = &cobra.Command{
c := client.NewClient(ip, "50051")
php := config.GetString("php", flagPhpVersion)

resp, err := c.PhpFpmService(cmd.Context(), &api.PhpFpmServiceRequest{Version: php, Action: api.PhpFpmServiceRequest_START})
resp, err := c.PhpFpmService(cmd.Context(), &api.PhpFpmServiceRequest{Version: php, Action: api.ServiceAction_START})
if err != nil {
return err
}
Expand All @@ -71,7 +71,7 @@ var phpStopCommand = &cobra.Command{
c := client.NewClient(ip, "50051")
php := config.GetString("php", flagPhpVersion)

resp, err := c.PhpFpmService(cmd.Context(), &api.PhpFpmServiceRequest{Version: php, Action: api.PhpFpmServiceRequest_STOP})
resp, err := c.PhpFpmService(cmd.Context(), &api.PhpFpmServiceRequest{Version: php, Action: api.ServiceAction_STOP})
if err != nil {
return err
}
Expand Down

0 comments on commit e3eecb5

Please # to comment.