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

Commit

Permalink
Add post install commands for php and xdebug
Browse files Browse the repository at this point in the history
Signed-off-by: Jason McCallister <jason@craftcms.com>
  • Loading branch information
jasonmccallister committed Apr 9, 2020
1 parent 7f4e6e2 commit 1b4b1a6
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 0 deletions.
56 changes: 56 additions & 0 deletions internal/action/post_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package action

import (
"fmt"

"github.com/craftcms/nitro/validate"
)

func ConfigurePHPMemoryLimit(name, php, limit string) (*Action, error) {
if err := validate.MachineName(name); err != nil {
return nil, err
}
if err := validate.PHPVersion(php); err != nil {
return nil, err
}

cmd := fmt.Sprintf("s|memory_limit = 128M|memory_limit = %s|g", limit)

return &Action{
Type: "exec",
UseSyscall: false,
Args: []string{"exec", name, "--", "sudo", "sed", "-i", cmd, "/etc/php/" + php + "/fpm/php.ini"},
}, nil
}

func ConfigurePHPExecutionTimeLimit(name, php, limit string) (*Action, error) {
if err := validate.MachineName(name); err != nil {
return nil, err
}
if err := validate.PHPVersion(php); err != nil {
return nil, err
}

cmd := fmt.Sprintf("s|max_execution_time = 30|max_execution_time = %s|g", limit)

return &Action{
Type: "exec",
UseSyscall: false,
Args: []string{"exec", name, "--", "sudo", "sed", "-i", cmd, "/etc/php/" + php + "/fpm/php.ini"},
}, nil
}

func ConfigureXdebug(name, php string) (*Action, error) {
if err := validate.MachineName(name); err != nil {
return nil, err
}
if err := validate.PHPVersion(php); err != nil {
return nil, err
}

return &Action{
Type: "exec",
UseSyscall: false,
Args: []string{"exec", name, "--", "sudo", "sed", "-i", "-e", `\$axdebug.remote_enable=1\nxdebug.remote_connect_back=0\nxdebug.remote_host=localhost\nxdebug.remote_port=9000\nxdebug.remote_log=/var/log/nginx/xdebug.log`, "/etc/php/" + php + "/mods-available/xdebug.ini"},
}, nil
}
167 changes: 167 additions & 0 deletions internal/action/post_install_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package action

import (
"reflect"
"testing"
)

func TestConfigurePHPMemoryLimit(t *testing.T) {
type args struct {
name string
php string
limit string
}
tests := []struct {
name string
args args
want *Action
wantErr bool
}{
{
name: "run the sed command for a specific version",
args: args{
name: "somename",
php: "7.4",
limit: "256M",
},
want: &Action{
Type: "exec",
UseSyscall: false,
Args: []string{"exec", "somename", "--", "sudo", "sed", "-i", "s|memory_limit = 128M|memory_limit = 256M|g", "/etc/php/7.4/fpm/php.ini"},
},
wantErr: false,
},
{
name: "wrong php returns error",
args: args{
name: "somename",
php: "7.9",
limit: "256M",
},
want: nil,
wantErr: true,
},
{
name: "bad name returns error",
args: args{
name: "",
php: "7.9",
limit: "256M",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ConfigurePHPMemoryLimit(tt.args.name, tt.args.php, tt.args.limit)
if (err != nil) != tt.wantErr {
t.Errorf("ConfigurePHPMemoryLimit() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ConfigurePHPMemoryLimit() got = \n%v, \nwant \n%v", got, tt.want)
}
})
}
}

func TestConfigurePHPExecutionTimeLimit(t *testing.T) {
type args struct {
name string
php string
limit string
}
tests := []struct {
name string
args args
want *Action
wantErr bool
}{
{
name: "run the sed command for a specific version",
args: args{
name: "somename",
php: "7.4",
limit: "240",
},
want: &Action{
Type: "exec",
UseSyscall: false,
Args: []string{"exec", "somename", "--", "sudo", "sed", "-i", "s|max_execution_time = 30|max_execution_time = 240|g", "/etc/php/7.4/fpm/php.ini"},
},
wantErr: false,
},
{
name: "wrong php returns error",
args: args{
name: "somename",
php: "7.9",
limit: "240",
},
want: nil,
wantErr: true,
},
{
name: "bad name returns error",
args: args{
name: "",
php: "7.9",
limit: "240",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ConfigurePHPExecutionTimeLimit(tt.args.name, tt.args.php, tt.args.limit)
if (err != nil) != tt.wantErr {
t.Errorf("ConfigurePHPExecutionTimeLimit() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ConfigurePHPExecutionTimeLimit() got = \n%v, \nwant \n%v", got, tt.want)
}
})
}
}

func TestConfigureXdebug(t *testing.T) {
type args struct {
name string
php string
}
tests := []struct {
name string
args args
want *Action
wantErr bool
}{
{
name: "returns the sed command",
args: args{
name: "somename",
php: "7.4",
},
want: &Action{
Type: "exec",
UseSyscall: false,
Args: []string{"exec", "somename", "--", "sed", "-i", "-e", `\$axdebug.remote_enable=1\nxdebug.remote_connect_back=0\nxdebug.remote_host=localhost\nxdebug.remote_port=9000\nxdebug.remote_log=/var/log/nginx/xdebug.log`, "/etc/php/7.4/mods-available/xdebug.ini"},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ConfigureXdebug(tt.args.name, tt.args.php)
if (err != nil) != tt.wantErr {
t.Errorf("ConfigureXdebug() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ConfigureXdebug() got = %v, want %v", got, tt.want)
}
})
}
}
19 changes: 19 additions & 0 deletions internal/cmd/machine_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ var createCommand = &cobra.Command{
}
actions = append(actions, *installAction)

// configure php settings that are specific to Craft
configurePhpMemoryAction, err := action.ConfigurePHPMemoryLimit(name, phpVersion, "256M")
if err != nil {
return err
}
actions = append(actions, *configurePhpMemoryAction)

configureExecutionTimeAction, err := action.ConfigurePHPExecutionTimeLimit(name, phpVersion, "240")
if err != nil {
return err
}
actions = append(actions, *configureExecutionTimeAction)

configureXdebugAction, err := action.ConfigureXdebug(name, phpVersion)
if err != nil {
return err
}
actions = append(actions, *configureXdebugAction)

for _, database := range databases {
volumeAction, err := action.CreateDatabaseVolume(name, database.Engine, database.Version, database.Port)
if err != nil {
Expand Down

0 comments on commit 1b4b1a6

Please # to comment.