legion-data-java 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +171 -0
  3. data/.gitignore +12 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +33 -0
  6. data/CHANGELOG.md +16 -0
  7. data/Gemfile +4 -0
  8. data/Gemfile.lock +99 -0
  9. data/README.md +37 -0
  10. data/Rakefile +55 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/bitbucket-pipelines.yml +26 -0
  14. data/legion-data.gemspec +50 -0
  15. data/lib/legion/data.rb +60 -0
  16. data/lib/legion/data/connection.rb +61 -0
  17. data/lib/legion/data/migration.rb +15 -0
  18. data/lib/legion/data/migrations/001_add_schema_columns.rb +17 -0
  19. data/lib/legion/data/migrations/002_add_users.rb +17 -0
  20. data/lib/legion/data/migrations/003_add_groups.rb +16 -0
  21. data/lib/legion/data/migrations/004_add_chains.rb +25 -0
  22. data/lib/legion/data/migrations/005_add_envs.rb +24 -0
  23. data/lib/legion/data/migrations/006_add_dcs.rb +24 -0
  24. data/lib/legion/data/migrations/007_add_nodes.rb +26 -0
  25. data/lib/legion/data/migrations/008_add_settings.rb +18 -0
  26. data/lib/legion/data/migrations/009_add_extensions.rb +25 -0
  27. data/lib/legion/data/migrations/010_add_runners.rb +21 -0
  28. data/lib/legion/data/migrations/011_add_functions.rb +29 -0
  29. data/lib/legion/data/migrations/012_add_tasks.rb +28 -0
  30. data/lib/legion/data/migrations/013_add_task_logs.rb +23 -0
  31. data/lib/legion/data/migrations/014_add_relationships.rb +27 -0
  32. data/lib/legion/data/migrations/015_add_default_extensions.rb +24 -0
  33. data/lib/legion/data/migrations/016_change_task_args.rb +7 -0
  34. data/lib/legion/data/migrations/017_add_payload_task.rb +7 -0
  35. data/lib/legion/data/model.rb +36 -0
  36. data/lib/legion/data/models/chain.rb +11 -0
  37. data/lib/legion/data/models/datacenter.rb +11 -0
  38. data/lib/legion/data/models/environment.rb +11 -0
  39. data/lib/legion/data/models/extension.rb +11 -0
  40. data/lib/legion/data/models/function.rb +15 -0
  41. data/lib/legion/data/models/group.rb +10 -0
  42. data/lib/legion/data/models/node.rb +13 -0
  43. data/lib/legion/data/models/relationship.rb +16 -0
  44. data/lib/legion/data/models/runner.rb +15 -0
  45. data/lib/legion/data/models/setting.rb +10 -0
  46. data/lib/legion/data/models/task.rb +16 -0
  47. data/lib/legion/data/models/task_log.rb +12 -0
  48. data/lib/legion/data/models/user.rb +10 -0
  49. data/lib/legion/data/settings.rb +68 -0
  50. data/lib/legion/data/version.rb +5 -0
  51. data/sonar-project.properties +12 -0
  52. metadata +295 -0
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'legion/data'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ image: ruby:2.4.0
2
+
3
+ pipelines:
4
+ branches:
5
+ master:
6
+ - step:
7
+ caches:
8
+ - bundler
9
+ script:
10
+ - gem install bundle rubocop
11
+ - bundle install
12
+ - rubocop
13
+ - rake
14
+ services:
15
+ - mysql
16
+ definitions:
17
+ services:
18
+ mysql:
19
+ image: mysql:5.7
20
+ environment:
21
+ MYSQL_DATABASE: 'legion'
22
+ MYSQL_ROOT_PASSWORD: 'legion'
23
+ MYSQL_USER: 'legion'
24
+ MYSQL_PASSWORD: 'legion'
25
+ caches:
26
+ bundler: vendor/bundle
@@ -0,0 +1,50 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'legion/data/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = (RUBY_ENGINE == 'jruby' ? 'legion-data-java' : 'legion-data')
7
+ spec.version = Legion::Data::VERSION
8
+ spec.authors = ['Esity']
9
+ spec.email = ['matthewdiverson@gmail.com']
10
+
11
+ spec.summary = 'Used by Legion to connect to the database'
12
+ spec.description = 'The Legion connect gem'
13
+ spec.homepage = 'https://bitbucket.org/legion-io/legion-data'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
15
+
16
+ spec.metadata['bug_tracker_uri'] = 'https://bitbucket.org/legion-io/legion-data/issues?status=new&status=open'
17
+ spec.metadata['changelog_uri'] = 'https://bitbucket.org/legion-io/legion-data/src/CHANGELOG.md'
18
+ spec.metadata['documentation_uri'] = 'https://bitbucket.org/legion-io/legion-data'
19
+ spec.metadata['homepage_uri'] = 'https://bitbucket.org/legion-io/legion-data'
20
+ spec.metadata['source_code_uri'] = 'https://bitbucket.org/legion-io/legion-data'
21
+ spec.metadata['wiki_uri'] = 'https://bitbucket.org/legion-io/legion-data/wiki/Home'
22
+
23
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
24
+ f.match(%r{^(test|spec|features)/})
25
+ end
26
+ spec.bindir = 'bin'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_development_dependency 'bundler'
31
+ spec.add_development_dependency 'codecov'
32
+ spec.add_development_dependency 'rake'
33
+ spec.add_development_dependency 'rspec'
34
+ spec.add_development_dependency 'rspec_junit_formatter'
35
+ spec.add_development_dependency 'rubocop'
36
+ spec.add_development_dependency 'rubocop-md'
37
+ spec.add_development_dependency 'rubocop-performance'
38
+ spec.add_development_dependency 'rubocop-rspec'
39
+ spec.add_development_dependency 'rubocop-sequel'
40
+
41
+ spec.add_dependency 'legion-logging'
42
+ spec.add_dependency 'legion-settings'
43
+
44
+ if RUBY_ENGINE == 'jruby'
45
+ spec.add_dependency 'jdbc-mysql'
46
+ else
47
+ spec.add_dependency 'mysql2'
48
+ end
49
+ spec.add_dependency 'sequel'
50
+ end
@@ -0,0 +1,60 @@
1
+ require 'legion/data/version'
2
+ require 'legion/data/settings'
3
+ require 'sequel'
4
+
5
+ require 'legion/data/connection'
6
+ require 'legion/data/model'
7
+ require 'legion/data/migration'
8
+
9
+ module Legion
10
+ module Data
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
38
+ return if Legion::Settings[:data][:cache][:enabled]
39
+
40
+ return unless defined?(::Legion::Cache)
41
+
42
+ Legion::Data::Model::Relationship.plugin :caching, Legion::Cache, ttl: 10
43
+ Legion::Data::Model::Chain.plugin :caching, Legion::Cache, ttl: 60
44
+ Legion::Data::Model::Datacenter.plugin :caching, Legion::Cache, ttl: 120
45
+ Legion::Data::Model::Extension.plugin :caching, Legion::Cache, ttl: 120
46
+ Legion::Data::Model::Function.plugin :caching, Legion::Cache, ttl: 120
47
+ Legion::Data::Model::Extension.plugin :caching, Legion::Cache, ttl: 120
48
+ Legion::Data::Model::Node.plugin :caching, Legion::Cache, ttl: 10
49
+ Legion::Data::Model::TaskLog.plugin :caching, Legion::Cache, ttl: 12
50
+ Legion::Data::Model::Task.plugin :caching, Legion::Cache, ttl: 10
51
+ Legion::Data::Model::User.plugin :caching, Legion::Cache, ttl: 120
52
+ Legion::Data::Model::Group.plugin :caching, Legion::Cache, ttl: 120
53
+ end
54
+
55
+ def shutdown
56
+ Legion::Data::Connection.shutdown
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,61 @@
1
+ require 'sequel'
2
+
3
+ module Legion
4
+ module Data
5
+ module Connection
6
+ class << self
7
+ attr_accessor :sequel
8
+
9
+ def adapter
10
+ @adapter ||= RUBY_ENGINE == 'jruby' ? :jdbc : :mysql2
11
+ end
12
+
13
+ def setup
14
+ @sequel = if adapter == :mysql2
15
+ ::Sequel.connect(adapter: adapter, **creds_builder)
16
+ else
17
+ ::Sequel.connect("jdbc:mysql://#{creds_builder[:host]}:#{creds_builder[:port]}/#{creds_builder[:database]}?user=#{creds_builder[:username]}&password=#{creds_builder[:password]}&serverTimezone=UTC") # rubocop:disable Layout/LineLength
18
+ end
19
+ Legion::Settings[:data][:connected] = true
20
+ return if Legion::Settings[:data][:connection].nil? || Legion::Settings[:data][:connection][:log].nil?
21
+
22
+ @sequel.logger = Legion::Logging
23
+ @sequel.sql_log_level = Legion::Settings[:data][:connection][:sql_log_level]
24
+ @sequel.log_warn_duration = Legion::Settings[:data][:connection][:log_warn_duration]
25
+ end
26
+
27
+ def shutdown
28
+ @sequel&.disconnect
29
+ Legion::Settings[:data][:connected] = false
30
+ end
31
+
32
+ def creds_builder
33
+ final_creds = {}
34
+ final_creds.merge! default_creds
35
+ final_creds.merge! Legion::Settings[:data][:creds] if Legion::Settings[:data][:creds].is_a? Hash
36
+ return final_creds if Legion::Settings[:vault].nil?
37
+
38
+ if Legion::Settings[:vault][:connected] && ::Vault.sys.mounts.key?(:database)
39
+ temp_vault_creds = Legion::Crypt.read('database/creds/legion')
40
+ final_creds[:user] = temp_vault_creds[:username]
41
+ final_creds[:password] = temp_vault_creds[:password]
42
+ end
43
+
44
+ final_creds
45
+ end
46
+
47
+ def default_creds
48
+ {
49
+ host: '127.0.0.1',
50
+ port: 3306,
51
+ username: 'legion',
52
+ password: 'legion',
53
+ database: 'legion',
54
+ max_connections: 32,
55
+ preconnect: 'concurrently'
56
+ }
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,15 @@
1
+ require 'sequel/extensions/migration'
2
+
3
+ module Legion
4
+ module Data
5
+ module Migration
6
+ class << self
7
+ def migrate(connection = Legion::Data.connection, path = "#{__dir__}/migrations", **opts)
8
+ Legion::Settings[:data][:migrations][:version] = Sequel::Migrator.run(connection, path, **opts)
9
+ Legion::Logging.info("Legion::Data::Migration ran successfully to version #{Legion::Settings[:data][:migrations][:version]}") # rubocop:disable Layout/LineLength
10
+ Legion::Settings[:data][:migrations][:ran] = true
11
+ end
12
+ end
13
+ end
14
+ end
15
+ 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_info) do
12
+ drop_column :catalog
13
+ drop_column :updated_at
14
+ drop_column :created_at
15
+ end
16
+ end
17
+ 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_table :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_table :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_table :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_table :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_table :datacenters
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `nodes` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `name` varchar(128) NOT NULL DEFAULT '',
6
+ `datacenter_id` int(11) unsigned DEFAULT NULL,
7
+ `environment_id` int(11) unsigned DEFAULT NULL,
8
+ `status` varchar(255) NOT NULL DEFAULT 'unknown',
9
+ `active` tinyint(1) unsigned NOT NULL DEFAULT '1',
10
+ `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
11
+ `updated` 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
+ KEY `node_datacenter_id` (`datacenter_id`),
17
+ KEY `node_environment_id` (`environment_id`),
18
+ CONSTRAINT `node_datacenter_id` FOREIGN KEY (`datacenter_id`) REFERENCES `datacenters` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
19
+ CONSTRAINT `node_environment_id` FOREIGN KEY (`environment_id`) REFERENCES `environments` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
20
+ ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;"
21
+ end
22
+
23
+ down do
24
+ drop_table :nodes
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `settings` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `key` varchar(128) NOT NULL,
6
+ `value` varchar(256) NOT NULL,
7
+ `encrypted` tinyint(1) unsigned NOT NULL DEFAULT '0',
8
+ `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
9
+ `updated` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
10
+ PRIMARY KEY (`id`),
11
+ UNIQUE KEY `key` (`key`)
12
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
13
+ end
14
+
15
+ down do
16
+ drop_table :settings
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ Sequel.migration do
2
+ up do
3
+ run "CREATE TABLE `extensions` (
4
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
5
+ `active` tinyint(1) unsigned NOT NULL DEFAULT '1',
6
+ `name` varchar(128) NOT NULL,
7
+ `namespace` varchar(128) NOT NULL DEFAULT '',
8
+ `exchange` varchar(255) DEFAULT NULL,
9
+ `uri` varchar(256) DEFAULT NULL,
10
+ `user_owner` int(11) unsigned DEFAULT NULL,
11
+ `group_owner` int(11) unsigned DEFAULT NULL,
12
+ `updated` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
13
+ `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
14
+ PRIMARY KEY (`id`),
15
+ UNIQUE KEY `name_namespace` (`name`,`namespace`),
16
+ KEY `active` (`active`),
17
+ KEY `name` (`name`),
18
+ KEY `namespace` (`namespace`)
19
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
20
+ end
21
+
22
+ down do
23
+ drop_table :extensions
24
+ end
25
+ end