Skip to content

Commit 0f9d9a3

Browse files
committed
Basic supervisor working
1 parent 1e920c9 commit 0f9d9a3

File tree

16 files changed

+74
-96
lines changed

16 files changed

+74
-96
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ runlist.json
44
broker/
55
.tmp/
66
.pid
7+
.mongo_pid
8+
stdout.log
79

810
# Test app
911
test_app/

Gemfile

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
source 'http://rubygems.org'
22

3-
gem 'semantic', '~> 1.4'
43
gem "ansi", "~> 1.5"
4+
gem 'bson', '~> 3.0'
55
gem 'git', '~> 1.2'
6+
gem 'nutella_lib','~>0.4', '>=0.4.24'
7+
gem 'nokogiri', '~>1.6'
8+
gem 'slop', '~>4.0'
9+
gem 'semantic', '~> 1.4'
610
gem 'sinatra', '~>1.4'
711
gem 'sinatra-cross_origin', '~> 0.3.2'
812
gem 'thin', '~>1.6'
9-
gem 'nokogiri', '~>1.6'
10-
gem 'slop', '~>4.0'
11-
gem 'nutella_lib','~>0.4', '>=0.4.24'
12-
gem 'activesupport', '~>4.2'
13-
gem 'bson', '~> 3.0'
13+
gem "xmlrpc", "~> 0.3.0"
1414

1515
group :development do
1616
gem 'yard', '~> 0.9.11'
@@ -24,5 +24,3 @@ group :test do
2424
gem 'rspec', '~> 3.8'
2525
gem 'fuubar', '~> 2.4'
2626
end
27-
28-

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ nutella is a framework to build and run Macroworlds. The original prototype was
77
# Installing
88
nutella works on OSX and Linux (tested on Ubuntu) and it depends on a couple other things to work correctly. You will need:
99

10-
1. _ruby_ (version >= 2.1.0). Do yourself a favor and use [RVM](https://rvm.io/rvm/install) to install Ruby.
10+
1. _ruby_ (version >= 2.3.0). Do yourself a favor and use [RVM](https://rvm.io/rvm/install) to install Ruby.
1111
1. _git_ (version >= 1.8.0). Should come with the OS, yay!
12-
1. _tmux_ (version >= 1.8.0). Do yourself a favor and use [Homebrew](http://brew.sh/) to install tmux, if you are on OSX.
13-
1. _Docker_ (version >= 17.03.0). We use Docker to run the broker that handles all communications between all the pieces of the framework. Do yourself a favor and use [Docker for mac](https://store.docker.com/editions/community/docker-ce-desktop-mac), if you are on OSX.
12+
1. _supervisor_ (version >= 4.1.0). You can use [Homebrew](http://brew.sh/) to install supervisor on OSX.
13+
1. _docker_ (version >= 17.03.0). We use Docker to run the broker that handles all communications between all the pieces of the framework. Do yourself a favor and use [Docker for mac](https://store.docker.com/editions/community/docker-ce-desktop-mac), if you are on OSX.
1414

15-
Once you have all of installed, simply do:
15+
Once you have all of thi stuff installed you can do:
1616
```
1717
gem install nutella_framework
1818
```
1919
Once the installation is complete you should be able to type `nutella` in your shell and get a welcome message.
2020

2121
## nutella checkup
22-
If you are reading this you probably already saw the warning: "Looks like this is a fresh installation of nutella. Please run `nutella checkup` to check all dependencies are installed".
22+
If you are reading this you probably already saw the warning: "Looks like this is a fresh installation of nutella. Please run `nutella checkup` to check all dependencies are installed correctly". **Please follow the prompt!**
2323

2424
```
2525
nutella checkup

lib/bots/binary-files-manager/startup

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
$stdout.sync = true ## nutella woudl do this
2+
# $stdout.sync = true
3+
puts "Hi, I'm a basic ruby bot and all I do is idle and print stuff"
4+
$stderr.puts "certainly first param is set #{ARGV[0]}"
5+
6+
7+
begin
8+
i = 0
9+
while true
10+
puts "#{i} A log line!"
11+
i = i + 1
12+
sleep 1
13+
end
14+
rescue SignalException => e
15+
puts "HEY I WAS KILLED!!!"
16+
$stderr.puts "AND I COMPLAIN IN STDERR"
17+
puts e
18+
end

lib/bots/main_interface/main_interface_bot.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
require 'sinatra'
33
require 'nokogiri'
44

5-
require_relative '../../lib/config/runlist'
6-
require_relative '../../lib/config/config'
7-
require_relative '../../nutella_lib/framework_core'
5+
require_relative '../../config/config'
6+
require_relative '../../config/runlist'
87

98

109
# Set Sinatra to run in production mode
1110
set :environment, :production
1211

1312
# Set Sinatra's port to nutella's main_interface_port
14-
set :port, Nutella.config['main_interface_port']
13+
set :port, Nutella::Config.file['main_interface_port']
1514
# Disable X-Frame-Options header to allow iframes
1615
set :protection, :except => :frame_options
1716

@@ -63,7 +62,7 @@
6362
# If the index file doesn't exist, render error page
6463
return erb( :not_found_404, :locals => {:not_found_type => 'idx'} ) unless File.exist? index_file_path
6564
# If the index file exists, compose query string and redirect
66-
index_with_query_url = "#{request.path}/index.html?broker=#{Nutella.config['broker']}&app_id=#{app_id}&run_id=#{run_id}"
65+
index_with_query_url = "#{request.path}/index.html?broker=#{Nutella::Config.file['broker']}&app_id=#{app_id}&run_id=#{run_id}"
6766
redirect index_with_query_url
6867
end
6968

lib/bots/main_interface/startup

Lines changed: 0 additions & 5 deletions
This file was deleted.

lib/bots/order.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

lib/bots/runs_list_bot/startup

Lines changed: 0 additions & 5 deletions
This file was deleted.

lib/cli/cli.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def self.print_nutella_logo
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
74-
if Nutella.config['ready'].nil?
74+
if Config.file['ready'].nil?
7575
console.warn 'Looks like this is a fresh installation of nutella. Please run \'nutella checkup\' to check all dependencies are installed.'
7676
end
7777
end

lib/cli/commands/checkup.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def run( args=nil )
2222
return
2323
end
2424
end
25+
26+
# Check that supervisor is properly configured
27+
return unless supervisor_configured_correctly?
2528

2629
# Set ready flag in config.json
2730
Config.file['ready'] = true
@@ -74,8 +77,8 @@ def all_dependencies_installed?
7477
semver
7578
end
7679
# Immortal version lambda
77-
immortal_semver = lambda do
78-
out = `immortal -v`
80+
supervisor_semver = lambda do
81+
out = `supervisorctl version`
7982
out.gsub("\n",'')
8083
Semantic::Version.new out
8184
end
@@ -86,7 +89,7 @@ def all_dependencies_installed?
8689
Semantic::Version.new out[0..4]
8790
end
8891
# Check versions
89-
return true if check_version?('docker', '17.0.0', docker_semver) && check_version?('git', '1.8.0', git_semver) && check_version?('immortal', '0.23.0', immortal_semver) && check_version?('mongodb', '2.6.9', mongo_semver)
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)
9093
# If even one of the checks fails, return false
9194
false
9295
end
@@ -110,6 +113,14 @@ def check_version?(dep, req_version, lambda)
110113
end
111114
end
112115

116+
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
121+
true
122+
end
123+
113124
end
114125

115126
end

lib/nutella_framework.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
# This is the entry point to the gem. The code here gets executed BEFORE
2-
# anything else. For this reason, this is a great place to import all the
3-
# nutella modules.
4-
require 'config_files_management/config'
5-
require 'cli/nutella_cli'
6-
# require 'logging/nutella_logging'
7-
# require 'config/runlist'
8-
# require 'config/current_app_utils'
1+
require 'config/config'
2+
require 'cli/cli'
93

104
module Nutella
115

lib/util/components_list.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
module Nutella
2+
3+
end
14
# Utility methods to list components
25
class ComponentsList
36

lib/util/framework_components_starter.rb

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
# require_relative 'components_list'
1+
require 'util/supervisor'
22

33
module Nutella
44
# Utility functions to start components
55
class FrameworkComponentsStarter
6-
include PidFile
76

87
def self.start
9-
FrameworkComponentsStarter.new.start_framework_components
8+
FrameworkComponentsStarter.new.start
109
end
1110

12-
# Starts all framework components. If order.json is present, components are started
13-
# in that order.
11+
# Starts all framework components.
1412
# @return [boolean] true if all components are started correctly, false otherwise
15-
def start_framework_components
16-
nutella_components_dir = "#{NUTELLA_HOME}framework_components"
17-
if File.exist? "#{nutella_components_dir}/order.json"
18-
components_list = JSON.parse IO.read "#{nutella_components_dir}/order.json"
19-
else
20-
components_list = ComponentsList.components_in_dir nutella_components_dir
21-
end
22-
components_list.each do |component|
23-
if File.exist? "#{nutella_components_dir}/#{component}/startup"
24-
unless start_framework_component "#{nutella_components_dir}/#{component}"
25-
return false
26-
end
13+
def start
14+
supervisor = Supervisor.new
15+
nutella_components_dir = "#{Nutella::NUTELLA_HOME}lib/bots"
16+
framework_components.each do |c|
17+
if File.exist? "#{nutella_components_dir}/#{c}/startup"
18+
supervisor.add("nutella_f_#{c}", "#{nutella_components_dir}/#{c}/startup")
2719
end
2820
end
21+
framework_components.each do |c|
22+
supervisor.start("nutella_f_#{c}")
23+
end
2924
true
3025
end
3126

27+
# Finds the framework level components that need to be started
28+
def framework_components
29+
d = "#{Nutella::NUTELLA_HOME}lib/bots"
30+
Dir.entries(d).select {|entry| File.directory?(File.join(d, entry)) && !(entry =='.' || entry == '..') }
31+
end
32+
3233

3334
# Starts the application level bots
3435
# @return [boolean] true if all bots are started correctly, false otherwise
@@ -69,28 +70,6 @@ def self.start_run_bots( bots_list, app_path, app_id, run_id )
6970

7071
#--- Private class methods --------------
7172

72-
73-
# Starts a single framework component
74-
# @return [boolean] true if the component has been started successfully, false otherwise
75-
def self.start_framework_component( component_dir )
76-
pid_file_path = "#{component_dir}/.pid"
77-
return true if sanitize_pid_file pid_file_path
78-
# Component is not running and there is no pid file so we try to start it
79-
# and create a new pid file. Note that the pid file is created by
80-
# the startup script!
81-
# Framework components are started without any parameters passed to them because they have
82-
# full access to config, runlist and framework APIs using 'require_relative'
83-
command = "#{component_dir}/startup"
84-
pid = fork
85-
exec(command) if pid.nil?
86-
# Give it a second so they can start properly
87-
sleep 1
88-
# All went well so we return true
89-
true
90-
end
91-
private_class_method :start_framework_component
92-
93-
9473
# Starts a run level bot
9574
def self.start_run_level_bot( bots_dir, bot, tmux )
9675
# If there is no 'startup' script output a warning (because

lib/util/immortal.rb

Lines changed: 0 additions & 4 deletions
This file was deleted.

spec/cli/commands/server_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
require 'spec_helper'
2-
require 'cli/cli'
2+
require 'nutella_framework'
33

44
module Nutella
55
describe Server do
66
skip 'Starts the MQTT broker' do
77
NutellaCLI.execute_command('server')
8-
# TODO resume from here... need to implement
9-
# framework components start using immortal...
108
end
119
end
1210
end

0 commit comments

Comments
 (0)