-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.py
More file actions
118 lines (106 loc) · 3.71 KB
/
deploy.py
File metadata and controls
118 lines (106 loc) · 3.71 KB
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
#!/usr/bin/env python3
import yaml
import os
import subprocess
# Below is the config.yaml template
"""
# Global
# eg. http://192.168.0.1:7890
proxy: ""
######## Config for docker image build ########
# Force build docker image every time
force_build: false
tag_name: "openeuler/kuasar:latest"
######## Config for running a container ########
# Workspace mounted into docker environment
workspace: "/workspace"
# Virtual block image for creating devicemapper
block_img: "~/kuasar_env.img"
# MB
block_size: 5000
# Loop device to mount block image
loop: "/dev/loop123"
# LVM group name for devicemapper
group: "isulad0"
# Container name for development environment
name: "kuasar_env"
# Container name for development environment
vmm_task: ""
"""
dir_path = os.path.dirname(os.path.realpath(__file__))
script_dir = os.path.join(dir_path, 'scripts')
config_path = os.path.join(dir_path, 'config.yaml')
def check_image(config):
# check if tag_name is already exist
repo_name = config.get('tag_name').split(':')[0]
process = subprocess.run(['docker', 'images'], stdout=subprocess.PIPE, universal_newlines=True)
for line in process.stdout.split('\n'):
if repo_name in line:
return True
return False
def build_image(config):
# check if tag_name is already exist
to_build = False
if not check_image(config):
to_build = True
if config.get('force_build'):
to_build = True
if to_build:
build_script = os.path.join(script_dir, 'build.sh')
process = subprocess.run(['bash', build_script, '-p', config.get('proxy'), '-t', config.get('tag_name')],
universal_newlines=True)
if process.returncode != 0:
print('Build docker image failed!')
return False
print('Build docker image successfully!')
else:
print('Docker image already exist!')
return True
def check_container(config):
# check if container is already exist
process = subprocess.run(['docker', 'ps', '-a'], stdout=subprocess.PIPE, universal_newlines=True)
for line in process.stdout.split('\n'):
if config.get('name') in line:
return True
return False
def run_container(config):
if check_container(config):
print('Container already exist!')
return True
run_script = os.path.join(script_dir, 'start.sh')
print(run_script)
cmd_list = ['bash', run_script, '-n', config.get('name'),
'-p', config.get('proxy'), '-w', config.get('workspace'),
'-b', config.get('block_img'), '-s', str(config.get('block_size')),
'-l', config.get('loop'), '-g', config.get('group'),
'-i', config.get('tag_name'), '-t', config.get("vmm_task")]
process = subprocess.run(cmd_list, universal_newlines=True)
if process.returncode != 0:
print('Run container failed!')
return False
print('Run container successfully!')
return True
# Write a main function
def main():
# config.yaml is in the same directory as this script, get the realpath path of config.yaml
with open(config_path) as f:
config = yaml.safe_load(f)
if not build_image(config):
return
# check if container is already exist
if not run_container(config):
return
print('############ Tips ############')
print('Now you can enter the container by running:')
print('docker exec -it {} /bin/bash'.format(config.get('name')))
print('')
print('To start sandboxer in container:')
print('start-sandboxer')
print('')
print('To stop vmm sandboxer in container:')
print('kill-sandboxer')
print('')
print('To start isuald in container:')
print('isulad')
if __name__ == '__main__':
main()