@@ -3,27 +3,186 @@ package main
3
3
import (
4
4
"fmt"
5
5
"net/http"
6
+ "os"
7
+
8
+ "github.com/Azure/azure-sdk-for-go/arm/compute"
9
+ "github.com/Azure/azure-sdk-for-go/arm/examples/helpers"
10
+ "github.com/Azure/azure-sdk-for-go/arm/network"
11
+ "github.com/Azure/go-autorest/autorest/azure"
6
12
)
7
13
14
+ // AzureWeb interface for all Azure calls
8
15
type AzureWeb struct {
9
16
}
10
17
11
- //TODO: implement these using Azure specific API's
12
-
18
+ // TokenManager obtain the Azure Swarm Manager Token
13
19
func (a AzureWeb ) TokenManager (w http.ResponseWriter , r * http.Request ) {
14
20
RequestInfo (r )
21
+ ip := RequestIP (r )
22
+ inSwarm := alreadyInSwarm (ip )
23
+ isManager := isManagerNode (a , ip )
24
+
25
+ if inSwarm || ! isManager {
26
+ // they are either already in the swarm, or they are not a manager
27
+ w .WriteHeader (http .StatusForbidden )
28
+ fmt .Fprintln (w , "Access Denied" )
29
+ return
30
+ }
15
31
16
- w .WriteHeader (http .StatusNotImplemented )
17
- fmt .Fprintf (w , "Not implmented Yet" )
32
+ // They are not in the swarm, and they are a manager, so good to go.
33
+ cli , ctx := DockerClient ()
34
+ swarm , err := cli .SwarmInspect (ctx )
35
+ if err != nil {
36
+ w .WriteHeader (http .StatusInternalServerError )
37
+ fmt .Fprintf (w , "%v" , err )
38
+ return
39
+ }
18
40
19
- fmt .Println ( "Endpoint Hit: tokenManager" )
41
+ fmt .Fprintf ( w , swarm . JoinTokens . Manager )
20
42
}
21
43
44
+ // TokenWorker obtain the Azure Swarm Worker Token
22
45
func (a AzureWeb ) TokenWorker (w http.ResponseWriter , r * http.Request ) {
46
+ // get the swarm worker token, if they are a worker node,
47
+ // and are not already in the swarm. block otherwise
23
48
RequestInfo (r )
24
49
25
- w .WriteHeader (http .StatusNotImplemented )
26
- fmt .Fprintf (w , "Not implmented Yet" )
50
+ ip := RequestIP (r )
51
+ inSwarm := alreadyInSwarm (ip )
52
+ isWorker := isWorkerNode (a , ip )
53
+
54
+ if inSwarm || ! isWorker {
55
+ // they are either already in the swarm, or they are not a worker
56
+ w .WriteHeader (http .StatusForbidden )
57
+ fmt .Fprintln (w , "Access Denied" )
58
+ return
59
+ }
60
+
61
+ // They are not in the swarm, and they are a worker, so good to go.
62
+ cli , ctx := DockerClient ()
63
+ swarm , err := cli .SwarmInspect (ctx )
64
+ if err != nil {
65
+ w .WriteHeader (http .StatusInternalServerError )
66
+ fmt .Fprintf (w , "%v" , err )
67
+ return
68
+ }
69
+
70
+ fmt .Fprintf (w , swarm .JoinTokens .Worker )
71
+ }
72
+
73
+ // Managers get list of Azure manager instances
74
+ func (a AzureWeb ) Managers () []WebInstance {
75
+ // get the clients for Network and Compute
76
+ env := map [string ]string {
77
+ "AZURE_CLIENT_ID" : os .Getenv ("APP_ID" ),
78
+ "AZURE_CLIENT_SECRET" : os .Getenv ("APP_SECRET" ),
79
+ "AZURE_SUBSCRIPTION_ID" : os .Getenv ("SUBSCRIPTION_ID" ),
80
+ "AZURE_TENANT_ID" : os .Getenv ("TENANT_ID" ),
81
+ "AZURE_GROUP_NAME" : os .Getenv ("GROUP_NAME" ),
82
+ "AZURE_PREFIX" : os .Getenv ("PREFIX" )}
83
+ nicClient , vmssClient := initClients (env )
84
+ // Get list of VMSS Network Interfaces for Managers
85
+ managerIPTable , err := getVMSSNic (nicClient , env , "managervmss" )
86
+ if err != nil {
87
+ fmt .Printf ("Couldn't get Manager Nic for VMSS: %v" , err )
88
+ return []WebInstance {}
89
+ }
90
+ // Get list of VMSS for Managers
91
+ managerVMs , err := getVMSSList (vmssClient , env , "managervmss" , managerIPTable )
92
+ if err != nil {
93
+ fmt .Printf ("Couldn't get List of Manager VMSS: %v" , err )
94
+ }
95
+ return managerVMs
96
+ }
97
+
98
+ // Workers get list of Azure worker instances
99
+ func (a AzureWeb ) Workers () []WebInstance {
100
+ // get the clients for Network and Compute
101
+ env := map [string ]string {
102
+ "AZURE_CLIENT_ID" : os .Getenv ("APP_ID" ),
103
+ "AZURE_CLIENT_SECRET" : os .Getenv ("APP_SECRET" ),
104
+ "AZURE_SUBSCRIPTION_ID" : os .Getenv ("SUBSCRIPTION_ID" ),
105
+ "AZURE_TENANT_ID" : os .Getenv ("TENANT_ID" ),
106
+ "AZURE_GROUP_NAME" : os .Getenv ("GROUP_NAME" ),
107
+ "AZURE_PREFIX" : os .Getenv ("PREFIX" )}
108
+ nicClient , vmssClient := initClients (env )
109
+ // Get list of VMSS Network Interfaces for Managers
110
+ workerIPTable , err := getVMSSNic (nicClient , env , "worker-vmss" )
111
+ if err != nil {
112
+ fmt .Errorf ("Couldn't get Worker Nic for VMSS: %v" , err )
113
+ return []WebInstance {}
114
+ }
115
+ // Get list of VMSS for Managers
116
+ workerVMs , err := getVMSSList (vmssClient , env , "worker-vmss" , workerIPTable )
117
+ if err != nil {
118
+ fmt .Printf ("Couldn't get List of Worker VMSS: %v" , err )
119
+ }
120
+ return workerVMs
121
+ }
122
+
123
+ func initClients (env map [string ]string ) (network.InterfacesClient , compute.VirtualMachineScaleSetVMsClient ) {
124
+
125
+ spt , err := helpers .NewServicePrincipalTokenFromCredentials (env , azure .PublicCloud .ResourceManagerEndpoint )
126
+ if err != nil {
127
+ fmt .Printf ("ERROR: Getting SP token - check that all ENV variables are set" )
128
+ os .Exit (1 )
129
+ }
130
+ // Create Network Interface Client
131
+ nicClient := network .NewInterfacesClient (env ["AZURE_SUBSCRIPTION_ID" ])
132
+ nicClient .Authorizer = spt
133
+ // Create VMSS Client
134
+ vmssClient := compute .NewVirtualMachineScaleSetVMsClient (env ["AZURE_SUBSCRIPTION_ID" ])
135
+ vmssClient .Authorizer = spt
136
+ return nicClient , vmssClient
137
+ }
138
+
139
+ func getVMSSNic (client network.InterfacesClient , env map [string ]string , vmType string ) (IPTable map [string ]string , err error ) {
140
+ vmss := fmt .Sprintf ("%s-%s" , env ["AZURE_PREFIX" ], vmType )
141
+ result , err := client .ListVirtualMachineScaleSetNetworkInterfaces (env ["AZURE_GROUP_NAME" ], vmss )
142
+ if err != nil {
143
+ // Message from an error.
144
+ fmt .Println ("Error: " , err .Error ())
145
+ return IPTable , err
146
+ }
147
+
148
+ IPTable = map [string ]string {}
149
+
150
+ for _ , nic := range * result .Value {
151
+ if * nic .Properties .Primary {
152
+ for _ , ipConfig := range * nic .Properties .IPConfigurations {
153
+ if * ipConfig .Properties .Primary {
154
+ fmt .Printf ("Adding: %s to table at index: %s\n \n " , * ipConfig .Properties .PrivateIPAddress , * nic .ID )
155
+ IPTable [* nic .ID ] = * ipConfig .Properties .PrivateIPAddress
156
+ }
157
+ }
158
+ }
159
+ }
160
+ return IPTable , nil
161
+ }
162
+
163
+ func getVMSSList (client compute.VirtualMachineScaleSetVMsClient , env map [string ]string , vmType string , nicIPTable map [string ]string ) ([]WebInstance , error ) {
164
+ vmss := fmt .Sprintf ("%s-%s" , env ["AZURE_PREFIX" ], vmType )
165
+ vms := []WebInstance {}
166
+
167
+ result , err := client .List (env ["AZURE_GROUP_NAME" ], vmss , "" , "" , "" )
168
+ if err != nil {
169
+ // Message from an error.
170
+ fmt .Println ("Error: " , err .Error ())
171
+ return vms , err
172
+ }
27
173
28
- fmt .Println ("Endpoint Hit: tokenWorker" )
174
+ for _ , vm := range * result .Value {
175
+ nics := * vm .Properties .NetworkProfile .NetworkInterfaces
176
+ privateIP := nicIPTable [* nics [0 ].ID ]
177
+ newVM := WebInstance {
178
+ ID : * vm .ID ,
179
+ InstanceID : * vm .InstanceID ,
180
+ InstanceName : * vm .Name ,
181
+ InstanceType : * vm .Type ,
182
+ InstanceNic : * nics [0 ].ID ,
183
+ PrivateIPAddress : privateIP ,
184
+ InstanceState : * vm .Properties .ProvisioningState }
185
+ vms = append (vms , newVM )
186
+ }
187
+ return vms , nil
29
188
}
0 commit comments