Skip to content

Commit eb83985

Browse files
authored
MAGECLOUD-3243: OOTB Docker Startup (#166)
1 parent 09533c9 commit eb83985

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

bin/init-docker.sh

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# complain to STDERR and exit with error
6+
die()
7+
{
8+
echo "$*" >&2
9+
print_usage
10+
exit 2
11+
}
12+
13+
print_usage()
14+
{
15+
echo -e "$USAGE"
16+
}
17+
18+
needs_arg()
19+
{
20+
if [ -z "$OPTARG" ]; then
21+
OPTARG="${!OPTIND}"
22+
OPTIND=$(( $OPTIND + 1 ))
23+
fi
24+
}
25+
26+
parse_bool_flag()
27+
{
28+
case "$(echo $OPTARG | tr '[:upper:]' '[:lower:]')" in
29+
y | yes | t | true | 1 )
30+
OPTARG=true
31+
;;
32+
n | no | f | false | 0 )
33+
OPTARG=false
34+
;;
35+
* )
36+
die "Invalid value $OPTARG for $OPT"
37+
;;
38+
esac
39+
}
40+
41+
version_is_valid()
42+
{
43+
if [[ ! $OPTARG =~ ^[0-9]+\.[0-9]+$ ]]; then
44+
die "Invalid version number $OPTARG for $OPT"
45+
fi
46+
}
47+
48+
domain_is_valid()
49+
{
50+
# Check that a flag isn't being interpreted as the domain
51+
if [[ -z $OPTARG ]] || [[ $OPTARG == -* ]]; then
52+
die "Invalid domain $OPTARG for $OPT"
53+
fi
54+
}
55+
56+
composer_install()
57+
{
58+
docker run --rm -e "MAGENTO_ROOT=/app" -v "$(pwd)":/app -v ~/.composer/cache:/root/.composer/cache "magento/magento-cloud-docker-php:${PHP_VERSION}-cli-${IMAGE_VERSION}" composer install --ansi
59+
}
60+
61+
add_host()
62+
{
63+
if grep -Eq "^\s*\d+\.\d+\.\d+\.\d+\s+${DOMAIN}$" /etc/hosts; then
64+
echo -e "\033[33m\033[1mThere is already an entry for $DOMAIN in /etc/hosts, skipping.\033[0m"
65+
66+
return
67+
fi
68+
69+
echo "127.0.0.1 $DOMAIN" | sudo tee -a /etc/hosts
70+
}
71+
72+
PHP_VERSION="7.2"
73+
IMAGE_VERSION="1.1"
74+
ADD_HOST=true
75+
DOMAIN="magento2.docker"
76+
USAGE="Init Docker
77+
78+
\033[33mDescription:\033[0m
79+
Initialize a Magento Cloud Docker based project
80+
81+
\033[33mOptions:\033[0m
82+
\033[32m-p, --php\033[0m PHP version (for installing dependencies) \033[33m[default: ${PHP_VERSION}]\033[0m
83+
\033[32m-i, --image\033[0m image version (for installing dependencies) \033[33m[default: ${IMAGE_VERSION}]\033[0m
84+
\033[32m --host\033[0m domain name to add to /etc/hosts \033[33m[default: ${DOMAIN}]\033[0m
85+
\033[32m --add-host\033[0m add domain name to /etc/hosts file \033[33m[default: ${ADD_HOST}]\033[0m
86+
\033[32m-h, --help\033[0m show this help text
87+
88+
\033[33mExample usage:\033[0m
89+
\033[32mbin/init-docker.sh\033[0m perform default actions
90+
\033[32mbin/init-docker.sh --php 7.3 --add-host no\033[0m use PHP 7.3, skip adding domain to /etc/hosts"
91+
92+
while getopts "hp:i:-:" OPT; do
93+
if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG
94+
OPT="${OPTARG%%=*}" # extract long option name
95+
96+
OPTARG="${OPTARG#$OPT}" # extract long option argument (may be empty)
97+
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
98+
fi
99+
100+
case "$OPT" in
101+
p | php )
102+
needs_arg "$@"
103+
version_is_valid
104+
PHP_VERSION="$OPTARG"
105+
;;
106+
107+
i | image )
108+
needs_arg "$@"
109+
version_is_valid
110+
IMAGE_VERSION="$OPTARG"
111+
;;
112+
113+
add-host )
114+
needs_arg "$@"
115+
parse_bool_flag
116+
ADD_HOST="$OPTARG"
117+
;;
118+
119+
host )
120+
needs_arg "$@"
121+
domain_is_valid
122+
DOMAIN="$OPTARG"
123+
;;
124+
125+
h | help )
126+
print_usage
127+
exit 0
128+
;;
129+
130+
\? )
131+
print_usage
132+
exit 1
133+
;;
134+
135+
??* )
136+
die "Illegal option --$OPT"
137+
;;
138+
esac
139+
done
140+
141+
shift $((OPTIND-1)) # remove parsed options and args from $@ list
142+
143+
echo -e "\033[32m\033[1mInstalling Composer Packages\033[0m"
144+
composer_install
145+
146+
if [ $ADD_HOST == true ]; then
147+
echo -e "\033[32m\033[1mAdding $DOMAIN to /etc/hosts\033[0m"
148+
echo -e "Your system password may be required"
149+
add_host
150+
fi

0 commit comments

Comments
 (0)