lib/mkit.rb in mkit-0.4.1 vs lib/mkit.rb in mkit-0.4.2

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'rubygems' require 'bundler' require 'json' require 'yaml' require 'optparse' @@ -8,17 +10,18 @@ require 'net/http' require 'sinatra/base' require 'mkit/config/environment' require 'mkit/app/mkit_server' require 'mkit/config/load_default_configs' -require_relative "mkit/version" +require_relative 'mkit/version' require_relative 'mkit/mkit_interface' require_relative 'mkit/mkit_dns' require_relative 'mkit/docker_listener' require 'mkit/app/helpers/haproxy' require 'mkit/app/controllers/services_controller' require 'mkit/app/controllers/mkitjobs_controller' +require 'mkit/app/controllers/mkit_controller' require 'mkit/mkit_interface' require 'mkit/mkit_dns' require 'mkit/job_manager' require 'mkit/workers/worker_manager' require 'mkit/sagas/saga_manager' @@ -38,27 +41,27 @@ def self.configure(options:) @root = MKIt::Utils.root MKItLogger.debug! # # config dir - if ENV["RACK_ENV"] != "development" - @config_dir = options[:config_dir].nil? ? '/etc/mkit' : options[:config_dir] - else - @config_dir = options[:config_dir].nil? ? "#{@root}/config" : options[:config_dir] - end + @config_dir = if ENV['RACK_ENV'] != 'development' + options[:config_dir].nil? ? '/etc/mkit' : options[:config_dir] + else + options[:config_dir].nil? ? "#{@root}/config" : options[:config_dir] + end MKIt::Utils.set_config_dir(@config_dir) # create dirs - if ENV["RACK_ENV"] != "development" || !options[:config_dir].nil? + if ENV['RACK_ENV'] != 'development' || !options[:config_dir].nil? check_config_files = false - if ! File.exists?(@config_dir) + unless File.exist?(@config_dir) FileUtils.mkdir_p(@config_dir) check_config_files = true end - FileUtils.mkdir_p('/var/lib/mkitd') unless File.exists?('/var/lib/mkitd') - FileUtils.cp( "#{@root}/config/mkit_config.yml", @config_dir) unless File.exists?("#{@config_dir}/mkit_config.yml") - FileUtils.cp( "#{@root}/config/database.yml", @config_dir) unless File.exists?("#{@config_dir}/database.yml") - FileUtils.cp( "#{@root}/config/mkitd_config.sh", @config_dir) unless File.exists?("#{@config_dir}/mkitd_config.sh") + FileUtils.mkdir_p('/var/lib/mkitd') unless File.exist?('/var/lib/mkitd') + FileUtils.cp("#{@root}/config/mkit_config.yml", @config_dir) unless File.exist?("#{@config_dir}/mkit_config.yml") + FileUtils.cp("#{@root}/config/database.yml", @config_dir) unless File.exist?("#{@config_dir}/database.yml") + FileUtils.cp("#{@root}/config/mkitd_config.sh", @config_dir) unless File.exist?("#{@config_dir}/mkitd_config.sh") if check_config_files MKItLogger.info "Configuration files copied to #{@config_dir}. Please check it and restart." exit end end @@ -67,95 +70,91 @@ MKIt::Initializers.load_my_configuration # # run config based tasks FileUtils.mkdir_p(MKIt::Config.mkit.haproxy.config_dir) # ...haproxy defaults file - FileUtils.cp( - "#{MKIt::Utils.root}/lib/mkit/app/templates/haproxy/0000_defaults.cfg", - MKIt::Config.mkit.haproxy.config_dir - ) unless File.exists?("#{MKIt::Config.mkit.haproxy.config_dir}/0000_defaults.cfg") + unless File.exist?("#{MKIt::Config.mkit.haproxy.config_dir}/0000_defaults.cfg") + FileUtils.cp( + "#{MKIt::Utils.root}/lib/mkit/app/templates/haproxy/0000_defaults.cfg", + MKIt::Config.mkit.haproxy.config_dir + ) + end # # conn = { adapter: "sqlite3", database: ":memory:" } # ActiveRecord::Base.establish_connection(conn) # include ActiveRecord::Tasks # DatabaseTasks.database_configuration = YAML.load_file('my_database_config.yml') # DatabaseTasks.db_dir = 'db' # DatabaseTasks.database_configuration = MKIt::Utils.load_db_config - DatabaseTasks.env=MKIt::Config.mkit.database.env - DatabaseTasks.migrations_paths=[ "#{@root}/db/migrate" ] - DatabaseTasks.db_dir="db" - DatabaseTasks.root=@root + DatabaseTasks.env = MKIt::Config.mkit.database.env + DatabaseTasks.migrations_paths = ["#{@root}/db/migrate"] + DatabaseTasks.db_dir = 'db' + DatabaseTasks.root = @root end def self.establish_db_connection - # ActiveRecord::Base.establish_connection(DatabaseTasks.database_configuration[DatabaseTasks.env]) ActiveRecord::Base.connection.migration_context.migrations_paths.clear ActiveRecord::Base.connection.migration_context.migrations_paths << "#{@root}/db/migrate" - # MKItLogger.debug "database_tasks migration paths #{DatabaseTasks.migrations_paths}" MKItLogger.debug "active_record_base migration_paths #{ActiveRecord::Base.connection.migration_context.migrations_paths.inspect}" MKItLogger.debug "active_record_base configurations #{ActiveRecord::Base.configurations.inspect}" MKItLogger.debug "active_record_base conn_db_config #{ActiveRecord::Base.connection_db_config.inspect}" end def self.migrate - ActiveRecord::Base.connection.migration_context.migrate if ActiveRecord::Base.connection.migration_context.needs_migration? + if ActiveRecord::Base.connection.migration_context.needs_migration? + ActiveRecord::Base.connection.migration_context.migrate + end end def self.restore_operation - MKItLogger.info "restoring operations..." + MKItLogger.info 'restoring operations...' # create interfaces of deployed apps otherwise haproxy won't start - Service.all.each { | srv | + Service.all.each do |srv| srv.deploy_network srv.update_status! - } + end # daemontools would eventually start haproxy; systemd does not. # so, restart here. + MKItLogger.debug 'restarting proxy...' MKIt::HAProxy.restart end def self.startup(options: {}) - self.configure(options: options) - self.establish_db_connection - self.migrate + configure(options: options) + establish_db_connection + migrate MKIt::Initializers.load_default_configs MKIt::Interface.up - System.register(:job_manager, memoize: true) { + System.register(:job_manager, memoize: true) do MKIt::JobManager.new - } - System.register(:mkit_dns, memoize: true) { - Thread.new { + end + System.register(:mkit_dns, memoize: true) do + Thread.new do dns = MKIt::DNS.new dns.run - } - } - System.register(:docker_listener, memoize: true) { + end + end + System.register(:docker_listener, memoize: true) do MKIt::DockerListener.new - } + end # watchdog feature is to be re-evaluated # System.register(:watchdog, memoize: true) { # MKIt::WatchdogManager.new # } # register workers - WorkerManager.register_workers + WorkerManager.register_workers SagaManager.register_workers - # System[:job_manager].start System[:docker_listener].start # watchdog feature is to be re-evaluated # System[:watchdog].start System[:mkit_dns].run - # - self.restore_operation - # - MKItLogger.debug "restarting proxy..." - MKIt::HAProxy.restart - MKItLogger.info "MKIt is up and running!" + restore_operation + MKItLogger.info 'MKIt is up and running!' end - end -