Skip to content

Commit 235f11e

Browse files
committed
Update checkup to build docker image and install mongo via docker
1 parent 9ab3425 commit 235f11e

23 files changed

+156
-255
lines changed

Dockerfile.rubyimage

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Builds an image to run framework level bots
2+
FROM ruby:2.3.8
3+
RUN bundle config --global frozen 1
4+
WORKDIR /app
5+
COPY Gemfile Gemfile.lock ./
6+
RUN gem install bundler
7+
RUN bundle install

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ruby '2.3.8'
44

55
gem "ansi", "~> 1.5"
66
gem 'bson', '~> 3.0'
7+
gem "docker-api", "~> 1.34"
78
gem 'git', '~> 1.2'
89
gem "grpc", "~> 1.24"
910
gem 'nutella_lib','~>0.5'

lib/bots/binary-files-manager/bin_files_mngr.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626

2727
# Set data folder
28-
data_folder = "#{ENV['HOME']}/.nutella/data/binary-files-manager"
28+
# We should use nutella config!!!!
29+
# data_folder = "#{ENV['HOME']}/.nutella/data/binary-files-manager"
2930

3031
# If data folder doesn't exist, create it
3132
unless Dir.exists? data_folder

lib/bots/commands_server/commands/new.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def create_dir_structure( app_id )
4646
config_file_hash = {
4747
:name => app_id,
4848
:version => '0.1.0',
49-
:nutella_version => File.open("#{Nutella::NUTELLA_HOME}VERSION", 'rb').read,
49+
:nutella_version => File.open("#{Nutella::NUTELLA_SRC}VERSION", 'rb').read,
5050
:type => 'application',
5151
:description => 'A quick description of your application'
5252
}

lib/bots/commands_server/commands/stop.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def stop_app_bots( app_id )
6666

6767

6868
def stop_framework_components
69-
nutella_components_dir = "#{Nutella::NUTELLA_HOME}framework_components"
69+
nutella_components_dir = "#{Nutella::NUTELLA_SRC}framework_components"
7070
ComponentsList.for_each_component_in_dir nutella_components_dir do |component|
7171
pid_file_path = "#{nutella_components_dir}/#{component}/.pid"
7272
kill_process_with_pid pid_file_path
@@ -81,7 +81,7 @@ def stop_internal_broker
8181

8282

8383
def stop_mongo
84-
pid_file_path = "#{Nutella.config['config_dir']}.mongo_pid"
84+
pid_file_path = "#{Nutella.config['home_dir']}.mongo_pid"
8585
kill_process_with_pid pid_file_path
8686
end
8787

lib/bots/commands_server/commands/template.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ def create_template_files(json, template_dir)
130130
# Create nutella.json file
131131
File.open("#{template_dir}/nutella.json", 'w') { |f| f.write(JSON.pretty_generate json) }
132132
# Add bot/interface specific files
133-
FileUtils.copy(File.join(Nutella::NUTELLA_HOME, 'data/startup'), template_dir) if json['type']=='bot'
134-
FileUtils.copy(File.join(Nutella::NUTELLA_HOME, 'data/index.html'), template_dir) if json['type']=='interface'
133+
FileUtils.copy(File.join(Nutella::NUTELLA_SRC, 'data/startup'), template_dir) if json['type']=='bot'
134+
FileUtils.copy(File.join(Nutella::NUTELLA_SRC, 'data/index.html'), template_dir) if json['type']=='interface'
135135
console.success "Template #{json['name']} created successfully!"
136136
end
137137

lib/cli/cli.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def self.command_exists?(command)
6767
# Print nutella logo
6868
def self.print_nutella_logo
6969
console.info(NUTELLA_LOGO)
70-
nutella_version = File.open("#{Nutella::NUTELLA_HOME}VERSION", 'rb').read
70+
nutella_version = File.open("#{Nutella::NUTELLA_SRC}VERSION", 'rb').read
7171
console.info("Welcome to nutella version #{nutella_version}! For a complete lists of available commands type 'nutella help'\n")
7272
# If nutella is not ready to be used (i.e. nobody has invoked the "nutella checkup" command yet),
7373
# append warning/reminder message

lib/cli/commands/checkup.rb

+107-57
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative 'meta/command'
22
require 'util/config'
33
require 'semantic'
4+
require 'docker-api'
45

56
module Nutella
67
class Checkup < Command
@@ -12,19 +13,16 @@ def run( args=nil )
1213
return unless all_dependencies_installed?
1314

1415
# Check if we have a local broker installed
15-
# and install one if we don't
16-
if broker_exists
17-
console.info 'You have a local broker installed. Yay!'
18-
else
19-
console.warn 'You don\'t seem to have a local broker installed so we are going to go ahead and install one for you. This might take some time...'
20-
unless install_local_broker
21-
console.error 'Whoops...something went wrong while installing the broker'
22-
return
23-
end
24-
end
16+
# and installs one if we don't
17+
return unless broker_docker_image_ready?
18+
19+
# Check if we have a mongo installed locally
20+
# and installs one if we don't
21+
return unless mongo_docker_image_ready?
2522

26-
# Check that supervisor is properly configured
27-
return unless supervisor_configured_correctly?
23+
# Check that we have a nutella ruby image built
24+
# if not, build one
25+
return unless nutella_docker_image_ready?
2826

2927
# Set ready flag in config.json
3028
Config.file['ready'] = true
@@ -34,36 +32,13 @@ def run( args=nil )
3432
end
3533

3634

37-
private
35+
private
3836

39-
40-
def broker_exists
41-
# Check if Docker image for the broker was already pulled
42-
if `docker images matteocollina/mosca:v2.3.0 --format "{{.ID}}"` != ""
43-
# If so, check that a broker configuration exists and create one if it doesn't
44-
Config.file['broker'] = '127.0.0.1' if Config.file['broker'].nil?
45-
true
46-
else
47-
false
48-
end
49-
end
5037

51-
52-
def install_local_broker
53-
# Docker pull to install
54-
system "docker pull matteocollina/mosca:v2.3.0 > /dev/null 2>&1"
55-
# Write broker setting inside config.json
56-
Config.file['broker'] = '127.0.0.1'
57-
end
58-
59-
6038
def all_dependencies_installed?
6139
# Docker version lambda
6240
docker_semver = lambda do
63-
out = `docker --version`
64-
token = out.split(' ')
65-
token[2].slice(0..1)
66-
Semantic::Version.new token[2].slice(0..1).concat('.0.0')
41+
Semantic::Version.new(Docker.version['Version'].slice(0..1).concat('.0.0'))
6742
end
6843
# Git version lambda
6944
git_semver = lambda do
@@ -76,21 +51,9 @@ def all_dependencies_installed?
7651
end
7752
semver
7853
end
79-
# Immortal version lambda
80-
supervisor_semver = lambda do
81-
out = `supervisorctl version`
82-
out.gsub("\n",'')
83-
Semantic::Version.new out
84-
end
85-
# Mongo version lambda
86-
mongo_semver = lambda do
87-
out = `mongod --version`
88-
out.slice!(0,12)
89-
Semantic::Version.new out[0..4]
90-
end
9154
# Check versions
92-
return true if check_version?('docker', '17.0.0', docker_semver) && check_version?('git', '1.8.0', git_semver) && check_version?('supervisor', '4.1.0', supervisor_semver) && check_version?('mongodb', '2.6.9', mongo_semver)
93-
# If even one of the checks fails, return false
55+
return true if check_version?('docker', '17.0.0', docker_semver) && check_version?('git', '1.8.0', git_semver)
56+
# If any of the checks fail, return false instead
9457
false
9558
end
9659

@@ -114,14 +77,101 @@ def check_version?(dep, req_version, lambda)
11477
end
11578

11679

117-
def supervisor_configured_correctly?
118-
# TODO Check that supervisor's MAC_CONFIG_DIR exists, if not create
119-
# TODO Make sure the MAC_CONFIG_DIR is inluded in supervisor's MAC_CONFIG_FILE, if not include
120-
# TODO Make sure that [inet_http_server] (rpc server) is enabled in MAC_CONFIG_FILE
80+
# Checks that the broker image has been pulled and pulls it if not
81+
def broker_docker_image_ready?
82+
if broker_image_exists?
83+
console.info 'You have a local broker installed. Yay!'
84+
else
85+
console.warn 'You don\'t seem to have a local broker installed so we are going to go ahead and install one for you. This might take some time...'
86+
begin
87+
install_local_broker
88+
rescue => e
89+
puts e
90+
console.error 'Whoops...something went wrong while installing the broker, try running \'nutella checkup\' again'
91+
false
92+
end
93+
console.info 'Broker installed successfully!'
94+
end
12195
true
12296
end
12397

124-
end
12598

126-
end
99+
# Checks that: 1. The Docker image for the broker has been pulled and
100+
# 2. config.json has been correctly configured
101+
def broker_image_exists?
102+
Docker::Image.exist?('matteocollina/mosca:v2.3.0') && !Config.file['broker'].nil?
103+
end
127104

105+
106+
def install_local_broker
107+
# Docker pull to install
108+
Docker::Image.create('fromImage': 'matteocollina/mosca:v2.3.0')
109+
# Write broker setting inside config.json
110+
Config.file['broker'] = '127.0.0.1'
111+
end
112+
113+
114+
# Checks that the mongo image has been pulled and pulls it if not
115+
def mongo_docker_image_ready?
116+
if mongo_image_exists?
117+
console.info 'You have mongo installed locally. Yay!'
118+
else
119+
console.warn 'You don\'t seem to have a mongo installed locally so we are going to go ahead and install it for you. This might take some time...'
120+
begin
121+
install_local_mongo
122+
rescue => e
123+
puts e
124+
console.error 'Whoops...something went wrong while installing mongo, try running \'nutella checkup\' again'
125+
return false
126+
end
127+
console.info 'Mongo installed successfully!'
128+
end
129+
true
130+
end
131+
132+
133+
# Checks that: 1. The Docker image for mongo has been pulled and
134+
# 2. config.json has been correctly configured
135+
def mongo_image_exists?
136+
Docker::Image.exist?('mongo:3.2.21') && !Config.file['mongo'].nil?
137+
end
138+
139+
140+
def install_local_mongo
141+
# Docker pull to install
142+
Docker::Image.create('fromImage': 'mongo:3.2.21')
143+
# Write mongo setting inside config.json
144+
Config.file['mongo'] = '127.0.0.1'
145+
end
146+
147+
148+
def nutella_docker_image_ready?
149+
if nutella_image_exists?
150+
console.info 'You have a nutella docker image ready. Yay!'
151+
else
152+
console.warn 'You don\'t seem to have a nutella docker image ready. We\'re gonna go ahead and build one for you. This might take some time...'
153+
begin
154+
build_nutella_docker_image
155+
rescue => e
156+
puts e
157+
console.error 'Whoops...something went wrong while building the nutella docker image, try running \'nutella checkup\' again'
158+
return false
159+
end
160+
console.info 'nutella docker image built successfully!'
161+
end
162+
true
163+
end
164+
165+
# Checks that the nutella image exists and if not tries to build it
166+
def nutella_image_exists?
167+
Docker::Image.exist?('nutella:1.0.0')
168+
end
169+
170+
171+
def build_nutella_docker_image
172+
img = Docker::Image.build_from_dir(NUTELLA_SRC, { 'dockerfile': 'Dockerfile.rubyimage' })
173+
img.tag('repo': 'nutella', 'tag': '1.0.0', force: true)
174+
end
175+
176+
end
177+
end

lib/nutella_framework.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
module Nutella
55

66
# Initialize nutella home to the folder where this source code is
7-
NUTELLA_HOME = File.dirname(__FILE__)[0..-4]
8-
NUTELLA_TMP = "#{NUTELLA_HOME}.tmp/"
7+
NUTELLA_SRC = File.dirname(__FILE__)[0..-4]
8+
NUTELLA_TMP = "#{NUTELLA_SRC}.tmp/"
9+
NUTELLA_HOME = "#{ENV['HOME']}.nutella/"
910

1011
# If the nutella configuration file (config.json) is empty (or doesn't exist) we're going to initialize it
1112
# with nutella constants and defaults

lib/supervisor/supervisor.proto

-16
This file was deleted.

lib/supervisor/supervisor.rb

-21
This file was deleted.

lib/supervisor/supervisor_pb.rb

-21
This file was deleted.

lib/supervisor/supervisor_services_pb.rb

-20
This file was deleted.

lib/supervisor/test_client.rb

-6
This file was deleted.

lib/util/config.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ class Config
88
# - immortal_dir: directory used to store immortal yaml files
99
# - main_interface_port: the port used to serve interfaces
1010
def self.init
11-
file['config_dir'] = "#{ENV['HOME']}/.nutella/"
12-
file['broker_dir'] = "#{file['config_dir']}broker/"
11+
file['src_dir'] = NUTELLA_SRC
12+
file['tmp_dir'] = NUTELLA_TMP
13+
file['home_dir'] = NUTELLA_HOME
1314
file['main_interface_port'] = 57880
1415
end
1516

0 commit comments

Comments
 (0)