@@ -3,27 +3,187 @@ 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_VMSS_MGR" : os .Getenv ("VMSS_MGR" ),
83
+ "AZURE_VMSS_WRK" : os .Getenv ("VMSS_WRK" )}
84
+ nicClient , vmssClient := initClients (env )
85
+ // Get list of VMSS Network Interfaces for Managers
86
+ managerIPTable , err := getVMSSNic (nicClient , env , env ["AZURE_VMSS_MGR" ])
87
+ if err != nil {
88
+ fmt .Printf ("Couldn't get Manager Nic for VMSS: %v" , err )
89
+ return []WebInstance {}
90
+ }
91
+ // Get list of VMSS for Managers
92
+ managerVMs , err := getVMSSList (vmssClient , env , env ["AZURE_VMSS_MGR" ], managerIPTable )
93
+ if err != nil {
94
+ fmt .Printf ("Couldn't get List of Manager VMSS: %v" , err )
95
+ return []WebInstance {}
96
+ }
97
+ return managerVMs
98
+ }
99
+
100
+ // Workers get list of Azure worker instances
101
+ func (a AzureWeb ) Workers () []WebInstance {
102
+ // get the clients for Network and Compute
103
+ env := map [string ]string {
104
+ "AZURE_CLIENT_ID" : os .Getenv ("APP_ID" ),
105
+ "AZURE_CLIENT_SECRET" : os .Getenv ("APP_SECRET" ),
106
+ "AZURE_SUBSCRIPTION_ID" : os .Getenv ("SUBSCRIPTION_ID" ),
107
+ "AZURE_TENANT_ID" : os .Getenv ("TENANT_ID" ),
108
+ "AZURE_GROUP_NAME" : os .Getenv ("GROUP_NAME" ),
109
+ "AZURE_VMSS_MGR" : os .Getenv ("VMSS_MGR" ),
110
+ "AZURE_VMSS_WRK" : os .Getenv ("VMSS_WRK" )}
111
+ nicClient , vmssClient := initClients (env )
112
+ // Get list of VMSS Network Interfaces for Managers
113
+ workerIPTable , err := getVMSSNic (nicClient , env , env ["AZURE_VMSS_WRK" ])
114
+ if err != nil {
115
+ fmt .Printf ("Couldn't get Worker Nic for VMSS: %v" , err )
116
+ return []WebInstance {}
117
+ }
118
+ // Get list of VMSS for Managers
119
+ workerVMs , err := getVMSSList (vmssClient , env , env ["AZURE_VMSS_WRK" ], workerIPTable )
120
+ if err != nil {
121
+ fmt .Printf ("Couldn't get List of Worker VMSS: %v" , err )
122
+ return []WebInstance {}
123
+ }
124
+ return workerVMs
125
+ }
126
+
127
+ func initClients (env map [string ]string ) (network.InterfacesClient , compute.VirtualMachineScaleSetVMsClient ) {
128
+
129
+ spt , err := helpers .NewServicePrincipalTokenFromCredentials (env , azure .PublicCloud .ResourceManagerEndpoint )
130
+ if err != nil {
131
+ fmt .Printf ("ERROR: Getting SP token - check that all ENV variables are set" )
132
+ os .Exit (1 )
133
+ }
134
+ // Create Network Interface Client
135
+ nicClient := network .NewInterfacesClient (env ["AZURE_SUBSCRIPTION_ID" ])
136
+ nicClient .Authorizer = spt
137
+ // Create VMSS Client
138
+ vmssClient := compute .NewVirtualMachineScaleSetVMsClient (env ["AZURE_SUBSCRIPTION_ID" ])
139
+ vmssClient .Authorizer = spt
140
+ return nicClient , vmssClient
141
+ }
142
+
143
+ func getVMSSNic (client network.InterfacesClient , env map [string ]string , vmss string ) (IPTable map [string ]string , err error ) {
144
+ result , err := client .ListVirtualMachineScaleSetNetworkInterfaces (env ["AZURE_GROUP_NAME" ], vmss )
145
+ if err != nil {
146
+ // Message from an error.
147
+ fmt .Println ("Error: " , err .Error ())
148
+ return IPTable , err
149
+ }
150
+
151
+ IPTable = map [string ]string {}
152
+
153
+ for _ , nic := range * result .Value {
154
+ if * nic .Properties .Primary {
155
+ for _ , ipConfig := range * nic .Properties .IPConfigurations {
156
+ if * ipConfig .Properties .Primary {
157
+ IPTable [* nic .ID ] = * ipConfig .Properties .PrivateIPAddress
158
+ }
159
+ }
160
+ }
161
+ }
162
+ return IPTable , nil
163
+ }
164
+
165
+ func getVMSSList (client compute.VirtualMachineScaleSetVMsClient , env map [string ]string , vmss string , nicIPTable map [string ]string ) ([]WebInstance , error ) {
166
+ vms := []WebInstance {}
167
+
168
+ result , err := client .List (env ["AZURE_GROUP_NAME" ], vmss , "" , "" , "" )
169
+ if err != nil {
170
+ // Message from an error.
171
+ fmt .Println ("Error: " , err .Error ())
172
+ return vms , err
173
+ }
27
174
28
- fmt .Println ("Endpoint Hit: tokenWorker" )
175
+ for _ , vm := range * result .Value {
176
+ nics := * vm .Properties .NetworkProfile .NetworkInterfaces
177
+ privateIP := nicIPTable [* nics [0 ].ID ]
178
+ newVM := WebInstance {
179
+ ID : * vm .ID ,
180
+ InstanceID : * vm .InstanceID ,
181
+ InstanceName : * vm .Name ,
182
+ InstanceType : * vm .Type ,
183
+ InstanceNic : * nics [0 ].ID ,
184
+ PrivateIPAddress : privateIP ,
185
+ InstanceState : * vm .Properties .ProvisioningState }
186
+ vms = append (vms , newVM )
187
+ }
188
+ return vms , nil
29
189
}
0 commit comments