Skip to content

Commit

Permalink
Refactor (#31)
Browse files Browse the repository at this point in the history
* Restructure for vgo friendliness

* Bump version

* Add canonical import path

* Some more polish

* Avoid some gometalinter pitfalls in lintsetup
  • Loading branch information
sevagh authored Apr 17, 2018
1 parent 3c2c9c2 commit 257328f
Show file tree
Hide file tree
Showing 44 changed files with 260 additions and 581 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
bin/
rpm-package/ebs/goat-ebs
rpm-package/eni/goat-eni
rpm-package/goat
35 changes: 9 additions & 26 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
VERSION:=0.5.0
VERSION:=0.6.0
GOAT_FILES?=$$(find . -name '*.go' | grep -v vendor)
GOAT_CMDS=$(shell find cmd/ -maxdepth 1 -mindepth 1 -type d)

STATIC_ENV:=CGO_ENABLED=0 GOOS=linux GOARCH=amd64
STATIC_FLAGS:=-a -tags netgo -ldflags '-extldflags "-static" -X main.VERSION=$(VERSION)'
RELEASE_FLAGS:=-a -tags netgo -ldflags '-w -extldflags "-static" -X main.VERSION=$(VERSION)'

all: build_static
all: build

build: deps
@$(foreach cmd,$(GOAT_CMDS),\
cd $(cmd) &&\
$(STATIC_ENV) go build $(STATIC_FLAGS) \
-o ../../bin/$(notdir $(cmd)) &&\
cd - 2>&1 >/dev/null;)

release: deps
@$(foreach cmd,$(GOAT_CMDS),\
cd $(cmd) &&\
$(STATIC_ENV) go build $(RELEASE_FLAGS) \
-o ../../bin/$(notdir $(cmd)) &&\
cd - 2>&1 >/dev/null;)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags '-w -extldflags "-static" -X main.VERSION=$(VERSION)' -o bin/goat

test:
@$(foreach cmd,$(GOAT_CMDS),\
go vet ./$(cmd) &&\
go test -v ./$(cmd);)
@go vet ./pkg/...
@go test -v ./pkg/...
@go vet ./...
@go test -v ./...

deps:
@command -v dep 2>&1 >/dev/null || go get -u github.com/golang/dep/cmd/dep
Expand All @@ -40,11 +21,13 @@ lint:
lintsetup:
@go get -u gopkg.in/alecthomas/gometalinter.v2
@gometalinter.v2 --install 2>&1 >/dev/null
@go install ./...

clean:
-rm -rf bin

package: release
@GOAT_VERSION=$(VERSION) $(MAKE) -C ./rpm-package/
rpm: build
@cp bin/goat rpm-package/
GOAT_VERSION=$(VERSION) $(MAKE) -C ./rpm-package/

.PHONY: clean test
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ By setting your tags correctly, `goat` can discover and attach EBS volumes and E

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](./cmd/goat-ebs/README.md) and [goat-eni](./cmd/goat-eni/README.md).
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](./hcl-example/iam_role.tf). Your roles can be even more permissive (i.e. full EC2 access) but that comes with its own risks.
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.

Expand Down Expand Up @@ -47,4 +47,4 @@ $ sudo systemctl start goat-eni

### Examples

[Link to the example Terraform HCL scripts](./hcl-example).
[Link to the example Terraform HCL scripts](./docs/hcl-example).
25 changes: 21 additions & 4 deletions pkg/awsutil/attach.go → attach.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package awsutil
package main

import (
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
log "github.com/sirupsen/logrus"

"github.com/sevagh/goat/pkg/driveutil"
"github.com/sevagh/goat/filesystem"
)

//AttachEbsVolumes attaches the given map of {'VolumeName':[]EbsVol} with the EC2 client in the provided ec2Instance
Expand All @@ -20,7 +21,7 @@ func (e *EC2Instance) AttachEbsVolumes(dryRun bool) map[string][]EbsVol {
volLogger := log.WithFields(log.Fields{"vol_id": volume.EbsVolID, "vol_name": volume.VolumeName})
if volume.AttachedName == "" {
volLogger.Info("Volume is unattached, picking drive name")
if deviceName, err = driveutil.RandDriveNamePicker(); err != nil {
if deviceName, err = randDriveNamePicker(); err != nil {
volLogger.Fatal("Couldn't find an unused drive name")
}
attachVolIn := &ec2.AttachVolumeInput{
Expand All @@ -37,7 +38,7 @@ func (e *EC2Instance) AttachEbsVolumes(dryRun bool) map[string][]EbsVol {
volLogger.Info(volAttachments)
volume.AttachedName = deviceName

if !dryRun && !driveutil.DoesDriveExistWithTimeout(deviceName, 10) {
if !dryRun && !filesystem.DoesDriveExistWithTimeout(deviceName, 10) {
volLogger.Fatalf("Drive %s doesn't exist after attaching - checked with stat 10 times", deviceName)
}
localVolumes[key] = append(localVolumes[key], volume)
Expand Down Expand Up @@ -68,3 +69,19 @@ func (e *EC2Instance) AttachEnis(dryRun bool) {
}
}
}

func randDriveNamePicker() (string, error) {
ctr := 0
deviceName := "/dev/xvd"
runes := []rune("bcdefghijklmnopqrstuvwxyz")
for {
if ctr >= len(runes) {
return "", fmt.Errorf("Ran out of drive names")
}
if !filesystem.DoesDriveExist(deviceName + string(runes[ctr])) {
break
}
ctr++
}
return deviceName + string(runes[ctr]), nil
}
118 changes: 0 additions & 118 deletions cmd/goat-ebs/ebs.go

This file was deleted.

27 changes: 0 additions & 27 deletions cmd/goat-eni/eni.go

This file was deleted.

44 changes: 0 additions & 44 deletions cmd/goat-eni/main.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 257328f

Please sign in to comment.