-
Notifications
You must be signed in to change notification settings - Fork 1
/
lxc-push.sh
executable file
·78 lines (60 loc) · 1.39 KB
/
lxc-push.sh
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
#!/bin/sh -e
# Copy a single file or directory to an LXD container.
process_args() {
source="$1"
IFS='/' read -r container path <<- EOF
$2
EOF
if [ "${source}" = "" ] ||
[ "${container}" = "" ] ||
[ "${path}" = "" ]; then
help=1
fi
path="/${path}"
}
show_help() {
if [ ! ${help} ]; then return; fi
cat <<- EOF
Usage: lxc-push.sh <source> <container>/<path>
Source and path must both be a directory or a file.
Path will be created if necessary.
EOF
exit 0
}
push() {
if [ -d "${source}" ]; then
push_directory
elif [ -f "${source}" ]; then
push_file
else
echo "Source is not a file or directory."
exit 1
fi
}
push_directory() {
device="lxc-push-$$.tmp"
tmp="/tmp/${device}"
lxc config device add "${container}" "${device}" \
disk "source=${source}" "path=${tmp}" > /dev/null
lxc exec "${container}" -- cp -a --no-preserve=ownership \
"${tmp}" "${path}" || cleanup_directory 1
cleanup_directory 0
}
cleanup_directory() {
lxc config device remove "${container}" "${device}" > /dev/null
lxc exec "${container}" -- rmdir "${tmp}"
exit "$1"
}
push_file() {
mode=$(stat --format '%a' "${source}")
# work-around for overwrite bug in lxc file push
lxc exec "${container}" -- rm -f "${path}"
lxc file push "--mode=${mode}" "${source}" "${container}/${path}"
}
main() {
process_args "$@"
show_help
push
}
main "$@"
exit 0