Skip to content

Commit 0235f8e

Browse files
committed
fixed comments from diogo
Signed-off-by: Ken Cochrane <[email protected]>
1 parent e7538d4 commit 0235f8e

File tree

9 files changed

+84
-33
lines changed

9 files changed

+84
-33
lines changed

aws/dockerfiles/build_and_push_all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -e
55
NAMESPACE="${NAMESPACE:-docker4x}"
66
VERSION="${VERSION:-latest}"
77

8-
for IMAGE in shell init guide ddc-init cloud
8+
for IMAGE in shell init guide ddc-init cloud meta
99
do
1010
FINAL_IMAGE="${NAMESPACE}/${IMAGE}-aws:${VERSION}"
1111
docker build -t "${FINAL_IMAGE}" -f "Dockerfile.${IMAGE}" .

aws/release/full_aws_release.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ echo "= Build Buoy ="
172172
cd ../../tools/buoy
173173
./build_buoy.sh
174174

175+
echo "= Build Metaserver ="
176+
cd ../metaserver
177+
./build.sh
178+
175179
cd $BASE_DIR
176180
cd ../dockerfiles
177181

aws/release/nightly.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export VERSION=aws-v$DOCKER_VERSION-$EDITION_VERSION
7878
cd $BUILD_HOME/code/editions/tools/buoy/
7979
./build_buoy.sh
8080

81+
cd $BUILD_HOME/code/editions/tools/metaserver/
82+
./build.sh
83+
8184
cd $BUILD_HOME/code/editions/aws/dockerfiles/
8285

8386
# build images

aws/release/run_ddc_release.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ CURRPATH=`pwd`
113113
cd ../../tools/buoy/
114114
./build_buoy.sh
115115

116+
cd ../metaserver/
117+
./build.sh
118+
116119
echo "=== CURRPATH=$CURRPATH ==="
117120
# back to release dir
118121
cd $CURRPATH

tools/metaserver/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
default: build
2+
3+
build:
4+
./build.sh

tools/metaserver/compile.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
set -e
44

55
cd /go/src/metaserver
6+
go vet
67
go build
78
cp metaserver /go/bin

tools/metaserver/src/metaserver/aws.go

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/aws/aws-sdk-go/service/ec2"
1212
)
1313

14-
type AwsInstance struct {
14+
type awsInstance struct {
1515
InstanceID string
1616
InstanceType string
1717
PublicIPAddress string
@@ -27,10 +27,11 @@ func (a AWSWeb) TokenManager(w http.ResponseWriter, r *http.Request) {
2727
// get the swarm manager token, if they are a manager node,
2828
// and are not already in the swarm. Block otherwise
2929
RequestInfo(r)
30-
found := alreadyInSwarm(r)
31-
isManager := isManagerNode(r)
30+
ip := RequestIP(r)
31+
inSwarm := alreadyInSwarm(ip)
32+
isManager := isManagerNode(ip)
3233

33-
if found || !isManager {
34+
if inSwarm || !isManager {
3435
// they are either already in the swarm, or they are not a manager
3536
w.WriteHeader(http.StatusForbidden)
3637
fmt.Fprintln(w, "Access Denied")
@@ -43,6 +44,7 @@ func (a AWSWeb) TokenManager(w http.ResponseWriter, r *http.Request) {
4344
if err != nil {
4445
w.WriteHeader(http.StatusInternalServerError)
4546
fmt.Fprintf(w, "%v", err)
47+
return
4648
}
4749

4850
fmt.Fprintf(w, swarm.JoinTokens.Manager)
@@ -53,10 +55,11 @@ func (a AWSWeb) TokenWorker(w http.ResponseWriter, r *http.Request) {
5355
// and are not already in the swarm. block otherwise
5456
RequestInfo(r)
5557

56-
found := alreadyInSwarm(r)
57-
isWorker := isWorkerNode(r)
58+
ip := RequestIP(r)
59+
inSwarm := alreadyInSwarm(ip)
60+
isWorker := isWorkerNode(ip)
5861

59-
if found || !isWorker {
62+
if inSwarm || !isWorker {
6063
// they are either already in the swarm, or they are not a worker
6164
w.WriteHeader(http.StatusForbidden)
6265
fmt.Fprintln(w, "Access Denied")
@@ -69,40 +72,36 @@ func (a AWSWeb) TokenWorker(w http.ResponseWriter, r *http.Request) {
6972
if err != nil {
7073
w.WriteHeader(http.StatusInternalServerError)
7174
fmt.Fprintf(w, "%v", err)
75+
return
7276
}
7377

7478
fmt.Fprintf(w, swarm.JoinTokens.Worker)
7579
}
7680

77-
func alreadyInSwarm(r *http.Request) bool {
81+
func alreadyInSwarm(ip string) bool {
7882
// Is the node making the request, already in the swarm.
79-
ip := RequestIP(r)
8083
nodes := SwarmNodes()
8184
for _, node := range nodes {
82-
nodeIP := convertAWSHostToIP(node.Description.Hostname)
85+
nodeIP := ConvertAWSHostToIP(node.Description.Hostname)
8386
if ip == nodeIP {
8487
return true
8588
}
8689
}
8790
return false
8891
}
8992

90-
func isManagerNode(r *http.Request) bool {
93+
func isManagerNode(ip string) bool {
9194
// Is the node making the request a manager node
92-
ip := RequestIP(r)
93-
instances := awsManagers()
94-
for _, instance := range instances {
95-
if ip == instance.PrivateIPAddress {
96-
return true
97-
}
98-
}
99-
return false
95+
return isNodeInList(ip, awsManagers())
10096
}
10197

102-
func isWorkerNode(r *http.Request) bool {
98+
func isWorkerNode(ip string) bool {
10399
// Is the node making the request a worker node
104-
ip := RequestIP(r)
105-
instances := awsWorkers()
100+
return isNodeInList(ip, awsWorkers())
101+
}
102+
103+
func isNodeInList(ip string, instances []awsInstance) bool {
104+
// given an IP, find out if it is in the instance list.
106105
for _, instance := range instances {
107106
if ip == instance.PrivateIPAddress {
108107
return true
@@ -111,9 +110,8 @@ func isWorkerNode(r *http.Request) bool {
111110
return false
112111
}
113112

114-
func awsWorkers() []AwsInstance {
113+
func awsWorkers() []awsInstance {
115114
// get the instances from AWS worker security group
116-
117115
customFilter := []*ec2.Filter{
118116
&ec2.Filter{
119117
Name: aws.String("tag:swarm-node-type"),
@@ -130,9 +128,8 @@ func awsWorkers() []AwsInstance {
130128
return awsInstances(customFilter)
131129
}
132130

133-
func awsManagers() []AwsInstance {
131+
func awsManagers() []awsInstance {
134132
// get the instances from AWS Manager security group
135-
136133
customFilter := []*ec2.Filter{
137134
&ec2.Filter{
138135
Name: aws.String("tag:swarm-node-type"),
@@ -149,9 +146,8 @@ func awsManagers() []AwsInstance {
149146
return awsInstances(customFilter)
150147
}
151148

152-
func awsInstances(customFilters []*ec2.Filter) []AwsInstance {
149+
func awsInstances(customFilters []*ec2.Filter) []awsInstance {
153150
// get the instances from AWS, takes a filter to limit the results.
154-
155151
client := ec2.New(session.New(&aws.Config{}))
156152

157153
// Only grab instances that are running or just started
@@ -177,24 +173,24 @@ func awsInstances(customFilters []*ec2.Filter) []AwsInstance {
177173
fmt.Println(err.Error())
178174
}
179175

180-
var instances []AwsInstance
176+
var instances []awsInstance
181177
for _, reservation := range result.Reservations {
182178
for _, instance := range reservation.Instances {
183-
awsInstance := AwsInstance{
179+
aInstance := awsInstance{
184180
InstanceID: *instance.InstanceId,
185181
InstanceType: *instance.InstanceType,
186182
PublicIPAddress: *instance.PublicIpAddress,
187183
PrivateIPAddress: *instance.PrivateIpAddress,
188184
InstanceState: *instance.State.Name,
189185
InstanceAZ: *instance.Placement.AvailabilityZone,
190186
}
191-
instances = append(instances, awsInstance)
187+
instances = append(instances, aInstance)
192188
}
193189
}
194190
return instances
195191
}
196192

197-
func convertAWSHostToIP(hostStr string) string {
193+
func ConvertAWSHostToIP(hostStr string) string {
198194
// This is risky, this assumes the following formation for hosts in swarm node ls
199195
// ip-10-0-3-149.ec2.internal
200196
// there was one use case when someone had an old account, and their hostnames were not
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestConvertAWSHostToIP(t *testing.T) {
6+
if ConvertAWSHostToIP("ip-10-0-3-149.ec2.internal") != "10.0.3.149" {
7+
t.Error("Expected 10.0.3.149")
8+
}
9+
if ConvertAWSHostToIP("ip-10-0-3-149") != "10.0.3.149" {
10+
t.Error("Expected 10.0.3.149")
11+
}
12+
}

tools/metaserver/src/metaserver/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"net/http"
8+
"os"
89
"time"
910

1011
"github.com/gorilla/mux"
@@ -77,12 +78,39 @@ func handleRequests(flavor string) {
7778
log.Fatal(srv.ListenAndServe())
7879
}
7980

81+
func validate(flavor string) {
82+
// validate that we have everything we need in order to start. If anything is missing.
83+
// Then exit.
84+
if flavor == "aws" {
85+
// make sure our required ENV variables are available, if not fail.
86+
workerGroupId := os.Getenv("WORKER_SECURITY_GROUP_ID")
87+
managerGroupId := os.Getenv("MANAGER_SECURITY_GROUP_ID")
88+
if workerGroupId == "" {
89+
fmt.Printf("ERROR: Missing environment variable: WORKER_SECURITY_GROUP_ID.")
90+
os.Exit(1)
91+
}
92+
if managerGroupId == "" {
93+
fmt.Printf("ERROR: Missing environment variable: MANAGER_SECURITY_GROUP_ID")
94+
os.Exit(1)
95+
}
96+
} else if flavor == "azure" {
97+
// add azure validation code here.
98+
99+
} else {
100+
fmt.Printf("ERROR: -flavor %v was not a valid option. please pick either 'aws' or 'azure'", flavor)
101+
os.Exit(1)
102+
}
103+
104+
}
105+
80106
func main() {
81107
// pass in the flavor to determine which IAAS provider we are on.
82108
// currently defaults to AWS, since that is the only one implemnted
83109
flavor := flag.String("flavor", "aws", "IAAS Flavor (aws, azure, etc)")
84110
flag.Parse()
85111

112+
// make sure we are good to go, before we fully start up.
113+
validate(*flavor)
86114
// lets handle those requests
87115
handleRequests(*flavor)
88116
}

0 commit comments

Comments
 (0)