legion-data 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2 -0
  3. data/legion-data.gemspec +2 -2
  4. data/lib/legion/data.rb +55 -35
  5. data/lib/legion/data/connection.rb +52 -19
  6. data/lib/legion/data/connections/base.rb +19 -3
  7. data/lib/legion/data/connections/mysql2.rb +2 -17
  8. data/lib/legion/data/connections/mysql_base.rb +1 -1
  9. data/lib/legion/data/migration.rb +7 -20
  10. data/lib/legion/data/migrations/002_add_users.rb +17 -0
  11. data/lib/legion/data/migrations/003_add_groups.rb +16 -0
  12. data/lib/legion/data/migrations/004_add_chains.rb +25 -0
  13. data/lib/legion/data/migrations/005_add_envs.rb +24 -0
  14. data/lib/legion/data/migrations/006_add_dcs.rb +24 -0
  15. data/lib/legion/data/migrations/007_add_nodes.rb +26 -0
  16. data/lib/legion/data/migrations/008_add_settings.rb +18 -0
  17. data/lib/legion/data/migrations/009_add_extensions.rb +25 -0
  18. data/lib/legion/data/migrations/010_add_runners.rb +21 -0
  19. data/lib/legion/data/migrations/011_add_functions.rb +29 -0
  20. data/lib/legion/data/migrations/012_add_tasks.rb +25 -0
  21. data/lib/legion/data/migrations/013_add_task_logs.rb +17 -0
  22. data/lib/legion/data/migrations/014_add_relationships.rb +27 -0
  23. data/lib/legion/data/model.rb +24 -26
  24. data/lib/legion/data/models/chain.rb +2 -1
  25. data/lib/legion/data/models/datacenter.rb +2 -1
  26. data/lib/legion/data/models/environment.rb +2 -1
  27. data/lib/legion/data/models/extension.rb +13 -0
  28. data/lib/legion/data/models/function.rb +7 -4
  29. data/lib/legion/data/models/group.rb +10 -0
  30. data/lib/legion/data/models/namespace.rb +3 -1
  31. data/lib/legion/data/models/node.rb +1 -1
  32. data/lib/legion/data/models/relationship.rb +3 -1
  33. data/lib/legion/data/models/runner.rb +17 -0
  34. data/lib/legion/data/models/setting.rb +10 -0
  35. data/lib/legion/data/models/task.rb +2 -1
  36. data/lib/legion/data/models/task_log.rb +0 -1
  37. data/lib/legion/data/models/user.rb +10 -0
  38. data/lib/legion/data/settings.rb +62 -0
  39. data/lib/legion/data/version.rb +1 -1
  40. metadata +29 -26
  41. data/lib/legion/data/migrations/002_add_chains_table.rb +0 -21
  42. data/lib/legion/data/migrations/003_add_datacenters_table.rb +0 -21
  43. data/lib/legion/data/migrations/004_add_envs_table.rb +0 -21
  44. data/lib/legion/data/migrations/005_add_functions_table.rb +0 -25
  45. data/lib/legion/data/migrations/006_add_namespaces_table.rb +0 -24
  46. data/lib/legion/data/migrations/007_add_nodes_table.rb +0 -22
  47. data/lib/legion/data/migrations/008_add_relationships_table.rb +0 -28
  48. data/lib/legion/data/migrations/009_add_task_logs_table.rb +0 -17
  49. data/lib/legion/data/migrations/010_add_tasks_table.rb +0 -20
  50. data/lib/legion/data/migrations/011_add_users_and_groups.rb +0 -28
  51. data/lib/legion/data/migrations/012_foreign_keys_users_and_groups.rb +0 -42
  52. data/lib/legion/data/migrations/013_function_foreign_keys.rb +0 -10
  53. data/lib/legion/data/migrations/014_nodes_foreign_keys.rb +0 -12
  54. data/lib/legion/data/migrations/015_relationships_foreign_keys.rb +0 -14
  55. data/lib/legion/data/migrations/016_tasks_foreign_keys.rb +0 -22
  56. data/lib/legion/data/migrations/017_add_relationship_to_tasks.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1201c65f7728a2af01e9b77ea56b2bf5b1f357c246f12cf84e59790d917a314c
4
- data.tar.gz: e7a17e1c023b5514a31952fad0898e4048dc89938888ffcb76632403a1aa3a94
3
+ metadata.gz: dd32f907af81657448d1fdbebc681b795d536754cec212ccaa154239c13ca48f
4
+ data.tar.gz: 527fd840b7ae7bdf09f65a5b60af7491bea793427719404448f4c05967fc8266
5
5
  SHA512:
6
- metadata.gz: '08530a83befaf49097ffcb3caff420f92d4d2fe942e88dce7b847a854d4241ab5d8370cef6168995e725db5fe26ff7773bb6763b7391992974b39bf444563638'
7
- data.tar.gz: cfa8d6055185cbea52085d845058538148f497096e13ff3370cd13086b00a062e4dc9861663f749c58781bc23bb37ea87e588b049a20b884dd1f881025d16a50
6
+ metadata.gz: b41d14cbf4d9e3c62a84a2361ecb2b6143cdc508866bee245bec6c46bf263c8b421ef944ea353f360ac1d704d8777b570cf2ea279be466ceef17f939f9902ad5
7
+ data.tar.gz: e0acb9226a6c7987b008be0d7e5e9eef22ebb58194ae9613a73846a85d464a071ddc82b17cf5bf3267d467ea1cf42d180047565b85d0565516fb121dbd1f45d1
data/CHANGELOG.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # Legion::Data Changelog
2
+ ## v0.2.0
3
+ * Redesigning schema
2
4
 
3
5
  ## v0.1.1
4
6
  * Updated `rubocop.yml` with updated syntax
data/legion-data.gemspec CHANGED
@@ -37,8 +37,8 @@ Gem::Specification.new do |spec|
37
37
  spec.add_development_dependency 'rubocop-rspec'
38
38
  spec.add_development_dependency 'rubocop-sequel'
39
39
 
40
- spec.add_dependency 'legion-logging', '~>0.1'
41
- spec.add_dependency 'legion-settings', '~>0.1'
40
+ spec.add_dependency 'legion-logging'
41
+ spec.add_dependency 'legion-settings'
42
42
 
43
43
  if RUBY_ENGINE == 'jruby'
44
44
  spec.add_dependency 'jdbc-mysql'
data/lib/legion/data.rb CHANGED
@@ -1,43 +1,63 @@
1
1
  require 'legion/data/version'
2
+ require_relative 'data/settings'
2
3
  require 'sequel'
3
- require 'legion/data/connection'
4
- require 'legion/data/migration'
5
- require 'legion/data/model'
4
+
5
+ require_relative 'data/connection'
6
+ require_relative 'data/model'
7
+ require_relative 'data/migration'
6
8
 
7
9
  module Legion
8
10
  module Data
9
- def build(options = {})
10
- @connected = false
11
- options.merge!(default_options) { |_key, v1, _v2| v1 }
12
- @connection = connect(options[:connection]) if options[:connection][:auto_connect]
13
- return unless options[:connection][:auto_connect]
14
-
15
- migrate(@connection, options[:migration])
16
- model(options[:model]) if options[:model][:auto_load]
17
- end
18
-
19
- def connect(_options = {})
20
- Legion::Data::Connection.new
21
- end
22
-
23
- def migrate(connection, options = {})
24
- return false if options.key?(:auto_migrate) && options[:auto_migrate]
25
-
26
- Legion::Data::Migration.new(connection.database.connection)
27
- end
28
-
29
- def model(_options = {})
30
- Legion::Data::Models.new
31
- end
32
-
33
- def default_options
34
- { migration: { auto_migrate: true },
35
- model: { auto_load: true },
36
- connection: { auto_connect: true } }
37
- end
38
-
39
- def merge_options(options, default = default_options)
40
- options.merge!(default) { |_key, v1, _v2| v1 }
11
+ class << self
12
+ def setup
13
+ connection_setup
14
+ migrate
15
+ load_models
16
+ # setup_cache
17
+ end
18
+
19
+ def connection_setup
20
+ return if Legion::Settings[:data][:connected]
21
+
22
+ Legion::Data::Connection.setup
23
+ end
24
+
25
+ def migrate
26
+ Legion::Data::Migration.migrate
27
+ end
28
+
29
+ def load_models
30
+ Legion::Data::Models.load
31
+ end
32
+
33
+ def connection
34
+ Legion::Data::Connection.sequel
35
+ end
36
+
37
+ def setup_cache # rubocop:disable Metrics/AbcSize
38
+ return if Legion::Settings[:data][:cache][:enable]
39
+
40
+ unless Legion::Settings[:cache][:connected]
41
+ Legion::Logging.warn 'Legion::Data has caching enabled but Legion::Cache is not started'
42
+ end
43
+ return unless Legion::Settings[:cache][:connected]
44
+
45
+ # Legion::Data::Model::Relationship.plugin :caching, Legion::Cache, ttl: 10
46
+ Legion::Data::Model::Chain.plugin :caching, Legion::Cache, ttl: 60
47
+ Legion::Data::Model::Datacenter.plugin :caching, Legion::Cache, ttl: 120
48
+ Legion::Data::Model::Extension.plugin :caching, Legion::Cache, ttl: 120
49
+ Legion::Data::Model::Function.plugin :caching, Legion::Cache, ttl: 120
50
+ Legion::Data::Model::Namespace.plugin :caching, Legion::Cache, ttl: 120
51
+ Legion::Data::Model::Node.plugin :caching, Legion::Cache, ttl: 10
52
+ Legion::Data::Model::TaskLog.plugin :caching, Legion::Cache, ttl: 12
53
+ Legion::Data::Model::Task.plugin :caching, Legion::Cache, ttl: 10
54
+ Legion::Data::Model::User.plugin :caching, Legion::Cache, ttl: 120
55
+ Legion::Data::Model::Group.plugin :caching, Legion::Cache, ttl: 120
56
+ end
57
+
58
+ def shutdown
59
+ Legion::Data::Connection.shutdown
60
+ end
41
61
  end
42
62
  end
43
63
  end
@@ -2,28 +2,61 @@ require 'sequel'
2
2
 
3
3
  module Legion
4
4
  module Data
5
- class Connection
6
- attr_accessor :database
7
- def initialize(options = {})
8
- options.merge!(default_options) { |_key, v1, _v2| v1 }
9
- return unless options[:auto_connect]
10
-
11
- jruby(options) if RUBY_ENGINE == 'jruby'
12
- mri(options) unless RUBY_ENGINE == 'jruby'
13
- end
5
+ module Connection
6
+ class << self
7
+ attr_reader :sequel
8
+ if RUBY_ENGINE == 'jruby'
9
+ require_relative 'connections/jdbc'
10
+ include Legion::Data::Connections::JDBC
11
+ else
12
+ require_relative 'connections/mysql2'
13
+ include Legion::Data::Connections::MySQL2
14
+ end
14
15
 
15
- def default_options
16
- { auto_connect: true }
17
- end
16
+ def setup
17
+ @sequel = ::Sequel.connect(adapter: adapter, **creds_builder)
18
+ Legion::Settings[:data][:connected] = true
19
+ return unless Legion::Settings[:data][:connection][:log]
18
20
 
19
- def jruby(_options = {})
20
- require 'legion/data/connections/jdbc'
21
- @database = Legion::Data::Connections::JDBC.new
22
- end
21
+ @sequel.logger = Legion::Logging::Logger.new(level: 'debug')
22
+ @sequel.sql_log_level = Legion::Settings[:data][:connection][:sql_log_level]
23
+ @sequel.log_warn_duration = Legion::Settings[:data][:connection][:log_warn_duration]
24
+ end
25
+
26
+ def shutdown
27
+ @sequel.disconnect
28
+ end
29
+
30
+ def creds_builder # rubocop:disable Metrics/AbcSize
31
+ final_creds = {}
32
+ final_creds.merge! default_creds
33
+ final_creds.merge! Legion::Settings[:data][:creds]
34
+ return final_creds if Legion::Settings[:vault].nil?
35
+
36
+ if Legion::Settings[:vault][:connected] && ::Vault.sys.mounts.key?(:database)
37
+ temp_vault_creds = Legion::Crypt.read('database/creds/legion')
38
+ final_creds[:user] = temp_vault_creds[:username]
39
+ final_creds[:password] = temp_vault_creds[:password]
40
+ end
41
+
42
+ final_creds
43
+ end
44
+
45
+ def default_creds
46
+ {
47
+ host: '127.0.0.1',
48
+ port: 3306,
49
+ username: 'legion',
50
+ password: 'legion',
51
+ database: 'legion',
52
+ max_connections: 32,
53
+ preconnect: 'concurrently'
54
+ }
55
+ end
23
56
 
24
- def mri(_options = {})
25
- require 'legion/data/connections/mysql2'
26
- @database = Legion::Data::Connections::MySQL2.new
57
+ def adapter
58
+ 'mysql2'
59
+ end
27
60
  end
28
61
  end
29
62
  end
@@ -2,23 +2,29 @@ module Legion
2
2
  module Data
3
3
  module Connections
4
4
  class Base
5
+ attr_reader :sequel
5
6
  def check_gem(gem = adapter)
6
7
  Gem::Specification.find_by_name(gem)
7
8
  rescue Gem::MissingSpecError, ArgumentError
8
9
  false
9
10
  end
10
11
 
11
- def creds_builder(credentials = {})
12
+ def creds_builder(credentials = {}) # rubocop:disable Metrics/AbcSize
12
13
  creds = {}
14
+ if Legion::Settings[:vault][:connected] && ::Vault.sys.mounts.key?(:database)
15
+ creds = Legion::Crypt.read('database/creds/legion')
16
+ Legion::Logging.info 'Grabbing database creds from vault'
17
+ end
18
+
13
19
  creds = creds.merge(default_creds) if creds.is_a? Hash
14
- creds = creds.merge(Legion::Settings[:data][:mysql]) if Legion::Settings[:data][:mysql].is_a? Hash
20
+ creds = creds.merge(Legion::Settings[:data][:creds]) if Legion::Settings[:data][:creds].is_a? Hash
15
21
  creds = creds.merge(credentials) if credentials.is_a? Hash
16
22
  creds
17
23
  end
18
24
 
19
25
  def connect(adapter, creds, _options = {})
20
26
  creds[:adapter] = adapter
21
- Sequel.connect(creds)
27
+ @sequel = Sequel.connect(creds)
22
28
  end
23
29
 
24
30
  def settings(options, _connection = @connection)
@@ -39,6 +45,16 @@ module Legion
39
45
  def adapter
40
46
  'mysql2'
41
47
  end
48
+
49
+ def default_creds
50
+ { data: { creds: {
51
+ host: '127.0.0.1',
52
+ user: 'legion',
53
+ password: 'legion',
54
+ max_connections: 1000,
55
+ database: 'legion'
56
+ } } }
57
+ end
42
58
  end
43
59
  end
44
60
  end
@@ -1,24 +1,9 @@
1
- require 'legion/data/connections/mysql_base'
2
-
3
1
  module Legion
4
2
  module Data
5
3
  module Connections
6
- class MySQL2 < Legion::Data::Connections::MySQLBase
7
- attr_accessor :connection
8
- def initialize(_options = {})
9
- Legion::Logging.debug('Connecting to MySQL with MySQL2 Connector')
10
- @connection = connect
11
-
12
- Legion::Logging.info("Legion is connected to database #{Legion::Settings[:data][:mysql][:database]}")
13
- Legion::Settings[:data][:connected] = true
14
- end
15
-
4
+ module MySQL2
16
5
  def adapter
17
- 'mysql2'
18
- end
19
-
20
- def connect
21
- super(adapter, creds_builder)
6
+ :mysql2
22
7
  end
23
8
  end
24
9
  end
@@ -3,7 +3,7 @@ require 'legion/data/connections/base'
3
3
  module Legion
4
4
  module Data
5
5
  module Connections
6
- class MySQLBase < Legion::Data::Connections::Base
6
+ module MySQLBase
7
7
  def default_creds
8
8
  { data: { mysql: {
9
9
  host: '127.0.0.1',
@@ -2,26 +2,13 @@ require 'sequel/extensions/migration'
2
2
 
3
3
  module Legion
4
4
  module Data
5
- class Migration
6
- def initialize(connection, options = {})
7
- options.merge!(default_options) { |_key, v1, _v2| v1 }
8
- migrate(options[:migration_path], connection) if options[:auto_migrate]
9
- end
10
-
11
- def default_options
12
- { auto_migrate: true, migration_path: __dir__ + '/migrations' }
13
- end
14
-
15
- def migrate(path, connection)
16
- Legion::Logging.debug("Running Legion::Data.migrate with path: #{path}")
17
- Sequel::Migrator.run(connection, path)
18
- Legion::Logging.info('Legion::Data finished migrations')
19
- rescue Sequel::Migrator::Error => e
20
- Legion::Logging.error(e.message)
21
- raise e
22
- rescue StandardError => e
23
- Legion::Logging.error(e.message)
24
- raise e unless Legion::Settings[:data][:migrations][:continue_on_fail]
5
+ module Migration
6
+ class << self
7
+ def migrate(connection = Legion::Data.connection, path = __dir__ + '/migrations')
8
+ Legion::Settings[:data][:migrations][:version] = Sequel::Migrator.run(connection, path)
9
+ Legion::Logging.info 'Legion::Data::Migration ran successfully to version ' + Legion::Settings[:data][:migrations][:version].to_s # rubocop:disable Layout/LineLength
10
+ Legion::Settings[:data][:migrations][:ran] = true
11
+ end
25
12
  end
26
13
  end
27
14
  end
@@ -0,0 +1,17 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `users` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `active` tinyint(1) unsigned DEFAULT '1',
6
+ `name` varchar(128) DEFAULT NULL,
7
+ `version` tinyint(5) unsigned NOT NULL DEFAULT '1',
8
+ `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
9
+ `updated` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
10
+ PRIMARY KEY (`id`)
11
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
12
+ end
13
+
14
+ down do
15
+ drop :users
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `groups` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `name` varchar(128) DEFAULT NULL,
6
+ `active` tinyint(1) unsigned DEFAULT '1',
7
+ `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
8
+ `updated` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
9
+ PRIMARY KEY (`id`)
10
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
11
+ end
12
+
13
+ down do
14
+ drop :groups
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `chains` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `name` varchar(128) NOT NULL DEFAULT '',
6
+ `active` tinyint(1) unsigned DEFAULT '1',
7
+ `version` int(11) unsigned NOT NULL DEFAULT '1',
8
+ `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
9
+ `updated` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
10
+ `user_owner` int(11) unsigned DEFAULT NULL,
11
+ `group_owner` int(11) unsigned DEFAULT NULL,
12
+ PRIMARY KEY (`id`),
13
+ UNIQUE KEY `name` (`name`),
14
+ KEY `active` (`active`),
15
+ KEY `chains_user_owner` (`user_owner`),
16
+ KEY `chains_group_owner` (`group_owner`),
17
+ CONSTRAINT `chains_group_owner` FOREIGN KEY (`group_owner`) REFERENCES `groups` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
18
+ CONSTRAINT `chains_user_owner` FOREIGN KEY (`user_owner`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
19
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
20
+ end
21
+
22
+ down do
23
+ drop :chains
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `environments` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `name` varchar(128) NOT NULL DEFAULT '',
6
+ `active` tinyint(1) unsigned NOT NULL DEFAULT '1',
7
+ `user_owner` int(11) unsigned DEFAULT NULL,
8
+ `group_owner` int(11) unsigned DEFAULT NULL,
9
+ `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
10
+ `updated` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
11
+ PRIMARY KEY (`id`),
12
+ UNIQUE KEY `name` (`name`),
13
+ KEY `active` (`active`),
14
+ KEY `environments_user_owner` (`user_owner`),
15
+ KEY `environments_group_owner` (`group_owner`),
16
+ CONSTRAINT `environments_group_owner` FOREIGN KEY (`group_owner`) REFERENCES `groups` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
17
+ CONSTRAINT `environments_user_owner` FOREIGN KEY (`user_owner`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
18
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
19
+ end
20
+
21
+ down do
22
+ drop :environments
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `datacenters` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `name` varchar(128) NOT NULL DEFAULT '',
6
+ `active` tinyint(1) unsigned NOT NULL DEFAULT '1',
7
+ `user_owner` int(11) unsigned DEFAULT NULL,
8
+ `group_owner` int(11) unsigned DEFAULT NULL,
9
+ `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
10
+ `updated` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
11
+ PRIMARY KEY (`id`),
12
+ UNIQUE KEY `name` (`name`),
13
+ KEY `active` (`active`),
14
+ KEY `datacenters_user_owner` (`user_owner`),
15
+ KEY `datacenters_group_owner` (`group_owner`),
16
+ CONSTRAINT `datacenters_group_owner` FOREIGN KEY (`group_owner`) REFERENCES `groups` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
17
+ CONSTRAINT `datacenters_user_owner` FOREIGN KEY (`user_owner`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
18
+ ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;"
19
+ end
20
+
21
+ down do
22
+ drop :datacenters
23
+ end
24
+ end