Browse Source

Update: copied execute script function and go.mod

master
Darshil Chanpura 2 years ago
parent
commit
f45fb16a19
  1. 6
      go.mod
  2. 23
      go.sum
  3. 6
      main.go
  4. 36
      utils/init.go

6
go.mod

@ -2,4 +2,8 @@ module github.com/dtchanpura/nginx-org-asg
go 1.13
require github.com/aws/aws-sdk-go v1.25.16
require (
github.com/aws/aws-sdk-go v1.25.43
github.com/stretchr/testify v1.4.0 // indirect
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 // indirect
)

23
go.sum

@ -1,6 +1,21 @@
github.com/aws/aws-sdk-go v1.25.8 h1:n7I+HUUXjun2CsX7JK+1hpRIkZrlKhd3nayeb+Xmavs=
github.com/aws/aws-sdk-go v1.25.8/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.25.16 h1:k7Fy6T/uNuLX6zuayU/TJoP7yMgGcJSkZpF7QVjwYpA=
github.com/aws/aws-sdk-go v1.25.16/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.25.43 h1:R5YqHQFIulYVfgRySz9hvBRTWBjudISa+r0C8XQ1ufg=
github.com/aws/aws-sdk-go v1.25.43/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 h1:e6HwijUxhDe+hPNjZQQn9bA5PW3vNmnN64U2ZW759Lk=
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

6
main.go

@ -13,7 +13,6 @@ import (
"text/template"
"time"
hookutils "github.com/dtchanpura/deployment-agent/utils"
"github.com/dtchanpura/nginx-org-asg/awsasg"
"github.com/dtchanpura/nginx-org-asg/models"
"github.com/dtchanpura/nginx-org-asg/utils"
@ -151,7 +150,10 @@ func checkChange(existingInstances []models.Instance) []models.Instance {
existingInstances = make([]models.Instance, len(newInstances))
copy(existingInstances, newInstances)
// Run Hooks
logger.Println(hookutils.ExecuteScript(".", "/Users/darshil/Projects/go/src/github.com/dtchanpura/nginx-org-asg/test.sh"))
err = utils.ExecuteScript(".", "/Users/darshil/Projects/go/src/github.com/dtchanpura/nginx-org-asg/test.sh")
if err != nil {
logger.Println(err)
}
}
return existingInstances
}

36
utils/init.go

@ -2,13 +2,28 @@ package utils
import (
"encoding/json"
"errors"
"log"
"os"
"os/exec"
"text/template"
"github.com/dtchanpura/nginx-org-asg/models"
)
var (
logger *log.Logger
)
const (
// ErrorFileNotExecutable message when the file is not executable
ErrorFileNotExecutable = "file is not executable"
)
func init() {
logger = log.New(os.Stdout, "utils: ", log.Ltime|log.Lshortfile)
}
// GetInstances for reading reference file and decoding it to currentInstances
func GetInstances(ref *os.File) ([]models.Instance, error) {
var currentInstances []models.Instance
@ -67,3 +82,24 @@ func ExecuteTemplate(filename string, tmpl *template.Template, instances []model
}
return nil
}
// ExecuteScript for executing script
func ExecuteScript(workdir string, execpath string, args ...string) error {
if fileInfo, err := os.Stat(execpath); !os.IsPermission(err) && !os.IsNotExist(err) && fileInfo.Mode()&0111 != 0 {
cmd := exec.Command(execpath, args...)
if dirInfo, err := os.Stat(workdir); err == nil && dirInfo.IsDir() {
cmd.Dir = workdir
} else {
log.Println(err.Error())
}
// err := cmd.Run()
outputBytes, err := cmd.CombinedOutput()
if err != nil {
logger.Printf("Command Error: %s\n", string(outputBytes[:]))
return err
}
logger.Printf("Command Output: %s\n", string(outputBytes[:]))
return nil
}
return errors.New(ErrorFileNotExecutable)
}

Loading…
Cancel
Save