This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jason McCallister <jason@craftcms.com>
- Loading branch information
1 parent
c7188f9
commit 042ff0c
Showing
4 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package action | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/craftcms/nitro/validate" | ||
) | ||
|
||
const ( | ||
php70Packages = "php7.0 php7.0-mbstring php7.0-cli php7.0-curl php7.0-fpm php7.0-gd php7.0-intl php7.0-json php7.0-mysql php7.0-opcache php7.0-pgsql php7.0-zip php7.0-xml php-xdebug php-imagick blackfire-agent blackfire-php" | ||
php71Packages = "php7.1 php7.1-mbstring php7.1-cli php7.1-curl php7.1-fpm php7.1-gd php7.1-intl php7.1-json php7.1-mysql php7.1-opcache php7.1-pgsql php7.1-zip php7.1-xml php-xdebug php-imagick blackfire-agent blackfire-php" | ||
php72Packages = "php7.2 php7.2-mbstring php7.2-cli php7.2-curl php7.2-fpm php7.2-gd php7.2-intl php7.2-json php7.2-mysql php7.2-opcache php7.2-pgsql php7.2-zip php7.2-xml php-xdebug php-imagick blackfire-agent blackfire-php" | ||
php73Packages = "php7.3 php7.3-mbstring php7.3-cli php7.3-curl php7.3-fpm php7.3-gd php7.3-intl php7.3-json php7.3-mysql php7.3-opcache php7.3-pgsql php7.3-zip php7.3-xml php-xdebug php-imagick blackfire-agent blackfire-php" | ||
php74Packages = "php7.4 php7.4-mbstring php7.4-cli php7.4-curl php7.4-fpm php7.4-gd php7.4-intl php7.4-json php7.4-mysql php7.4-opcache php7.4-pgsql php7.4-zip php7.4-xml php-xdebug php-imagick blackfire-agent blackfire-php" | ||
) | ||
|
||
// InstallPackages is used to install the core PHP packages needed by the | ||
// nitro machine to run. | ||
func InstallPackages(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 | ||
} | ||
|
||
args := []string{"exec", name, "--", "sudo", "apt-get", "install", "-y"} | ||
|
||
switch php { | ||
case "7.0": | ||
args = append(args, strings.Split(php70Packages, " ")...) | ||
case "7.1": | ||
args = append(args, strings.Split(php71Packages, " ")...) | ||
case "7.2": | ||
args = append(args, strings.Split(php72Packages, " ")...) | ||
case "7.3": | ||
args = append(args, strings.Split(php73Packages, " ")...) | ||
default: | ||
args = append(args, strings.Split(php74Packages, " ")...) | ||
} | ||
|
||
return &Action{ | ||
Type: "exec", | ||
UseSyscall: false, | ||
Args: args, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package action | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestInstallCorePackages(t *testing.T) { | ||
type args struct { | ||
name string | ||
php string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *Action | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "can get commands to install PHP 7.4", | ||
args: args{ | ||
name: "somename", | ||
php: "7.4", | ||
}, | ||
want: &Action{ | ||
Type: "exec", | ||
UseSyscall: false, | ||
Args: []string{"exec", "somename", "--", "sudo", "apt-get", "install", "-y", "php7.4", "php7.4-mbstring", "php7.4-cli", "php7.4-curl", "php7.4-fpm", "php7.4-gd", "php7.4-intl", "php7.4-json", "php7.4-mysql", "php7.4-opcache", "php7.4-pgsql", "php7.4-zip", "php7.4-xml", "php-xdebug", "php-imagick", "blackfire-agent", "blackfire-php"}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "wrong version of php returns an error", | ||
args: args{ | ||
name: "somename", | ||
php: "7.9", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := InstallPackages(tt.args.name, tt.args.php) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("InstallPackages() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("InstallPackages() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package action | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
|
||
"github.com/craftcms/nitro/validate" | ||
) | ||
|
||
type Action struct { | ||
Type string | ||
UseSyscall bool | ||
Input string | ||
Args []string | ||
} | ||
|
||
// Launch is responsible for the creation of a virtual machine, each parameter must be provided and validated | ||
// prior to making the machine. The input param needs to be a valid cloud-config string. | ||
func Launch(name string, cpus int, memory, disk, input string) (*Action, error) { | ||
if name == "" { | ||
return nil, errors.New("the name of the machine cannot be empty") | ||
} | ||
if cpus == 0 { | ||
return nil, errors.New("the number of CPUs cannot be 0") | ||
} | ||
if err := validate.DiskSize(disk); err != nil { | ||
return nil, err | ||
} | ||
if err := validate.Memory(memory); err != nil { | ||
return nil, err | ||
} | ||
if input == "" { | ||
return nil, errors.New("input cannot be empty") | ||
} | ||
|
||
return &Action{ | ||
Type: "launch", | ||
UseSyscall: false, | ||
Input: input, | ||
Args: []string{"--name", name, "--cpus", strconv.Itoa(cpus), "--mem", memory, "--disk", disk, "--cloud-init", "-"}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package action | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestLaunch(t *testing.T) { | ||
type args struct { | ||
name string | ||
cpus int | ||
memory string | ||
disk string | ||
input string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *Action | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "can launch a new instance", | ||
args: args{ | ||
name: "machine", | ||
cpus: 4, | ||
memory: "2G", | ||
disk: "20G", | ||
input: "someinput", | ||
}, | ||
want: &Action{ | ||
Type: "launch", | ||
UseSyscall: false, | ||
Args: []string{"--name", "machine", "--cpus", "4", "--mem", "2G", "--disk", "20G", "--cloud-init", "-"}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "missing disk param returns an error", | ||
args: args{ | ||
name: "machine", | ||
cpus: 4, | ||
memory: "2G", | ||
input: "someinput", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing input param returns an error", | ||
args: args{ | ||
name: "machine", | ||
cpus: 4, | ||
memory: "2G", | ||
disk: "20G", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing cpus param returns an error", | ||
args: args{ | ||
name: "machine", | ||
memory: "2G", | ||
disk: "20G", | ||
input: "someinput", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing memory param returns an error", | ||
args: args{ | ||
name: "machine", | ||
cpus: 4, | ||
disk: "20G", | ||
input: "someinput", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing name param returns an error", | ||
args: args{ | ||
cpus: 4, | ||
memory: "2G", | ||
disk: "20G", | ||
input: "someinput", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := Launch(tt.args.name, tt.args.cpus, tt.args.memory, tt.args.disk, tt.args.input) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Launch() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Launch() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |