Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements #46

Merged
merged 3 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
goat
pkg/
bin/
37 changes: 37 additions & 0 deletions Dockerfile.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM golang:1.11-stretch as build

ENV REPO sevagh/goat
ENV GO111MODULE=on

RUN apt-get update -y &&\
apt-get install -y zip

COPY . /go/src/github.com/$REPO
WORKDIR /go/src/github.com/$REPO

RUN make build zip tarball

FROM fedora as rpmbuild

RUN dnf update -y &&\
dnf install -y make rpm-build alien

COPY ./rpm /goat-rpm-pkg/

COPY --from=build /go/src/github.com/sevagh/goat/bin/goat /goat-rpm-pkg/goat
COPY --from=build /go/src/github.com/sevagh/goat/bin/version-file /goat-rpm-pkg/version-file

WORKDIR goat-rpm-pkg

RUN make rpm

WORKDIR /root/rpmbuild/RPMS/x86_64/

RUN alien ./*.rpm

FROM alpine

COPY --from=build /go/src/github.com/sevagh/goat/pkg /_goat-pkg
COPY --from=rpmbuild /root/rpmbuild/RPMS/x86_64/* /_goat-pkg/

CMD cp _goat-pkg/* goat-pkg/
68 changes: 24 additions & 44 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,67 +1,47 @@
NAME:=goat
VERSION:=1.0.0
VERSION:=$(shell git describe --tags)
OS:=linux
ARCH:=amd64
GOAT_FILES?=$$(find . -name '*.go' | grep -v vendor)
BINPATH=usr/sbin
GOAT_FILES:=$$(find . -name '*.go' | grep -v vendor)
BINPATH:=$(PWD)/bin
PKGDIR?=$(PWD)/pkg
DEBIANDIR:=debian/$(NAME)-$(VERSION)

all: build

build: deps
docker-build:
mkdir -p $(PKGDIR)
docker build --no-cache -t "goat-builder" -f Dockerfile.build .
docker run -v $(PKGDIR):/goat-pkg goat-builder

build:
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build -a -tags netgo -ldflags '-w -extldflags "-static" -X main.VERSION=$(VERSION)' -o $(BINPATH)/$(NAME)
strip $(BINPATH)/$(NAME)
echo $(VERSION) > $(BINPATH)/version-file

test:
@go vet ./...
@go test -v ./...

deps:
@command -v dep 2>&1 >/dev/null || go get -u github.com/golang/dep/cmd/dep
@dep ensure -v

fmt:
@gofmt -s -w $(GOAT_FILES)

lint:
-gometalinter.v2 --enable-all $(GOAT_FILES) --exclude=_test.go
pkgclean:
rm -rf $(PKGDIR)

package_all: pkgclean build deb rpm zip
tarball:
@tar -czf $(PKGDIR)/$(NAME)_$(VERSION)_$(OS)_$(ARCH).tar.gz -C $(BINPATH) $(NAME)

zip:
@zip pkg/$(NAME)_$(VERSION)_$(OS)_$(ARCH).zip -j $(BINPATH)/$(NAME)

deb:
@mkdir -p pkg
fpm -s dir -t deb -n $(NAME) -v $(VERSION) -C . \
-p pkg/$(NAME)_VERSION_ARCH.deb \
-d "mdadm" \
--deb-systemd ./goat@.service \
$(BINPATH)
@zip $(PKGDIR)/$(NAME)_$(VERSION)_$(OS)_$(ARCH).zip -j $(BINPATH)/$(NAME)

rpm:
@mkdir -p pkg
fpm -s dir -t rpm -n $(NAME) -v $(VERSION) -C . \
-p pkg/$(NAME)_VERSION_ARCH.rpm \
-d "mdadm" \
--rpm-systemd ./goat@.service \
$(BINPATH)

pkgclean:
@rm -rf pkg
test:
@go test -v ./...

lintsetup:
@go get -u gopkg.in/alecthomas/gometalinter.v2
@gometalinter.v2 --install 2>&1 >/dev/null
@go install ./...
fmt:
@gofmt -s -w $(GOAT_FILES)

dev-env: ## Build a local development environment using Docker
@docker run -it --rm \
-v $(shell pwd):/go/src/github.com/sevagh/$(NAME) \
-w /go/src/github.com/sevagh/$(NAME) \
golang:1.10 \
/bin/bash -c 'make deps; make install; bash'
golang:1.11 \
/bin/bash -c 'make install; bash'

install: ## Build and install locally the binary (dev purpose)
go install .

.PHONY: dev-env clean install test
.PHONY: docker-build dev-env clean install test
114 changes: 0 additions & 114 deletions Gopkg.lock

This file was deleted.

11 changes: 0 additions & 11 deletions Gopkg.toml

This file was deleted.

96 changes: 68 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
# goat :goat:
# goat

### Attach EBS volumes and ENIs to running EC2 instances

`goat` is a Go application which runs from inside the EC2 instance.
`goat` is a Go program which runs from inside the EC2 instance.

By setting your tags correctly, `goat` can discover and attach EBS volumes and ENIs. Furthermore, for EBS volumes, it can perform additional actions such as RAID (with mdadm), mkfs, and mount EBS volumes to the EC2 instance where it's running.

The `goat` package consists of the subcommands [goat ebs](./docs/EBS.md) and [goat eni](./docs/ENI.md).

### Permission model

It's necessary for the instance to have an IAM Role with _at least_ access to the EBS and ENI resources that it will be attaching - see [here](./docs/hcl-example/iam_role.tf). Your roles can be even more permissive (i.e. full EC2 access) but that comes with its own risks.

Unfortunately, resource-level permissions are [currently not supported](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/ec2-api-permissions.html#ec2-api-unsupported-resource-permissions) for attaching network interfaces. This means that to use `goat@eni`, your instances must have full permissions for __all__ ENIs.
By setting your tags correctly, `goat` can discover and attach EBS volumes and ENIs. For EBS volumes, it can perform additional actions such as RAID (with mdadm), mkfs, and mount EBS volumes to the EC2 instance where it's running.

### Install and run

Goat is a Go binary that should be able to run on any Linux instance.

In the releases tab you can find a zip of the binary, and a `.deb` and `.rpm` package with systemd support generated by [fpm](https://github.com/jordansissel/fpm).

To use goat, run it during the launch process of your EC2 instance - you can `systemctl enable goat@TARGET` and `systemctl start goat@TARGET` (where TARGET is one of ebs or eni).
Goat is a Go binary that should be able to run on any Linux instance. In the releases tab you can find a zip of the binary, and a `.deb` and `.rpm` package with systemd support. Goat needs `mdadm` to perform RAID (which is a dependency in the deb and rpm).

It's a Linux-specific tool which needs `mdadm` to work for the RAID features.
To use goat, run it during the launch process of your EC2 instance - you can `systemctl enable goat@TARGET` and `systemctl start goat@TARGET` (where TARGET is one of ebs or eni) in the EC2 user data script. [Full Terraform example here](./terraform-example).

#### Usage
### Usage

In the most basic case, you should run `goat ebs` or `goat eni`.

Expand All @@ -44,23 +32,75 @@ OPTIONS
Display version and exit
```

### Environment variables

You can use the environment variables:
You can set `-tagPrefix` and `-logLevel` with environment variables (which take precedence):

* `GOAT_LOG_LEVEL`
* `GOAT_TAG_PREFIX`

Set these the same as `-logLevel` and `-tagPrefix` - the environment variables take precedence.
### Tags

These are the tags you need (recall that the `GOAT-IN` prefix is configurable):

| Tag Name | Description | Resource type | Required | Effect |
| :------------------- | :---------------------- | :---------------- | :------------------ | :---------- |
| GOAT-IN:Prefix | Logical app name | EC2, EBS, ENI | :heavy_check_mark: | attach |
| GOAT-IN:NodeId | Node id | EC2, EBS, ENI | :heavy_check_mark: | attach |
| GOAT-IN:VolumeName | Distinct volume name | EBS | | |
| GOAT-IN:VolumeSize | # of disks in vol group | EBS | | mdadm |
| GOAT-IN:RaidLevel | level of RAID (0 or 1) | EBS | | mdadm |
| GOAT-IN:MountPath | Linux path to mount vol | EBS | | mount |
| GOAT-IN:FsType | Linux filesystem type | EBS | | mkfs |

If non-required tags are omitted, that step is skipped. The barest case will simply attach the EBS volumes and perform no further actions.

The filesystem and fstab entries are created with the label `GOAT-{VolumeName}` for convenience. Running `goat` multiple times will result in it detecting the existing label it intended to create and not proceeding.

Aside from the `mount` syscall, `goat` shells out to `mdadm`, `blkid`, and `mkfs`. If the mount and RAID steps are performed, the configs will be persisted to `/etc/fstab` and `/etc/mdadm.conf`.

Check the [Terraform example](./terraform-example) for example tag values.

### Permissions

It's necessary for the instance to have an IAM Role with _at least_ access to the EBS and ENI resources that it will be attaching - see [here](./terraform-example/iam_role.tf). Your roles can be even more permissive (i.e. full EC2 access) but that comes with its own risks.

Unfortunately, resource-level permissions are [currently not supported](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/ec2-api-permissions.html#ec2-api-unsupported-resource-permissions) for attaching network interfaces. This means that to use `goat@eni`, your instances must have full permissions for __all__ ENIs.

### Example EBS usecase - attaching old disks to a new instance

The specific use-case that `goat` was developed to solve is the following. Say you have 3 instances with their own respective disks, and you receive a termination notice for instance 1. I want the `goat` workflow to be:

1. Terminate instance 1
2. Create a new instance with the same GOAT tags (to indicate to `goat` that it's the logical equivalent as the machine it is replacing)
1. **No need to modify or manipulate the EBS volumes or their tags**
4. On boot, everything works magically

After `goat` ran on the first fresh run, the EBS volumes got the correct filesystems, labels, and in the case of RAID, `mdadm` metadata on them.

The event flow on a re-created instance is:

1. Get EC2 metadata on the running instance, create an EC2 client, and search EBS volumes
2. Attach the volumes it needs based on their tags
3. Discover that `/dev/disk/by-label` already contains the correct disks
1. From `mdadm` magic, after the EBS attachment the RAID array is already detected correctly
4. Proceed to perform the `fstab` and `mount` phases - skip `mdadm`, `mkfs`

**CAVEAT**: the mdadm metadata will have the hostname of the previous EC2 instance:

```
[centos@ip-172-31-29-69 ~]$ sudo mdadm --detail --scan --verbose
ARRAY /dev/md127 level=raid0 num-devices=3 metadata=1.2 name="ip-172-31-25-105:'GOAT-data'" UUID=2d08b310:fd13bd21:bc2417a4:56a1ec57
devices=/dev/xvdb,/dev/xvdc,/dev/xvdd
[centos@ip-172-31-29-69 ~]$
```

### Additional dependencies for ENI
To avoid this, define a good/persistent hostname for the EC2 instance, that you will then re-apply to any instance taking ownership of the previous instance's disks.

Goat by itself is sufficient for the EBS feature, but needs help for setting up an ENI. Refer to [this](./docs/ENI.md#setting-up-the-eni---ec2-net-utils) document.
### ENI notes

### Hack
As mentioned, the only action `goat` will do for ENIs is attaching them. You can try to use [ec2-net-utils](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-eni.html#ec2-net-utils), a tool available on Amazon Linux AMIs, or [this port to CentOS/systemd](https://github.com/etuttle/ec2-utils), to configure an ENI after `goat` attaches it.

If you have docker locally, you can use the following command in order to quickly get a development env ready: `make dev-env`.
ENI attachments take a parameter called [DeviceIndex](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-network-interface-attachment.html). Goat isn't smart, and always starts from DeviceIndex `1`. This means that your EC2 instance should have no attached ENIs to use `goat`. If it does, they should be the ones that `goat` was going to attach anyway, not external ENIs that have no `goat` tags.

### Examples
### Build and develop

[Link to the example Terraform HCL scripts](./docs/hcl-example).
The deb, rpm, and zip are generated from a multi-stage [Dockerfile.build](./Dockerfile.build). Invoke it with `make docker-build`. If you have docker locally, you can use the following command in order to quickly get a development env ready: `make dev-env`.
Loading