-
Notifications
You must be signed in to change notification settings - Fork 1
/
speedup
executable file
·144 lines (127 loc) · 3.83 KB
/
speedup
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
#!/bin/bash
: ${1?"Usage: $0 edit"}
set -e
SERVICES="$@"
IFS=''
# Docker for Mac performance improvement
#
# Selectively mount only the volumes you're working on for a 2-5x
# performance improvement, depending on project over-using host mounts
#
# Use in place of docker-compose up -d service1 serviceN
#
# Creates & invokes a temporary, more limited docker-compose yaml
# and also supports docker-compose.override.yml
#
# Alternative to docker-machine, d4m-nfs, docker-sync.io
#
# This will add "delegated" volume mount mode to the services
# you invoke with the script and will also remove the volume mounts
# to all other services. This means that container file systems will
# not be linked to your source folders, except for the services you
# specify.
#
# The delegated mode is new as of Docker for Mac 17.06 and
# is highly performant, at the cost of consistency; the container's file
# system is more trusted, and writes made on the container are not
# guaranteed to be sync'd to the host quickly or reliably. If you do
# not want to use "delegated" you can force "cached" or "consistent"
# in your docker-compose.yml.
#
# This has been tested with Docker for Mac 17.06 Stable.
#
# Install:
#
# Place this script alongside docker-compose.yml, or symlink it to ~/bin
#
# Usage:
#
# speedup edit rover
#
# http://github.com/magoon/dockermac-speedup
function remove_unneeded_volumes_from() {
# Iterate services, removing volumes from those we don't need
# And adding ":delegated" to those that we do want to keep
#
# Function parameter: docker-compose filename
#
# Overview: When a matching service is found after an empty line,
# matching_service variable becomes set to 1. Then, when "volumes:" is
# found, any item in that list is conditionally removed or modified.
FILE=$( IFS='' cat $1 )
echo $FILE | awk '
matching_service && volumes {
# This does the actual skipping of volumes
if (/\s*-\s*"?[^"]*"?/)
next;
}
volumes {
if (/\s*-\s*"?[^"]*"?/) {
# Add :delegated mode to volume mount if not exist,
# not in quotes, and absolute
if (!/.*\:.*\:/ && /:/ && !/:(consistent|:delegated|:cached)/ && !/"$/) {
print $0":delegated";
next;
}
}
else
volumes=0
}
/\volumes:/ {
volumes=1;
if (matching_service) {
next;
}
}
empty {
matching_service = 1
split ("'$SERVICES'", servicelist, " ");
for (i in servicelist)
if ($1 == servicelist[i]":")
matching_service = 0;
}
{
# Services are to be preceded by empty lines, sadly
if (NF)
empty = 0;
else
empty = 1
}
{
if (/services:/)
empty = 1
}
1
'
}
# Start docker if it's not already running
if ! $(docker info > /dev/null 2>&1); then
open -a /Applications/Docker.app
echo -ne "\nWait until Docker for Mac is running."
while ! $(docker run --rm hello-world > /dev/null 2>&1); do
echo -n "."
sleep .5
done
echo ""
fi
# Lower the priority of hyperkit
renice -n 19 $(pgrep com.docker.hyperkit)
# if there's no docker-compose.yml file, exit
if [ ! -f docker-compose.yml ]; then
echo 'Error: docker-compose.yml: No such file'
exit 1
fi
# Write out a temporary docker-compose file with modified volume mounts
remove_unneeded_volumes_from docker-compose.yml > .tmp.docker-compose.yml
# Also process your override file if needed
if [ -f docker-compose.override.yml ]; then
remove_unneeded_volumes_from docker-compose.override.yml > .tmp.docker-compose.override.yml
docker-compose -f .tmp.docker-compose.yml -f .tmp.docker-compose.override.yml up -d $@
else
docker-compose -f .tmp.docker-compose.yml up -d $@
fi
# Clean up
rm -f .tmp.docker-compose.yml
rm -f .tmp.docker-compose.override.yml
# End the script successfully
exit 0