Module: MKIt

Includes:
ActiveRecord::Tasks
Defined in:
lib/mkit/ctypes.rb,
lib/mkit.rb,
lib/mkit/utils.rb,
lib/mkit/status.rb,
lib/mkit/version.rb,
lib/mkit/mkit_dns.rb,
lib/mkit/cmd_runner.rb,
lib/mkit/exceptions.rb,
lib/mkit/job_manager.rb,
lib/mkit/sagas/asaga.rb,
lib/mkit/ssl/easy_ssl.rb,
lib/mkit/config/config.rb,
lib/mkit/mkit_interface.rb,
lib/mkit/app/mkit_server.rb,
lib/mkit/workers/aworker.rb,
lib/mkit/cmd/shell_client.rb,
lib/mkit/client/http_client.rb,
lib/mkit/sagas/saga_manager.rb,
lib/mkit/workers/pod_worker.rb,
lib/mkit/app/helpers/haproxy.rb,
lib/mkit/client/mkitd_client.rb,
lib/mkit/pods/docker_listener.rb,
lib/mkit/sagas/create_pod_saga.rb,
lib/mkit/app/helpers/erb_helper.rb,
lib/mkit/workers/haproxy_worker.rb,
lib/mkit/workers/service_worker.rb,
lib/mkit/workers/worker_manager.rb,
lib/mkit/app/helpers/pods_helper.rb,
lib/mkit/client/websocket_client.rb,
lib/mkit/pods/docker_exec_command.rb,
lib/mkit/pods/docker_log_listener.rb,
lib/mkit/app/helpers/docker_helper.rb,
lib/mkit/app/helpers/params_helper.rb,
lib/mkit/app/helpers/services_helper.rb,
lib/mkit/client/log_websocket_client.rb,
lib/mkit/config/load_default_configs.rb,
lib/mkit/app/helpers/interface_helper.rb,
lib/mkit/client/console_websocket_client.rb

Overview

requires Hash.to_o

Defined Under Namespace

Modules: CType, Config, DockerHelper, ERBHelper, HAProxy, Initializers, InterfaceHelper, ParamsHelper, PodsHelper, PoolStatus, ServicesHelper, Status, Templates, Utils Classes: ASaga, AWorker, AppAlreadyDeployedException, BaseException, CmdRunner, CmdRunnerException, ConsoleWebSocketClient, CreatePodSaga, DNS, DockerExecCommand, DockerListener, DockerLogListener, EasySSL, Error, ExitShell, HAProxyWorker, HttpClient, Interface, InvalidPortMappingTypeException, InvalidPortsConfiguration, JobManager, LogWebSocketClient, MKItCType, MKItStatus, MKItdClient, PodNotFoundException, PodWorker, PoolExaustedException, SagaManager, Server, ServiceAlreadyExists, ServiceNameMismatch, ServiceNotFoundException, ServiceWorker, ShellClient, StopThread, WebSocketClient, WorkerManager

Constant Summary collapse

System =
Dry::Container.new
VERSION =
"0.9.0"

Class Method Summary collapse

Class Method Details

.configure(options:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mkit.rb', line 42

def self.configure(options:)
  @root = MKIt::Utils.root
  @options = options
  MKItLogger.debug!
  #
  # config dir
  @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)
  # defaults
  @bind = options[:bind] ||= 'localhost'
  @port = options[:port] ||= 4567
  @ssl = options[:ssl].nil? ? true : options[:ssl] && true
  @verify_peer = options[:verify_peer].nil? ? false : options[:verify_peer] && true
  @cert_chain_file = options[:cert_chain_file] ||= "#{@config_dir}/#{MKIt::Utils::MKIT_CRT}"
  @private_key_file = options[:private_key_file] ||= "#{@config_dir}/#{MKIt::Utils::MKIT_KEY}"

  # create dirs
  if ENV['RACK_ENV'] != 'development' || !options[:config_dir].nil?
    check_config_files = false
    unless File.exist?(@config_dir)
      FileUtils.mkdir_p(@config_dir)
      check_config_files = true
    end
    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

  # load configuration
  MKIt::Initializers.load_my_configuration
  # cert
  MKIt::EasySSL.create_self_certificate(@config_dir)
  #
  # run config based tasks
  FileUtils.mkdir_p(MKIt::Config.mkit.haproxy.config_dir)
  # ...haproxy defaults file
  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
end

.establish_db_connectionObject



121
122
123
124
125
126
127
128
129
# File 'lib/mkit.rb', line 121

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

.migrateObject



131
132
133
134
135
# File 'lib/mkit.rb', line 131

def self.migrate
  if ActiveRecord::Base.connection.migration_context.needs_migration?
    ActiveRecord::Base.connection.migration_context.migrate
  end
end

.options(server) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mkit.rb', line 107

def self.options(server)
  if @ssl
    ssl_options = {
      private_key_file:  @private_key_file,
      cert_chain_file: @cert_chain_file,
      verify_peer: @verify_peer
    }
    server.ssl = true
    server.ssl_options = ssl_options
  end
  server.backend.port = @port
  server.backend.host = @bind
end

.restore_operationObject



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mkit.rb', line 137

def self.restore_operation
  MKItLogger.info 'restoring operations...'
  # create interfaces of deployed apps  otherwise haproxy won't start
  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

.startup(options: {}) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/mkit.rb', line 150

def self.startup(options: {})
  configure(options: options)
  establish_db_connection
  migrate

  MKIt::Initializers.load_default_configs
  MKIt::Interface.up

  System.register(:job_manager, memoize: true) do
    MKIt::JobManager.new
  end
  System.register(:mkit_dns, memoize: true) do
    Thread.new do
      dns = MKIt::DNS.new
      dns.run
    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
  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
  restore_operation
  MKItLogger.info 'MKIt is up and running!'
end