-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from BashtonLtd/dwatson/unify
Unify AWS and GCE branches.
- Loading branch information
Showing
3,936 changed files
with
3,532,352 additions
and
113 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
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 |
---|---|---|
@@ -1,6 +1,17 @@ | ||
sudo: required | ||
|
||
language: go | ||
|
||
go: | ||
- 1.5 | ||
- 1.6 | ||
- 1.7 | ||
- tip | ||
- 1.7.x | ||
- tip | ||
|
||
services: | ||
- docker | ||
|
||
install: | ||
- go get github.com/Masterminds/glide | ||
|
||
script: | ||
- glide install | ||
- go test $(glide nv -x) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,18 @@ | ||
package: github.com/BashtonLtd/varnish-purge-proxy | ||
import: | ||
- package: github.com/aws/aws-sdk-go | ||
version: ^1.6.18 | ||
subpackages: | ||
- aws | ||
- aws/ec2metadata | ||
- aws/session | ||
- service/ec2 | ||
- package: gopkg.in/alecthomas/kingpin.v1 | ||
version: ^2.2.3 | ||
- package: cloud.google.com/go | ||
version: ^0.6.0 | ||
subpackages: | ||
- compute/ | ||
- package: google.golang.org/api | ||
subpackages: | ||
- compute |
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,99 @@ | ||
package providers | ||
|
||
/* | ||
* varnish-purge-proxy | ||
* (C) Copyright Bashton Ltd, 2014 | ||
* | ||
* varnish-purge-proxy is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* varnish-purge-proxy is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with varnish-purge-proxy. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/ec2metadata" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
) | ||
|
||
// AWSProvider struct | ||
type AWSProvider struct { | ||
Service *ec2.EC2 | ||
Tags []string | ||
Debug bool | ||
} | ||
|
||
// Auth takes config values and configures this service | ||
func (a *AWSProvider) Auth() error { | ||
region, err := ec2metadata.New(session.New()).Region() | ||
if err != nil { | ||
log.Printf("Unable to retrieve the region from the EC2 instance %v\n", err) | ||
return err | ||
} | ||
|
||
// Set up access to ec2 | ||
svc := ec2.New(session.New(), &aws.Config{Region: ®ion}) | ||
|
||
a.Service = svc | ||
return nil | ||
} | ||
|
||
// GetPrivateIPs returns the IPs of instances matching specific tags | ||
func (a *AWSProvider) GetPrivateIPs() []string { | ||
instances := []string{} | ||
filters, err := a.buildFilter() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
|
||
request := ec2.DescribeInstancesInput{Filters: filters} | ||
result, err := a.Service.DescribeInstances(&request) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
|
||
for _, reservation := range result.Reservations { | ||
for _, instance := range reservation.Instances { | ||
if instance.PrivateIpAddress != nil { | ||
if a.Debug { | ||
log.Printf("Adding %s to IP list\n", *instance.PrivateIpAddress) | ||
} | ||
instances = append(instances, *instance.PrivateIpAddress) | ||
} | ||
} | ||
} | ||
|
||
return instances | ||
} | ||
|
||
func (a *AWSProvider) buildFilter() ([]*ec2.Filter, error) { | ||
filters := []*ec2.Filter{} | ||
|
||
for _, tag := range a.Tags { | ||
parts := strings.SplitN(tag, ":", 2) | ||
if len(parts) != 2 { | ||
return nil, fmt.Errorf("expected TAG:VALUE got %s", tag) | ||
} | ||
tagName := fmt.Sprintf("tag:%s", *aws.String(parts[0])) | ||
filters = append(filters, &ec2.Filter{ | ||
Name: &tagName, | ||
Values: []*string{aws.String(parts[1])}, | ||
}) | ||
} | ||
return filters, 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,56 @@ | ||
package providers | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
/* | ||
* varnish-purge-proxy | ||
* (C) Copyright Bashton Ltd, 2014 | ||
* | ||
* varnish-purge-proxy is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* varnish-purge-proxy is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with varnish-purge-proxy. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
func expect(t *testing.T, k string, a interface{}, b interface{}) { | ||
if a != b { | ||
t.Fatalf("%s: Expected %v (type %v) - Got %v (type %v)", k, b, reflect.TypeOf(b), a, reflect.TypeOf(a)) | ||
} | ||
} | ||
|
||
func TestBuildFilter(t *testing.T) { | ||
testTags := []string{"machinetype:varnish", "env:stage"} | ||
awsService := AWSProvider{ | ||
Tags: testTags, | ||
} | ||
|
||
filter, err := awsService.buildFilter() | ||
expect(t, "buildfilter", err, nil) | ||
expect(t, "buildfilter", *filter[0].Name, "tag:machinetype") | ||
value0 := filter[0].Values[0] | ||
expect(t, "buildfilter", *value0, "varnish") | ||
expect(t, "buildfilter", *filter[1].Name, "tag:env") | ||
value1 := filter[1].Values[0] | ||
expect(t, "buildfilter", *value1, "stage") | ||
} | ||
|
||
func TestBuildFilterInvalid(t *testing.T) { | ||
testTags := []string{"machinetypevarnish", "env:stage"} | ||
awsService := AWSProvider{ | ||
Tags: testTags, | ||
} | ||
_, err := awsService.buildFilter() | ||
expect(t, "buildfilterinvalid", err.Error(), "expected TAG:VALUE got machinetypevarnish") | ||
} |
Oops, something went wrong.