Skip to content

Commit

Permalink
#282: Project URLs in output. (#283)
Browse files Browse the repository at this point in the history
* Initial work for #282
* gometalinter adjustments
* Do not return and validate endpoints for exited containers
* adjustments to endpoint testing
* add pygmy status command
* Add stdout domain output validation for example projects
* grep string literal
* escape unsafe
* remove silverstripe stdout checks
  • Loading branch information
fubarhouse committed Jan 28, 2021
1 parent 8dde1a6 commit 22642df
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/pygmy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ jobs:
sleep 5;
curl --HEAD http://drupal8-example-simple.docker.amazee.io;
curl --HEAD http://drupal8-example-simple.docker.amazee.io | grep "X-LAGOON";
../../pygmy-go-linux-x86 --config ../../examples/pygmy.basic.yml status | grep '\- http://drupal8-example-simple.docker.amazee.io';
docker-compose -p drupal8-example-simple down;
docker-compose -p drupal8-example-simple rm;
cd ../../;
Expand All @@ -135,6 +136,7 @@ jobs:
sleep 5;
curl --HEAD http://drupal9-example-advanced.docker.amazee.io;
curl --HEAD http://drupal9-example-advanced.docker.amazee.io | grep "X-LAGOON";
../../pygmy-go-linux-x86 --config ../../examples/pygmy.basic.yml status | grep '\- http://drupal9-example-advanced.docker.amazee.io';
docker-compose -p drupal9-advanced down;
docker-compose -p drupal9-advanced rm;
cd ../../;
Expand All @@ -147,6 +149,7 @@ jobs:
sleep 5;
curl --HEAD http://drupal9-example-simple.docker.amazee.io;
curl --HEAD http://drupal9-example-simple.docker.amazee.io | grep "X-LAGOON";
../../pygmy-go-linux-x86 --config ../../examples/pygmy.basic.yml status | grep '\- http://drupal9-example-simple.docker.amazee.io';
docker-compose -p drupal-example-simple down;
docker-compose -p drupal-example-simple rm;
cd ../../;
Expand All @@ -158,6 +161,7 @@ jobs:
docker-compose -p node up -d;
curl --HEAD http://node.docker.amazee.io;
curl --HEAD http://node.docker.amazee.io | grep "X-LAGOON";
../../pygmy-go-linux-x86 --config ../../examples/pygmy.basic.yml status | grep '\- http://node.docker.amazee.io';
docker-compose -p node down;
docker-compose -p node rm;
cd ../../;
Expand Down Expand Up @@ -195,6 +199,7 @@ jobs:
sleep 5;
curl --HEAD http://wordpress-nginx.docker.amazee.io;
curl --HEAD http://wordpress-nginx.docker.amazee.io | grep "X-LAGOON";
../../pygmy-go-linux-x86 --config ../../examples/pygmy.basic.yml status | grep '\- http://wordpress-nginx.docker.amazee.io';
docker-compose -p wordpress-simple down;
docker-compose -p wordpress-simple rm;
cd ../../;
Expand Down
16 changes: 10 additions & 6 deletions service/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ func Validate(url string) bool {
// Housekeeping
defer resp.Body.Close()

// Check for the desired result
if resp.StatusCode == 200 || resp.StatusCode == 401 {
// Test passed!
return true
// The default response for a failed loopback is a 503.
// Because server errors are 503, we should make sure
// we do not get a 5xx response code from the endpoint.
// 500 is known to be a success as well, so start from 501.

// Check for known failure status response codes (failures):
if resp.StatusCode >= 501 && resp.StatusCode < 600 {
return false
}

// Test failed.
return false
// Test passed.
return true
}
12 changes: 12 additions & 0 deletions service/interface/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,18 @@ func DockerVolumeCreate(volume types.Volume) (types.Volume, error) {
})
}

// DockerInspect will return the full container object.
func DockerInspect(container string) (types.ContainerJSON, error) {
ctx := context.Background()
cli, err := client.NewClientWithOpts()
cli.NegotiateAPIVersion(ctx)
if err != nil {
return types.ContainerJSON{}, err
}

return cli.ContainerInspect(context.Background(), container)
}

// DockerExec will run a command in a Docker container and return the output.
func DockerExec(container string, command string) ([]byte, error) {
ctx := context.Background()
Expand Down
30 changes: 30 additions & 0 deletions service/library/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,34 @@ func Status(c Config) {
}
}

// List out all running projects to get their URL.
containers, _ := docker.DockerContainerList()
var urls []string
for _, container := range containers {
if container.State == "running" && !strings.Contains(fmt.Sprint(container.Names), "amazeeio") {
obj, _ := docker.DockerInspect(container.ID)
vars := obj.Config.Env
for _, v := range vars {
// Look for the environment variable $LAGOON_ROUTE.
if strings.Contains(v, "LAGOON_ROUTE=") {
url := strings.TrimPrefix(v, "LAGOON_ROUTE=")
if !strings.HasPrefix(url, "http") && !strings.HasPrefix(url, "https") {
url = "http://" + url
}
urls = append(urls, url)
}
}
}
}

cleanurls := unique(urls)
for _, url := range cleanurls {
endpoint.Validate(url)
if r := endpoint.Validate(url); r {
fmt.Printf(" - %v\n", url)
} else {
fmt.Printf(" ! %v\n", url)
}
}

}
30 changes: 30 additions & 0 deletions service/library/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,34 @@ func Up(c Config) {
}
}
}

// List out all running projects to get their URL.
containers, _ := docker.DockerContainerList()
var urls []string
for _, container := range containers {
if container.State == "running" && !strings.Contains(fmt.Sprint(container.Names), "amazeeio") {
obj, _ := docker.DockerInspect(container.ID)
vars := obj.Config.Env
for _, v := range vars {
// Look for the environment variable $LAGOON_ROUTE.
if strings.Contains(v, "LAGOON_ROUTE=") {
url := strings.TrimPrefix(v, "LAGOON_ROUTE=")
if !strings.HasPrefix(url, "http") && !strings.HasPrefix(url, "https") {
url = "http://" + url
}
urls = append(urls, url)
}
}
}
}

cleanurls := unique(urls)
for _, url := range cleanurls {
endpoint.Validate(url)
if r := endpoint.Validate(url); r {
fmt.Printf(" - %v\n", url)
} else {
fmt.Printf(" ! %v\n", url)
}
}
}

0 comments on commit 22642df

Please sign in to comment.