From aea7d8c3a5ea25d7e6e2042a57a8e61280c6057e Mon Sep 17 00:00:00 2001 From: Radu Topala Date: Wed, 7 Dec 2022 20:09:58 +0200 Subject: [PATCH] updating utils, examples --- examples/simple/main.go | 6 +- examples/symfony/main.go | 2 +- recipes/deploy/cleanup.go | 29 --------- recipes/deploy/clear_paths.go | 21 ------- recipes/deploy/copy_dirs.go | 21 ------- recipes/deploy/defaults.go | 108 ---------------------------------- recipes/deploy/lock.go | 21 ------- recipes/deploy/prepare.go | 25 -------- recipes/deploy/release.go | 106 --------------------------------- recipes/deploy/rollback.go | 23 -------- recipes/deploy/shared.go | 56 ------------------ recipes/deploy/stage.go | 11 ---- recipes/deploy/symlink.go | 17 ------ recipes/deploy/update_code.go | 73 ----------------------- recipes/deploy/writable.go | 100 ------------------------------- recipes/php/defaults.go | 37 ------------ recipes/php/vendors.go | 10 ---- recipes/symfony/recipe.go | 31 ---------- utils.go | 46 +++++++++++---- 19 files changed, 40 insertions(+), 703 deletions(-) delete mode 100644 recipes/deploy/cleanup.go delete mode 100644 recipes/deploy/clear_paths.go delete mode 100644 recipes/deploy/copy_dirs.go delete mode 100644 recipes/deploy/defaults.go delete mode 100644 recipes/deploy/lock.go delete mode 100644 recipes/deploy/prepare.go delete mode 100644 recipes/deploy/release.go delete mode 100644 recipes/deploy/rollback.go delete mode 100644 recipes/deploy/shared.go delete mode 100644 recipes/deploy/stage.go delete mode 100644 recipes/deploy/symlink.go delete mode 100644 recipes/deploy/update_code.go delete mode 100644 recipes/deploy/writable.go delete mode 100644 recipes/php/defaults.go delete mode 100644 recipes/php/vendors.go delete mode 100644 recipes/symfony/recipe.go diff --git a/examples/simple/main.go b/examples/simple/main.go index 5a8f3c6..4cba1f7 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -181,7 +181,7 @@ func main() { exec. Task("onservers:a", func() { - exec.RunIfNoBinary("docker", []string{ + exec.RemoteRunIfNoBinary("docker", []string{ "echo 'a'", "echo 'b'", }) @@ -192,7 +192,7 @@ func main() { exec. Task("onservers:b", func() { - exec.RunIfNoBinary("wget", []string{ + exec.RemoteRunIfNoBinary("wget", []string{ "echo 'a'", "echo 'b'", }) @@ -203,7 +203,7 @@ func main() { exec. Task("onservers:c", func() { - exec.RunIfNoBinary("docker", []string{ + exec.RemoteRunIfNoBinary("docker", []string{ "echo 'a'", "echo 'b'", }) diff --git a/examples/symfony/main.go b/examples/symfony/main.go index 3c95356..1c5c190 100644 --- a/examples/symfony/main.go +++ b/examples/symfony/main.go @@ -2,7 +2,7 @@ package main import ( "github.com/go-exec/exec" - _ "github.com/go-exec/exec/recipes/symfony" + _ "github.com/go-exec/exec/examples/recipes/symfony" ) /* diff --git a/recipes/deploy/cleanup.go b/recipes/deploy/cleanup.go deleted file mode 100644 index cc59a9c..0000000 --- a/recipes/deploy/cleanup.go +++ /dev/null @@ -1,29 +0,0 @@ -package deploy - -import ( - "fmt" - "github.com/go-exec/exec" -) - -func init() { - exec := exec.Instance - exec.Task("cleanup", func() { - releases := exec.Get("releases_list").Slice() - keep := exec.Get("keep_releases").Int() - - if keep <= 0 || len(releases)+1 <= keep { - exec.Println("No cleanup needed.") - return - } else { - releases = releases[keep-1:] - } - - // releases to be deleted, old ones - for _, release := range releases { - exec.Remote(fmt.Sprintf("rm -rf {{deploy_path}}/releases/%s", release)) - } - - exec.Remote("cd {{deploy_path}} && if [ -e release ]; then rm release; fi") - exec.Remote("cd {{deploy_path}} && if [ -h release ]; then rm release; fi") - }).ShortDescription("Cleaning up old releases") -} diff --git a/recipes/deploy/clear_paths.go b/recipes/deploy/clear_paths.go deleted file mode 100644 index 28c1081..0000000 --- a/recipes/deploy/clear_paths.go +++ /dev/null @@ -1,21 +0,0 @@ -package deploy - -import ( - "fmt" - "github.com/go-exec/exec" -) - -func init() { - exec := exec.Instance - exec.Task("deploy:clear_paths", func() { - paths := exec.Get("clear_paths").Slice() - sudo := "" - if exec.Get("clear_use_sudo").Bool() { - sudo = "sudo" - } - - for _, path := range paths { - exec.Remote(fmt.Sprintf("%s rm -rf {{release_path}}/%s", sudo, path)) - } - }).ShortDescription("Cleaning up files and/or directories") -} diff --git a/recipes/deploy/copy_dirs.go b/recipes/deploy/copy_dirs.go deleted file mode 100644 index 9cfa245..0000000 --- a/recipes/deploy/copy_dirs.go +++ /dev/null @@ -1,21 +0,0 @@ -package deploy - -import ( - "fmt" - "github.com/go-exec/exec" -) - -func init() { - exec := exec.Instance - exec.Task("deploy:copy_dirs", func() { - dirs := exec.Get("copy_dirs").Slice() - - for _, dir := range dirs { - // Delete directory if exists. - exec.Remote(fmt.Sprintf("if [ -d $(echo {{release_path}}/%s) ]; then rm -rf {{release_path}}/%s; fi", dir, dir)) - - // Copy directory. - exec.Remote(fmt.Sprintf("if [ -d $(echo {{deploy_path}}/current/%s) ]; then cp -rpf {{deploy_path}}/current/%s {{release_path}}%s; fi", dir, dir, dir)) - } - }).ShortDescription("Copy directories") -} diff --git a/recipes/deploy/defaults.go b/recipes/deploy/defaults.go deleted file mode 100644 index 575bae6..0000000 --- a/recipes/deploy/defaults.go +++ /dev/null @@ -1,108 +0,0 @@ -package deploy - -import ( - "fmt" - e "github.com/go-exec/exec" - "regexp" - "strconv" - "strings" - "time" -) - -func init() { - exec := e.Instance - exec.Set("keep_releases", 5) - - exec.Set("repository", "") // Repository to deploy. - exec.Set("branch", "") // Branch to deploy. - exec.Set("tag", "") // Tag to deploy. - - exec.Set("shared_dirs", []string{}) - exec.Set("shared_files", []string{}) - - exec.Set("copy_dirs", []string{}) - - exec.Set("writable_dirs", []string{}) - exec.Set("writable_mode", "acl") // chmod, chown, chgrp or acl. - exec.Set("writable_use_sudo", false) // Using sudo in writable commands? - exec.Set("writable_chmod_mode", "0755") // For chmod mode - - exec.Set("clear_paths", []string{}) // Relative path from deploy_path - exec.Set("clear_use_sudo", false) // Using sudo in clean commands? - - exec.Set("use_relative_symlink", true) - - exec.Set("git_cache", func() interface{} { //whether to use git cache - faster cloning by borrowing objects from existing clones. - gitVersion := exec.Remote("{{bin/git}} version").String() - version := "1.0.0" - - re := regexp.MustCompile(`((\d+\.?)+)`) - if re.MatchString(gitVersion) { - version = re.FindStringSubmatch(gitVersion)[0] - } - - versionS := strings.Split(version, ".") - - i1, _ := strconv.Atoi(versionS[0]) - i2, _ := strconv.Atoi(versionS[1]) - - if i1 >= 2 && i2 >= 3 { - return true - } else { - return false - } - }) - - exec.Set("bin/git", func() interface{} { - return exec.Remote("which git").String() - }) - - exec.Set("bin/symlink", func() interface{} { - if exec.Get("use_relative_symlink").Bool() { - // Check if target system supports relative symlink. - if exec.Remote("if [[ \"$(man ln 2>/dev/null)\" =~ \"--relative\" ]]; then echo 'true'; fi").Bool() { - return "ln -nfs --relative" - } - } - return "ln -nfs" - }) - - branch := exec.NewOption("branch", "Branch to deploy") - branch.Type = e.String - exec.AddOption(branch) - - tag := exec.NewOption("tag", "Tag to deploy") - tag.Type = e.String - exec.AddOption(tag) - - revision := exec.NewOption("revision", "Revision to deploy") - revision.Type = e.String - exec.AddOption(revision) - - exec.Task("current", func() { - exec.Println("Current release: {{current_path}}") - }) - - /** - * Success message - */ - exec.Task("success", func() { - exec.Println("Successfully deployed!") - }).Private() - - /** - * Deploy failure - */ - exec.Task("deploy:failed", func() { - }).Private() - - exec.Task("onStart", func() { - exec.Println("Start") - exec.Set("startTime", time.Now()) - }).Once().Private() - - exec.Task("onEnd", func() { - exec.Println(fmt.Sprintf("Finished in %s!", time.Since(exec.Get("startTime").Time()).String())) - exec.Println("End") - }).Once().Private() -} diff --git a/recipes/deploy/lock.go b/recipes/deploy/lock.go deleted file mode 100644 index a3d53d8..0000000 --- a/recipes/deploy/lock.go +++ /dev/null @@ -1,21 +0,0 @@ -package deploy - -import "github.com/go-exec/exec" - -func init() { - exec := exec.Instance - exec.Task("deploy:lock", func() { - locked := exec.Remote("if [ -f {{deploy_path}}/.dep/deploy.lock ]; then echo 'true'; fi").Bool() - - if locked { - exec.Println("Deploy locked.\nRun deploy:unlock command to unlock.") - return - } else { - exec.Remote("touch {{deploy_path}}/.dep/deploy.lock") - } - }).ShortDescription("Lock deploy") - - exec.Task("deploy:unlock", func() { - exec.Remote("rm {{deploy_path}}/.dep/deploy.lock") - }).ShortDescription("Unlock deploy") -} diff --git a/recipes/deploy/prepare.go b/recipes/deploy/prepare.go deleted file mode 100644 index 74d6a97..0000000 --- a/recipes/deploy/prepare.go +++ /dev/null @@ -1,25 +0,0 @@ -package deploy - -import "github.com/go-exec/exec" - -func init() { - exec := exec.Instance - exec.Task("deploy:prepare", func() { - exec.Remote("if [ ! -d {{deploy_path}} ]; then mkdir -p {{deploy_path}}; fi") - - // Check for existing /current directory (not symlink) - result := exec.Remote("if [ ! -L {{deploy_path}}/current ] && [ -d {{deploy_path}}/current ]; then echo true; fi").Bool() - if result { - exec.Println("There already is a directory (not symlink) named `current` in {{deploy_path}}. Remove this directory so it can be replaced with a symlink for atomic deployments.") - } - - // Create metadata .dep dir. - exec.Remote("cd {{deploy_path}} && if [ ! -d .dep ]; then mkdir .dep; fi") - - // Create releases dir. - exec.Remote("cd {{deploy_path}} && if [ ! -d releases ]; then mkdir releases; fi") - - // Create shared dir. - exec.Remote("cd {{deploy_path}} && if [ ! -d shared ]; then mkdir shared; fi") - }).ShortDescription("Preparing server for deploy") -} diff --git a/recipes/deploy/release.go b/recipes/deploy/release.go deleted file mode 100644 index c7f9a73..0000000 --- a/recipes/deploy/release.go +++ /dev/null @@ -1,106 +0,0 @@ -package deploy - -import ( - "fmt" - "github.com/go-exec/exec" - "regexp" - "strconv" - "strings" -) - -func init() { - exec := exec.Instance - exec.Set("keep_releases", -1) - - exec.Set("release_name", func() interface{} { - list := exec.Get("releases_list").Slice() - var max int = 0 - - for _, lv := range list { - if value, err := strconv.Atoi(lv); err == nil && value > max { - max = value - } - } - - return strconv.Itoa(max + 1) - }) - - /** - * Return list of releases on server. - */ - exec.Set("releases_list", func() interface{} { - exec.Cd("{{deploy_path}}") - - // If there is no releases return empty list. - if !exec.Remote("[ -d releases ] && [ \"$(ls -A releases)\" ] && echo \"true\" || echo \"false\"").Bool() { - return []string{} - } - - // Will list only dirs in releases. - re := regexp.MustCompile(`[\d]+/`) - list := exec.Remote("cd releases && ls -t -d */ -1").Slice("\n") - for k, lv := range list { - lv = strings.TrimSpace(lv) - if re.MatchString(lv) { - list[k] = strings.TrimRight(lv, "/") - } - } - - releases := []string{} // Releases list. - - // Collect releases based on .dep/releases info. - // Other will be ignored. - if exec.Remote("if [ -f .dep/releases ]; then echo \"true\"; fi").Bool() { - meta := exec.Remote("cat .dep/releases").Slice("\n") - - for _, lv := range list { - for _, mv := range meta { - vs := strings.Split(strings.TrimSpace(mv), ",") - release := vs[1] - - if lv == release { - releases = append(releases, release) - } - } - } - } - - return releases - }) - - exec.Set("release_path", func() interface{} { - if !exec.Remote("if [ -h {{deploy_path}}/release ]; then echo 'true'; fi").Bool() { - exec.Println("Release path does not found.\n" + - "Run deploy:release to create a new release.") - return nil - } - - link := exec.Remote("readlink {{deploy_path}}/release").String() - - if strings.HasPrefix(link, "/") { - return link - } else { - return exec.Get("deploy_path").String() + "/" + link - } - }) - - exec.Task("deploy:release", func() { - exec.Cd("{{deploy_path}}") - - // Clean up if there is unfinished release. - if exec.Remote("if [ -h release ]; then echo 'true'; fi").Bool() { - exec.Remote("rm -rf \"$(readlink release)\"") // Delete release. - exec.Remote("rm release") // Delete symlink. - } - - releasePath := exec.Parse("{{deploy_path}}/releases/{{release_name}}") - - // Metainfo. - // Save metainfo about release. - exec.Remote("echo `%s`,{{release_name}} >> .dep/releases", "date +\"%Y%m%d%H%M%S\"") - - // Make new release. - exec.Remote(fmt.Sprintf("mkdir %s", releasePath)) - exec.Remote(fmt.Sprintf("{{bin/symlink}} %s {{deploy_path}}/release", releasePath)) - }) -} diff --git a/recipes/deploy/rollback.go b/recipes/deploy/rollback.go deleted file mode 100644 index 94b520d..0000000 --- a/recipes/deploy/rollback.go +++ /dev/null @@ -1,23 +0,0 @@ -package deploy - -import ( - "fmt" - "github.com/go-exec/exec" -) - -func init() { - exec := exec.Instance - exec.Task("rollback", func() { - releases := exec.Get("releases_list").Slice() - - if len(releases) > 1 { - // Symlink to old release. - exec.Remote(fmt.Sprintf("cd {{deploy_path}} && {{bin/symlink}} {{deploy_path}}/releases/%s current", releases[1])) - - // Remove release - exec.Remote(fmt.Sprintf("rm -rf {{deploy_path}}/releases/%s", releases[0])) - - exec.Println(fmt.Sprintf("Rollback to `%s` release was successful.", releases[1])) - } - }).ShortDescription("Rollback to previous release") -} diff --git a/recipes/deploy/shared.go b/recipes/deploy/shared.go deleted file mode 100644 index de2e4e3..0000000 --- a/recipes/deploy/shared.go +++ /dev/null @@ -1,56 +0,0 @@ -package deploy - -import ( - "fmt" - "github.com/go-exec/exec" - "path" - "strings" -) - -func init() { - exec := exec.Instance - exec.Task("deploy:shared", func() { - sharedPath := "{{deploy_path}}/shared" - - for _, dir := range exec.Get("shared_dirs").Slice() { - // Create shared dir if it does not exist. - exec.Remote(fmt.Sprintf("mkdir -p $sharedPath/%s", dir)) - - // Copy shared dir files if they does not exist. - exec.Remote(fmt.Sprintf("if [ -d $(echo {{release_path}}/%s) ]; then cp -rn {{release_path}}/%s %s; fi", dir, dir, sharedPath)) - - // Remove from source. - exec.Remote(fmt.Sprintf("if [ -d $(echo {{release_path}}/%s) ]; then rm -rf {{release_path}}/%s; fi", dir, dir)) - - // Create path to shared dir in release dir if it does not exist. - // (symlink will not create the path and will fail otherwise) - exec.Remote(fmt.Sprintf("mkdir -p `dirname {{release_path}}/%s`", dir)) - - // Symlink shared dir to release dir - exec.Remote(fmt.Sprintf("{{bin/symlink}} %s/%s {{release_path}}/%s", sharedPath, dir, dir)) - } - - for _, file := range exec.Get("shared_files").Slice() { - dir := path.Dir(file) - i := strings.LastIndex(dir, "/") - - dirname := dir[i+1:] - - // Remove from source. - exec.Remote(fmt.Sprintf("if [ -f $(echo {{release_path}}/%s) ]; then rm -rf {{release_path}}/%s; fi", file, file)) - - // Ensure dir is available in release - exec.Remote(fmt.Sprintf("if [ ! -d $(echo {{release_path}}/%s) ]; then mkdir -p {{release_path}}/%s;fi", dirname, dirname)) - - // Create dir of shared file - exec.Remote(fmt.Sprintf("mkdir -p %s/%s", sharedPath, dirname)) - - // Touch shared - exec.Remote(fmt.Sprintf("touch %s/%s", sharedPath, file)) - - // Symlink shared dir to release dir - exec.Remote(fmt.Sprintf("{{bin/symlink}} %s/%s {{release_path}}/%s", sharedPath, file, file)) - } - }).ShortDescription("Creating symlinks for shared files and dirs") - -} diff --git a/recipes/deploy/stage.go b/recipes/deploy/stage.go deleted file mode 100644 index 23cbf93..0000000 --- a/recipes/deploy/stage.go +++ /dev/null @@ -1,11 +0,0 @@ -package deploy - -import "github.com/go-exec/exec" - -func init() { - exec := exec.Instance - stage := exec.NewArgument("stage", "Provide the running stage") - stage.Default = "qa" - - exec.AddArgument(stage) -} diff --git a/recipes/deploy/symlink.go b/recipes/deploy/symlink.go deleted file mode 100644 index ec2ace7..0000000 --- a/recipes/deploy/symlink.go +++ /dev/null @@ -1,17 +0,0 @@ -package deploy - -import "github.com/go-exec/exec" - -func init() { - exec := exec.Instance - exec.Task("deploy:symlink", func() { - if exec.Remote("if [[ \"$(man mv 2>/dev/null)\" =~ '--no-target-directory' ]]; then echo 'true'; fi").Bool() { - exec.Remote("mv -T {{deploy_path}}/release {{deploy_path}}/current") - } else { - // Atomic symlink does not supported. - // Will use simple two steps switch. - exec.Remote("cd {{deploy_path}} && {{bin/symlink}} {{release_path}} current") // Atomic override symlink. - exec.Remote("cd {{deploy_path}} && rm release") // Remove release link. - } - }).ShortDescription("Creating symlink to release") -} diff --git a/recipes/deploy/update_code.go b/recipes/deploy/update_code.go deleted file mode 100644 index c6e8446..0000000 --- a/recipes/deploy/update_code.go +++ /dev/null @@ -1,73 +0,0 @@ -package deploy - -import ( - "fmt" - "github.com/go-exec/exec" -) - -func init() { - exec := exec.Instance - exec.Task("deploy:update_code", func() { - repository := exec.Get("repository").String() - branch := exec.Get("branch").String() - tag := exec.Get("tag").String() - git := exec.Get("bin/git").String() - gitCache := exec.Get("git_cache").Bool() - depth := "" - at := "" - if !gitCache { - depth = "--depth 1" - } - - // If option `branch` is set. - if exec.TaskContext.HasOption("branch") { - inputBranch := exec.TaskContext.GetOption("branch").String() - if inputBranch != "" { - branch = inputBranch - } - } - - // Branch may come from option or from configuration. - if branch != "" { - at = "-b " + branch - } - - // If option `tag` is set - if exec.TaskContext.HasOption("tag") { - inputTag := exec.TaskContext.GetOption("tag").String() - if inputTag != "" { - tag = inputTag - } - } - - // Tag may come from option or from configuration. - if tag != "" { - at = "-b " + tag - } - - // If option `tag` is not set and option `revision` is set - revision := "" - if tag == "" && exec.TaskContext.HasOption("revision") { - revision = exec.TaskContext.GetOption("revision").String() - if revision != "" { - depth = "" - } - } - - releases := exec.Get("releases_list").Slice() - - if gitCache && len(releases) > 0 { - if exec.Remote(fmt.Sprintf("%s clone %s --recursive -q --reference {{deploy_path}}/releases/%s --dissociate %s {{release_path}}", git, at, releases[0], repository)).HasError() { - // If {{deploy_path}}/releases/{$releases[0]} has a failed git clone, is empty, shallow etc, git would throw error and give up. So we're forcing it to act without reference in this situation - exec.Remote(fmt.Sprintf("%s clone %s --recursive -q %s {{release_path}}", git, at, repository)) - } - } else { - // if we're using git cache this would be identical to above code in catch - full clone. If not, it would create shallow clone. - exec.Remote(fmt.Sprintf("%s clone %s %s --recursive -q %s {{release_path}}", git, at, depth, repository)) - } - - if revision != "" { - exec.Remote(fmt.Sprintf("cd {{release_path}} && %s checkout %s", git, revision)) - } - }).ShortDescription("Update code") -} diff --git a/recipes/deploy/writable.go b/recipes/deploy/writable.go deleted file mode 100644 index c06ac89..0000000 --- a/recipes/deploy/writable.go +++ /dev/null @@ -1,100 +0,0 @@ -package deploy - -import ( - "fmt" - "github.com/go-exec/exec" - "log" - "strings" -) - -func init() { - exec := exec.Instance - exec.Task("deploy:writable", func() { - dirs := strings.Join(exec.Get("writable_dirs").Slice(), " ") - mode := exec.Get("writable_mode").String() - sudo := "" - if exec.Get("writable_use_sudo").Bool() { - sudo = "sudo" - } - - httpUser := exec.Get("http_user").String() - - if dirs == "" { - return - } - - if httpUser == "" && mode != "chmod" { - // Detect http user in process list. - httpUser = exec.Remote("ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\\ -f1").String() - - if httpUser == "" { - log.Panicln("Can't detect http user name.\n Please setup `http_user` config parameter.") - } - } - - //try { - exec.Cd("{{release_path}}") - - if mode == "chown" { - // Change owner. - // -R operate on files and directories recursively - // -L traverse every symbolic link to a directory encountered - exec.Remote(fmt.Sprintf("%s chown -RL %s %s", sudo, httpUser, dirs)) - } else if mode == "chgrp" { - // Change group ownership. - // -R operate on files and directories recursively - // -L if a command line argument is a symbolic link to a directory, traverse it - httpGroup := exec.Get("http_group").String() - if httpGroup == "" { - log.Panicln("Please setup `http_group` config parameter.") - } - exec.Remote(fmt.Sprintf("%s chgrp -RH %s %s", sudo, httpGroup, dirs)) - } else if mode == "chmod" { - exec.Remote(fmt.Sprintf("%s chmod -R {{writable_chmod_mode}} %s", sudo, dirs)) - } else if mode == "acl" { - if strings.Contains(exec.Remote("chmod 2>&1; true").String(), "+a") { - // Try OS-X specific setting of access-rights - - exec.Remote(fmt.Sprintf("%s chmod +a \"%s allow delete,write,append,file_inherit,directory_inherit\" %s", sudo, httpUser, dirs)) - exec.Remote(fmt.Sprintf("%s chmod +a \"`whoami` allow delete,write,append,file_inherit,directory_inherit\" %s", sudo, dirs)) - } else if exec.CommandExist("setfacl") { - if sudo != "" { - exec.Remote(fmt.Sprintf("%s setfacl -R -m u:\"%s\":rwX -m u:`whoami`:rwX %s", sudo, httpUser, dirs)) - exec.Remote(fmt.Sprintf("%s setfacl -dR -m u:\"%s\":rwX -m u:`whoami`:rwX %s", sudo, httpUser, dirs)) - } else { - // When running without sudo, exception may be thrown - // if executing setfacl on files created by http user (in directory that has been setfacl before). - // These directories/files should be skipped. - // Now, we will check each directory for ACL and only setfacl for which has not been set before. - writeableDirs := exec.Get("writable_dirs").Slice() - for _, dir := range writeableDirs { - // Check if ACL has been set or not - hasfacl := exec.Remote(fmt.Sprintf("getfacl -p %s | grep \"^user:%s:.*w\" | wc -l", dir, httpUser)).Bool() - // Set ACL for directory if it has not been set before - if !hasfacl { - exec.Remote(fmt.Sprintf("setfacl -R -m u:\"%s\":rwX -m u:`whoami`:rwX %s", httpUser, dir)) - exec.Remote(fmt.Sprintf("setfacl -dR -m u:\"%s\":rwX -m u:`whoami`:rwX %s", httpUser, dir)) - } - } - } - } else { - log.Panicln("Cant't set writable dirs with ACL.") - } - } else { - log.Panicln("Unknown writable_mode `$mode`.") - } - /*} catch (\RuntimeException $e) { - $formatter = exec::get()->getHelper('formatter'); - - $errorMessage = [ - "Unable to setup correct permissions for writable dirs. ", - "You need to configure sudo's sudoers files to not prompt for password,", - "or setup correct permissions manually. ", - ]; - write($formatter->formatBlock($errorMessage, 'error', true)); - - throw $e; - }*/ - }).ShortDescription("Make writable dirs") - -} diff --git a/recipes/php/defaults.go b/recipes/php/defaults.go deleted file mode 100644 index 3608be8..0000000 --- a/recipes/php/defaults.go +++ /dev/null @@ -1,37 +0,0 @@ -package php - -import ( - "github.com/go-exec/exec" - _ "github.com/go-exec/exec/recipes/deploy" -) - -func init() { - exec := exec.Instance - exec.Set("http_user", false) - exec.Set("http_group", false) - - exec.Set("composer_action", "install") - exec.Set("composer_options", "{{composer_action}} --verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloader") - - exec.Set("env_vars", "") // Variable assignment before cmds (for example, SYMFONY_ENV={{set}}) - - exec.Set("bin/php", func() interface{} { - return exec.Remote("which php").String() - }) - - exec.Set("bin/composer", func() interface{} { - var composer string - - if exec.CommandExist("composer") { - composer = exec.Remote("which composer").String() - } - - if composer == "" { - exec.Remote("cd {{release_path}} && curl -sS https://getcomposer.org/installer | {{bin/php}}") - composer = "{{bin/php}} {{release_path}}/composer.phar" - } - - return composer - }) - -} diff --git a/recipes/php/vendors.go b/recipes/php/vendors.go deleted file mode 100644 index 5fbc5f1..0000000 --- a/recipes/php/vendors.go +++ /dev/null @@ -1,10 +0,0 @@ -package php - -import "github.com/go-exec/exec" - -func init() { - exec := exec.Instance - exec.Task("deploy:vendors", func() { - exec.Remote("cd {{release_path}} && {{env_vars}} {{bin/composer}} {{composer_options}}") - }).ShortDescription("Installing vendors") -} diff --git a/recipes/symfony/recipe.go b/recipes/symfony/recipe.go deleted file mode 100644 index f542177..0000000 --- a/recipes/symfony/recipe.go +++ /dev/null @@ -1,31 +0,0 @@ -package symfony - -import ( - "github.com/go-exec/exec" - _ "github.com/go-exec/exec/recipes/php" -) - -func init() { - exec := exec.Instance - exec. - TaskGroup( - "deploy", - "deploy:prepare", - "deploy:lock", - "deploy:release", - "deploy:update_code", - "deploy:clear_paths", - //"deploy:create_cache_dir", - "deploy:shared", - //"deploy:assets", - //"deploy:vendors", - //"deploy:assets:install", - //"deploy:assetic:dump", - //"deploy:cache:warmup", - //"deploy:writable", - "deploy:symlink", - "deploy:unlock", - "cleanup", - ). - ShortDescription("Deploy code") -} diff --git a/utils.go b/utils.go index 927f737..9f2a12a 100644 --- a/utils.go +++ b/utils.go @@ -48,29 +48,55 @@ func (e *Exec) Parse(text string) string { }) } -// RunIfNoBinary runs a remote command if a binary is not found +// RemoteRunIfNoBinary runs a remote command if a binary is not found // command can be an array of string commands or one a string command -func (e *Exec) RunIfNoBinary(binary string, command interface{}) (o Output) { +func (e *Exec) RemoteRunIfNoBinary(binary string, command interface{}) (o Output) { return e.Remote("if [ ! -e \"`which %s`\" ]; then %s; fi", binary, commandToString(command)) } -// RunIfNoBinaries runs multiple RunIfNoBinary -func (e *Exec) RunIfNoBinaries(config map[string]interface{}) { +// LocalRunIfNoBinary runs a local command if a binary is not found +// command can be an array of string commands or one a string command +func (e *Exec) LocalRunIfNoBinary(binary string, command interface{}) (o Output) { + return e.Local("if [ ! -e \"`which %s`\" ]; then %s; fi", binary, commandToString(command)) +} + +// RemoteRunIfNoBinaries runs multiple RemoteRunIfNoBinary +func (e *Exec) RemoteRunIfNoBinaries(config map[string]interface{}) { + for binary, command := range config { + e.RemoteRunIfNoBinary(binary, command) + } +} + +// LocalRunIfNoBinaries runs multiple LocalRunIfNoBinary +func (e *Exec) LocalRunIfNoBinaries(config map[string]interface{}) { for binary, command := range config { - e.RunIfNoBinary(binary, command) + e.LocalRunIfNoBinary(binary, command) } } -// RunIf runs a remote command if condition is true +// RemoteRunIf runs a remote command if condition is true // command can be an array of string commands or one a string command -func (e *Exec) RunIf(condition string, command interface{}) (o Output) { +func (e *Exec) RemoteRunIf(condition string, command interface{}) (o Output) { return e.Remote("if %s; then %s; fi", condition, commandToString(command)) } -// RunIfs runs multiple RunIf -func (e *Exec) RunIfs(config map[string]interface{}) { +// LocalRunIf runs a local command if condition is true +// command can be an array of string commands or one a string command +func (e *Exec) LocalRunIf(condition string, command interface{}) (o Output) { + return e.Local("if %s; then %s; fi", condition, commandToString(command)) +} + +// RemoteRunIfs runs multiple RemoteRunIf +func (e *Exec) RemoteRunIfs(config map[string]interface{}) { + for condition, command := range config { + e.RemoteRunIf(condition, command) + } +} + +// LocalRunIfs runs multiple LocalRunIf +func (e *Exec) LocalRunIfs(config map[string]interface{}) { for condition, command := range config { - e.RunIf(condition, command) + e.LocalRunIf(condition, command) } }