diff --git a/devops-service/config.ru b/devops-service/config.ru index 5938bd7..0f436a9 100644 --- a/devops-service/config.ru +++ b/devops-service/config.ru @@ -4,21 +4,15 @@ require "rubygems" require "bundler/setup" require_relative "devops-service" -require_relative "devops_loader" +require_relative "devops_config" require_relative "routes" root = File.dirname(__FILE__) # Read configuration file -config_file = File.join(root, "config.rb") -config = {} -if File.exists? config_file - eval File.read config_file -else - raise "No config file '#{config_file}' found" -end +DevopsConfig.read +config = DevopsConfig.config -config[:devops_dir] = File.join(ENV["HOME"], ".devops") if config[:devops_dir].nil? puts "Devops home: #{config[:devops_dir]}" unless File.exists?(config[:devops_dir]) FileUtils.mkdir_p config[:devops_dir] @@ -30,5 +24,5 @@ config[:report_dir_v2] = File.expand_path(File.join(config[:devops_dir], "report :report_dir_v2 ].each {|key| d = config[key]; FileUtils.mkdir_p(d) unless File.exists?(d) } -DevopsService.init(config) +DevopsService.init run Rack::URLMap.new(Devops::Routes.routes) diff --git a/devops-service/devops-service.rb b/devops-service/devops-service.rb index 56185b4..e2249d8 100644 --- a/devops-service/devops-service.rb +++ b/devops-service/devops-service.rb @@ -1,5 +1,3 @@ -#!/usr/bin/env ruby - require "wisper" $:.push File.dirname(__FILE__) @@ -11,6 +9,7 @@ require "db/mongo/mongo_connector" require "providers/provider_factory" require "loader" +require "devops_db" require "sidekiq/web" @@ -28,8 +27,9 @@ class DevopsService class << self - def routes config - #Devops::Routes.preffix = "" + def routes + config = DevopsConfig.config + Devops::Routes.preffix = config[:url_prefix] Devops::Routes.route "/version", DevopsVersion Devops::Routes.route "/v2.0", Devops::Version2_0::Application Devops::Routes.route "/client", Client.new(config) @@ -37,12 +37,18 @@ class DevopsService Devops::Routes.route "/sidekiq", Sidekiq::Web end - def init config - routes config - Devops::Version2_0::Application.init config - DevopsLoader.load - DevopsLoader.routes - Devops::Loader.plugins config + def init + # init database + Devops::Db.init + # init plugins + Devops::Loader.init_plugins + # init routes classes + Devops::Version2_0::Application.init + Devops::Version2_0::Application.register_routes + # init routes paths + routes + # add plugins routes + Devops::Loader.routes end end diff --git a/devops-service/devops_config.rb b/devops-service/devops_config.rb new file mode 100644 index 0000000..1b6502d --- /dev/null +++ b/devops-service/devops_config.rb @@ -0,0 +1,48 @@ +require "socket" + +class DevopsConfig + + @@config = nil + + class << self + def read config_file=nil + if config_file.nil? + root = File.dirname(__FILE__) + config_file = File.join(root, "config.rb") + end + config = {:url_prefix => ""} + if File.exists? config_file + eval File.read config_file + else + raise "No config file '#{config_file}' found" + end + config[:devops_dir] = File.join(ENV["HOME"], ".devops") if config[:devops_dir].nil? + addr = first_public_ipv4 || first_private_ipv4 + config[:address] = if addr.nil? + "localhost" + else + addr.ip_address + end + + DevopsConfig.config = config + end + + def config= config + @@config = config + end + + def config + @@config + end + + def first_private_ipv4 + Socket.ip_address_list.detect{|intf| intf.ipv4_private?} + end + + def first_public_ipv4 + Socket.ip_address_list.detect{|intf| intf.ipv4? and !intf.ipv4_loopback? and !intf.ipv4_multicast? and !intf.ipv4_private?} + end + + end + +end diff --git a/devops-service/devops_db.rb b/devops-service/devops_db.rb new file mode 100644 index 0000000..bd01610 --- /dev/null +++ b/devops-service/devops_db.rb @@ -0,0 +1,23 @@ +module Devops + class Db + + @@db = nil + + class << self + def init + config = DevopsConfig.config + mongo_db = config[:mongo_db] || "devops" + mongo_host = config[:mongo_host] || "localhost" + mongo_port = config[:mongo_port] || 27017 + mongo_user = config[:mongo_user] + mongo_password = config[:mongo_password] + @@db = MongoConnector.new(mongo_db, mongo_host, mongo_port, mongo_user, mongo_password) + end + + def connector + @@db + end + + end + end +end diff --git a/devops-service/devops_loader.rb b/devops-service/devops_loader.rb index abc4f99..0f63c4c 100644 --- a/devops-service/devops_loader.rb +++ b/devops-service/devops_loader.rb @@ -4,14 +4,4 @@ class DevopsLoader #Devops::Routes.route "/version", DevopsVersion end - def self.routes - if defined?(Devops::Plugin) - routes = {} - plugins = Devops::Plugin.constants.collect{|s| Devops::Plugin.const_get(s)}.select {|const| const.class == Module} - puts plugins.inspect - plugins.each do |p| - p.routes - end - end - end end diff --git a/devops-service/loader.rb b/devops-service/loader.rb index f6f3181..4a36eb5 100644 --- a/devops-service/loader.rb +++ b/devops-service/loader.rb @@ -2,18 +2,28 @@ module Devops module Loader class << self - def plugins config + def init_plugins + plugins do |plugin| + plugin.init + end + end + def routes + plugins do |plugin| + plugin.routes + end + end + + def plugins &block if defined?(Devops::Plugin) - routes = {} plugins = Devops::Plugin.constants.collect{|s| Devops::Plugin.const_get(s)}.select {|const| const.class == Module} puts plugins.inspect plugins.each do |p| - p.init(config) + yield p end end - end + end end end diff --git a/devops-service/routes/v2.0.rb b/devops-service/routes/v2.0.rb index e7b6e03..1e71e42 100644 --- a/devops-service/routes/v2.0.rb +++ b/devops-service/routes/v2.0.rb @@ -1,4 +1,3 @@ -require "bundler/setup" require "sinatra/base" require "sinatra/streaming" require "helpers/version_2" @@ -41,15 +40,10 @@ module Devops class Application < Sinatra::Base class << self - def init config + def init + config = DevopsConfig.config set :devops_home, config[:devops_dir] - set :config, config - - set :mongo_db, config[:mongo_db] || "devops" - set :mongo_host, config[:mongo_host] || "localhost" - set :mongo_port, config[:mongo_port] || 27017 - set :mongo_user, config[:mongo_user] - set :mongo_password, config[:mongo_password] + #set :config, config set :keys_dir, (config[:keys_dir] || File.join(config[:devops_dir], "files/keys")) set :scripts_dir, (config[:scripts_dir] || File.join(config[:devops_dir], "files/scripts")) @@ -64,8 +58,25 @@ module Devops end def init_mongo - mongo = MongoConnector.new(settings.mongo_db, settings.mongo_host, settings.mongo_port, settings.mongo_user, settings.mongo_password) - set :mongo, mongo + set :mongo, Devops::Db.connector + end + + def register_routes + register Devops::Version2_0::Routes::ProviderRoutes + register Devops::Version2_0::Routes::BootstrapTemplatesRoutes + register Devops::Version2_0::Routes::UserRoutes + register Devops::Version2_0::Routes::FilterRoutes + register Devops::Version2_0::Routes::FlavorRoutes + register Devops::Version2_0::Routes::GroupRoutes + register Devops::Version2_0::Routes::ImageRoutes + register Devops::Version2_0::Routes::KeyRoutes + register Devops::Version2_0::Routes::NetworkRoutes + register Devops::Version2_0::Routes::ProjectRoutes + register Devops::Version2_0::Routes::ScriptRoutes + register Devops::Version2_0::Routes::ServerRoutes + register Devops::Version2_0::Routes::StatusRoutes + register Devops::Version2_0::Routes::TagRoutes + register Devops::Version2_0::Routes::DeployRoutes end private @@ -87,22 +98,6 @@ module Devops helpers Sinatra::Streaming helpers Devops::Version2_0::Helpers - register Devops::Version2_0::Routes::ProviderRoutes - register Devops::Version2_0::Routes::BootstrapTemplatesRoutes - register Devops::Version2_0::Routes::UserRoutes - register Devops::Version2_0::Routes::FilterRoutes - register Devops::Version2_0::Routes::FlavorRoutes - register Devops::Version2_0::Routes::GroupRoutes - register Devops::Version2_0::Routes::ImageRoutes - register Devops::Version2_0::Routes::KeyRoutes - register Devops::Version2_0::Routes::NetworkRoutes - register Devops::Version2_0::Routes::ProjectRoutes - register Devops::Version2_0::Routes::ScriptRoutes - register Devops::Version2_0::Routes::ServerRoutes - register Devops::Version2_0::Routes::StatusRoutes - register Devops::Version2_0::Routes::TagRoutes - register Devops::Version2_0::Routes::DeployRoutes - use Rack::Auth::Basic do |username, password| begin settings.mongo.user_auth(username, password)