-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVagrantfile
211 lines (169 loc) · 8.38 KB
/
Vagrantfile
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# -*- mode: ruby -*-
# vi: set ft=ruby :
$ip_file = "db_ip.txt"
Vagrant.configure("2") do |config|
config.vm.box = 'digital_ocean'
config.vm.box_url = "https://github.com/devopsgroup-io/vagrant-digitalocean/raw/master/box/digital_ocean.box"
config.ssh.private_key_path = '~/.ssh/id_rsa'
config.vm.synced_folder ".", "/vagrant", type: "rsync"
config.vm.define "dbserver", primary: true do |server|
server.vm.provider :digital_ocean do |provider|
provider.ssh_key_name = ENV["SSH_KEY_NAME"]
provider.token = ENV["DIGITAL_OCEAN_TOKEN"]
provider.image = 'ubuntu-18-04-x64'
provider.region = 'fra1'
provider.size = 's-1vcpu-1gb'
provider.privatenetworking = true
end
server.vm.hostname = "dbserver"
server.trigger.after :up do |trigger|
trigger.info = "Writing dbserver's IP to file..."
trigger.ruby do |env,machine|
remote_ip = machine.instance_variable_get(:@communicator).instance_variable_get(:@connection_ssh_info)[:host]
File.write($ip_file, remote_ip)
end
end
server.vm.provision "shell", inline: <<-SHELL
echo "Installing postgresql"
sudo apt-get -y install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
sudo apt-get -y update
sudo apt-get -y install postgresql-15 postgresql-contrib-15
echo "Starting postgresql"
service postgresql start
echo "Checking status of postgresql"
service postgresql status
echo "Updating config files"
echo "listen_addresses='*'" >> /etc/postgresql/15/main/postgresql.conf
echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/15/main/pg_hba.conf
sudo -u postgres psql -c "CREATE DATABASE minitwitdb;"
sudo -u postgres psql -c "CREATE USER admin WITH ENCRYPTED PASSWORD 'admin';"
sudo -u postgres psql -c "ALTER DATABASE minitwitdb OWNER TO admin;"
sudo -u postgres psql -c "\\l"
sudo service postgresql restart
sudo -u postgres psql -c "show listen_addresses;"
SHELL
end
config.vm.define "webserver", primary: false do |server|
server.vm.provider :digital_ocean do |provider|
provider.ssh_key_name = ENV["SSH_KEY_NAME"]
provider.token = ENV["DIGITAL_OCEAN_TOKEN"]
provider.image = 'ubuntu-18-04-x64'
provider.region = 'fra1'
provider.size = 's-1vcpu-1gb'
provider.privatenetworking = true
end
server.vm.hostname = "webserver"
server.trigger.before :up do |trigger|
trigger.info = "Waiting to create server until dbserver's IP is available."
trigger.ruby do |env,machine|
ip_file = "db_ip.txt"
while !File.file?($ip_file) do
sleep(1)
end
db_ip = File.read($ip_file).strip()
puts "Now, I have it..."
puts db_ip
end
end
server.trigger.after :provision do |trigger|
trigger.ruby do |env,machine|
File.delete($ip_file) if File.exists? $ip_file
end
end
server.vm.provision "shell", inline: 'echo "export DOCKER_USERNAME=' + "'" + ENV["DOCKER_USERNAME"] + "'" + '" >> ~/.bash_profile'
server.vm.provision "shell", inline: 'echo "export DOCKER_PASSWORD=' + "'" + ENV["DOCKER_PASSWORD"] + "'" + '" >> ~/.bash_profile'
server.vm.provision "shell", inline: 'echo "export DB_USER=' + "'" + ENV["DB_USER"] + "'" + '" >> ~/.bash_profile'
server.vm.provision "shell", inline: 'echo "export DB_PASSWORD=' + "'" + ENV["DB_PASSWORD"] + "'" + '" >> ~/.bash_profile'
server.vm.provision "shell", inline: 'echo "export DB_NAME=' + "'" + ENV["DB_NAME"] + "'" + '" >> ~/.bash_profile'
server.vm.provision "shell", inline: 'echo "export DB_PORT=' + "'" + ENV["DB_PORT"] + "'" + '" >> ~/.bash_profile'
server.vm.provision "shell", inline: 'echo "export PORT=' + "'" + ENV["PORT"] + "'" + '" >> ~/.bash_profile'
server.vm.provision "shell", inline: 'echo "export SESSION_KEY=' + "'" + ENV["SESSION_KEY"] + "'" + '" >> ~/.bash_profile'
server.vm.provision "shell", inline: 'echo "export GIN_MODE=' + "'" + ENV["GIN_MODE"] + "'" + '" >> ~/.bash_profile'
server.vm.provision "shell", inline: <<-SHELL
export DB_IP=`cat /vagrant/db_ip.txt`
echo $DB_IP
echo "Installing docker..."
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
apt-cache policy docker-ce
sudo apt install -y docker-ce
sudo systemctl status docker
sudo usermod -aG docker ${USER}
echo "Verifying that docker works"
docker run --rm hello-world
docker rmi hello-world
echo "Adding environment variables to bash profile"
echo ". $HOME/.bashrc" >> $HOME/.bash_profile
echo "Adding DB_HOST environment variable to bash profile"
echo "export DB_HOST='$DB_IP'" >> $HOME/.bash_profile
cp -r /vagrant/* $HOME
source $HOME/.bash_profile
echo "Assigning permission to run deploy.sh and env_file.sh"
chmod +x deploy.sh
chmod +x env_file.sh
echo "Building application"
docker build -t $DOCKER_USERNAME/minitwit:latest --build-arg db_user=$DB_USER --build-arg db_host=$DB_HOST --build-arg db_password=$DB_PASSWORD --build-arg db_name=$DB_NAME --build-arg db_port=$DB_PORT --build-arg port=$PORT --build-arg session_key=$SESSION_KEY --build-arg gin_mode=$GIN_MODE .
echo "Logging into docker"
docker login --username $DOCKER_USERNAME --pasword $DOCKER_PASSWORD¨
echo "Installing loki-docker-driver"
docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions || true
echo "Running docker image..."
docker run --rm -d -p $PORT:$PORT --name minitwit $DOCKER_USERNAME/minitwit:latest
echo "================================================================="
echo "= DONE ="
echo "================================================================="
echo "Navigate in your browser to:"
THIS_IP=`hostname -I | cut -d" " -f1`
echo "http://${THIS_IP}:8080"
SHELL
end
config.vm.define "monitoring", primary: true do |server|
server.vm.provider :digital_ocean do |provider|
provider.ssh_key_name = ENV["SSH_KEY_NAME"]
provider.token = ENV["DIGITAL_OCEAN_TOKEN"]
provider.image = 'ubuntu-18-04-x64'
provider.region = 'fra1'
provider.size = 's-1vcpu-1gb'
provider.privatenetworking = true
end
server.vm.hostname = "monitoring"
server.vm.provision "shell", inline: <<-SHELL
cp -r /vagrant/* $HOME
echo "Installing docker..."
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
apt-cache policy docker-ce
sudo apt install -y docker-ce
sudo systemctl status docker
sudo usermod -aG docker ${USER}
echo "Verifying that docker works"
docker run --rm hello-world
docker rmi hello-world
echo "Installing loki"
docker pull grafana/loki
echo "Installing prometheus"
docker pull prom/prometheus
echo "Installing grafana"
docker pull grafana/grafana:9.4.7
echo "Removing grafana"
docker stop grafana || true && docker rm -f grafana || true
echo "Removing loki"
docker stop loki || true && docker rm -f loki || true
echo "Removing prometheus"
docker stop prometheus || true && docker rm -f prometheus || true
echo "Running prometheus"
docker run -d -p 9090:9090 -u $(id -u) -v ./prometheus.yml:/etc/prometheus/prometheus.yml -v /prometheus:/prometheus --name prometheus prom/prometheus
echo "Running grafana"
docker run -d -p 3000:3000 --name grafana grafana/grafana:9.4.7
echo "Running loki"
docker run -d --name=loki --mount source=loki-data,target=/loki -p 3100:3100 grafana/loki
SHELL
end
config.vm.provision "shell", privileged: false, inline: <<-SHELL
sudo apt-get update
SHELL
end