11require_relative 'meta/command'
22require 'util/config'
33require 'semantic'
4+ require 'docker-api'
45
56module 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
0 commit comments