Skip to content

Commit cb1f960

Browse files
committed
Complete automatic remote deployment and startup
1 parent ece6e11 commit cb1f960

17 files changed

+18044
-8
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
.vagrant/
2+
.vscode/
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]

Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
define CLEAN_SCRIPT
2+
use test;
3+
db.dropDatabase();
4+
endef
5+
6+
cleandb:
7+
mongo ${CLEAN_SCRIPT}
8+
9+
filldb:
10+
python sqlite_to_mongo.py
11+
12+
dumpdb:
13+
mongodump --db test --gzip
14+
15+
start:
16+
python minitwit.py
17+
18+
startdb:
19+
brew services start mongodb-community@4.2
20+
21+
stopdb:
22+
brew services stop mongodb-community@4.2
23+
24+
deploy_local:
25+
vagrant up
26+
27+
deploy_remote:
28+
rm db_ip.txt | vagrant up | python store_ip.py

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ Installation
3131
python minitwit.py
3232

3333
5. Open your favorite browser and go to http://127.0.0.1:5000/
34+
35+
36+
37+
38+
pip install Flask-PyMongo

Vagrantfile

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
# Since the webserver needs the IP of the DB server the two have to be started
5+
# in the right order and with storing the IP of the latter on the way:
6+
#
7+
# $ rm db_ip.txt | vagrant up | python store_ip.py
8+
9+
$ip_file = "db_ip.txt"
10+
11+
Vagrant.configure("2") do |config|
12+
config.vm.box = 'digital_ocean'
13+
config.vm.box_url = "https://github.com/devopsgroup-io/vagrant-digitalocean/raw/master/box/digital_ocean.box"
14+
config.ssh.private_key_path = '~/.ssh/id_rsa'
15+
config.vm.synced_folder ".", "/vagrant", type: "rsync"
16+
17+
config.vm.define "dbserver", primary: true do |server|
18+
server.vm.provider :digital_ocean do |provider|
19+
provider.ssh_key_name = ENV["SSH_KEY_NAME"]
20+
provider.token = ENV["DIGITAL_OCEAN_TOKEN"]
21+
provider.image = 'ubuntu-18-04-x64'
22+
provider.region = 'fra1'
23+
provider.size = '1gb'
24+
provider.privatenetworking = true
25+
end
26+
27+
server.vm.hostname = "dbserver"
28+
29+
server.trigger.after :up do |trigger|
30+
trigger.info = "Writing dbserver's IP to file..."
31+
trigger.ruby do |env,machine|
32+
remote_ip = machine.instance_variable_get(:@communicator).instance_variable_get(:@connection_ssh_info)[:host]
33+
File.write($ip_file, remote_ip)
34+
end
35+
end
36+
37+
server.vm.provision "shell", inline: <<-SHELL
38+
echo "Installing MongoDB"
39+
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
40+
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
41+
sudo apt-get update
42+
# sudo apt-get install -y mongodb-org-shell mongodb-org-server mongodb-org-mongos mongodb-org
43+
sudo apt-get install -y mongodb-org
44+
45+
sudo mkdir -p /data/db
46+
sudo sed -i '/ bindIp:/ s/127.0.0.1/0.0.0.0/' /etc/mongod.conf
47+
48+
sudo systemctl start mongod
49+
mongorestore --gzip /vagrant/dump
50+
SHELL
51+
end
52+
53+
config.vm.define "webserver", primary: false do |server|
54+
55+
server.vm.provider :digital_ocean do |provider|
56+
provider.ssh_key_name = ENV["SSH_KEY_NAME"]
57+
provider.token = ENV["DIGITAL_OCEAN_TOKEN"]
58+
provider.image = 'ubuntu-18-04-x64'
59+
provider.region = 'fra1'
60+
provider.size = '1gb'
61+
provider.privatenetworking = true
62+
end
63+
64+
server.vm.hostname = "webserver"
65+
66+
server.trigger.before :up do |trigger|
67+
trigger.info = "Waiting to create server until dbserver's IP is available."
68+
trigger.ruby do |env,machine|
69+
ip_file = "db_ip.txt"
70+
while !File.file?($ip_file) do
71+
sleep(1)
72+
end
73+
db_ip = File.read($ip_file).strip()
74+
puts "Now, I have it..."
75+
puts db_ip
76+
end
77+
end
78+
79+
server.trigger.after :provision do |trigger|
80+
trigger.ruby do |env,machine|
81+
File.delete($ip_file) if File.exists? $ip_file
82+
end
83+
end
84+
85+
server.vm.provision "shell", inline: <<-SHELL
86+
export DB_IP=`cat /vagrant/db_ip.txt`
87+
echo $DB_IP
88+
89+
echo "Installing Anaconda..."
90+
sudo wget https://repo.anaconda.com/archive/Anaconda3-2019.07-Linux-x86_64.sh -O $HOME/Anaconda3-2019.07-Linux-x86_64.sh
91+
92+
bash ~/Anaconda3-2019.07-Linux-x86_64.sh -b
93+
94+
echo ". $HOME/.bashrc" >> $HOME/.bash_profile
95+
echo "export PATH=$HOME/anaconda3/bin:$PATH" >> $HOME/.bash_profile
96+
export PATH="$HOME/anaconda3/bin:$PATH"
97+
rm Anaconda3-2019.07-Linux-x86_64.sh
98+
source $HOME/.bash_profile
99+
100+
echo $DB_IP
101+
102+
103+
pip install Flask-PyMongo
104+
105+
106+
cp -r /vagrant/* $HOME
107+
nohup python minitwit.py > out.log &
108+
echo "================================================================="
109+
echo "= DONE ="
110+
echo "================================================================="
111+
echo "Navigate in your browser to:"
112+
THIS_IP=`hostname -I | cut -d" " -f1`
113+
echo "http://${THIS_IP}:5000"
114+
SHELL
115+
end
116+
config.vm.provision "shell", privileged: false, inline: <<-SHELL
117+
sudo apt-get update
118+
SHELL
119+
120+
end

dump/test/follower.bson.gz

17.7 KB
Binary file not shown.
148 Bytes
Binary file not shown.

dump/test/message.bson.gz

556 KB
Binary file not shown.

dump/test/message.metadata.json.gz

147 Bytes
Binary file not shown.

dump/test/user.bson.gz

15.2 KB
Binary file not shown.

dump/test/user.metadata.json.gz

145 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)