-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
335 lines (275 loc) · 9.7 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package service
import (
"context"
"fmt"
"log"
"math"
"math/rand"
"strconv"
"strings"
"bufio"
"os"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/container"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/versions"
"github.com/phayes/freeport"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type version struct {
image string
replication int
servers []string
}
func findThisVersion(name string, allVersions []version) version {
var v version
for _, element := range allVersions {
if element.image == name {
return element
}
}
return v
}
func newCreateCommand(dockerCli command.Cli) *cobra.Command {
//////////////////////////////////////////////////////////////////////////
///////////////////////////// START OF CHANGES ///////////////////////////
//////////////////////////////////////////////////////////////////////////
var runOpts container.RunOptions
var copts *container.ContainerOptions
execOpts := container.NewExecOptions()
var restartOpts container.RestartOptions
cmd := &cobra.Command{
Use: "create [OPTIONS] IMAGE [COMMAND] [ARG...]",
Short: "Create a new service",
Args: cli.RequiresMinArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// The format of the command:
// ./build/docker service create e env_var=value hostIP network nginxName port ruleSet image1 rep1 memory1 swap1 cpu1 ...
// list of enviroment variables
index := 0
my_env := []string{}
for true {
if args[index] == "e" {
index++
my_env = append(my_env, args[index])
index++
} else {
break
}
}
hostIP := args[index] // Host IP address that the containers will be created on
index++
network := args[index] // The name of an Overlay network to connect containers together
index++
nginxName := args[index] // The name of the service (The NGINX container)
index++
containerPort := args[index] // The port that the containers of the service listen on and need to expose it
index++
ruleAddress := args[index] // The absolute address of the file containing the rules for the load balancer
index++
nginx_image := "sgholami/nginx-monitoring" // The NGINX image with monitoring
// Assigning the NGINX a random port
nginxPort, err2 := freeport.GetFreePort()
nginxPortStr := strconv.Itoa(nginxPort)
if err2 != nil {
log.Fatal(err2)
}
// Checking if a specific service port is mentioned
// if not use the random port
for _, element := range my_env {
env := strings.Split(element, "=")
if env[0] == "SERVICE_PORT" {
nginxPortStr = env[1]
}
}
// Creating containers
var containers_version []version
servers := ""
for i := index; i < len(args); i++ {
var v version
copts.Image = args[i]
v.image = args[i]
i++
rep, _ := strconv.Atoi(args[i])
v.replication = rep
i++
memory := args[i] // The memory limit of this version of containers, if do not want to specify it type "_"
i++
swap := args[i] // The swap memory limit of this version of containers, if do not want to specify it type "_"
i++
cpu := args[i] // The CPU limit of this version of containers, if do not want to specify it type "_"
for j := 0; j < rep; j++ {
temp, err3 := freeport.GetFreePort()
tmpPort := strconv.Itoa(temp)
if err3 != nil {
log.Fatal(err3)
}
// Set the published port value
copts.Publish.Clear()
copts.Publish.Set(tmpPort + ":" + containerPort)
v.servers = append(v.servers, hostIP + ":" + tmpPort)
// Set the network value
copts.NetMode = network
// Set the memory limit value
if memory != "_" {
copts.Memory.Set(memory)
}
// Set the swap memory limit value
if swap != "_" {
copts.MemorySwap.Set(swap)
}
// Set the CPU limit value
if cpu != "_" {
copts.Cpus.Set(cpu)
}
// Set the enviroment variables value
copts.Env.Clear()
for _, element := range my_env {
copts.Env.Set(element)
}
// Assign a name to the container
contName := nginxName + "_" + randomString(8)
runOpts.CreateOption.Name = contName
container.RunRun(dockerCli, cmd.Flags(), &runOpts, copts)
// Keeping the servers for NGINX config file
servers += "server " + hostIP + ":" + tmpPort + "; "
}
containers_version = append(containers_version, v)
}
// Creating the rule set
ruleSet := ""
file, err := os.Open(ruleAddress)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// Reading each line of rules
line := scanner.Text()
serverWeight := ""
// parts[0] is the condition which is the same in rule-set
// part[1] is the servers which should be updated
parts:= strings.Split(line, " , ")
upstreams := strings.Split(parts[1], ";")
for l, element := range upstreams {
if l < len(upstreams) - 1 {
e := strings.TrimSpace(element)
// upstream_parts[0] is version, not used
// upstream_parts[1] is the image name
// upstream_parts[2] is the percentage
upstreamParts := strings.Split(e, " ")
cont_v := findThisVersion(upstreamParts[1], containers_version)
temp := strings.Split(upstreamParts[2], "=")
totalWeight, _ := strconv.Atoi(temp[1])
weight := int(math.Ceil(float64(totalWeight) / float64(len(cont_v.servers))))
for _, server := range cont_v.servers {
serverWeight = serverWeight + " server " + server + " weight=" + strconv.Itoa(weight) + ";"
}
}
}
// Writing the rule
ruleSet = ruleSet + parts[0] + " ," + serverWeight + "\n"
}
ruleSet = strings.TrimSpace(ruleSet)
// NGINX configuration
var nginxOpts []string
copts.Args = nginxOpts
copts.Image = nginx_image
copts.Publish.Clear()
copts.Publish.Set(nginxPortStr + ":80") // NGINX listens on port 80. hostport:containerport
runOpts.CreateOption.Name = nginxName
container.RunRun(dockerCli, cmd.Flags(), &runOpts, copts)
execOpts.Container = nginxName
// The content of the config file of the NGINX
conf := "echo 'upstream servers {" + servers + "} server {listen 80 default_server; access_log /var/log/nginx/access.log main; server_name _; location / { proxy_pass http://servers;}}' > /etc/nginx/conf.d/default.conf"
conf2 := "echo '" + ruleSet + "' > rules.txt"
execOpts.Command = []string{"sh", "-c", conf + "&&" + conf2}
container.RunExec(dockerCli, execOpts)
restartOpts.Containers = []string{nginxName}
restartOpts.NSecondsChanged = cmd.Flags().Changed("time")
container.RunRestart(dockerCli, &restartOpts)
//////////////////////////////////////////////////////////////////////////
///////////////////////////// END OF CHANGES /////////////////////////////
//////////////////////////////////////////////////////////////////////////
},
}
flags := cmd.Flags()
flags.SetInterspersed(false)
// These are flags not stored in Config/HostConfig
flags.BoolVarP(&runOpts.Detach, "detach", "d", true, "Run container in background and print container ID")
flags.BoolVar(&runOpts.SigProxy, "sig-proxy", true, "Proxy received signals to the process")
flags.StringVar(&runOpts.CreateOption.Name, "name", "", "Assign a name to the container")
flags.StringVar(&runOpts.DetachKeys, "detach-keys", "", "Override the key sequence for detaching a container")
// Add an explicit help that doesn't have a `-h` to prevent the conflict
// with hostname
flags.Bool("help", false, "Print usage")
command.AddPlatformFlag(flags, &runOpts.CreateOption.Platform)
command.AddTrustVerificationFlags(flags, &runOpts.CreateOption.Untrusted, dockerCli.ContentTrustEnabled())
copts = container.AddFlags(flags)
return cmd
}
func randomString(len int) string {
bytes := make([]byte, len)
for i := 0; i < len; i++ {
bytes[i] = byte(65 + rand.Intn(25)) //A=65 and Z = 65+25
}
return string(bytes)
}
func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, opts *serviceOptions) error {
apiClient := dockerCli.Client()
createOpts := types.ServiceCreateOptions{}
ctx := context.Background()
service, err := opts.ToService(ctx, apiClient, flags)
if err != nil {
return err
}
specifiedSecrets := opts.secrets.Value()
if len(specifiedSecrets) > 0 {
// parse and validate secrets
secrets, err := ParseSecrets(apiClient, specifiedSecrets)
if err != nil {
return err
}
service.TaskTemplate.ContainerSpec.Secrets = secrets
}
specifiedConfigs := opts.configs.Value()
if len(specifiedConfigs) > 0 {
// parse and validate configs
configs, err := ParseConfigs(apiClient, specifiedConfigs)
if err != nil {
return err
}
service.TaskTemplate.ContainerSpec.Configs = configs
}
if err := resolveServiceImageDigestContentTrust(dockerCli, &service); err != nil {
return err
}
// only send auth if flag was set
if opts.registryAuth {
// Retrieve encoded auth token from the image reference
encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, opts.image)
if err != nil {
return err
}
createOpts.EncodedRegistryAuth = encodedAuth
}
// query registry if flag disabling it was not set
if !opts.noResolveImage && versions.GreaterThanOrEqualTo(apiClient.ClientVersion(), "1.30") {
createOpts.QueryRegistry = true
}
response, err := apiClient.ServiceCreate(ctx, service, createOpts)
if err != nil {
return err
}
for _, warning := range response.Warnings {
fmt.Fprintln(dockerCli.Err(), warning)
}
fmt.Fprintf(dockerCli.Out(), "%s\n", response.ID)
if opts.detach || versions.LessThan(apiClient.ClientVersion(), "1.29") {
return nil
}
return waitOnService(ctx, dockerCli, response.ID, opts.quiet)
}