legionio 0.3.5 → 1.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 (95) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rspec.yml +45 -0
  3. data/.github/workflows/rubocop.yml +28 -0
  4. data/.github/workflows/sourcehawk-scan.yml +20 -0
  5. data/.gitignore +15 -0
  6. data/.rubocop.yml +90 -0
  7. data/CHANGELOG.md +4 -0
  8. data/CODE_OF_CONDUCT.md +75 -0
  9. data/CONTRIBUTING.md +55 -0
  10. data/Dockerfile +9 -0
  11. data/Gemfile +10 -0
  12. data/INDIVIDUAL_CONTRIBUTOR_LICENSE.md +30 -0
  13. data/LICENSE +201 -0
  14. data/NOTICE.txt +9 -0
  15. data/README.md +162 -0
  16. data/SECURITY.md +9 -0
  17. data/attribution.txt +1 -0
  18. data/docker_deploy.rb +10 -0
  19. data/exe/legion +4 -0
  20. data/exe/legionio +53 -0
  21. data/exe/lex_gen +5 -0
  22. data/legionio.gemspec +53 -0
  23. data/lib/legion.rb +24 -0
  24. data/lib/legion/cli.rb +56 -0
  25. data/lib/legion/cli/chain.rb +35 -0
  26. data/lib/legion/cli/cohort.rb +10 -0
  27. data/lib/legion/cli/function.rb +41 -0
  28. data/lib/legion/cli/lex/actor.rb +31 -0
  29. data/lib/legion/cli/lex/exchange.rb +32 -0
  30. data/lib/legion/cli/lex/message.rb +32 -0
  31. data/lib/legion/cli/lex/queue.rb +45 -0
  32. data/lib/legion/cli/lex/runner.rb +70 -0
  33. data/lib/legion/cli/lex/templates/actor.erb +6 -0
  34. data/lib/legion/cli/lex/templates/actor_spec.erb +0 -0
  35. data/lib/legion/cli/lex/templates/base/bitbucket.yml.erb +69 -0
  36. data/lib/legion/cli/lex/templates/base/gemfile.erb +3 -0
  37. data/lib/legion/cli/lex/templates/base/gemspec.erb +26 -0
  38. data/lib/legion/cli/lex/templates/base/gitignore.erb +11 -0
  39. data/lib/legion/cli/lex/templates/base/lex.erb +9 -0
  40. data/lib/legion/cli/lex/templates/base/lex_spec.erb +5 -0
  41. data/lib/legion/cli/lex/templates/base/lic.erb +21 -0
  42. data/lib/legion/cli/lex/templates/base/rakefile.erb +6 -0
  43. data/lib/legion/cli/lex/templates/base/readme.md.erb +2 -0
  44. data/lib/legion/cli/lex/templates/base/rubocop.yml.erb +15 -0
  45. data/lib/legion/cli/lex/templates/base/spec_helper.rb.erb +11 -0
  46. data/lib/legion/cli/lex/templates/base/version.erb +7 -0
  47. data/lib/legion/cli/lex/templates/exchange.erb +11 -0
  48. data/lib/legion/cli/lex/templates/exchange_spec.erb +0 -0
  49. data/lib/legion/cli/lex/templates/message.erb +23 -0
  50. data/lib/legion/cli/lex/templates/message_spec.erb +0 -0
  51. data/lib/legion/cli/lex/templates/queue.erb +12 -0
  52. data/lib/legion/cli/lex/templates/queue_helper.erb +24 -0
  53. data/lib/legion/cli/lex/templates/queue_spec.erb +11 -0
  54. data/lib/legion/cli/lex/templates/runner.erb +11 -0
  55. data/lib/legion/cli/lex/templates/runner_spec.erb +11 -0
  56. data/lib/legion/cli/relationship.rb +22 -0
  57. data/lib/legion/cli/task.rb +49 -0
  58. data/lib/legion/cli/trigger.rb +88 -0
  59. data/lib/legion/cli/version.rb +5 -0
  60. data/lib/legion/extensions.rb +229 -0
  61. data/lib/legion/extensions/actors/base.rb +47 -0
  62. data/lib/legion/extensions/actors/defaults.rb +28 -0
  63. data/lib/legion/extensions/actors/every.rb +48 -0
  64. data/lib/legion/extensions/actors/loop.rb +32 -0
  65. data/lib/legion/extensions/actors/nothing.rb +15 -0
  66. data/lib/legion/extensions/actors/once.rb +40 -0
  67. data/lib/legion/extensions/actors/poll.rb +87 -0
  68. data/lib/legion/extensions/actors/subscription.rb +139 -0
  69. data/lib/legion/extensions/builders/actors.rb +61 -0
  70. data/lib/legion/extensions/builders/base.rb +36 -0
  71. data/lib/legion/extensions/builders/helpers.rb +24 -0
  72. data/lib/legion/extensions/builders/runners.rb +62 -0
  73. data/lib/legion/extensions/core.rb +131 -0
  74. data/lib/legion/extensions/data.rb +61 -0
  75. data/lib/legion/extensions/data/migrator.rb +33 -0
  76. data/lib/legion/extensions/data/model.rb +8 -0
  77. data/lib/legion/extensions/helpers/base.rb +82 -0
  78. data/lib/legion/extensions/helpers/cache.rb +23 -0
  79. data/lib/legion/extensions/helpers/core.rb +41 -0
  80. data/lib/legion/extensions/helpers/data.rb +23 -0
  81. data/lib/legion/extensions/helpers/lex.rb +48 -0
  82. data/lib/legion/extensions/helpers/logger.rb +44 -0
  83. data/lib/legion/extensions/helpers/task.rb +60 -0
  84. data/lib/legion/extensions/helpers/transport.rb +44 -0
  85. data/lib/legion/extensions/transport.rb +159 -0
  86. data/lib/legion/lex.rb +89 -0
  87. data/lib/legion/process.rb +124 -0
  88. data/lib/legion/runner.rb +57 -0
  89. data/lib/legion/runner/log.rb +10 -0
  90. data/lib/legion/runner/status.rb +69 -0
  91. data/lib/legion/service.rb +130 -0
  92. data/lib/legion/supervision.rb +15 -0
  93. data/lib/legion/version.rb +3 -0
  94. data/sourcehawk.yml +4 -0
  95. metadata +142 -168
data/NOTICE.txt ADDED
@@ -0,0 +1,9 @@
1
+ LegionIO
2
+ Copyright 2021 Optum
3
+
4
+ Project Description:
5
+ ====================
6
+ LegionIO is an automation framework for create IFTTT style relationships, scheduling and managing sync and async tasks/jobs
7
+
8
+ Author(s):
9
+ Esity
data/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # LegionIO
2
+
3
+ LegionIO is a framework for automating and connecting things. You can see all the docs inside confluence
4
+ https://legionio.atlassian.net/wiki/spaces/LEGION/overview
5
+ https://legionio.atlassian.net/wiki/spaces/LEX/pages/7864551/Extensions
6
+ *Soon to be migrated to GitHub Wiki*
7
+
8
+ ### What does it do?
9
+ LegionIO is an async job engine designed for scheduling tasks and creating relationships between things that wouldn't
10
+ otherwise be connected. Relationships do not have to be a single path. Both of these would work
11
+ * `foo → bar → cat → dog`
12
+ ```
13
+ a → b → c
14
+ b → e → z
15
+ e → g
16
+ ```
17
+ In the second scenario, when a runs, it causes b to run which then causes both c and e to run in parallel
18
+
19
+ It supports both conditions and transformation. The idea of a transformation is you can't connect two indepedent services
20
+ and expect them to know how to talk to each other.
21
+
22
+ ### Running
23
+ Run `gem install legionio` to install legion. If you want to use database features, you will need to
24
+ run `gem install legion-data` also.
25
+
26
+ After installing gem you can use the commands `legionio` to start legion, `legion` to access things
27
+ and `lex_gen` to generate a new legion extension
28
+
29
+ ### Example Legion Extensions(LEX)
30
+ * [lex-http](https://github.com/LegionIO/lex-http/src/master/) - Gives legion the ability to make http requests, [docs](https://legionio.atlassian.net/wiki/spaces/LEX/pages/12910593/Lex+Http)
31
+ * [lex-influxdb](https://github.com/LegionIO/lex-influxdb/src/master/) - Write, read, and manage influxdb nodes, [docs](https://legionio.atlassian.net/wiki/spaces/LEX/pages/614891774/Lex+Influxdb)
32
+ * [lex-log](https://github.com/LegionIO/lex-log/src/master/) - Send log items to either stdout or a file with lex-log, [docs](https://legionio.atlassian.net/wiki/spaces/LEX/pages/614858995/Lex+Log)
33
+ * [lex-memcache](https://github.com/LegionIO/lex-memcached/src/master/) - run memcached commands like set, add, append, delete, flush, reset_stats against memcached servers, [docs](https://legionio.atlassian.net/wiki/spaces/LEX/pages/614858753/Lex+Memcached)
34
+ * [lex-pihole](https://github.com/LegionIO/lex-pihole/src/master/) - Allows Legion to interact with [Pi-Hole](https://pi-hole.net/). Can do things like get status, add/remove domains from the list, etc
35
+ * [lex-ping](https://github.com/LegionIO/lex-ping/src/master/) - You can ping things?, [docs](https://legionio.atlassian.net/wiki/spaces/LEX/pages/631373895/Lex+Ping)
36
+ * [lex-pushover](https://github.com/LegionIO/lex-pushover/src/master/) - Connects Legion to [Pushover](https://pushover.net/), [docs]()
37
+ * [lex-redis](https://github.com/LegionIO/lex-redis/src/master/) - similiar to lex-memcached but for redis
38
+ * [lex-sleepiq](https://github.com/LegionIO/lex-sleepiq/src/master/) - Control your SleepIQ bed with Legion!
39
+ * [lex-ssh](https://github.com/LegionIO/lex-ssh/src/master/) - Send commands to a server via SSH in an async fashion, [docs](https://legionio.atlassian.net/wiki/spaces/LEX/pages/614891551/Lex+SSH)
40
+
41
+ Bitbucket repos for extensions that are active or being worked on
42
+ [lex list](https://github.com/LegionIO/workspace/projects/LEX)
43
+ A nice list in the wiki to view all the extensions, their docs and status
44
+ [Legion Extensions](https://github.com/topics/legionio?l=ruby)
45
+
46
+ ### Scheduling Tasks
47
+ 1) Ensure you have the Legion::Data gem installed and configured
48
+ 2) Make sure to have `lex-scheduler` extension installed so that it generates the schedules table in the database
49
+ 3) From there you can add a function to be run at a given cron syntax or interval
50
+ 4) Setting the interval column will make the job run X seconds after the last time it is completed and will ignore the cron colum
51
+ 5) Setting the cron column will ensure the job runs at the given times regardless of when it was run last, only works if interval is null
52
+ 6) Cron supports both `*/5 * * * *` style and verbose like `every minute` and `every day at noon`
53
+
54
+ ### Creating Relationships
55
+ *To be populated*
56
+
57
+ ### Conditions
58
+ You can create complex conditional statements to ensure that when a triggers b, b only runs if certain conditions
59
+ are met. Example conditional statement
60
+ ```json
61
+ {
62
+ "all": [{
63
+ "fact": "pet.type",
64
+ "value": "dog",
65
+ "operator": "equal"
66
+ },{
67
+ "fact":"pet.hungry",
68
+ "operator":"is_true"
69
+ }]
70
+ }
71
+
72
+ ```
73
+ You can nest conditions in an unlimited fashion to create and/or scenarios to meet your needs
74
+ ```json
75
+ {
76
+ "all": [
77
+ "any":[
78
+ {"fact":"pet.type", "value":"dog","operator":"equal"},
79
+ {"fact":"pet.type", "value":"cat","operator":"equal"}
80
+ ],
81
+ {
82
+ "fact": "pet.hungry",
83
+ "operator": "is_true"
84
+ },{
85
+ "fact":"pet.overweight",
86
+ "operator":"is_false"
87
+ }]
88
+ }
89
+ ```
90
+ *Conditions are supported by the `lex-conditioner` extension and are not required to be run inside the legion framework*
91
+ You can read the docs with more examples in the [wiki](https://legionio.atlassian.net/wiki/spaces/LEX/pages/614957181/Lex+Conditioner)
92
+
93
+
94
+ ### Transformations
95
+ Transformations are a critical piece of interconnecting two independent items. Without it, service B doesn't know what
96
+ to do with the result from service A
97
+ `lex-conditioner` uses a combination of the [tilt](https://rubygems.org/gems/tilt) gem and erb style syntax.
98
+ ##### Examples
99
+ Creating a new pagerduty incident
100
+ ```json
101
+ {"message":"New PagerDuty incident assigned to <%= assignee %> with a priority of <%= severity %>","from":"PagerDuty"}
102
+ ```
103
+ Example transformation to make the `lex-log` extension output a message
104
+ ```json
105
+ {"message":"transform2","level":"fatal"}
106
+ ```
107
+ You can also call Legion services to get the data you need, example sending a pushover message
108
+ ```json
109
+ {"message":"This is my pushover body", "title": "this is my title", "token":"<%= Legion::Settings['lex']['pushover']['token'] %>" }
110
+ ```
111
+ Or if you wanted to make a real time call via `Legion::Crypt` to get a [Hashicorp Vault](https://www.vaultproject.io/) value
112
+ ```json
113
+ {"message":"this is another body", "title":"vault token example", "token":"<%= Legion::Crypt.read('pushover/token') %> "}
114
+ ```
115
+ *Transformations are supported by the `lex-transformation` extension and are not "technically" required to be run inside the legion framework*
116
+ You can read the docs with more examples in the [wiki](https://legionio.atlassian.net/wiki/spaces/LEX/pages/612270222/Lex+Transformer)
117
+
118
+ ## FAQ
119
+ ### Does it scale?
120
+ Yes. Actually quite well. The framework uses RabbitMQ to ensure jobs are scheduled and run in a FIFO order. As you add
121
+ more works, it just subscribes to the queues the workers can support and does more work. It is really geared towards a
122
+ docker/K8 type of environment however it can be run locally, on a VM, etc.
123
+
124
+ As of right now, it has been tested to around 100 workers running in docker without any performance issues. You will
125
+ likely see performance issues on the DB or RabbitMQ side before Legion has issues.
126
+
127
+ Another benefit is that you can run multiple LEXs in one worker or you could have dedicated workers that only run a single LEX.
128
+ In example if you have to make a ton of ssh connections via `lex-ssh`, maybe you want to run 10 pods with no other extensions in them
129
+ but then run a pod with `lex-pagerduty`, `lex-log` and `lex-http` to send out notifications after each ssh task is completed
130
+
131
+ ### High Availability
132
+ Because you can run this thing with multiple processes and it will distribute the work, it is naturally HA oriented.
133
+ if a worker goes down for some reason, another one should pick it up(assuming another work has that LEX enabled). There
134
+ are no hidden features, pay walls, etc to get HA. Just run more instances of LegionIO
135
+
136
+ ### Price and License
137
+ LegionIO is completely free. It was build using free time. There are no features held back, no private repos.
138
+ Everything is under an MIT license to keep it as open as possible. With that, the devs can't always help with support,
139
+ well because it's free.
140
+
141
+ ### Who is it geared for?
142
+ Anyone? Everyone? It could be used in a homelab to automate updating VMs. It could be used by someone to take ESPHome
143
+ sensor data and pipe it to influxdb. At least that is what @Esity does. It could also be used by a company or enterprise looking
144
+ to replace other tools.
145
+
146
+ ### But it is written in ruby
147
+ Yep.
148
+
149
+ ### Similiar projects
150
+ There are multiple projects that are similiar. Some things like IFTTT are great(but is it?) but then again, cost money.
151
+ * [Node-Red](https://nodered.org/) - No HA but has some good features and a great drag and drop interface
152
+ * [n8n.io](https://n8n.io/) - Working on HA but [not there yet](https://github.com/n8n-io/n8n/pull/1294)
153
+ * [StackStorm](https://stackstorm.com/) - Written in Python, has potential but I feel they are removing features to convince you to pay for it
154
+ * [Jenkins](https://www.jenkins.io/) - It's jenkins. I don't need to say anything else
155
+ * [Huginn]() - Another IFTTT style app written in ruby. Not sure on this one but it doesn't have HA from what I can tell [github issue](https://github.com/huginn/huginn/issues/2198)
156
+
157
+ ### Other fun facts
158
+ * Supports Hashicorp vault for storing secrets/settings/etc
159
+ * Can enable global message encryption so that all messages going through RMQ are encrypted with aes-256-cbc
160
+ * Each worker generates a private/public key that can be used for internode communication, it also will generate a cluster secret
161
+ for all nodes to have so they can share data accross the entire cluster. The cluster secret by default is stored only in memory and
162
+ and is generated when the first worker starts
data/SECURITY.md ADDED
@@ -0,0 +1,9 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+ | Version | Supported |
5
+ | ------- | ------------------ |
6
+ | 1.x.x | :white_check_mark: |
7
+
8
+ ## Reporting a Vulnerability
9
+ To be added
data/attribution.txt ADDED
@@ -0,0 +1 @@
1
+ Add attributions here.
data/docker_deploy.rb ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require './lib/legion/version'
4
+ puts "Building docker image for Legion v#{Legion::VERSION}"
5
+ system("docker build --tag legionio/legion:v#{Legion::VERSION} .")
6
+ puts 'Pushing to hub.docker.com'
7
+ system("docker push legionio/legion:v#{Legion::VERSION}")
8
+ system("docker tag legionio/legion:v#{Legion::VERSION} legionio/legion:lastest")
9
+ system("docker push legionio/legion:v#{Legion::VERSION}")
10
+ puts 'completed'
data/exe/legion ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'thor'
3
+ require 'legion/cli'
4
+ Legion::CLI.start
data/exe/legionio ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'optparse'
7
+ options = { action: :run }
8
+
9
+ daemonize_help = 'run daemonized in the background (default: false)'
10
+ pidfile_help = 'the pid filename'
11
+ logfile_help = 'the log filename'
12
+ include_help = 'an additional $LOAD_PATH (may be used more than once)'
13
+ debug_help = 'set $DEBUG to true'
14
+ warn_help = 'enable warnings'
15
+ time_help = 'only run legion for X seconds'
16
+
17
+ op = OptionParser.new
18
+ op.banner = 'An example of how to daemonize a long running Ruby process.'
19
+ op.separator ''
20
+ op.separator 'Usage: server [options]'
21
+ op.separator ''
22
+
23
+ op.separator ''
24
+ op.separator 'Process options:'
25
+ op.on('-d', '--daemonize', daemonize_help) { options[:daemonize] = true }
26
+ op.on('-p', '--pid PIDFILE', pidfile_help) { |value| options[:pidfile] = value }
27
+ op.on('-l', '--log LOGFILE', logfile_help) { |value| options[:logfile] = value }
28
+ op.on('-t', '--time 10', time_help) { |value| options[:time_limit] = value }
29
+
30
+ op.separator ''
31
+ op.separator 'Ruby options:'
32
+ op.on('-I', '--include PATH', include_help) do |value|
33
+ $LOAD_PATH.unshift(*value.split(':').map do |v|
34
+ File.expand_path(v)
35
+ end)
36
+ end
37
+ op.on('--debug', debug_help) { $DEBUG = true }
38
+ op.on('--warn', warn_help) { $-w = true }
39
+
40
+ op.separator ''
41
+ op.separator 'Common options:'
42
+ op.on('-h', '--help') { options[:action] = :help }
43
+ op.on('-v', '--version') { options[:action] = :version }
44
+
45
+ op.separator ''
46
+ op.parse!(ARGV)
47
+
48
+ unless options[:action] == :help
49
+ require 'legion'
50
+ Legion.start
51
+ require 'legion/process'
52
+ Legion::Process.new(options).run!
53
+ end
data/exe/lex_gen ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'thor'
3
+ require 'legion/lex'
4
+
5
+ Legion::Cli::LexBuilder.start
data/legionio.gemspec ADDED
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'legion/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'legionio'
9
+ spec.version = Legion::VERSION
10
+ spec.authors = ['Esity']
11
+ spec.email = ['matthewdiverson@gmail.com']
12
+
13
+ spec.summary = 'The primary gem to run the LegionIO Framework'
14
+ spec.description = 'LegionIO is an extensible framework for running, scheduling and building relationships of tasks in a concurrent matter'
15
+ spec.homepage = 'https://github.com/Optum/LegionIO'
16
+ spec.license = 'Apache-2.0'
17
+ spec.require_paths = ['lib']
18
+ spec.required_ruby_version = '>= 2.5.0'
19
+
20
+ spec.metadata = {
21
+ 'bug_tracker_uri' => 'https://github.com/Optum/LegionIO/issues',
22
+ 'changelog_uri' => 'https://github.com/Optum/LegionIO/src/main/CHANGELOG.md',
23
+ 'documentation_uri' => 'https://github.com/Optum/LegionIO',
24
+ 'homepage_uri' => 'https://github.com/Optum/LegionIO',
25
+ 'source_code_uri' => 'https://github.com/Optum/LegionIO',
26
+ 'wiki_uri' => 'https://github.com/Optum/LegionIO'
27
+ }
28
+
29
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
30
+ f.match(%r{^(test|spec|features)/})
31
+ end
32
+ spec.test_files = spec.files.select { |p| p =~ %r{^test/.*_test.rb} }
33
+
34
+ spec.bindir = 'exe'
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+
37
+ spec.add_dependency 'concurrent-ruby', '>= 1.1.7'
38
+ spec.add_dependency 'concurrent-ruby-ext', '>= 1.1.7'
39
+ spec.add_dependency 'daemons', '>= 1.3.1'
40
+ spec.add_dependency 'oj', '>= 3.10'
41
+ spec.add_dependency 'thor', '>= 1'
42
+
43
+ spec.add_dependency 'legion-cache', '>= 0.2.0'
44
+ spec.add_dependency 'legion-crypt', '>= 0.2.0'
45
+ spec.add_dependency 'legion-json', '>= 0.2.0'
46
+ spec.add_dependency 'legion-logging', '>= 0.2.0'
47
+ spec.add_dependency 'legion-settings', '>= 0.2.0'
48
+ spec.add_dependency 'legion-transport', '>= 1.1.9'
49
+
50
+ spec.add_dependency 'lex-node'
51
+
52
+ spec.add_development_dependency 'legion-data'
53
+ end
data/lib/legion.rb ADDED
@@ -0,0 +1,24 @@
1
+ Process.setproctitle('Legion')
2
+ require 'concurrent'
3
+ require 'securerandom'
4
+ require 'legion/version'
5
+ require 'legion/process'
6
+ require 'legion/service'
7
+ require 'legion/extensions'
8
+
9
+ module Legion
10
+ attr_reader :service
11
+
12
+ def self.start
13
+ @service = Legion::Service.new
14
+ Legion::Logging.info("Started Legion v#{Legion::VERSION}")
15
+ end
16
+
17
+ def self.shutdown
18
+ @service.shutdown
19
+ end
20
+
21
+ def self.reload
22
+ @service.reload
23
+ end
24
+ end
data/lib/legion/cli.rb ADDED
@@ -0,0 +1,56 @@
1
+ # require 'legion/cli/version'
2
+ require 'thor'
3
+ require 'legion'
4
+ require 'legion/service'
5
+
6
+ require 'legion/lex'
7
+ require 'legion/cli/cohort'
8
+
9
+ require 'legion/cli/relationship'
10
+ require 'legion/cli/task'
11
+ require 'legion/cli/chain'
12
+ require 'legion/cli/trigger'
13
+ require 'legion/cli/function'
14
+
15
+ module Legion
16
+ class CLI < Thor
17
+ include Thor::Actions
18
+ check_unknown_options!
19
+
20
+ def self.exit_on_failure?
21
+ true
22
+ end
23
+
24
+ def self.source_root
25
+ File.dirname(__FILE__)
26
+ end
27
+
28
+ desc 'version', 'Display MyGem version'
29
+ map %w[-v --version] => :version
30
+
31
+ def version
32
+ say "Legion::CLI #{VERSION}"
33
+ end
34
+
35
+ desc 'lex', 'used to build LEXs'
36
+ subcommand 'lex', Legion::Cli::LexBuilder
37
+
38
+ desc 'cohort', ''
39
+ subcommand 'cohort', Legion::Cli::Cohort
40
+
41
+ desc 'function', 'deal with functions'
42
+ subcommand 'function', Legion::Cli::Function
43
+
44
+ desc 'relationship', 'creates and manages relationships'
45
+ subcommand 'relationship', Legion::Cli::Relationship
46
+
47
+ desc 'task', 'creates and manages tasks'
48
+ subcommand 'task', Legion::Cli::Task
49
+
50
+ desc 'chain', 'creates and manages chains'
51
+ subcommand 'chain', Legion::Cli::Chain
52
+
53
+ desc 'trigger', 'sends a task to a worker'
54
+ subcommand 'trigger', Legion::Cli::Trigger
55
+ end
56
+ end
@@ -0,0 +1,35 @@
1
+ module Legion
2
+ class Cli
3
+ class Chain < Thor
4
+ desc 'create', 'create'
5
+ def create(name)
6
+ Legion::Service.new(cache: false, crypt: false, extensions: false, log_level: 'error')
7
+ say "chain created, id: #{Legion::Data::Model::Chain.insert({ name: name })}", :green
8
+ end
9
+
10
+ desc 'show', 'show'
11
+ option :limit, type: :numeric, required: true, default: 10, desc: 'how many tasks to return'
12
+ def show
13
+ Legion::Service.new(cache: false, crypt: false, extensions: false, log_level: 'error')
14
+ rows = [%w[id name active]]
15
+ Legion::Data::Model::Chain.limit(options[:limit]).order(:id).reverse_each do |row|
16
+ rows.push([row.values[:id], row.values[:name], row.values[:active]])
17
+ end
18
+
19
+ print_table rows
20
+ end
21
+
22
+ desc 'delete', 'delete'
23
+ option :confirm, type: :boolean
24
+ def delete(id)
25
+ Legion::Service.new(cache: false, crypt: false, extensions: false, log_level: 'error')
26
+ return if !options[:confirm] && !(yes? "Are you sure you want to delete chain #{id} and all dependent relationships", :red)
27
+
28
+ Legion::Data::Model::Chain[id].delete
29
+ say 'Deleted!'
30
+ end
31
+
32
+ default_task :show
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ module Legion
2
+ class Cli
3
+ class Cohort < Thor
4
+ desc 'import :id', 'imports a cohort for usage'
5
+ def import(id)
6
+ say "Importing Cohort #{id}", :green
7
+ end
8
+ end
9
+ end
10
+ end