Skip to content

Commit

Permalink
commit server,agents,clients, AMP Bootstrap and fixes reports (#519)
Browse files Browse the repository at this point in the history
* add bootstrap feature to AMP cluster CLI (#306)

* update vendors (huge diff)
  • Loading branch information
freignat91 authored and subfuzion committed Dec 19, 2016
1 parent cd528ec commit fc727f7
Show file tree
Hide file tree
Showing 210 changed files with 11,309 additions and 1,919 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/amplifier-gateway
/amp-agent
/amp-log-worker
/amp-cluster
/dist
/*.exe
coverage.out
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile.shrink
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ COPY ./amplifier /usr/local/bin/amplifier
COPY ./amp-agent /usr/local/bin/amp-agent
COPY ./amp-log-worker /usr/local/bin/amp-log-worker
COPY ./amplifier-gateway /usr/local/bin/amplifier-gateway
COPY ./adm-server /usr/local/bin/adm-server
COPY ./adm-agent /usr/local/bin/adm-agent
EXPOSE 50101
ENTRYPOINT []
CMD [ "amplifier" ]
57 changes: 44 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ SERVER := amplifier
AGENT := amp-agent
LOGWORKER := amp-log-worker
GATEWAY := amplifier-gateway
CLUSTERSERVER := adm-server
CLUSTERAGENT := adm-agent
AMPADM := ampadm

TAG ?= latest
IMAGE := $(OWNER)/amp:$(TAG)
Expand Down Expand Up @@ -125,7 +128,9 @@ bin-clean:

clean: proto-clean bin-clean

install: install-cli install-server install-agent install-log-worker install-gateway
clean: proto-clean bin-clean

install: install-cli install-server install-agent install-log-worker install-gateway install-adm-server install-adm-agent install-ampadm

DATASRC := $(shell find ./data -type f -name '*.go' -not -name '*.pb.go' -not -name '*.pb.gw.go')
APISRC := $(shell find ./api -type f -name '*.go' -not -name '*.pb.go' -not -name '*.pb.gw.go')
Expand All @@ -136,6 +141,8 @@ SERVERSRC := $(shell find ./cmd/amplifier -type f -name '*.go' -not -name '*.pb.
AGENTSRC := $(shell find ./cmd/amp-agent -type f -name '*.go' -not -name '*.pb.go' -not -name '*.pb.gw.go')
LOGWORKERSRC := $(shell find ./cmd/amp-log-worker -type f -name '*.go' -not -name '*.pb.go' -not -name '*.pb.gw.go')
GATEWAYSRC := $(shell find ./cmd/amplifier-gateway -type f -name '*.go' -not -name '*.pb.go' -not -name '*.pb.gw.go')


install-cli: $(CLISRC) $(DATASRC) $(APISRC) $(VENDORSRC) $(PROTOALLTARGETS)
@go install $(LDFLAGS) $(REPO)/$(CMDDIR)/$(CLI)
install-server: $(SERVERSRC) $(DATASRC) $(APISRC) $(VENDORSRC) $(PROTOALLTARGETS)
Expand All @@ -146,6 +153,12 @@ install-log-worker: $(LOGWORKERSRC) $(DATASRC) $(APISRC) $(VENDORSRC) $(PROTOALL
@go install $(LDFLAGS) $(REPO)/$(CMDDIR)/$(LOGWORKER)
install-gateway: $(GATEWAYSRC) $(DATASRC) $(APISRC) $(VENDORSRC) $(PROTOALLTARGETS)
@go install $(LDFLAGS) $(REPO)/$(CMDDIR)/$(GATEWAY)
install-adm-server: $(GATEWAYSRC) $(DATASRC) $(APISRC) $(VENDORSRC) $(PROTOALLTARGETS)
@go install $(LDFLAGS) $(REPO)/$(CMDDIR)/$(CLUSTERSERVER)
install-adm-agent: $(GATEWAYSRC) $(DATASRC) $(APISRC) $(VENDORSRC) $(PROTOALLTARGETS)
@go install $(LDFLAGS) $(REPO)/$(CMDDIR)/$(CLUSTERAGENT)
install-ampadm: $(GATEWAYSRC) $(DATASRC) $(APISRC) $(VENDORSRC) $(PROTOALLTARGETS)
@go install $(LDFLAGS) $(REPO)/$(CMDDIR)/$(AMPADM)

build: build-cli build-server build-agent build-log-worker build-gateway

Expand All @@ -159,48 +172,66 @@ build-log-worker: proto
@hack/build $(LOGWORKER)
build-gateway: proto
@hack/build $(GATEWAY)
build-adm-server: proto
@hack/build $(CLUSTERSERVER)
build-adm-agent: proto
@hack/build $(CLUSTERAGENT)
build-ampadm: proto
@hack/build $(CLUSTERADM)

build-server-image:
@docker build --build-arg BUILD=$(BUILD) -t appcelerator/$(SERVER):$(TAG) .

build-cli-linux:
@rm -f $(CLI)
@env GOOS=linux GOARCH=amd64 VERSION=$(VERSION) hack/build $(CLI)
@env GOOS=linux GOARCH=amd64 VERSION=$(VERSION) BUILD=$(BUILD) hack/build $(CLI)

build-cli-darwin:
@rm -f $(CLI)
@env GOOS=darwin GOARCH=amd64 VERSION=$(VERSION) hack/build $(CLI)
@env GOOS=darwin GOARCH=amd64 VERSION=$(VERSION) BUILD=$(BUILD) hack/build $(CLI)

build-cli-windows:
@rm -f $(CLI).exe
@env GOOS=windows GOARCH=amd64 VERSION=$(VERSION) hack/build $(CLI)
@env GOOS=windows GOARCH=amd64 VERSION=$(VERSION) BUILD=$(BUILD) hack/build $(CLI)

build-server-linux:
@rm -f $(SERVER)
@env GOOS=linux GOARCH=amd64 VERSION=$(VERSION) hack/build $(SERVER)
@env GOOS=linux GOARCH=amd64 VERSION=$(VERSION) BUILD=$(BUILD) hack/build $(SERVER)

build-server-darwin:
@rm -f $(SERVER)
@env GOOS=darwin GOARCH=amd64 VERSION=$(VERSION) hack/build $(SERVER)
@env GOOS=darwin GOARCH=amd64 VERSION=$(VERSION) BUILD=$(BUILD) hack/build $(SERVER)

build-server-windows:
@rm -f $(SERVER).exe
@env GOOS=windows GOARCH=amd64 VERSION=$(VERSION) hack/build $(SERVER)
@env GOOS=windows GOARCH=amd64 VERSION=$(VERSION) BUILD=$(BUILD) hack/build $(SERVER)

build-ampadm-linux:
@rm -f $(AMPADM)
@env GOOS=linux GOARCH=amd64 VERSION=$(VERSION) BUILD=$(BUILD) hack/build $(AMPADM)

build-ampadm-darwin:
@rm -f $(AMPADM)
@env GOOS=darwin GOARCH=amd64 VERSION=$(VERSION) BUILD=$(BUILD) hack/build $(AMPADM)

dist-linux: build-cli-linux build-server-linux
build-ampadm-windows:
@rm -f $(AMPADM).exe
@env GOOS=windows GOARCH=amd64 VERSION=$(VERSION) BUILD=$(BUILD) hack/build $(AMPADM)

dist-linux: build-cli-linux build-server-linux build-ampadm-linux
@rm -f dist/Linux/x86_64/amp-$(VERSION).tgz
@mkdir -p dist/Linux/x86_64
@tar czf dist/Linux/x86_64/amp-$(VERSION).tgz $(CLI) $(SERVER)
@tar czf dist/Linux/x86_64/amp-$(VERSION).tgz $(CLI) $(SERVER) $(AMPADM)

dist-darwin: build-cli-darwin build-server-darwin
dist-darwin: build-cli-darwin build-server-darwin build-ampadm-darwin
@rm -f dist/Darwin/x86_64/amp-$(VERSION).tgz
@mkdir -p dist/Darwin/x86_64
@tar czf dist/Darwin/x86_64/amp-$(VERSION).tgz $(CLI) $(SERVER)
@tar czf dist/Darwin/x86_64/amp-$(VERSION).tgz $(CLI) $(SERVER) $(AMPADM)

dist-windows: build-cli-windows build-server-windows
dist-windows: build-cli-windows build-server-windows build-ampadm-windows
@rm -f dist/Windows/x86_64/amp-$(VERSION).zip
@mkdir -p dist/Windows/x86_64
@zip -q dist/Windows/x86_64/amp-$(VERSION).zip $(CLI).exe $(SERVER).exe
@zip -q dist/Windows/x86_64/amp-$(VERSION).zip $(CLI).exe $(SERVER).exe $(AMPADM).exe

dist: dist-linux dist-darwin dist-windows

Expand Down
15 changes: 8 additions & 7 deletions api/client/amp.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ func (l logger) Println(args ...interface{}) {

// Configuration is for all configurable client settings
type Configuration struct {
Verbose bool
Github string
Target string
Images []string
Port string
ServerAddress string
CmdTheme string
Verbose bool
Github string
Target string
Images []string
Port string
ServerAddress string
AdminServerAddress string
CmdTheme string
}

// AMP holds the state for the current environment
Expand Down
15 changes: 13 additions & 2 deletions build-amp-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ if [ $? -ne 0 ]; then
exit 1
fi
$DOCKER cp amp-builder:/go/bin/amplifier-gateway ./amplifier-gateway >&2
if [ $? -ne 0 ]; then
echo "failed"
exit 1
fi
$DOCKER cp amp-builder:/go/bin/adm-server ./adm-server >&2
if [ $? -ne 0 ]; then
echo "failed"
exit 1
fi
$DOCKER cp amp-builder:/go/bin/adm-agent ./adm-agent >&2

if [ $? -ne 0 ]; then
echo "failed"
exit 1
Expand All @@ -68,8 +79,8 @@ fi
echo "OK"
mv .dockerignore.bak .dockerignore

echo "cleanup... "
rm -f amp amplifier amp-agent amp-log-worker amplifier-gateway
echo -n "cleanup... "
rm -f amp amplifier amp-agent amp-log-worker amplifier-gateway adm-server adm-agent
$DOCKER kill amp-builder >/dev/null 2>&1
$DOCKER rm amp-builder >/dev/null 2>&1
$DOCKER rmi $REPOSITORY_NAME:builder
Expand Down
149 changes: 149 additions & 0 deletions cmd/adm-agent/agentcore/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package agentcore

import (
"fmt"
"math/rand"
"net"
"os"
"os/signal"
"syscall"
"time"

"github.com/appcelerator/amp/cmd/adm-agent/agentgrpc"
"github.com/appcelerator/amp/cmd/adm-server/servergrpc"
//"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

// ClusterAgent struct
type ClusterAgent struct {
id string
realID string
nodeID string
hostname string
address string
dockerClient *client.Client
client servergrpc.ClusterServerServiceClient
conn *grpc.ClientConn
token string
ctx context.Context
healthy bool
}

//Init Connect to docker engine, get initial containers list and start the agent
func (g *ClusterAgent) Init(version string, build string) error {
g.setToken()
g.ctx = context.Background()
g.trapSignal()
conf.init(version, build)
g.id = os.Getenv("HOSTNAME")

// Connection to Docker
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
cli, err := client.NewClient(conf.dockerEngine, "v1.24", nil, defaultHeaders)
if err != nil {
return err
}
g.dockerClient = cli
fmt.Println("Connected to Docker-engine")
g.inspectContainer()
//start GRPC
g.startGRPCServer()
// Connection to server
if err := g.connectServer(); err != nil {
return err
}
fmt.Println("Connected to adm-server")
g.startHeartBeat()
return nil
}

func (g *ClusterAgent) connectServer() error {
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", conf.serverAddr, conf.serverPort),
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithTimeout(time.Second*20))
if err != nil {
return err
}
g.conn = conn
g.client = servergrpc.NewClusterServerServiceClient(conn)
logf.info("Connected to server\n")
ctx := metadata.NewContext(context.Background(), metadata.Pairs("token", g.token))
_, errReg := g.client.RegisterAgent(ctx, &servergrpc.RegisterRequest{
Id: g.id,
NodeId: g.nodeID,
Hostname: g.hostname,
Address: g.address,
})
if errReg != nil {
return fmt.Errorf("Register on server error: %v", errReg)
}
logf.info("Agent registered\n")
g.healthy = true
return nil
}

func (g *ClusterAgent) startGRPCServer() {
serv := grpc.NewServer()
agentgrpc.RegisterClusterAgentServiceServer(serv, g)
logf.info("Starting GRPC server\n")
lis, err := net.Listen("tcp", ":"+conf.grpcPort)
if err != nil {
logf.error("adm-agent is unable to listen on: %s\n%v", ":"+conf.grpcPort, err)
return
}
go func() {
logf.info("adm-agent is listening on port %s\n", conf.grpcPort)
if err := serv.Serve(lis); err != nil {
logf.error("Problem in adm-agent: %s\n", err)
return
}
}()
time.Sleep(2 * time.Second)
}

func (g *ClusterAgent) inspectContainer() {
inspect, err := g.dockerClient.ContainerInspect(g.ctx, g.id)
if err != nil {
logf.error("Error inspecting container: %v\n", err)
return
}
g.realID = inspect.ID
g.nodeID = inspect.Config.Labels["com.docker.swarm.node.id"]
g.address = fmt.Sprintf("%s:%s", inspect.NetworkSettings.Networks["amp-infra"].IPAddress, conf.grpcPort)
logf.debug("Agent: id=%s, address=%s nodeId=%s\n", g.id, g.address, g.nodeID)
}

func (g *ClusterAgent) setToken() {
rand.Seed(time.Now().UnixNano())
b := make([]byte, 64)
rand.Read(b)
g.token = fmt.Sprintf("%x", b)
}

func (g *ClusterAgent) startHeartBeat() {
for {
if _, err := g.client.AgentHealth(g.ctx, &servergrpc.AgentHealthRequest{Id: g.nodeID}); err != nil {
logf.error("Server is not reachable anymore. Exit\n")
os.Exit(1)
}
time.Sleep(10 * time.Second)
}
}

// Launch a routine to catch SIGTERM Signal
func (g *ClusterAgent) trapSignal() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
signal.Notify(ch, syscall.SIGTERM)
go func() {
<-ch
fmt.Println("adm-agent received SIGTERM signal")
g.conn.Close()
os.Exit(1)
}()
}
31 changes: 31 additions & 0 deletions cmd/adm-agent/agentcore/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package agentcore

import (
"fmt"
"net/http"
)

const baseURL = "/api/v1"

type agentAPI struct {
agent *ClusterAgent
}

//Start API server
func (g *agentAPI) initAPI(agent *ClusterAgent) {
g.agent = agent
fmt.Println("Start API server on port " + conf.apiPort)
go func() {
http.HandleFunc(baseURL+"/health", g.agentHealth)
http.ListenAndServe(":"+conf.apiPort, nil)
}()
}

//for HEALTHCHECK Dockerfile instruction
func (g *agentAPI) agentHealth(resp http.ResponseWriter, req *http.Request) {
if g.agent.healthy {
resp.WriteHeader(200)
} else {
resp.WriteHeader(400)
}
}
Loading

0 comments on commit fc727f7

Please sign in to comment.