Skip to content

Commit

Permalink
Merge pull request #7 from BashtonLtd/dwatson/unify
Browse files Browse the repository at this point in the history
Unify AWS and GCE branches.
  • Loading branch information
dwatson committed Feb 7, 2017
2 parents 2655779 + 7960fde commit 4b60f39
Show file tree
Hide file tree
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.
19 changes: 15 additions & 4 deletions .travis.yml
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)
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
varnish-purge-proxy
===================
# varnish-purge-proxy

[![Build Status](https://travis-ci.org/BashtonLtd/varnish-purge-proxy.svg?branch=master)](https://travis-ci.org/BashtonLtd/varnish-purge-proxy)

Proxy purge requests to multiple varnish servers

Specify tags to limit instances that receive the purge request, multiple tags can be used. You must specify at least one tag.
Works with AWS or GCE.

`./varnish-purge-proxy Service:varnish Environment:live`
## Global options

You can also specify host and port to listen on:

`./varnish-purge-proxy --listen=127.0.0.1 --port=8000`
`./varnish-purge-proxy aws --listen=127.0.0.1 --port=8000`

You can also specify the destination port to target:

`./varnish-purge-proxy --destport=6081`
`./varnish-purge-proxy aws --destport=6081`

varnish-purge-proxy will cache the IP lookup for 60 seconds, you can change this as follows:

`./varnish-purge-proxy --cache=120`
`./varnish-purge-proxy aws --cache=120`

## AWS

Specify tags to limit instances that receive the purge request, multiple tags can be used. You must specify at least one tag.

### Example

`./varnish-purge-proxy aws Service:varnish Environment:live`


Authentication
--------------
### Authentication

AWS access key and secret key can be added as environment variables, using either `AWS_ACCESS_KEY_ID` or `AWS_SECRET_ACCESS_KEY`. If these are not available then IAM credentials for the instance will be checked.

Building
--------

## GCE

Specify an instance name prefix to limit instances that receive the purge request with the `--nameprefix` argument.

### Example

`varnish-purge-proxy gce --credentials=creds.json --region=us-central1 --project=my-project --nameprefix=varnish`

### Authentication

GCE credentials should be provided using the `--credentials` argument.

## Building

Build a binary by running:

Expand Down
107 changes: 107 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions glide.yaml
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
99 changes: 99 additions & 0 deletions providers/aws.go
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: &region})

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

}
56 changes: 56 additions & 0 deletions providers/aws_test.go
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")
}
Loading

0 comments on commit 4b60f39

Please sign in to comment.