-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_docker_control.py
177 lines (126 loc) · 6.49 KB
/
old_docker_control.py
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
# docker_control.py
# Docker Controls from Python
# Mostly called from hw3_test.py to start containers
# can be used from command line (largely for testing specific executions by hand)
#last update: 11/17/18 - altered to use subnets, because Linux and Mac need them
# - also will spin up multiple containers from comand line at once now
import subprocess
import os
import sys
import time
import argparse
class docker_controller:
sudo = []
spinUpTime = 5
verbose = False
def __init__(self,mynet, needSudo=False):
self.mynet = mynet
if needSudo:
self.sudo = ["sudo"]
def dPrint(self, string, printIt, indentation=0):
if printIt:
print("%s%s"%("\t"*indentation, string))
def buildDockerImage(self, tag):
subprocess.run(self.sudo+["docker", "build", "-t", tag, "."])
def spinUpDockerContainerNoWait(self, tag, hostIp, networkIP, port, view):
#print("spinning docker container: %s:%s"%(hostIp, port))
instance = {"testScriptAddress":hostIp+":"+port,
"networkIpPortAddress": networkIP+":8080"}
# print(" ".join(self.sudo+["docker", "run",
# "-p", "%s:8080"%port,
# "--net=%s"%self.mynet,
# "--ip=%s"%networkIP,
# "-e", "VIEW=%s"%view,
# "-e", "IP_PORT=%s:8080"%networkIP,
# "-d", tag]))
instance["containerID"] = subprocess.getoutput(" ".join(self.sudo+["docker", "run",
"-p", "%s:8080"%port,
"--net=%s"%self.mynet,
"--ip=%s"%networkIP,
"-e", "VIEW=%s"%view,
"-e", "IP_PORT=%s:8080"%networkIP,
"-d", tag]))
if " " in instance["containerID"]:
print(instance["containerID"])
self.dPrint(instance["containerID"], self.verbose, 1)
return instance
def spinUpDockerContainer(self, tag, hostIp, networkIP, port, view):
instance = self.spinUpDockerContainerNoWait(tag, hostIp, networkIP, port, view)
time.sleep(self.spinUpTime)
#print("completed spinning docker container %s"%instance)
return instance
def spinUpManyContainers(self, tag, host_ip, network_ip_prefix, port_prefix, number):
view = []
for i in range(2, 2+number):
view.append( "%s%s:%s0"%(network_ip_prefix, i, port_prefix) )
viewString = ",".join(view)
view = []
for i in range(2, 2+number):
view.append(self.spinUpDockerContainerNoWait(tag, host_ip, network_ip_prefix+str(i), port_prefix+str(i), viewString))
time.sleep(self.spinUpTime)
#print("completed spinning docker containers")
return view
def partitionContainer(self, instance):
output = subprocess.getoutput(self.sudo + ["docker", "network", "connect", ])
def cleanUpDockerContainer(self, instance=None):
if instance == None:
#print("cleaning up all docker containers")
instance = subprocess.getoutput("sudo docker ps -q")
instance = instance.split("\n")
for inst in instance:
output = subprocess.getoutput(" ".join(self.sudo+["docker", "kill", inst]))
self.dPrint(output, self.verbose, 1)
else:
#print("cleaning up container %s"%instance)
output = subprocess.getoutput(" ".join(self.sudo+["docker", "kill", instance]))
self.dPrint(output, self.verbose, 1)
#print("done cleaning")
if __name__ == '__main__':
#change these variables to change default values
standardPortPrefix = "404"
standardIP = "localhost"
standardNetworkIPPrefix = "192.168.0."
standardBuildTag = "testing"
standardNetworkName = "mynetwork"
parser = argparse.ArgumentParser(description='docker controller')
parser.add_argument('-K', dest="is_kill_mode", action="store_true",
help="kill all docker containers that are running")
parser.add_argument('-B', dest="is_build_mode", action="store_true",
help="build a docker image")
parser.add_argument('-S', dest="is_start_mode", action="store_true",
help="start up a docker container")
parser.add_argument('-t', dest="buildTag", default=standardBuildTag,
help="set the build tag. If unset, tag will be: %s"%standardBuildTag)
parser.add_argument('-n', dest="number", default=1,
help="set number of containers to start. If unset, only one will start")
parser.add_argument('--port', dest="localPortPrefix", default=standardPortPrefix,
help="set the port to start your container on. Only used with -S. If unset will be: %s"%standardPortPrefix)
parser.add_argument('--hostIp', dest="hostIp", default=standardIP,
help="set the ip of your host machine. This is the address you should send curl requests to."\
+" Only used with -S. If unset will be: %s"%standardIP)
parser.add_argument('--networkIP', dest="networkIpPrefix", default=standardNetworkIPPrefix,
help="set the ip prefix of your network (everything up to the last period). This is the begining of the address your "\
+ "containers will use to talk to each other. Only used with -S. If unset will be: %s"%standardNetworkIPPrefix)
parser.add_argument('--net', dest="network", default=standardNetworkName,
help="the name of your network. Only used with -S. If unset will be: %s"%standardNetworkName)
parser.add_argument('-v', dest="verbose_mode", action="store_true",
help="print everything docker would print normally")
args = parser.parse_args()
kill = args.is_kill_mode
build = args.is_build_mode
start = args.is_start_mode
verbose = args.verbose_mode
dockerBuildTag = args.buildTag
containerNumber = int(args.number)
localPortPrefix = args.localPortPrefix
hostIp = args.hostIp
networkIpPrefix = args.networkIpPrefix
networkName = args.network
dc = docker_controller(networkName)
dc.verbose = verbose
if kill:
dc.cleanUpDockerContainer()
if build:
dc.buildDockerImage(dockerBuildTag)
if start:
dc.spinUpManyContainers(dockerBuildTag, hostIp, networkIpPrefix, localPortPrefix, containerNumber)