swarm_orca 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.overcommit.yml +33 -0
  4. data/.rubocop.yml +1 -0
  5. data/.rubocop_todo.yml +17 -0
  6. data/.ruby-gemset +1 -0
  7. data/.ruby-version +1 -0
  8. data/CHANGELOG.md +15 -0
  9. data/Gemfile +5 -0
  10. data/Gemfile.lock +57 -0
  11. data/README.md +335 -0
  12. data/Rakefile +3 -0
  13. data/bin/orca +7 -0
  14. data/lib/capistrano/scm/copy.rb +62 -0
  15. data/lib/capistrano/scm/tasks/copy.cap +33 -0
  16. data/lib/capistrano/scm/tasks/deploy.rb +16 -0
  17. data/lib/capistrano/swarm_orca/bash/crypt +1 -0
  18. data/lib/capistrano/swarm_orca/deploy.rb +3 -0
  19. data/lib/capistrano/swarm_orca/docker.rb +3 -0
  20. data/lib/capistrano/swarm_orca/helpers/fetch_config.rb +134 -0
  21. data/lib/capistrano/swarm_orca/local.rb +43 -0
  22. data/lib/capistrano/swarm_orca/set_global_config.rb +3 -0
  23. data/lib/capistrano/swarm_orca/tasks/deploy.cap +200 -0
  24. data/lib/capistrano/swarm_orca/tasks/docker.cap +318 -0
  25. data/lib/capistrano/swarm_orca/tasks/set_global_config.cap +28 -0
  26. data/lib/swarm_orca.rb +11 -0
  27. data/lib/swarm_orca/encrypt.rb +40 -0
  28. data/lib/swarm_orca/new.rb +73 -0
  29. data/lib/swarm_orca/orca_cli.rb +41 -0
  30. data/lib/swarm_orca/templates/orca/.gitignore +50 -0
  31. data/lib/swarm_orca/templates/orca/.ruby-gemset.tt +1 -0
  32. data/lib/swarm_orca/templates/orca/.ruby-version +1 -0
  33. data/lib/swarm_orca/templates/orca/README.md +93 -0
  34. data/lib/swarm_orca/templates/orca/application_stack/docker-stack-elasticsearch.yml.erb.tt +22 -0
  35. data/lib/swarm_orca/templates/orca/application_stack/docker-stack-errbit.yml.erb.tt +43 -0
  36. data/lib/swarm_orca/templates/orca/application_stack/docker-stack-mysql.yml.erb.tt +17 -0
  37. data/lib/swarm_orca/templates/orca/application_stack/docker-stack-nginx.yml.erb.tt +22 -0
  38. data/lib/swarm_orca/templates/orca/application_stack/docker-stack-rabbitmq.yml.erb.tt +19 -0
  39. data/lib/swarm_orca/templates/orca/application_stack/docker-stack-redis.yml.erb.tt +19 -0
  40. data/lib/swarm_orca/templates/orca/capistrano/Capfile +45 -0
  41. data/lib/swarm_orca/templates/orca/capistrano/Gemfile.tt +4 -0
  42. data/lib/swarm_orca/templates/orca/capistrano/config/deploy.rb.tt +30 -0
  43. data/lib/swarm_orca/templates/orca/capistrano/config/deploy/template_stage.rb +64 -0
  44. data/lib/swarm_orca/templates/orca/nginx/Dockerfile +5 -0
  45. data/lib/swarm_orca/templates/orca/nginx/nginx.conf +31 -0
  46. data/lib/swarm_orca/templates/orca/redis/Dockerfile +5 -0
  47. data/lib/swarm_orca/templates/orca/redis/redis.conf +2 -0
  48. data/lib/swarm_orca/version.rb +5 -0
  49. data/swarm_orca.gemspec +26 -0
  50. metadata +174 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 15764993dfe2fc2d4786cbec667bb3d07d3d3bda42c27a85b3383b9159f1a357
4
+ data.tar.gz: 43a0c48310e442f3e371a826aa37094695fc79bdfc9f058c0af553e9e282d993
5
+ SHA512:
6
+ metadata.gz: c90ac38d205a8189fc9b99448afdbe59cdf0e24e852f7ae67db54a8d42027bc6a8cadca513f8a6c8c67d0837c5f622c7c9f24769a222adce353c2b4a49bbce32
7
+ data.tar.gz: a8956bd5d8677a23371ce317da93636d0a6956f19948dd9cf8f1252b2dd4ffc67383e8e2c8a747d0f3c9d4adbd32898d150063c5c3c16c2e8fd64e65d5d5d619
@@ -0,0 +1 @@
1
+ .DS_Store
@@ -0,0 +1,33 @@
1
+ # Use this file to configure the Overcommit hooks you wish to use. This will
2
+ # extend the default configuration defined in:
3
+ # https://github.com/brigade/overcommit/blob/master/config/default.yml
4
+ #
5
+ # At the topmost level of this YAML file is a key representing type of hook
6
+ # being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
7
+ # customize each hook, such as whether to only run it on certain files (via
8
+ # `include`), whether to only display output if it fails (via `quiet`), etc.
9
+ #
10
+ # For a complete list of hooks, see:
11
+ # https://github.com/brigade/overcommit/tree/master/lib/overcommit/hook
12
+ #
13
+ # For a complete list of options that you can use to customize hooks, see:
14
+ # https://github.com/brigade/overcommit#configuration
15
+ #
16
+ # Uncomment the following lines to make the configuration take effect.
17
+
18
+ PreCommit:
19
+ RuboCop:
20
+ enabled: true
21
+ on_warn: fail # Treat all warnings as failures
22
+
23
+ TrailingWhitespace:
24
+ enabled: true
25
+ exclude:
26
+ - '**/db/structure.sql' # Ignore trailing whitespace in generated files.
27
+
28
+ #PostCheckout:
29
+ # ALL: # Special hook name that customizes all hooks of this type
30
+ # quiet: true # Change all post-checkout hooks to only display output on failure
31
+ #
32
+ # IndexTags:
33
+ # enabled: true # Generate a tags file with `ctags` each time HEAD changes
@@ -0,0 +1 @@
1
+ inherit_from: .rubocop_todo.yml
@@ -0,0 +1,17 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2019-04-01 17:01:07 +0200 using RuboCop version 0.66.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 2
10
+ Metrics/AbcSize:
11
+ Max: 21
12
+
13
+ # Offense count: 31
14
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
15
+ # URISchemes: http, https
16
+ Metrics/LineLength:
17
+ Max: 154
@@ -0,0 +1 @@
1
+ swarm_orca
@@ -0,0 +1 @@
1
+ 2.6.1
@@ -0,0 +1,15 @@
1
+ > This library uses Semantic Versioning: http://semver.org.
2
+
3
+ ### 0.1.0 / 2019-07-26
4
+ First Version:
5
+ Features:
6
+ * Deploy services to a swarm cluster.
7
+ * Support local deployment.
8
+ * Debug Mode.
9
+ * Generate encryption keys.
10
+ * Support Encrypted configs.
11
+ * Shared application configurations
12
+ * Database migrations.
13
+ * Manage swarm services.
14
+ * Build custome docker images.
15
+ * ERB templates for stack files.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+ # Specify your gem's dependencies in enc_env.gemspec
5
+ gemspec
@@ -0,0 +1,57 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ airbrussh (1.3.0)
5
+ sshkit (>= 1.6.1, != 1.7.0)
6
+ ast (2.4.0)
7
+ capistrano (3.10.2)
8
+ airbrussh (>= 1.0.0)
9
+ i18n
10
+ rake (>= 10.0.0)
11
+ sshkit (>= 1.9.0)
12
+ childprocess (0.9.0)
13
+ ffi (~> 1.0, >= 1.0.11)
14
+ concurrent-ruby (1.0.5)
15
+ ffi (1.10.0)
16
+ i18n (1.0.0)
17
+ concurrent-ruby (~> 1.0)
18
+ iniparse (1.4.4)
19
+ jaro_winkler (1.5.2)
20
+ net-scp (1.2.1)
21
+ net-ssh (>= 2.6.5)
22
+ net-ssh (4.2.0)
23
+ overcommit (0.47.0)
24
+ childprocess (~> 0.6, >= 0.6.3)
25
+ iniparse (~> 1.4)
26
+ parallel (1.16.0)
27
+ parser (2.6.2.0)
28
+ ast (~> 2.4.0)
29
+ psych (3.1.0)
30
+ rainbow (3.0.0)
31
+ rake (10.5.0)
32
+ rubocop (0.66.0)
33
+ jaro_winkler (~> 1.5.1)
34
+ parallel (~> 1.10)
35
+ parser (>= 2.5, != 2.5.1.1)
36
+ psych (>= 3.1.0)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 1.6)
40
+ ruby-progressbar (1.10.0)
41
+ sshkit (1.16.0)
42
+ net-scp (>= 1.1.2)
43
+ net-ssh (>= 2.8.0)
44
+ thor (0.20.0)
45
+ unicode-display_width (1.5.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler (~> 1.13)
52
+ overcommit (~> 0.47.0)
53
+ rake (~> 10.0)
54
+ rubocop (~> 0.66.0)
55
+
56
+ BUNDLED WITH
57
+ 1.17.2
@@ -0,0 +1,335 @@
1
+ # Swarm Orca
2
+ This gem includes a set of capistrano recipes used to deploy services and rails applications to a swarm cluster.
3
+
4
+ ## Command Line
5
+ ### Install
6
+
7
+ ```sh
8
+ $~> gem install swarm_orca
9
+ ```
10
+
11
+ ### Commands
12
+
13
+ ```sh
14
+ orca decrypt KEY CIPHER # This Command will decrypt the given cipher
15
+ orca encrypt KEY TEXT # This Command will encrypt the given text
16
+ orca gen_enc_key # This Command will generate new encryption key
17
+ orca help [COMMAND] # Describe available commands or one specific command
18
+ orca new ORCA_DIRECTORY_NAME GIT_FORK DOCKER_NETWORK # This Command will create a new Orca project
19
+ ```
20
+
21
+ ## Install
22
+
23
+ Add the following to your `Gemfile`.
24
+
25
+ ```ruby
26
+ group :deployment do
27
+ gem 'capistrano'
28
+ gem 'swarm_orca'
29
+ end
30
+ ```
31
+
32
+ Then run
33
+
34
+ $ bundle install
35
+
36
+ ## Usage
37
+
38
+ - Add the following to your `Capfile`.
39
+
40
+ ```ruby
41
+ require "swarm_orca"
42
+ ```
43
+
44
+
45
+ - Add the following to your `deploy.rb`.
46
+
47
+ ```ruby
48
+ require "capistrano/swarm_orca/set_global_config"
49
+
50
+ set :fork, "${YOUR_FORK_NAME}"
51
+ # example
52
+
53
+ set (:db_apps_stacks_mapping), {
54
+ core: %w(api-backend mobile-backedn),
55
+ frontend: %w(),
56
+ }
57
+
58
+ require "capistrano/swarm_orca/deploy"
59
+ require "capistrano/swarm_orca/docker"
60
+
61
+ ```
62
+
63
+ ## Define your services and application
64
+ Swarm orca define the managed services and applications in `deploy.rb`.
65
+
66
+ - service_stacks: array include the services stack names. That do not need custome docker images and do not have a database.
67
+ - service_stacks_with_build_image: services that need a custome docker image.
68
+ - db_apps_stacks_mapping: stacks for your rails application. keys are the stack names and values are the database application names. For example, the core stack include two database applications. While the frontend have no database application.
69
+ ```
70
+ set (:db_apps_stacks_mapping), {
71
+ core: %w(api-backend mobile-backedn),
72
+ frontend: %w(),
73
+ }
74
+ ```
75
+
76
+ - elasticsearch_apps: application names that are connected to elasticsearch.
77
+
78
+ ## Define your services and applications configurations
79
+ Swarm Orca manage the stacks and application configurations in the stage files under `capistrano/config/deploy`
80
+
81
+ - each stack must have the `stack_name` to define the stack name.
82
+ - docker image , tag and other configs can be configured in the same way. These items depend on the stack file `orca/application_stack` it self and the applications needed by the application.
83
+
84
+ ```
85
+ set :mysql,
86
+ stack_name: 'mysql',
87
+ mysql_docker_image: 'mysql',
88
+ mysql_docker_image_tag: '5.7',
89
+ mysql_volume: "#{fetch(:deploy_to)}/mysql"
90
+ ```
91
+
92
+ - Special config keys:
93
+ {application}_database_url: Database URL for the rails application.
94
+ docker_image_prefix: docker image prefix(host and namespace)
95
+ {application}_docker_image: Docker image name
96
+ {application}_docker_image_tag: Docker image tag.
97
+
98
+ ## Application Seeds
99
+
100
+ - Define your application seeds
101
+ - By default Swarm orca gem will execute `rake db:seed` to run the seeds for your defined applications.
102
+ - In Addition, Swarm orca gem support custom seeds. To implement a custom seed in your Orca please follow these instruction.
103
+ - Create a new folder in the root directory and call it "seeds".
104
+ - Add a seed file for each of your application. The file name should follow this schema "${application_name}.rb"
105
+ - Example of seeds for backend application
106
+
107
+ ```
108
+ cat seeds/backend.rb
109
+ Rails.application.load_tasks
110
+ Rake::Task['roles:reseed_defaults'].execute
111
+ ```
112
+
113
+ ## Application Database migrations
114
+
115
+ - Define migrations, seeds and elasticsearch reindex.
116
+ - To be able to execute migrations, seeds and elasticsearch reindex, you need to explicitly define the roles on your stage.
117
+ - Example: our backend stack is using db and elasticsearch
118
+
119
+ ```
120
+ server "${server}", user: "deploy", roles: %w{${stack}_db ${stack}_reindex}
121
+ server "${server}", user: "deploy", roles: %w{backend_db backend_reindex}
122
+ ```
123
+
124
+ ## Deployment Strategy
125
+ By default capistrano supports only git deployment strategy to support xopy strategy do the following:
126
+
127
+ - Apply these changes to your `Capfile`
128
+ ```diff
129
+ -require "capistrano/scm/git"
130
+ +scm = ENV.fetch('SCM', 'git')
131
+ +require "capistrano/scm/#{scm}"
132
+ +install_plugin Module.const_get("Capistrano::SCM::#{scm.capitalize}")
133
+ ```
134
+ - To deploy with Copy strategy add "SCM=copy" to the command line.
135
+ ```
136
+ SCM=copy bundle exec cap ${stage} deploy:${stack}
137
+ ```
138
+
139
+ ## One Time migrations
140
+
141
+ - Execute One Time migrations from orca.
142
+ - Orca generate a rake task for executing one time migrations for each of the defined database application.
143
+ - These tasks are defined like the following example.
144
+ ```
145
+ deploy:otm_${application}[${migration_name}]
146
+ # deploy:otm_backend[update_referral_bonus_transactions_text]
147
+ ```
148
+ - The migration task should be defined in the project source code under the name space "one_time_migrations"
149
+
150
+ ## Data migrations
151
+ To support data migration for individual applications, do the following.
152
+ - Add a new configuration item with the following pattern "${app_name}_data_migrate" to your stack configurations. The value of the item must be true to switch db:migrate to db:migrate:with_data. See the example below for add migrate with data support to a backend application.
153
+
154
+ ```
155
+ set :backend, {
156
+ stack_name: 'backend',
157
+ backend_data_migrate: 'true',
158
+ ....
159
+ }
160
+ ```
161
+
162
+ ## ERB templates
163
+ - Setup Stacks ERB templates
164
+ - Create an ERB template for each of your stacks. Use the extension "erb" for your stacks.
165
+ - Example : docker-stack-backend.yml.erb
166
+ - Add the following line to deploy.rb
167
+ - set (:docker_erb_templates) { true }
168
+ - Ensure the all container environment variables are double-qouted
169
+
170
+ ## Shared Configuration
171
+ - To set shared configurations that are available for all stacks, use the following syntax.
172
+ ```
173
+ set :shared, {
174
+ global_variable: 'value_123'
175
+ }
176
+ ```
177
+
178
+ - To set shared application configurations that are available in all stacks with special key.
179
+ Need to create shared application set like:
180
+ ```
181
+ set :backend_shared, {
182
+ environment_label: "staging",
183
+ }
184
+ ```
185
+ Add special key to include shared application config to application config
186
+ ```
187
+ set :web_backend {
188
+ include_shared_config: 'backend_shared',
189
+ }
190
+ set :mobile_backend {
191
+ include_shared_config: 'backend_shared',
192
+ }
193
+ ```
194
+
195
+ ## Local Deployment
196
+
197
+ - Support Local Deployment with Swarm Orca
198
+ - To Be able to deploy locally with orca, you need to implement the following changes on your orca project.
199
+ - Create a local stage with your configs. See below template.
200
+
201
+ ```
202
+ server "${server}", ENV.fetch('USER', '${user}'),
203
+ roles: %w{
204
+ swarm_manager
205
+ }
206
+ set (:deploy_to) { "${deploy_to_path}" }
207
+ ```
208
+
209
+ - ${server}: Can be replaced by by "localhost", "127.0.0.1" or any domain that include "local" in it.
210
+ - ${user}: Default it will use the ENV 'USER' value, but if this ENV 'USER' is not defined, you can also defined another/own user.
211
+ - ${deploy_to_path}: the destination deploy to path.
212
+ - You mast include at least "swarm_manager" role.
213
+ - Example deploying mysql and rabbitMq
214
+
215
+ ```
216
+ server "127.0.0.1", ENV.fetch('USER', 'shihadeh'),
217
+ roles: %w{
218
+ mysql
219
+ rabbitmq
220
+ swarm_manager
221
+ }
222
+
223
+ set (:deploy_to) { "/Users/shihadeh/ggs" }
224
+ # set the path to the docker command.
225
+ set (:docker_path) { "" }
226
+
227
+ set :mysql, {
228
+ stack_name: 'mysql',
229
+ mysql_docker_image: 'mysql',
230
+ mysql_docker_image_tag: '5.7',
231
+ }
232
+
233
+ set :rabbitmq, {
234
+ rabbitmq_docker_image: 'rabbitmq',
235
+ stack_name: 'rabbitmq_fr',
236
+ rabbitmq_docker_image_tag: '3.6-management',
237
+ rabbitmq_volume: '/Users/shihadeh/ggs/rmq'
238
+ }
239
+ ```
240
+
241
+ ## Encrypted attribute
242
+ To support encrypted configuration do the follwing:
243
+
244
+ - Create a new encryption key using orc cli.
245
+ - Use orca cli to encrypt the attributes.
246
+ - set the encrypted attributes (you must prefix the attribute key with 'encrypted_' ie. `encrypted_cs_database_url`). for instance
247
+ ```
248
+ set :backend,
249
+ stack_name: 'backend',
250
+ backend_docker_image: 'backend',
251
+ backend_docker_image_tag: 'develop',
252
+ encrypted_backend_database_url: 'h/UNau5AFvhxDbUkBZbPw6RBJzkTPjIMmWOQ+lQ==',
253
+ ```
254
+
255
+ - export the encryption key one the machine where the deployemnt scripts will be executed ie (your local machine or jenkins nodes).
256
+
257
+ ## Swarm Orca Deployment
258
+ - Start deployment
259
+ - You can use the following commands to setup and deploy locally
260
+
261
+ ```
262
+ # setup and deploy, it will created dbs, and run seeds
263
+ bundle exec cap ${stage} deploy:development_setup
264
+ ```
265
+
266
+ - Build custom docker images manually
267
+
268
+ ```
269
+ bundle exec cap ${stage} deploy:build_images
270
+ ```
271
+
272
+ - Deploy individual stacks
273
+
274
+ ```
275
+ bundle exec cap ${stage} deploy:${stack}
276
+ ```
277
+
278
+ - Deployment with specific fork
279
+
280
+ Swarm orca defines a defult for for deployment. The fork value can be changed by setting the ENV 'FORK'.
281
+ ```
282
+ FORK=${forkName} bundle exec cap ${stage} deploy:${stack}
283
+ ```
284
+ Example deploying with fork 'shihadeh'
285
+ ```
286
+ FORK=shihadeh bundle exec cap ${stage} deploy:${stack}
287
+ ```
288
+
289
+ - Deploy more than one stack
290
+
291
+ ```
292
+ DEPLOYED_STACKS="mysql rabbitmq" bundle exec cap ${stage} deploy:auto
293
+ ```
294
+
295
+ - Deploy without building docker images.
296
+
297
+ ```
298
+ BUILD_IMAGE=false bundle exec cap ${stage} deploy:${stack}
299
+ ```
300
+
301
+ - Deploy all
302
+
303
+ ```
304
+ bundle exec cap ${stage} deploy:all
305
+ ```
306
+ - Recreate DBS for an environment
307
+ Use deploy:recreate_all_dbs task to return an envornment(stage) databases to the initial stage
308
+ Example recreating DBS for an environment(stage)
309
+ ```
310
+ bundle exec cap ${stage} deploy:recreate_all_dbs
311
+ ```
312
+
313
+ - Deploy Debug Mode
314
+ ```
315
+ ORCA_DEBUG=true bundle exec cap ${stage} ${task}
316
+ ```
317
+
318
+ - Deployment without cleaning up old docker images.
319
+ - Add the `PRUNE=false` variable to your deployment command.
320
+ ```
321
+ PRUNE=false bundle exec cap local deploy:auto
322
+ ```
323
+
324
+ ## Swarm Orca special roles
325
+ - swarm_manager : Nodes with this command will be used to execute deployment commands. You only need one node this role per stage.
326
+ - swarm_node: Swarm Orca will try to cleanup old images or containers on the nodes with this role.
327
+ - stack: To be able to deploy service X to a given cluster, you must to include the X as a role for a manager node in that cluster.
328
+ - #{stack}_db: This role indeicate where the database migration will be executed.
329
+ - #{stack}_reindex: This role indeicate where the elasticsearch reindexing will be executed.
330
+
331
+
332
+
333
+
334
+
335
+