kybus-cli 0.1.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 (35) hide show
  1. checksums.yaml +7 -0
  2. data/bin/kybus +6 -0
  3. data/lib/kybus/aws/code_packager.rb +47 -0
  4. data/lib/kybus/aws/lambda.rb +166 -0
  5. data/lib/kybus/aws/log_group.rb +34 -0
  6. data/lib/kybus/aws/policy.rb +34 -0
  7. data/lib/kybus/aws/resource.rb +33 -0
  8. data/lib/kybus/aws/role.rb +76 -0
  9. data/lib/kybus/aws.rb +8 -0
  10. data/lib/kybus/cli/bot/controller_generator.rb +38 -0
  11. data/lib/kybus/cli/bot/deploy_init_generator.rb +33 -0
  12. data/lib/kybus/cli/bot/deployer.rb +70 -0
  13. data/lib/kybus/cli/bot/deployers/aws_bot_deployer.rb +95 -0
  14. data/lib/kybus/cli/bot/deployers/deployer_base.rb +23 -0
  15. data/lib/kybus/cli/bot/deployers/telegram_configurator.rb +33 -0
  16. data/lib/kybus/cli/bot/file_provider.rb +50 -0
  17. data/lib/kybus/cli/bot/file_providers/autoconfig_generator.rb +28 -0
  18. data/lib/kybus/cli/bot/file_providers/autoconfig_loader_generator.rb +37 -0
  19. data/lib/kybus/cli/bot/file_providers/bot_builder_generator.rb +25 -0
  20. data/lib/kybus/cli/bot/file_providers/bot_generator.rb +29 -0
  21. data/lib/kybus/cli/bot/file_providers/composefile_generator.rb +54 -0
  22. data/lib/kybus/cli/bot/file_providers/config_default_generator.rb +50 -0
  23. data/lib/kybus/cli/bot/file_providers/config_generator.rb +45 -0
  24. data/lib/kybus/cli/bot/file_providers/db_generator.rb +53 -0
  25. data/lib/kybus/cli/bot/file_providers/deployment_file_provider.rb +39 -0
  26. data/lib/kybus/cli/bot/file_providers/dockerfile_generator.rb +33 -0
  27. data/lib/kybus/cli/bot/file_providers/gemfile_generator.rb +41 -0
  28. data/lib/kybus/cli/bot/file_providers/lambda_handler_generator.rb +38 -0
  29. data/lib/kybus/cli/bot/file_providers/rakefile_generator.rb +40 -0
  30. data/lib/kybus/cli/bot/file_providers/test_helper_generator.rb +32 -0
  31. data/lib/kybus/cli/bot/project_generator.rb +76 -0
  32. data/lib/kybus/cli/bot.rb +60 -0
  33. data/lib/kybus/cli/file_writer.rb +17 -0
  34. data/lib/kybus/cli.rb +15 -0
  35. metadata +193 -0
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'deployer_base'
4
+
5
+ module Kybus
6
+ class CLI < Thor
7
+ class BotDeployerTelegramConfigurator < BotDeployerBase
8
+ attr_reader :url
9
+ attr_writer :url
10
+
11
+ def initialize(url, config)
12
+ @url = url
13
+ super(config)
14
+ end
15
+
16
+ def create_or_update!
17
+ raise 'Missing Token' if @config['secret_token'].nil?
18
+
19
+ uri = URI("https://api.telegram.org/bot#{@config['bot_token']}/setWebhook")
20
+ params = { url: @url, secret_token: @config['secret_token'] }
21
+ res = Net::HTTP.post_form(uri, params)
22
+ puts res.body
23
+ end
24
+
25
+ def destroy!
26
+ uri = URI("https://api.telegram.org/bot#{@config['bot_token']}/setWebhook")
27
+ params = { url: '', secret_token: '' }
28
+ res = Net::HTTP.post_form(uri, params)
29
+ puts res.body
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ class FileProvider
7
+ def initialize(name, config)
8
+ @file_writer = Kybus::CLI::FileWriter.new(name)
9
+ @config = config
10
+ @name = name
11
+ end
12
+
13
+ def skip_file?
14
+ false
15
+ end
16
+
17
+ def keep_files
18
+ []
19
+ end
20
+
21
+ def generate
22
+ @file_writer.write(saving_path, make_contents) unless skip_file?
23
+ keep_files.each do |file|
24
+ @file_writer.write(file, '')
25
+ end
26
+ end
27
+
28
+ def self.autoregister!
29
+ Kybus::CLI::Bot::ProjectGenerator.register_file_provider(self)
30
+ end
31
+
32
+ def bot_name
33
+ @name
34
+ end
35
+
36
+ def bot_name_class
37
+ @name.split('_').map(&:capitalize).join
38
+ end
39
+
40
+ def bot_name_constantize
41
+ bot_name_snake_case.upcase
42
+ end
43
+
44
+ def bot_name_snake_case
45
+ @name.gsub(' ', '_').downcase
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ module Config
7
+ class AutoconfigGenerator < FileProvider
8
+ autoregister!
9
+
10
+ def saving_path
11
+ 'config/autoconfig.yaml'
12
+ end
13
+
14
+ def make_contents
15
+ <<~YAML
16
+ autoconfig:
17
+ env_prefix: #{bot_name_constantize}
18
+ default_files:
19
+ - ./config/config.default.yaml
20
+ files:
21
+ - ./config/config.yaml
22
+ YAML
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ module Config
7
+ class AutoconfigLoaderGenerator < FileProvider
8
+ autoregister!
9
+
10
+ def saving_path
11
+ 'config_loaders/autoconfig.rb'
12
+ end
13
+
14
+ private
15
+
16
+ def make_contents
17
+ <<~RUBY
18
+ require 'kybus/bot'
19
+ require 'kybus/configs'
20
+
21
+
22
+ Dir[File.join(__dir__, './models', '*.rb')].each { |file| require file }
23
+
24
+ require_relative '../bot'
25
+
26
+ CONF_MANAGER = Kybus::Configuration.auto_load!
27
+ APP_CONF = CONF_MANAGER.configs
28
+ require_relative 'db'
29
+
30
+ require_relative "bot_builder"
31
+ RUBY
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ module Config
7
+ class BotBuilderGenerator < FileProvider
8
+ autoregister!
9
+
10
+ def saving_path
11
+ 'config_loaders/bot_builder.rb'
12
+ end
13
+
14
+ def make_contents
15
+ <<~RUBY
16
+ # frozen_string_literal: true
17
+
18
+ BOT = #{bot_name_class}.new(APP_CONF['bots']['main'])
19
+ RUBY
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ class BotGenerator < FileProvider
7
+ autoregister!
8
+ def saving_path
9
+ 'bot.rb'
10
+ end
11
+
12
+ def make_contents
13
+ <<~RUBY
14
+ # frozen_string_literal: true
15
+
16
+ class #{bot_name_class} < Kybus::Bot::Base
17
+ def initialize(configs)
18
+ super(configs)
19
+ register_command('/hello') do
20
+ send_message('Hi human')
21
+ end
22
+ end
23
+ end
24
+ RUBY
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ class ComposefileGenerator < FileProvider
7
+ autoregister!
8
+
9
+ def skip_file?
10
+ !@config[:with_docker_compose]
11
+ end
12
+
13
+ def saving_path
14
+ 'docker-compose.yml'
15
+ end
16
+
17
+ def make_contents
18
+ content = <<~DOCKERCOMPOSE
19
+ version: '3'
20
+ services:
21
+ app:
22
+ build: .
23
+ volumes:
24
+ - .:/app
25
+ DOCKERCOMPOSE
26
+
27
+ if @config[:db_adapter] == 'dynamoid'
28
+ content << <<-LOCALSTACK
29
+ localstack:
30
+ image: localstack/localstack
31
+ ports:
32
+ - "4566:4566"
33
+ environment:
34
+ - SERVICES=dynamodb
35
+ LOCALSTACK
36
+ elsif @config[:db_adapter] == 'sequel'
37
+ content << <<-DATABASE
38
+ db:
39
+ image: postgres
40
+ ports:
41
+ - "5432:5432"
42
+ environment:
43
+ POSTGRES_DB: app_development
44
+ POSTGRES_USER: user
45
+ POSTGRES_PASSWORD: password
46
+ DATABASE
47
+ end
48
+
49
+ content
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ module Config
7
+ class ConfigDefaultGenerator < FileProvider
8
+ autoregister!
9
+ def saving_path
10
+ 'config/config.default.yaml'
11
+ end
12
+
13
+ def keep_files
14
+ ['models/migrations/.keep']
15
+ end
16
+
17
+ def make_contents
18
+ content = <<~YAML
19
+ logger:
20
+ stdout: yes
21
+ severity: debug
22
+ bots:
23
+ main:
24
+ pool_size: 1
25
+ inline_args: true
26
+ provider:#{' '}
27
+ name: REPLACE_ME
28
+ token: REPLACE_ME
29
+ debug: true
30
+ state_repository:
31
+ YAML
32
+
33
+ if @config[:db_adapter] == 'sequel'
34
+ content << <<~SEQUEL
35
+ name: sequel
36
+ endpoint: 'sqlite://#{bot_name_snake_case}_botmeta.db'
37
+ database: 'sqlite://#{bot_name_snake_case}.db'
38
+ SEQUEL
39
+ elsif @config[:db_adapter] == 'dynamoid'
40
+ content << <<-DYNAMOID
41
+ name: json
42
+ storage: ./storage
43
+ DYNAMOID
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ module Config
7
+ class ConfigGenerator < FileProvider
8
+ autoregister!
9
+ def saving_path
10
+ 'config/config.yaml'
11
+ end
12
+
13
+ def make_contents
14
+ content = <<~YAML
15
+ bots:
16
+ main:
17
+ provider:
18
+ name: #{@config[:bot_provider]}
19
+ token: #{@config[:bot_token]}
20
+ mode: #{@config[:with_deployment_file] && @config[:cloud_provider] == 'aws' ? 'webhook_lambda' : 'polling'}
21
+ debug: true
22
+ state_repository:
23
+ YAML
24
+
25
+ if @config[:db_adapter] == 'sequel'
26
+ content << <<-SEQUEL
27
+ name: sequel
28
+ endpoint: 'sqlite://#{bot_name_snake_case}_botmeta.db'
29
+ SEQUEL
30
+ elsif @config[:db_adapter] == 'dynamoid'
31
+ content << <<-DYNAMOID
32
+ name: dynamoid
33
+ dynamoid_config: true
34
+ region: 'us-east-1'
35
+ namespace: '#{bot_name_snake_case}'
36
+ read_capacity: 3
37
+ write_capacity: 3
38
+ DYNAMOID
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ module Config
7
+ class DBGenerator < FileProvider
8
+ autoregister!
9
+
10
+ def saving_path
11
+ 'config_loaders/db.rb'
12
+ end
13
+
14
+ def make_contents
15
+ case @config[:db_adapter]
16
+ when 'sequel'
17
+ <<~RUBY
18
+ # frozen_string_literal: true
19
+
20
+ require 'sequel'
21
+
22
+ DB = Sequel.connect(APP_CONF['database'])
23
+
24
+ def run_migrations!
25
+ require 'kybus/bot/migrator'
26
+ require 'sequel/core'
27
+ Kybus::Bot::Migrator.run_migrations!(APP_CONF['bots']['main']['state_repository'])
28
+ Sequel.extension :migration
29
+ Sequel::Migrator.run(DB, 'models/migrations')
30
+ end
31
+ RUBY
32
+ when 'activerecord'
33
+ <<~RUBY
34
+ # frozen_string_literal: true
35
+
36
+ require 'active_record'
37
+
38
+ ActiveRecord::Base.establish_connection(APP_CONF['database'])
39
+ RUBY
40
+ when 'dynamoid'
41
+ <<~RUBY
42
+ def run_migrations!
43
+ require 'kybus/bot/migrator'
44
+ Kybus::Bot::Migrator.run_migrations!(APP_CONF['bots']['main']['state_repository'])
45
+ end
46
+ RUBY
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Kybus
6
+ class CLI < Thor
7
+ class Bot < Thor
8
+ module Config
9
+ class DeploymentFileProvide < FileProvider
10
+ autoregister!
11
+ def saving_path
12
+ './kybusbot.yaml'
13
+ end
14
+
15
+ def skip_file?
16
+ !@config[:with_deployment_file]
17
+ end
18
+
19
+ def make_contents
20
+ <<~YAML
21
+ name: #{bot_name_snake_case}
22
+ cloud_provider: #{@config[:cloud_provider]}
23
+ dynamo:
24
+ capacity: #{@config[:dynamo_capacity]}
25
+ table_name: #{@config[:dynamo_table]}
26
+ chat_provider: #{@config[:bot_provider]}
27
+ bot_token: #{@config[:bot_token]}
28
+ secret_token: #{generate_secret_token}
29
+ YAML
30
+ end
31
+
32
+ def generate_secret_token
33
+ SecureRandom.alphanumeric(64)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ class DockerfileGenerator
7
+ def initialize(name)
8
+ @name = name
9
+ @file_writer = Kybus::CLI::FileWriter.new(name)
10
+ end
11
+
12
+ def generate
13
+ @file_writer.write('Dockerfile', dockerfile_content)
14
+ end
15
+
16
+ private
17
+
18
+ def dockerfile_content
19
+ <<~DOCKERFILE
20
+ FROM ruby:#{RUBY_VERSION}
21
+
22
+ WORKDIR /app
23
+ COPY . /app
24
+
25
+ RUN bundle install
26
+
27
+ CMD ["ruby", "main.rb"]
28
+ DOCKERFILE
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ module Config
7
+ class GemfileGenerator < FileProvider
8
+ autoregister!
9
+ def saving_path
10
+ './Gemfile'
11
+ end
12
+
13
+ private
14
+
15
+ def make_contents
16
+ <<~GEMFILE
17
+ source 'https://rubygems.org'
18
+
19
+ gem 'kybus-bot'
20
+ gem '#{@config[:db_adapter]}'
21
+ gem 'minitest'
22
+ gem 'kybus-storage'
23
+ gem 'kybus-logger'
24
+ gem 'kybus-configs'
25
+ gem 'rake'
26
+ group :telegram do
27
+ gem 'telegram-bot-ruby'
28
+ end
29
+ group :discord do
30
+ gem 'discordrb'
31
+ end
32
+ group :development do
33
+ gem 'sqlite3'
34
+ end
35
+ GEMFILE
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ module Config
7
+ class LambdaHandlerGenerator < FileProvider
8
+ autoregister!
9
+ def saving_path
10
+ './handler.rb'
11
+ end
12
+
13
+ private
14
+
15
+ def make_contents
16
+ <<~AWSLAMBDA
17
+ require './main'
18
+
19
+ def lambda_handler(event:, context:)
20
+ secret_token = ENV['SECRET_TOKEN']
21
+ header_token = event.dig('headers', 'x-telegram-bot-api-secret-token')
22
+
23
+ unless header_token == secret_token
24
+ return { statusCode: 403, body: JSON.generate('Forbidden') }
25
+ end
26
+
27
+ body = JSON.parse(event['body'])
28
+
29
+ BOT.handle_message(body)
30
+ { statusCode: 200, body: '' }
31
+ end
32
+ AWSLAMBDA
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ module Config
7
+ class RakefileGenerator < FileProvider
8
+ autoregister!
9
+ def saving_path
10
+ './Rakefile'
11
+ end
12
+
13
+ private
14
+
15
+ def make_contents
16
+ <<~RAKEFILE
17
+ require 'rake/testtask'
18
+ task default: :test
19
+
20
+ Rake::TestTask.new do |t|
21
+ t.libs << 'test'
22
+ t.warning = false
23
+ t.pattern = 'test/**/test_*.rb'
24
+ t.warning = false
25
+ end
26
+
27
+ namespace :db do
28
+ desc 'Run database migrations'
29
+ task :migrate do
30
+ require_relative 'config_loaders/autoconfig'
31
+ run_migrations!
32
+ end
33
+ end
34
+ RAKEFILE
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class Bot < Thor
6
+ module Config
7
+ class TestHelperGenerator
8
+ def initialize(name, with_simplecov)
9
+ @file_writer = Kybus::CLI::FileWriter.new(name)
10
+ @with_simplecov = with_simplecov
11
+ end
12
+
13
+ def generate
14
+ @file_writer.write('test/test_helper.rb', test_helper_rb_content)
15
+ end
16
+
17
+ private
18
+
19
+ def test_helper_rb_content
20
+ content = <<~RUBY
21
+ # frozen_string_literal: true
22
+
23
+ require 'minitest/autorun'
24
+ RUBY
25
+ content << "\nrequire 'simplecov'\nSimpleCov.start" if @with_simplecov
26
+ content
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end