docker: migrate to python

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2018-11-11 00:00:00 +00:00
parent 32420eec33
commit 403d4a9d06
4 changed files with 79 additions and 36 deletions

View File

@@ -418,7 +418,7 @@ docker ps -a
.... ....
* `./run-docker start`: start the container as daemon on the background. * `./run-docker start`: start the container as daemon on the background.
+ +
Needed only after reboot, or if you call `stop` to save CPU or memory resources. Needed only after reboot, or if you run `./run-docker stop` to save CPU or memory resources.
+ +
The container can now be seen on the list of running containers: The container can now be seen on the list of running containers:
+ +
@@ -427,14 +427,12 @@ docker ps
.... ....
* `./run-docker sh`: open a shell on a previously started Docker daemon. * `./run-docker sh`: open a shell on a previously started Docker daemon.
+ +
Quit the shell as usual with `Ctrl` Quit the shell as usual with `Ctrl-D`
+ +
Can be called multiple times to open multiple shells. Can be called multiple times from different host terminals to open multiple shells.
The host git top level directory is mounted inside the guest with a link:https://stackoverflow.com/questions/23439126/how-to-mount-a-host-directory-in-a-docker-container[Docker volume], which means for example that you can use your host's GUI text editor directly on the files. Just don't forget that if you nuke that directory on the guest, then it gets nuked on the host as well! The host git top level directory is mounted inside the guest with a link:https://stackoverflow.com/questions/23439126/how-to-mount-a-host-directory-in-a-docker-container[Docker volume], which means for example that you can use your host's GUI text editor directly on the files. Just don't forget that if you nuke that directory on the guest, then it gets nuked on the host as well!
TODO make files created inside Docker be owned by the current user in host instead of `root`: https://stackoverflow.com/questions/23544282/what-is-the-best-way-to-manage-permissions-for-docker-shared-volumes
In order to use functionality such as <<gdb>> from inside Docker, you need a second shell inside the container. You can either do that from another shell with: In order to use functionality such as <<gdb>> from inside Docker, you need a second shell inside the container. You can either do that from another shell with:
.... ....
@@ -478,6 +476,8 @@ To actually delete the Docker build, run:
# sudo rm -rf out.docker # sudo rm -rf out.docker
.... ....
TODO make files created inside Docker be owned by the current user in host instead of `root`: https://stackoverflow.com/questions/23544282/what-is-the-best-way-to-manage-permissions-for-docker-shared-volumes
[[prebuilt]] [[prebuilt]]
=== Prebuilt Buildroot setup === Prebuilt Buildroot setup

11
build
View File

@@ -368,17 +368,18 @@ if args.download_dependencies:
} }
apt_get_pkgs.difference_update(interacive_pkgs) apt_get_pkgs.difference_update(interacive_pkgs)
if common.in_docker: if common.in_docker:
sudo = ['sudo'] sudo = []
# https://askubuntu.com/questions/909277/avoiding-user-interaction-with-tzdata-when-installing-certbot-in-a-docker-contai # https://askubuntu.com/questions/909277/avoiding-user-interaction-with-tzdata-when-installing-certbot-in-a-docker-contai
os.environ['DEBIAN_FRONTEND'] = 'noninteractive' os.environ['DEBIAN_FRONTEND'] = 'noninteractive'
# https://askubuntu.com/questions/496549/error-you-must-put-some-source-uris-in-your-sources-list # https://askubuntu.com/questions/496549/error-you-must-put-some-source-uris-in-your-sources-list
with open(os.path.join('/etc', 'apt', 'sources.list'), 'r') as f: sources_path = os.path.join('/etc', 'apt', 'sources.list')
with open(sources_path, 'r') as f:
sources_txt = f.read() sources_txt = f.read()
sources_txt = re.sub('^# deb-src ' 'deb-src ', sources_txt) sources_txt = re.sub('^# deb-src ', 'deb-src ', sources_txt, flags=re.MULTILINE)
with open(os.path.join('/etc', 'apt', 'sources.list'), 'w') as f: with open(sources_path, 'w') as f:
f.write(sources_txt) f.write(sources_txt)
else: else:
sudo = [] sudo = ['sudo']
if common.in_docker or args.travis: if common.in_docker or args.travis:
y = ['-y'] y = ['-y']
else: else:

View File

@@ -24,6 +24,7 @@ import urllib
import urllib.request import urllib.request
this_module = sys.modules[__name__] this_module = sys.modules[__name__]
repo_short_id = 'lkmc'
# https://stackoverflow.com/questions/20010199/how-to-determine-if-a-process-runs-inside-lxc-docker # https://stackoverflow.com/questions/20010199/how-to-determine-if-a-process-runs-inside-lxc-docker
in_docker = os.path.exists('/.dockerenv') in_docker = os.path.exists('/.dockerenv')
root_dir = os.path.dirname(os.path.abspath(__file__)) root_dir = os.path.dirname(os.path.abspath(__file__))

View File

@@ -1,26 +1,67 @@
#!/usr/bin/env bash #!/usr/bin/env python3
set -eu
cmd="$1" import argparse
shift import os
container_name=lkmc
target_dir=/root/linux-kernel-module-cheat import common
if [ "$cmd" = create ]; then
container_name = common.repo_short_id
container_hostname = common.repo_short_id
image_name = common.repo_short_id
target_dir = '/root/{}'.format(common.repo_short_id)
docker = ['sudo', 'docker']
def sh(args):
if args:
sh_args = ['-c'] + args
else:
sh_args = []
common.run_cmd(
docker +
[
'exec',
'-i',
'-t',
container_name,
'bash',
] +
sh_args +
[common.Newline],
)
cmd_action_map = {
'create': lambda args:
# --privileged for KVM: # --privileged for KVM:
# https://stackoverflow.com/questions/48422001/launching-qemu-kvm-from-inside-docker-container # https://stackoverflow.com/questions/48422001/launching-qemu-kvm-from-inside-docker-container
sudo docker create --name "$container_name" --net host -i --privileged -t -w "${target_dir}" -v "$(pwd):${target_dir}" ubuntu:18.04 bash common.run_cmd(
elif [ "$cmd" = start ]; then docker +
sudo docker start "$container_name" [
elif [ "$cmd" = stop ]; then 'create', common.Newline,
sudo docker stop "$container_name" '--hostname', container_hostname, common.Newline,
elif [ "$cmd" = sh ]; then '-i', common.Newline,
if [ "$#" -gt 0 ]; then '--name', container_name, common.Newline,
c=-c '--net', 'host', common.Newline,
fi '--privileged', common.Newline,
# https://stackoverflow.com/questions/39794509/how-to-open-multiple-terminals-in-docker '-t', common.Newline,
sudo docker exec -it "$container_name" bash $c "$*" '-w', target_dir, common.Newline,
elif [ "$cmd" = DESTROY ]; then '-v', '{}:{}'.format(os.getcwd(), target_dir), common.Newline,
sudo docker rm "$container_name" 'ubuntu:18.04', common.Newline,
else 'bash', common.Newline,
echo "error: unknown action: ${cmd}" 1>&2 ]
exit 2 ),
fi 'start': lambda args:
common.run_cmd(docker + [ 'start', container_name, common.Newline])
,
'stop': lambda args:
common.run_cmd(docker + ['stop', container_name, common.Newline])
,
'sh': lambda args: sh(args),
'DESTROY': lambda args:
common.run_cmd(docker + [ 'rm', container_name, common.Newline])
,
}
parser = argparse.ArgumentParser()
parser.add_argument('cmd', choices=cmd_action_map)
parser.add_argument('args', nargs='*')
common.add_dry_run_argument(parser)
args = parser.parse_args()
common.setup_dry_run_arguments(args)
cmd_action_map[args.cmd](args.args)