Skip to content

Commit

Permalink
Merge pull request #1 from jimbydamonk/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jimbydamonk authored Jul 5, 2016
2 parents b9e1c68 + 474c2e7 commit fd705a7
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
10 changes: 10 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Credits

## Development Lead

- Mike Buzzetti <michael.buzzetti@nytimes.com>
- Scott Stevenson <scott.stevenson@nytimes.com>

## Contributors

None yet. Why not be the first?
81 changes: 81 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

You can contribute in many ways:

## Types of Contributions

### Report Bugs

Report bugs at https://github.com/jimbydamonk/mock-ec2-metadata/issues.

If you are reporting a bug, please include:

* Your operating system name and version.
* Any details about your local setup that might be helpful in troubleshooting.
* Detailed steps to reproduce the bug.

### Fix Bugs

Look through the GitHub issues for bugs. Anything tagged with "bug"
is open to whoever wants to implement it.

### Implement Features

Look through the GitHub issues for features. Anything tagged with "feature"
is open to whoever wants to implement it.

### Write Documentation

mock-ec2-metadata could always use more documentation, whether as part of the
official mock-ec2-metadata docs, in docstrings, or even on the web in blog posts,
articles, and such.

### Submit Feedback

The best way to send feedback is to file an issue at https://github.com/jimbydamonk/mock-ec2-metadata/issues.

If you are proposing a feature:

* Explain in detail how it would work.
* Keep the scope as narrow as possible, to make it easier to implement.
* Remember that this is a volunteer-driven project, and that contributions
are welcome :)

## Get Started!

Ready to contribute? Here's how to set up `mock-ec2-metadata` for local development.

1. Fork the `mock-ec2-metadata` repo on GitHub.
2. Clone your fork locally::

$ git clone git@github.com:your_name_here/mock-ec2-metadata.git

3. Create a branch for local development::

$ git checkout -b name-of-your-bugfix-or-feature

Now you can make your changes locally.

4. When you're done making changes, check that your changes pass the tests::

$ make test

6. Commit your changes and push your branch to GitHub::

$ git add .
$ git commit -m "Your detailed description of your changes."
$ git push origin name-of-your-bugfix-or-feature

7. Submit a pull request through the GitHub website.

Pull Request Guidelines
-----------------------

Before you submit a pull request, check that it meets these guidelines:

1. The pull request should include tests.
2. If the pull request adds functionality, the docs should be updated. Put
your new functionality into a function with a docstring, and add the
feature to the list in README.md.
1 change: 1 addition & 0 deletions Godeps
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/NYTimes/gizmo 1cdaffd9dba57da6b53cd23db00f5b940aac02f1
81 changes: 81 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#
# Makefile
#
# The kickoff point for all project management commands.
#

# Program version
VERSION := $(shell grep "const Version " version.go | sed -E 's/.*"(.+)"$$/\1/')

# Binary name for bintray
BIN_NAME=mock-ec2-metadata

# Project owner for bintray
OWNER=NYTimes

# Project name for bintray
PROJECT_NAME=mock-ec2-metadata

# Project url used for builds
# examples: github.com, bitbucket.org
REPO_HOST_URL=github.com

# Grab the current commit
GIT_COMMIT=$(shell git rev-parse HEAD)

# Check if there are uncommited changes
GIT_DIRTY=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true)

# Use a local vendor directory for any dependencies; comment this out to
# use the global GOPATH instead
GOPATH=$(PWD)/.vendor

INSTALL_PATH=$(GOPATH)/src/github.com/NYTimes/mock-ec2-metadata

default: build

help:
@echo 'Management commands for mock-ec2-metadata:'
@echo
@echo 'Usage:'
@echo ' make build Compile the project.'
@echo ' make link Symlink this project into the GOPATH.'
@echo ' make test Run tests on a compiled project.'
@echo ' make fmt Reformat the source tree with gofmt.'
@echo ' make clean Clean the directory tree.'
@echo

build: .git $(GOPATH)/bin/gogpm $(INSTALL_PATH)
@echo "building ${OWNER} ${BIN_NAME} ${VERSION}"
@echo "GOPATH=${GOPATH}"
$(GOPATH)/bin/gogpm install &&\
go build -ldflags "-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY}" -o bin/${BIN_NAME} main.go

clean:
@test ! -e bin/${BIN_NAME} || rm bin/${BIN_NAME}

.git:
git init
git add -A .
git commit -m 'Initial scaffolding.'

link:
# relink into the go path
if [ ! $(INSTALL_PATH) -ef . ]; then \
mkdir -p `dirname $(INSTALL_PATH)`; \
ln -s $(PWD) $(INSTALL_PATH); \
fi

$(INSTALL_PATH):
make link

$(GOPATH)/bin/gogpm:
go get github.com/mtibben/gogpm

test:
go test ./...

fmt:
find . -name '*.go' -not -path './.vendor/*' -exec gofmt -w=true {} ';'

.PHONY: build dist clean test help default link fmt
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# mock-ec2-metadata

A simple service to mock the ec2 metadata service.

## Getting started

This project requires Go to be installed. On OS X with Homebrew you can just run `brew install go`.

Running it then should be as simple as:

```console
$ make
$ ./bin/mock-ec2-metadata
```


### Testing

``make test``

## License

See `LICENSE`

## Contributing

See `CONTRIBUTING.md` for more details.
15 changes: 15 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Server": {
"HTTPPort": 8111,
"Log": "./app.log",
"HTTPAccessLog": "./access.log"
},

"MetadataItems": {
"/latest": "meta-data",
"/2009-04-04": "meta-data",
"/2009-04-04/meta-data": "hostname",
"/2009-04-04/meta-data/hostname": "metadata-hostname"

}
}
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"github.com/NYTimes/mock-ec2-metadata/service"
"github.com/NYTimes/gizmo/config"
"github.com/NYTimes/gizmo/server"
)

func main() {
// showing 1 way of managing gizmo/config: importing from a local file
var cfg *service.Config
config.LoadJSONFile("./config.json", &cfg)

server.Init("mock-ec2-metadata", cfg.Server)
err := server.Register(service.NewMetadataService(cfg))
if err != nil {
server.Log.Fatal("unable to register service: ", err)
}

err = server.Run()
if err != nil {
server.Log.Fatal("server encountered a fatal error: ", err)
}
}
80 changes: 80 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package service

import (
"net/http"
"github.com/NYTimes/gizmo/server"
"github.com/Sirupsen/logrus"
)

type (

Config struct {
Server *server.Config
MetadataItems map[string]interface{}
}
MetadataService struct {
config *Config
}
)

func NewMetadataService(cfg *Config) *MetadataService {
return &MetadataService{cfg}
}

func (s *MetadataService) Middleware(h http.Handler) http.Handler {
return h
}

func (s *MetadataService) JSONMiddleware(j server.JSONEndpoint) server.JSONEndpoint {
return func(r *http.Request) (int, interface{}, error) {

status, res, err := j(r)
if err != nil {
server.LogWithFields(r).WithFields(logrus.Fields{
"error": err,
}).Error("problems with serving request")
return http.StatusServiceUnavailable, nil, &jsonErr{"sorry, this service is unavailable"}
}

server.LogWithFields(r).Info("success!")
return status, res, nil
}
}

func (s *MetadataService) GetMetadataItem(r *http.Request) (int, interface{}, error) {
res := s.config.MetadataItems[r.URL.Path]
return http.StatusOK, res, nil
}

func (s *MetadataService) GetIndex(r *http.Request) (int, interface{}, error) {
return http.StatusOK, "Mock EC2 Metadata Service", nil
}

// JSONEndpoints is a listing of all endpoints available in the MetadataService.
func (s *MetadataService) Endpoints() map[string]map[string]http.HandlerFunc {

handlers := make(map[string]map[string]http.HandlerFunc)
for url, value := range s.config.MetadataItems {
server.Log.Info("adding route for url", url, " value ", value)

handlers[url] = map[string]http.HandlerFunc {
"GET": server.JSONToHTTP(s.GetMetadataItem).ServeHTTP,
}
}
handlers["/"] = map[string]http.HandlerFunc {
"GET": server.JSONToHTTP(s.GetIndex).ServeHTTP,
}
return handlers
}

func (s *MetadataService) Prefix() string {
return "/"
}

type jsonErr struct {
Err string `json:"error"`
}

func (e *jsonErr) Error() string {
return e.Err
}
12 changes: 12 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

// The git commit that was compiled. This will be filled in by the compiler.
var GitCommit string

// The main version number that is being run at the moment.
const Version = "0.1.0"

// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
const VersionPrerelease = ""

0 comments on commit fd705a7

Please sign in to comment.