forked from 89luca89/distrobox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distrobox-ephemeral
executable file
·135 lines (119 loc) · 3.49 KB
/
distrobox-ephemeral
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
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project:
# https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.
# POSIX
# Optional env variables:
# DBX_CONTAINER_MANAGER
# DBX_CONTAINER_NAME
# DBX_NON_INTERACTIVE
# DBX_SUDO_PROGRAM
# Dont' run this command as sudo.
if [ "$(id -u)" -eq 0 ]; then
printf >&2 "Running %s as sudo is not supported.\n" "$(basename "${0}")"
printf >&2 " try instead running:\n"
printf >&2 " %s --root %s\n" "$(basename "${0}")" "$*"
exit 1
fi
container_command=""
create_flags=""
distrobox_path="$(dirname "${0}")"
extra_flags=""
rootful=0
verbose=0
version="1.4.1"
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
cat << EOF
distrobox version: ${version}
Usage:
distrobox-ephemeral --name container-name [--force]
Options:
--root/-r: launch podman/docker with root privileges. Note that if you need root this is the preferred
way over "sudo distrobox" (note: if using a program other than 'sudo' for root privileges is necessary,
specify it through the DBX_SUDO_PROGRAM env variable, or 'distrobox_sudo_program' config variable)
--verbose/-v: show more verbosity
--help/-h: show this message
--/-e: end arguments execute the rest as command to execute at login default: bash -l
--version/-V: show version
See also:
distrobox-create --help
for a list of additional flags to specify during the creation.
distrobox-ephemeral can use all flags from distrobox-create.
EOF
}
# Parse arguments
while :; do
case $1 in
-h | --help)
# Call a "show_help" function to display a synopsis, then exit.
show_help
exit 0
;;
-r | --root)
shift
rootful=1
;;
-v | --verbose)
verbose=1
shift
;;
-V | --version)
printf "distrobox: %s\n" "${version}"
exit 0
;;
-e | --exec | --)
shift
container_command="-- $*"
break
;;
*) # Default case: If no more options then break out of the loop.
# If we have a flagless option and container_name is not specified
# then let's accept argument as container_name
if [ -n "$1" ]; then
create_flags="${create_flags} $1"
shift
else
break
fi
;;
esac
done
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
extra_flags="${extra_flags} --verbose"
fi
# prepend sudo (or the specified sudo program) if we want podman or docker to be rootful
if [ "${rootful}" -ne 0 ]; then
extra_flags="${extra_flags} --root"
fi
name=$(mktemp -u distrobox-XXXXXXXXXX)
# shellcheck disable=SC2086
"${distrobox_path}"/distrobox-create --name ${name} --yes ${create_flags} ${extra_flags}
# shellcheck disable=SC2086
"${distrobox_path}"/distrobox-enter "${name}" ${container_command} ${extra_flags}
# shellcheck disable=SC2086
"${distrobox_path}"/distrobox-stop "${name}" --yes ${extra_flags}
# shellcheck disable=SC2086
"${distrobox_path}"/distrobox-rm --force "${name}" --yes ${extra_flags}