-
Notifications
You must be signed in to change notification settings - Fork 3
/
ign-docker-env.py
131 lines (103 loc) · 3.97 KB
/
ign-docker-env.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
#
# Copyright (C) 2020 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Actions to build and execute ignition docker containers.
Usage:
gzdev ign-docker-env IGN_RELEASE
[--linux-distro <linux-distro>]
[--rocker-args ROCKER_ARGS]
[--vol $LOCAL_PATH:$CONTAINER_PATH[::$LOCAL_PATH:$CONTAINER_PATH ...]]
gzdev ign-docker-env -h | --help
gzdev ign-docker-env --version
Options:
-h --help Show this screen
--version Show gzdev's version
--linux-distro linux-distro Linux distibution to use in docker env
--rocker-args ROCKER_ARGS Extra rocker arguments, captured in single quotes and separated by white space
--vol $LOCAL_PATH:$CONTAINER_PATH Load volumes into Docker container (separate multiple volumes with '::')
Notes:
Valid inputs for IGN_RELEASE are 'citadel' and 'fortress'.
Valid inputs for the --linux-distro arg are 'ubuntu:bionic' or 'ubuntu:focal'.
"""
import os
import subprocess
import sys
import time
from docopt import docopt
ROCKER_CMD = ['rocker', '--x11', '--user']
# TODO: use a yaml file to get this information
# keep track of all valid versions and their default linux distro
bionic = 'ubuntu:bionic'
focal = 'ubuntu:focal'
IGN_VERSIONS = {
'citadel': bionic,
'fortress': focal
}
# TODO: migrate this to use a gpu python library
def get_gpu_params():
# check for nvidia
for x in os.listdir('/dev/'):
if x.startswith('nvidia'):
return ['--nvidia']
# check for intel
if not os.path.isdir('/dev/dri'):
return []
for x in os.listdir('/dev/dri/'):
if x.startswith('card'):
return [f'--devices /dev/dri/{x}']
return []
def _check_call(cmd):
print('')
print('Invoking "%s"' % ' '.join(cmd))
print('')
# sleep temporarily so that users can see the command being used
time.sleep(1)
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError as ex:
print(ex)
def error(msg):
print(f'\n {msg} \n', file=sys.stderr)
exit(-1)
def build_rocker_command(igniton_release, linux_distro, docker_args, vol_args):
_, linux_distro_release = linux_distro.split(':')
cmd = ROCKER_CMD + get_gpu_params()
cmd += ['--ignition', f'{igniton_release}:{linux_distro_release}']
cmd += docker_args if docker_args else []
cmd += vol_args if vol_args else []
cmd += [linux_distro, '/bin/bash']
return cmd
def normalize_args(args):
ignition_version = args['IGN_RELEASE']
if args['--linux-distro']:
linux_distro = args['--linux-distro']
else:
linux_distro = IGN_VERSIONS[ignition_version]
docker_args = args['--rocker-args'].split(' ') if args['--rocker-args'] else None
vol_args = ['--vol', args['--vol']] if args['--vol'] else None
return ignition_version, linux_distro, docker_args, vol_args
def main():
try:
ignition_version, linux_distro, docker_args, vol_args = normalize_args(
docopt(str(__doc__), version='gzdev-docker-env 0.1.0'))
rocker_cmd = build_rocker_command(ignition_version, linux_distro, docker_args, vol_args)
_check_call(rocker_cmd)
except KeyError:
print('Invalid value given for IGN_RELEASE. Please choose from ', list(IGN_VERSIONS.keys()))
except KeyboardInterrupt:
print('docker-env was stopped with a Keyboard Interrupt.\n')
if __name__ == '__main__':
main()