forked from iory/docker-ros-realsense
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
162 lines (137 loc) · 5.24 KB
/
fabfile.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
from fabric.api import local
from fabric.decorators import task
from os.path import dirname, realpath, join
from functools import wraps
project_suffix = 'ros-'
# def name_assertion(func):
# @wraps(func)
# def function_wrapper(*args, **kwargs):
# try:
# assert not ('docker-' in project_name or 'ros-' in project_name)
# func(*args, **kwargs)
# except AssertionError:
# print("Project name should be stripped of 'docker-ros' suffix")
# return func(*args, **kwargs)
# return function_wrapper
def name_auto_fix(func):
@wraps(func)
def function_wrapper(*args, **kwargs):
args_copy = list(args)
project_name = args_copy[0]
if 'docker-' in project_name:
project_name = project_name[len('docker-'):]
if 'ros-' in project_name:
project_name = project_name[len('ros-'):]
print("Project name should be stripped of 'docker-ros' suffix.")
print("Auto fix result: {}".format(project_name))
args_copy[0] = project_name
# print('args:', args_copy, kwargs)
return func(*args_copy, **kwargs)
return function_wrapper
@task
@name_auto_fix
def docker_exec(project_name, cmdline='/bin/bash'):
"""
Execute command in running docker container
:param project_name: project to build. Should be stripped of 'docker-ros' suffix
:param cmdline: command to be executed
"""
local('docker exec -ti ros-{project_name} {cmdline}'.format(project_name=project_name, cmdline=cmdline))
@task
@name_auto_fix
def docker_build(project_name, options=''):
"""
Build docker image
:param project_name: project to build. Should be stripped of 'docker-ros' suffix
:param options: other optionss
"""
docker_stop(project_name)
local('docker build {options} -t yuxianggao/ros-{project_name}:$(dpkg --print-architecture) ./dockers/ros-{project_name}'.format(options=options, project_name=project_name))
@task
@name_auto_fix
def docker_start(project_name, map_repos=False, detach=False, cmd=False):
"""
Start docker container
:param project_name: project to build. Should be stripped of 'docker-ros' suffix
:param map_repos: wether mount ./package to catkin_ws/src
"""
docker_stop(project_name)
detach = '-d' if detach else ''
# volume_arg = ''
# if map_repos:
# for repos in map_repos
# volume_arg += '-v {dir}/packages/{volume_name}:$HOME/catkin_ws/src/{volume_name} '.format(dir=join(dirname(realpath(__file__))), volume_name=repos)
# map_repos = volume_arg if map_repos else ''
map_repos = '-v {dir}/packages:$HOME/catkin_ws/src'.format(dir=join(dirname(realpath(__file__))), volume_name=map_repos) if map_repos else ''
cmd = '/bin/bash -c "{}"'.format(cmd) if cmd else "/bin/bash"
local('xhost +local:root')
local('docker run -it --rm \
-e DISPLAY=$DISPLAY \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v $HOME/.Xauthority:$HOME/.Xauthority \
{map_repos} \
--net=host \
--privileged \
--name ros-{project_name} {detach} \
--volume /dev:/dev \
-t yuxianggao/ros-{project_name}:$(dpkg --print-architecture) \
{cmd}'.format(project_name=project_name,
map_repos=map_repos,
detach=detach,
cmd=cmd))
@task
@name_auto_fix
def docker_stop(project_name):
"""
Stop docker container
:param project_name: project to build. Should be stripped of 'docker-ros' suffix
"""
local('docker kill ros-{} || true'.format(project_name))
@task
@name_auto_fix
def docker_remove(project_name):
"""
Remove docker container
:param project_name: project to build. Should be stripped of 'docker-ros' suffix:param project_name: project to build. Should be stripped of 'docker-ros' suffix
"""
local('docker rm ros-{}'.format(project_name))
@task
def docker_sh():
"""
Execute command in docker container
"""
docker_exec('/bin/bash')
@task
def docker_logs(project_name):
"""
Print stdout/stderr from container entrypoint
:param project_name: project to build. Should be stripped of 'docker-ros' suffix
:return::param project_name: project to build. Should be stripped of 'docker-ros' suffix
"""
local('docker logs ros-{} -f'.format(project_name))
@task
def test(params=''):
"""
Run all tests in docker container
:param params: parameters to py.test
"""
docker_exec('py.test {}'.format(params))
@task
def test_sx(params=''):
"""
Execute all tests in docker container printing output and terminating tests at first failure
:param params: parameters to py.test
"""
docker_exec('py.test -sx {}'.format(params))
@task
def test_pep8():
"""
Execute only pep8 test in docker container
"""
docker_exec('py.test tests/test_pep8.py')
@task
def fix_pep8():
"""
Fix a few common and easy PEP8 mistakes in docker container
"""
docker_exec('autopep8 --select E251,E303,W293,W291,W391,W292,W391,E302 --aggressive --in-place --recursive .')