kybus-cli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../file_writer'
4
+ require_relative 'file_provider'
5
+ require 'kybus/dry/resource_injector'
6
+
7
+ module Kybus
8
+ class CLI < Thor
9
+ class Bot < Thor
10
+ class ProjectGenerator
11
+ extend Kybus::DRY::ResourceInjector
12
+ register(:providers, [])
13
+
14
+ def self.register_file_provider(file_provider)
15
+ providers = resource(:providers)
16
+ providers << file_provider
17
+ end
18
+
19
+ def initialize(name, configs)
20
+ @name = name.gsub('::', '/')
21
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
22
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
23
+ .tr('-', '_')
24
+ .downcase
25
+ @configs = configs
26
+ @file_writer = FileWriter.new(@name)
27
+ end
28
+
29
+ def generate
30
+ if File.directory?(@name)
31
+ puts "Directory exists #{@name}"
32
+ exit(1)
33
+ end
34
+
35
+ create_directories
36
+ write_files
37
+ puts `cd #{@name} && git init . && bundle install --path vendor/bundle && git add . && git commit -m "Initial Commit"`
38
+ puts "Project #{@name} initialized with #{@configs[:db_adapter]} adapter."
39
+ end
40
+
41
+ private
42
+
43
+ def create_directories
44
+ %w[helpers models config_loaders config test].each do |dir|
45
+ FileUtils.mkdir_p("#{@name}/#{dir}")
46
+ end
47
+ end
48
+
49
+ def write_files
50
+ @file_writer.write('main.rb', main_rb_content)
51
+ @file_writer.write('helpers/.keep', '')
52
+ @file_writer.write('.ruby-version', RUBY_VERSION)
53
+ providers = ProjectGenerator.resource(:providers)
54
+ providers.each do |provider|
55
+ provider.new(@name, @configs).generate
56
+ end
57
+ end
58
+
59
+ def main_rb_content
60
+ <<~RUBY
61
+ # frozen_string_literal: true
62
+
63
+ require_relative 'config_loaders/autoconfig'
64
+ require_relative 'config_loaders/bot_builder'
65
+
66
+ if $PROGRAM_NAME == __FILE__
67
+ BOT.run
68
+ end
69
+ RUBY
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ Dir[File.join(__dir__, 'file_providers', '*.rb')].each { |file| require file }
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+ require_relative 'bot/project_generator'
5
+ require_relative 'bot/controller_generator'
6
+ require_relative 'bot/deployer'
7
+
8
+ module Kybus
9
+ class CLI < Thor
10
+ class Bot < Thor
11
+ desc 'init NAME', 'Initialize a new bot project'
12
+ method_option :db_adapter, aliases: '-d', desc: 'Database adapter (sequel, activerecord, dynamoid)',
13
+ required: true
14
+ method_option :with_simplecov, type: :boolean, default: false, desc: 'Include SimpleCov for test coverage'
15
+ method_option :with_docker_compose, type: :boolean, default: false, desc: 'Include docker compose template file'
16
+ method_option :with_dockerfile, type: :boolean, default: false, desc: 'Include dockerfile template file'
17
+ method_option :bot_provider, type: :string, default: 'REPLACE_ME', desc: 'Defines bot platform'
18
+ method_option :bot_token, type: :string, default: 'REPLACE_ME',
19
+ desc: 'Defines bot token for authentication under API'
20
+ method_option :with_deployment_file, type: :boolean, default: false, desc: 'Include deployment configuration file'
21
+ method_option :cloud_provider, aliases: '-c', desc: 'Cloud provider (default: aws)', default: 'aws'
22
+ method_option :vpc, aliases: '-v', desc: 'VPC ID', default: 'REPLACE_ME'
23
+ method_option :subnets, aliases: '-s', desc: 'Subnets', default: 'REPLACE_ME'
24
+ method_option :dynamo_capacity, aliases: '-d', desc: 'DynamoDB capacity (on_demand or provisioned)',
25
+ default: 'REPLACE_ME'
26
+ method_option :dynamo_table, aliases: '-t', desc: 'DynamoDB table name', default: 'REPLACE_ME'
27
+
28
+ def init(name)
29
+ Kybus::CLI::Bot::ProjectGenerator.new(name, options).generate
30
+ end
31
+
32
+ desc 'add_controller NAME', 'Add a new controller to the bot project'
33
+ def add_controller(name)
34
+ Kybus::CLI::Bot::ControllerGenerator.new(name).generate
35
+ end
36
+
37
+ desc 'deploy-init', 'Initialize deployment configurations'
38
+ method_option :cloud_provider, aliases: '-c', desc: 'Cloud provider (default: aws)', default: 'aws'
39
+ method_option :vpc, aliases: '-v', desc: 'VPC ID'
40
+ method_option :subnets, aliases: '-s', desc: 'Subnets'
41
+ method_option :dynamo_capacity, aliases: '-d', desc: 'DynamoDB capacity (on_demand or provisioned)'
42
+ method_option :dynamo_table, aliases: '-t', desc: 'DynamoDB table name'
43
+ method_option :chat_provider, aliases: '-p', desc: 'Chat provider (default: telegram)'
44
+
45
+ def deploy_init(name)
46
+ DeployInitGenerator.new(name, options).generate
47
+ end
48
+
49
+ desc 'deploy', 'Deploy the bot to AWS'
50
+ def deploy
51
+ ::Kybus::CLI::Deployer.new(options).run
52
+ end
53
+
54
+ desc 'destroy', 'Destroys the provisiones infrastructure in AWS'
55
+ def destroy
56
+ ::Kybus::CLI::Deployer.new(options).destroy
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ class CLI < Thor
5
+ class FileWriter
6
+ def initialize(base_path)
7
+ @base_path = base_path
8
+ end
9
+
10
+ def write(relative_path, content)
11
+ full_path = File.join(@base_path, relative_path)
12
+ FileUtils.mkdir_p(File.dirname(full_path))
13
+ File.write(full_path, content)
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/kybus/cli.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+ require_relative 'cli/bot'
5
+
6
+ module Kybus
7
+ class CLI < Thor
8
+ desc 'bot SUBCOMMAND ...ARGS', 'Commands for managing bots'
9
+ subcommand 'bot', Kybus::CLI::Bot
10
+
11
+ def self.exit_on_failure?
12
+ true
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,193 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kybus-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gilberto Vargas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-cloudwatchlogs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-iam
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-lambda
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: kybus-core
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubyzip
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: thor
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '13.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '13.0'
125
+ description: A CLI tool to help initialize and manage Kybus projects, supporting various
126
+ database adapters and configurations.
127
+ email:
128
+ - tachoguitar@gmail.com
129
+ executables:
130
+ - kybus
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - bin/kybus
135
+ - lib/kybus/aws.rb
136
+ - lib/kybus/aws/code_packager.rb
137
+ - lib/kybus/aws/lambda.rb
138
+ - lib/kybus/aws/log_group.rb
139
+ - lib/kybus/aws/policy.rb
140
+ - lib/kybus/aws/resource.rb
141
+ - lib/kybus/aws/role.rb
142
+ - lib/kybus/cli.rb
143
+ - lib/kybus/cli/bot.rb
144
+ - lib/kybus/cli/bot/controller_generator.rb
145
+ - lib/kybus/cli/bot/deploy_init_generator.rb
146
+ - lib/kybus/cli/bot/deployer.rb
147
+ - lib/kybus/cli/bot/deployers/aws_bot_deployer.rb
148
+ - lib/kybus/cli/bot/deployers/deployer_base.rb
149
+ - lib/kybus/cli/bot/deployers/telegram_configurator.rb
150
+ - lib/kybus/cli/bot/file_provider.rb
151
+ - lib/kybus/cli/bot/file_providers/autoconfig_generator.rb
152
+ - lib/kybus/cli/bot/file_providers/autoconfig_loader_generator.rb
153
+ - lib/kybus/cli/bot/file_providers/bot_builder_generator.rb
154
+ - lib/kybus/cli/bot/file_providers/bot_generator.rb
155
+ - lib/kybus/cli/bot/file_providers/composefile_generator.rb
156
+ - lib/kybus/cli/bot/file_providers/config_default_generator.rb
157
+ - lib/kybus/cli/bot/file_providers/config_generator.rb
158
+ - lib/kybus/cli/bot/file_providers/db_generator.rb
159
+ - lib/kybus/cli/bot/file_providers/deployment_file_provider.rb
160
+ - lib/kybus/cli/bot/file_providers/dockerfile_generator.rb
161
+ - lib/kybus/cli/bot/file_providers/gemfile_generator.rb
162
+ - lib/kybus/cli/bot/file_providers/lambda_handler_generator.rb
163
+ - lib/kybus/cli/bot/file_providers/rakefile_generator.rb
164
+ - lib/kybus/cli/bot/file_providers/test_helper_generator.rb
165
+ - lib/kybus/cli/bot/project_generator.rb
166
+ - lib/kybus/cli/file_writer.rb
167
+ homepage: https://github.com/tachomex/kybus
168
+ licenses:
169
+ - MIT
170
+ metadata:
171
+ homepage_uri: https://github.com/tachomex/kybus
172
+ source_code_uri: https://github.com/tachomex/kybus
173
+ rubygems_mfa_required: 'true'
174
+ post_install_message:
175
+ rdoc_options: []
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ requirements: []
189
+ rubygems_version: 3.5.14
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: CLI for managing Kybus projects
193
+ test_files: []