legion-data 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +147 -0
  3. data/.gitignore +13 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +29 -0
  6. data/Gemfile +7 -0
  7. data/README.md +35 -0
  8. data/Rakefile +55 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/bitbucket-pipelines.yml +26 -0
  12. data/legion-data.gemspec +38 -0
  13. data/lib/legion/data.rb +43 -0
  14. data/lib/legion/data/connection.rb +30 -0
  15. data/lib/legion/data/connections/base.rb +45 -0
  16. data/lib/legion/data/connections/jdbc.rb +21 -0
  17. data/lib/legion/data/connections/mysql.rb +26 -0
  18. data/lib/legion/data/connections/mysql2.rb +26 -0
  19. data/lib/legion/data/connections/mysql_base.rb +19 -0
  20. data/lib/legion/data/migration.rb +28 -0
  21. data/lib/legion/data/migrations/001_add_schema_columns.rb +17 -0
  22. data/lib/legion/data/migrations/002_add_chains_table.rb +21 -0
  23. data/lib/legion/data/migrations/003_add_datacenters_table.rb +21 -0
  24. data/lib/legion/data/migrations/004_add_envs_table.rb +21 -0
  25. data/lib/legion/data/migrations/005_add_functions_table.rb +25 -0
  26. data/lib/legion/data/migrations/006_add_namespaces_table.rb +24 -0
  27. data/lib/legion/data/migrations/007_add_nodes_table.rb +22 -0
  28. data/lib/legion/data/migrations/008_add_relationships_table.rb +28 -0
  29. data/lib/legion/data/migrations/009_add_task_logs_table.rb +17 -0
  30. data/lib/legion/data/migrations/010_add_tasks_table.rb +20 -0
  31. data/lib/legion/data/migrations/011_add_users_and_groups.rb +28 -0
  32. data/lib/legion/data/migrations/012_foreign_keys_users_and_groups.rb +42 -0
  33. data/lib/legion/data/migrations/013_function_foreign_keys.rb +10 -0
  34. data/lib/legion/data/migrations/014_nodes_foreign_keys.rb +12 -0
  35. data/lib/legion/data/migrations/015_relationships_foreign_keys.rb +14 -0
  36. data/lib/legion/data/migrations/016_tasks_foreign_keys.rb +22 -0
  37. data/lib/legion/data/migrations/017_add_relationship_to_tasks.rb +14 -0
  38. data/lib/legion/data/model.rb +35 -0
  39. data/lib/legion/data/models/chain.rb +12 -0
  40. data/lib/legion/data/models/datacenter.rb +12 -0
  41. data/lib/legion/data/models/environment.rb +12 -0
  42. data/lib/legion/data/models/function.rb +14 -0
  43. data/lib/legion/data/models/namespace.rb +12 -0
  44. data/lib/legion/data/models/node.rb +13 -0
  45. data/lib/legion/data/models/relationship.rb +16 -0
  46. data/lib/legion/data/models/task.rb +17 -0
  47. data/lib/legion/data/models/task_log.rb +13 -0
  48. data/lib/legion/data/version.rb +5 -0
  49. metadata +230 -0
@@ -0,0 +1,30 @@
1
+ require 'sequel'
2
+
3
+ module Legion
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
14
+
15
+ def default_options
16
+ { auto_connect: true }
17
+ end
18
+
19
+ def jruby(_options = {})
20
+ require 'legion/data/connections/jdbc'
21
+ @database = Legion::Data::Connections::JDBC.new
22
+ end
23
+
24
+ def mri(_options = {})
25
+ require 'legion/data/connections/mysql2'
26
+ @database = Legion::Data::Connections::MySQL2.new
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,45 @@
1
+ module Legion
2
+ module Data
3
+ module Connections
4
+ class Base
5
+ def check_gem(gem = adapter)
6
+ Gem::Specification.find_by_name(gem)
7
+ rescue Gem::MissingSpecError, ArgumentError
8
+ false
9
+ end
10
+
11
+ def creds_builder(credentials = {})
12
+ creds = {}
13
+ 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
15
+ creds = creds.merge(credentials) if credentials.is_a? Hash
16
+ creds
17
+ end
18
+
19
+ def connect(adapter, creds, _options = {})
20
+ creds[:adapter] = adapter
21
+ Sequel.connect(creds)
22
+ end
23
+
24
+ def settings(options, _connection = @connection)
25
+ raise ArgumentError unless options.is_a? Hash
26
+ end
27
+
28
+ def debug_logging(enabled = true, connection = @connection, _options = {})
29
+ if enabled
30
+ connection.loggers = Legion::Logging
31
+ connection.sql_log_level = :debug
32
+ elsif enabled == false
33
+ connection.loggers = nil
34
+ connection.sql_log_level = :debug
35
+ end
36
+ connection
37
+ end
38
+
39
+ def adapter
40
+ 'mysql2'
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,21 @@
1
+ module Legion
2
+ module Data
3
+ module Connections
4
+ class JDBC
5
+ attr_accessor :connection
6
+ def initialize
7
+ Legion::Logging.debug('Connecting to MySQL with JBDC Connector')
8
+ connection_string = build_connection_string
9
+ @connection = Sequel.connect(connection_string, max_connetions: @args[:max_connections])
10
+ Legion::Logging.info("Legion is connected to database #{args[:database]}")
11
+ end
12
+
13
+ def build_connection_string
14
+ set = Legion::Settings[:data][:mysql]
15
+ con_string = "jdbc:mysql://#{set[:host]}/#{set[:database]}?user=#{set[:user]}&password=#{set[:password]}"
16
+ con_string
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ require 'legion/data/connections/mysql_base'
2
+
3
+ module Legion
4
+ module Data
5
+ module Connections
6
+ class MySQL < 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
+
16
+ def adapter
17
+ 'mysql2'
18
+ end
19
+
20
+ def connect
21
+ super(adapter, creds_builder)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ require 'legion/data/connections/mysql_base'
2
+
3
+ module Legion
4
+ module Data
5
+ 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
+
16
+ def adapter
17
+ 'mysql2'
18
+ end
19
+
20
+ def connect
21
+ super(adapter, creds_builder)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ require 'legion/data/connections/base'
2
+
3
+ module Legion
4
+ module Data
5
+ module Connections
6
+ class MySQLBase < Legion::Data::Connections::Base
7
+ def default_creds
8
+ { data: { mysql: {
9
+ host: '127.0.0.1',
10
+ user: 'legion',
11
+ password: 'legion',
12
+ max_connections: 10,
13
+ database: 'legion'
14
+ } } }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ require 'sequel/extensions/migration'
2
+
3
+ module Legion
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 => ex
20
+ Legion::Logging.error(ex.message)
21
+ raise ex
22
+ rescue StandardError => ex
23
+ Legion::Logging.error(ex.message)
24
+ raise ex unless Legion::Settings[:data][:migrations][:continue_on_fail]
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ require 'sequel/extensions/migration'
2
+
3
+ Sequel.migration do
4
+ up do
5
+ run 'ALTER TABLE `schema_info` ADD `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `version`;'
6
+ run 'ALTER TABLE `schema_info` ADD `updated_at` TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP AFTER `created_at`;'
7
+ run 'ALTER TABLE `schema_info` ADD `catalog` VARCHAR(255) NULL DEFAULT NULL AFTER `version`;'
8
+ end
9
+
10
+ down do
11
+ alter_table(:schema) do
12
+ drop_column :catalog
13
+ drop_column :updated_at
14
+ drop_column :created_at
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
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_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
9
+ `updated_at` 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
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
16
+ end
17
+
18
+ down do
19
+ drop_table :chains
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `datacenters` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `active` tinyint(1) unsigned NOT NULL DEFAULT '1',
6
+ `version` int(11) unsigned NOT NULL DEFAULT '1',
7
+ `name` varchar(128) NOT NULL DEFAULT '',
8
+ `user_owner` int(11) unsigned DEFAULT NULL,
9
+ `group_owner` int(11) unsigned DEFAULT NULL,
10
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
11
+ `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
12
+ PRIMARY KEY (`id`),
13
+ UNIQUE KEY `name` (`name`),
14
+ KEY `active` (`active`)
15
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
16
+ end
17
+
18
+ down do
19
+ drop_table :datacenters
20
+ end
21
+ end
@@ -0,0 +1,21 @@
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
+ `version` int(11) unsigned NOT NULL DEFAULT '1',
8
+ `user_owner` int(11) unsigned DEFAULT NULL,
9
+ `group_owner` int(11) unsigned DEFAULT NULL,
10
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
11
+ `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
12
+ PRIMARY KEY (`id`),
13
+ UNIQUE KEY `name` (`name`),
14
+ KEY `active` (`active`)
15
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
16
+ end
17
+
18
+ down do
19
+ drop_table :environments
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `functions` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `active` tinyint(1) unsigned NOT NULL DEFAULT '1',
6
+ `version` int(11) unsigned NOT NULL DEFAULT '1',
7
+ `name` varchar(128) NOT NULL DEFAULT '',
8
+ `namespace_id` int(11) unsigned NOT NULL,
9
+ `args` text,
10
+ `user_owner` int(11) unsigned DEFAULT NULL,
11
+ `group_owner` int(11) unsigned DEFAULT NULL,
12
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
13
+ `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
14
+ PRIMARY KEY (`id`),
15
+ UNIQUE KEY `namespace_id` (`namespace_id`,`name`),
16
+ KEY `active` (`active`),
17
+ KEY `namespace` (`namespace_id`),
18
+ KEY `name` (`name`)
19
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
20
+ end
21
+
22
+ down do
23
+ drop_table :functionss
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `namespaces` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `active` tinyint(1) unsigned NOT NULL DEFAULT '1',
6
+ `namespace` varchar(128) NOT NULL DEFAULT '',
7
+ `queue` varchar(128) NOT NULL DEFAULT '',
8
+ `uri` varchar(128) DEFAULT NULL,
9
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
10
+ `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
11
+ `group_owner` int(11) unsigned DEFAULT NULL,
12
+ `user_owner` int(11) unsigned DEFAULT NULL,
13
+ PRIMARY KEY (`id`),
14
+ UNIQUE KEY `namespace` (`namespace`),
15
+ KEY `active` (`active`),
16
+ KEY `queue` (`queue`),
17
+ KEY `uri` (`uri`)
18
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
19
+ end
20
+
21
+ down do
22
+ drop_table :namespaces
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `nodes` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `active` tinyint(1) unsigned NOT NULL DEFAULT '1',
6
+ `name` varchar(128) NOT NULL DEFAULT '',
7
+ `datacenter_id` int(11) unsigned DEFAULT NULL,
8
+ `environment_id` int(11) unsigned DEFAULT NULL,
9
+ `status` varchar(255) NOT NULL DEFAULT 'unknown',
10
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
11
+ `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
12
+ PRIMARY KEY (`id`),
13
+ UNIQUE KEY `name` (`name`),
14
+ KEY `active` (`active`),
15
+ KEY `status` (`status`)
16
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
17
+ end
18
+
19
+ down do
20
+ drop_table :nodes
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `relationships` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `active` tinyint(1) unsigned NOT NULL DEFAULT '1',
6
+ `name` varchar(128) DEFAULT NULL,
7
+ `chain_id` int(11) unsigned DEFAULT NULL,
8
+ `trigger_id` int(11) unsigned NOT NULL,
9
+ `action_id` int(11) unsigned NOT NULL,
10
+ `delay` int(11) unsigned NOT NULL DEFAULT '0',
11
+ `conditions` text,
12
+ `transformation` text,
13
+ `version` tinyint(5) unsigned NOT NULL DEFAULT '1',
14
+ `created_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
15
+ `updated_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
16
+ `group_owner` int(11) unsigned DEFAULT NULL,
17
+ `user_owner` int(11) unsigned DEFAULT NULL,
18
+ PRIMARY KEY (`id`),
19
+ KEY `active` (`active`),
20
+ KEY `name` (`name`),
21
+ KEY `deplay` (`delay`)
22
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
23
+ end
24
+
25
+ down do
26
+ drop_table :relationships
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `task_logs` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `task_id` int(11) unsigned NOT NULL,
6
+ `node_id` int(11) unsigned NOT NULL,
7
+ `log` text,
8
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
9
+ `updated_at` 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_table :task_logs
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `tasks` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `status` varchar(64) NOT NULL DEFAULT 'created',
6
+ `payload` text,
7
+ `parent_id` int(11) unsigned DEFAULT NULL,
8
+ `master_id` int(11) unsigned DEFAULT NULL,
9
+ `version` tinyint(5) NOT NULL DEFAULT '1',
10
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
11
+ `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
12
+ PRIMARY KEY (`id`),
13
+ KEY `status` (`status`)
14
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
15
+ end
16
+
17
+ down do
18
+ drop_table :tasks
19
+ end
20
+ end
@@ -0,0 +1,28 @@
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_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
9
+ `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
10
+ PRIMARY KEY (`id`)
11
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
12
+
13
+ run "CREATE TABLE `groups` (
14
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
15
+ `active` tinyint(1) unsigned DEFAULT '1',
16
+ `name` varchar(128) DEFAULT NULL,
17
+ `version` tinyint(5) unsigned NOT NULL DEFAULT '1',
18
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
19
+ `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
20
+ PRIMARY KEY (`id`)
21
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
22
+ end
23
+
24
+ down do
25
+ drop_table :groups
26
+ drop_table :users
27
+ end
28
+ end