-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-mean.sh
132 lines (120 loc) · 3.83 KB
/
install-mean.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
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
#!/bin/bash
# set -e making the commands if they were like &&
set -e
read -e -p "Enter the path to the install dir (or hit enter for default path): " -i "$HOME/server-mean" INSTALL_DIR
echo $INSTALL_DIR
DB_DIR=$INSTALL_DIR/mongodb
DBBAK_DIR=$INSTALL_DIR/dbbackup
REPO_DIR=$INSTALL_DIR/mean
WWW_DIR=$INSTALL_DIR/www
echo -e "\nCreating folder structure:"
mkdir -p $DB_DIR/data/db $DBBAK_DIR $REPO_DIR $WWW_DIR
echo -e "\
$DB_DIR/data/db\n\
$DBBAK_DIR\n\
$REPO_DIR\n\
$WWW_DIR\n\
Done!"
if test "$(ls -A "$REPO_DIR")"; then
echo -e "\n\"$REPO_DIR\" directory is not empty!\nYou have to remove everything from here to continue!\nRemove \"$REPO_DIR\" directory (y/n)?"
read answer
if echo "$answer" | grep -iq "^y" ;then
rm -rf $REPO_DIR/
echo -e "\"$REPO_DIR\" is removed, continue installation...";
mkdir -p $REPO_DIR
echo -e "\nCloning git repo into \"$REPO_DIR\":"
cd $REPO_DIR
git clone https://github.com/DJviolin/mean.git $REPO_DIR
echo -e "\nShowing working directory..."
ls -al $REPO_DIR
else
echo -e "\nScript aborted to run\nExiting..."; exit 1;
fi
else
echo -e "\nCloning git repo into \"$REPO_DIR\":"
cd $REPO_DIR
git clone https://github.com/DJviolin/mean.git $REPO_DIR
echo -e "Showing working directory..."
ls -al $REPO_DIR
fi
echo -e "\nCreating additional files for the stack:"
# bash variables in Here-Doc, don't use 'EOF'
# http://stackoverflow.com/questions/4937792/using-variables-inside-a-bash-heredoc
# http://stackoverflow.com/questions/17578073/ssh-and-environment-variables-remote-and-local
echo -e "\nCreating: $REPO_DIR/docker-compose.yml\n"
cat <<EOF > $REPO_DIR/docker-compose.yml
cadvisor:
image: google/cadvisor:latest
container_name: mean_cadvisor
ports:
- "8080:8080"
volumes:
- "/:/rootfs:ro"
- "/var/run:/var/run:rw"
- "/sys:/sys:ro"
- "/var/lib/docker/:/var/lib/docker:ro"
base:
build: ./base
container_name: mean_base_exited
mongodb:
build: ./mongodb
container_name: mean_mongodb
links:
- base
ports:
- "27017:27017"
volumes:
- $DB_DIR/data/db:/data/db
node:
build: ./node
container_name: mean_node_exited
links:
- base
app:
build: ./app
container_name: mean_app
links:
- base
- node
ports:
- "3000:3000"
volumes:
- $WWW_DIR:/usr/src/app:rw
EOF
cat $REPO_DIR/docker-compose.yml
echo -e "\nCreating: $REPO_DIR/mean.service\n"
cat <<EOF > $REPO_DIR/mean.service
[Unit]
Description=mean
After=etcd.service
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
#KillMode=none
ExecStartPre=-/usr/bin/docker cp mean_mongodb:/data $DBBAK_DIR
ExecStartPre=-/bin/bash -c '/usr/bin/tar -zcvf $DBBAK_DIR/dbbackup_\$\$(date +%%Y-%%m-%%d_%%H-%%M-%%S)_ExecStartPre.tar.gz $DBBAK_DIR/mongodb --remove-files'
ExecStartPre=-/opt/bin/docker-compose --file $REPO_DIR/docker-compose.yml kill
ExecStartPre=-/opt/bin/docker-compose --file $REPO_DIR/docker-compose.yml rm --force
ExecStart=/opt/bin/docker-compose --file $REPO_DIR/docker-compose.yml up --force-recreate
ExecStartPost=/usr/bin/etcdctl set /mean Running
ExecStop=/opt/bin/docker-compose --file $REPO_DIR/docker-compose.yml stop
ExecStopPost=/usr/bin/etcdctl rm /mean
ExecStopPost=-/usr/bin/docker cp mean_mongodb:/data $DBBAK_DIR
ExecStopPost=-/bin/bash -c 'tar -zcvf $DBBAK_DIR/dbbackup_\$\$(date +%%Y-%%m-%%d_%%H-%%M-%%S)_ExecStopPost.tar.gz $DBBAK_DIR/mongodb --remove-files'
Restart=always
#RestartSec=30s
[X-Fleet]
Conflicts=mean.service
EOF
cat $REPO_DIR/mean.service
cd $HOME
echo -e "\n
MEAN stack has successfully built!\n\n\
Run docker-compose with:\n\
$ docker-compose --file $REPO_DIR/docker-compose.yml build\n\
Run the systemd service with:\n\
$ cd $REPO_DIR && chmod +x service-start.sh && ./service-start.sh\n\
Stop the systemd service with:\n\
$ cd $REPO_DIR && chmod +x service-stop.sh && ./service-stop.sh"
echo -e "\nAll done! Exiting..."