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
fd68b28
commit 0bd44d4
Showing
4 changed files
with
137 additions
and
27 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
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 |
---|---|---|
@@ -1,9 +1,61 @@ | ||
package nitro | ||
|
||
func AddSiteScript(name, domain, php, dir string) Command { | ||
import "fmt" | ||
|
||
func CreateNewDirectoryForSite(name, site string) Command { | ||
return Command{ | ||
Machine: name, | ||
Type: "exec", | ||
Chainable: true, | ||
Args: []string{name, "--", "mkdir", "-p", "/home/ubuntu/sites/" + site}, | ||
} | ||
} | ||
|
||
func CopyNginxTemplate(name, site string) Command { | ||
return Command{ | ||
Machine: name, | ||
Type: "exec", | ||
Chainable: true, | ||
Args: []string{name, "--", "sudo", "cp", "/opt/nitro/nginx/template.conf", "/etc/nginx/sites-available/" + site}, | ||
} | ||
} | ||
|
||
func ChangeVariablesInTemplate(name, domain, dir, php string) []Command { | ||
var commands []Command | ||
|
||
commands = append(commands, changeVariableInNginxTemplate(name, domain, "CHANGEPATH", domain)) | ||
commands = append(commands, changeVariableInNginxTemplate(name, domain, "CHANGESERVERNAME", domain)) | ||
commands = append(commands, changeVariableInNginxTemplate(name, domain, "CHANGEPUBLICDIR", dir)) | ||
commands = append(commands, changeVariableInNginxTemplate(name, domain, "CHANGEPHPVERSION", php)) | ||
|
||
return commands | ||
} | ||
|
||
func changeVariableInNginxTemplate(name, site, variable, actual string) Command { | ||
file := fmt.Sprintf("/etc/nginx/sites-available/%v", site) | ||
sedCmd := "s|" + variable + "|" + actual + "|g" | ||
return Command{ | ||
Machine: name, | ||
Type: "exec", | ||
Chainable: true, | ||
Args: []string{name, "--", "sudo", "sed", "-i", sedCmd, file}, | ||
} | ||
} | ||
|
||
func LinkNginxSite(name, site string) Command { | ||
return Command{ | ||
Machine: name, | ||
Type: "exec", | ||
Chainable: true, | ||
Args: []string{name, "--", "sudo", "ln", "-s", "/etc/nginx/sites-available/" + site, "/etc/nginx/sites-enabled/"}, | ||
} | ||
} | ||
|
||
func ReloadNginx(name string) Command { | ||
return Command{ | ||
Machine: name, | ||
Type: "exec", | ||
Args: []string{"--", "sudo", "bash", "/opt/nitro/nginx/add-site.sh", domain, php, dir}, | ||
Machine: name, | ||
Type: "exec", | ||
Chainable: true, | ||
Args: []string{name, "--", "sudo", "service", "nginx", "restart"}, | ||
} | ||
} |
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,58 @@ | ||
package nitro | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_changePathInNginxPath(t *testing.T) { | ||
type args struct { | ||
name string | ||
site string | ||
variable string | ||
actual string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want Command | ||
}{ | ||
{ | ||
name: "returns the correct sed site", | ||
args: args{ | ||
name: "machine-name", | ||
site: "example.test", | ||
variable: "CHANGEPATH", | ||
actual: "example.test", | ||
}, | ||
want: Command{ | ||
Machine: "machine-name", | ||
Type: "exec", | ||
Chainable: true, | ||
Args: []string{"machine-name", "--", "sudo", "sed", "-i", "s|CHANGEPATH|example.test|g", "/etc/nginx/sites-available/example.test"}, | ||
}, | ||
}, | ||
{ | ||
name: "returns the correct sed php version", | ||
args: args{ | ||
name: "machine-name", | ||
site: "another-site.test", | ||
variable: "CHANGEPHPVERSION", | ||
actual: "7.4", | ||
}, | ||
want: Command{ | ||
Machine: "machine-name", | ||
Type: "exec", | ||
Chainable: true, | ||
Args: []string{"machine-name", "--", "sudo", "sed", "-i", "s|CHANGEPHPVERSION|7.4|g", "/etc/nginx/sites-available/another-site.test"}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := changeVariableInNginxTemplate(tt.args.name, tt.args.site, tt.args.variable, tt.args.actual); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("changeVariableInNginxTemplate() = \n%v, \nwant:\n %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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package nitro | ||
|
||
func Mount(name, path, domain string) Command { | ||
func Mount(name, folder, domain string) Command { | ||
return Command{ | ||
Machine: name, | ||
Type: "mount", | ||
Args: []string{path, name + ":/app/sites/" + domain}, | ||
Machine: name, | ||
Chainable: true, | ||
Type: "mount", | ||
Args: []string{folder, name + ":/app/sites/" + domain}, | ||
} | ||
} |