mrsk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ce03312fed10073e6fd3d8fe805a1088ff759674368032ef27cdc88cf1ed5658
4
+ data.tar.gz: e686e5c333552e06af7fedcf25a9d03ab55e0bfc12a92a40782fae4649917a8c
5
+ SHA512:
6
+ metadata.gz: 5cb4e0842dccb3ebccc1e156e86151d471d3b6ab4f902a0e206f9942ab920df512cbe4c467bd052fc04aec89723c8af27bfdd70b1f6fb37d94a39e44d0fbcf03
7
+ data.tar.gz: 79396a77da9f1d7b4bed0658f894f3bec0e8bbf6498f4d54b15b265e599a0d8594f46963b1064d483971b2840df5a4366f3cb880d675fd41433e89bde17f4cd6
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2023 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # MRSK
2
+
3
+ MRSK lets you do zero-downtime deploys of Rails apps packed as containers to any host running Docker. It uses the dynamic reverse-proxy Traefik to hold requests while the new application container is started and the old one is wound down. It works across multiple hosts at the same time, using SSHKit to execute commands.
4
+
5
+ ## Installation
6
+
7
+ Create a configuration file for MRSK in `config/deploy.yml` that looks like this:
8
+
9
+ ```yaml
10
+ service: my-app
11
+ image: name/my-app
12
+ servers:
13
+ - xxx.xxx.xxx.xxx
14
+ - xxx.xxx.xxx.xxx
15
+ env:
16
+ DATABASE_URL: mysql2://username@localhost/database_name/
17
+ REDIS_URL: redis://host:6379/1
18
+ ```
19
+
20
+ Then first login to the Docker Hub registry on the servers:
21
+
22
+ ```
23
+ rake mrsk:registry:login DOCKER_USER=name DOCKER_PASSWORD=pw
24
+ ```
25
+
26
+ Now you're ready to deploy a multi-arch image (FIXME: currently you need to manually run `docker buildx create --use` once first):
27
+
28
+ ```
29
+ rake mrsk:deploy
30
+ ```
31
+
32
+ This will:
33
+
34
+ 1. Build the image using the standard Dockerfile in the root of the application.
35
+ 2. Push the image to the registry.
36
+ 3. Pull the image on all the servers.
37
+ 4. Ensure Traefik is running and accepting traffic on port 80.
38
+ 5. Stop any containers running a previous versions of the app.
39
+ 6. Start a new container with the version of the app that matches the current git version hash.
40
+
41
+ Voila! All the servers are now serving the app on port 80, and you're ready to put them behind a load balancer to serve live traffic.
42
+
43
+ ## Stage of development
44
+
45
+ This is alpha software. Lots of stuff is missing. Here are some of the areas we seek to improve:
46
+
47
+ - Use of other registries than Docker Hub
48
+ - Adapterize commands to work with Podman and other container runners
49
+ - Better flow for secrets and ENV
50
+ - Possibly switching to a bin/mrsk command rather than raw rake
51
+ - Integrate wirmth cloud CI pipelines
52
+
53
+ ## License
54
+
55
+ Mrsk is released under the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,22 @@
1
+ class Mrsk::Commands::App < Mrsk::Commands::Base
2
+ def push
3
+ # TODO: Run 'docker buildx create --use' when needed
4
+ "docker buildx build --push --platform=linux/amd64,linux/arm64 -t #{config.absolute_image} ."
5
+ end
6
+
7
+ def pull
8
+ "docker pull #{config.absolute_image}"
9
+ end
10
+
11
+ def start
12
+ "docker run -d --rm --name #{config.service_with_version} #{config.envs} #{config.labels} #{config.absolute_image}"
13
+ end
14
+
15
+ def stop
16
+ "docker ps -q --filter label=service=#{config.service} | xargs docker stop"
17
+ end
18
+
19
+ def info
20
+ "docker ps --filter label=service=#{config.service}"
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ class Mrsk::Commands::Registry < Mrsk::Commands::Base
2
+ def login
3
+ "docker login #{config.registry["server"]} -u #{config.registry["username"]} -p #{config.registry["password"]}"
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
2
+ def start
3
+ "docker run --name traefik " +
4
+ "--rm -d " +
5
+ "-p 80:80 " +
6
+ "-v /var/run/docker.sock:/var/run/docker.sock " +
7
+ "traefik --providers.docker"
8
+ end
9
+
10
+ def stop
11
+ "docker container stop traefik"
12
+ end
13
+
14
+ def info
15
+ "docker ps --filter name=traefik"
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module Mrsk::Commands
2
+ class Base
3
+ attr_accessor :config
4
+
5
+ def initialize(config)
6
+ @config = config
7
+ end
8
+ end
9
+ end
10
+
11
+ require "mrsk/commands/app"
12
+ require "mrsk/commands/traefik"
13
+ require "mrsk/commands/registry"
@@ -0,0 +1,78 @@
1
+ require "active_support/ordered_options"
2
+
3
+ class Mrsk::Configuration
4
+ delegate :service, :image, :env, :registry, :ssh_user, to: :config, allow_nil: true
5
+
6
+ def self.load_file(file)
7
+ if file.exist?
8
+ new YAML.load_file(file).symbolize_keys
9
+ else
10
+ raise "Configuration file not found in #{file}"
11
+ end
12
+ end
13
+
14
+ def initialize(config)
15
+ @config = ActiveSupport::InheritableOptions.new(config)
16
+ ensure_required_keys_present
17
+ end
18
+
19
+ def servers
20
+ ENV["SERVERS"] || config.servers
21
+ end
22
+
23
+ def version
24
+ @version ||= ENV["VERSION"] || `git rev-parse HEAD`.strip
25
+ end
26
+
27
+ def absolute_image
28
+ [ config.registry["server"], image_with_version ].compact.join("/")
29
+ end
30
+
31
+ def image_with_version
32
+ "#{image}:#{version}"
33
+ end
34
+
35
+ def service_with_version
36
+ "#{service}-#{version}"
37
+ end
38
+
39
+ def envs
40
+ parameterize "-e", \
41
+ { "RAILS_MASTER_KEY" => master_key }.merge(env || {})
42
+ end
43
+
44
+ def labels
45
+ parameterize "--label", \
46
+ "service" => service,
47
+ "traefik.http.routers.#{service}.rule" => "'PathPrefix(`/`)'",
48
+ "traefik.http.services.#{service}.loadbalancer.healthcheck.path" => "/up",
49
+ "traefik.http.services.#{service}.loadbalancer.healthcheck.interval" => "1s",
50
+ "traefik.http.middlewares.#{service}.retry.attempts" => "3",
51
+ "traefik.http.middlewares.#{service}.retry.initialinterval" => "500ms"
52
+ end
53
+
54
+ def ssh_options
55
+ { user: config.ssh_user || "root", auth_methods: [ "publickey" ] }
56
+ end
57
+
58
+ private
59
+ attr_accessor :config
60
+
61
+ def ensure_required_keys_present
62
+ %i[ service image registry ].each do |key|
63
+ raise ArgumentError, "Missing required configuration for #{key}" unless config[key].present?
64
+ end
65
+
66
+ %w[ username password ].each do |key|
67
+ raise ArgumentError, "Missing required configuration for registry/#{key}" unless config.registry[key].present?
68
+ end
69
+ end
70
+
71
+ def parameterize(param, hash)
72
+ hash.collect { |k, v| "#{param} #{k}=#{v}" }.join(" ")
73
+ end
74
+
75
+ def master_key
76
+ ENV["RAILS_MASTER_KEY"] || File.read(Rails.root.join("config/master.key"))
77
+ end
78
+ end
@@ -0,0 +1,4 @@
1
+ module Mrsk
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Mrsk
2
+ VERSION = "0.0.1"
3
+ end
data/lib/mrsk.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Mrsk
2
+ end
3
+
4
+ require "mrsk/version"
5
+ require "mrsk/engine"
6
+
7
+ require "mrsk/configuration"
8
+ require "mrsk/commands"
@@ -0,0 +1,31 @@
1
+ require_relative "setup"
2
+
3
+ app = Mrsk::Commands::App.new(MRSK_CONFIG)
4
+
5
+ namespace :mrsk do
6
+ namespace :app do
7
+ desc "Build and push app image to servers"
8
+ task :push do
9
+ run_locally { execute app.push }
10
+ on(MRSK_CONFIG.servers) { execute app.pull }
11
+ end
12
+
13
+ desc "Start app on servers"
14
+ task :start do
15
+ on(MRSK_CONFIG.servers) { execute app.start }
16
+ end
17
+
18
+ desc "Stop app on servers"
19
+ task :stop do
20
+ on(MRSK_CONFIG.servers) { execute app.stop, raise_on_non_zero_exit: false }
21
+ end
22
+
23
+ desc "Restart app on servers"
24
+ task restart: %i[ stop start ]
25
+
26
+ desc "Display information about app containers"
27
+ task :info do
28
+ on(MRSK_CONFIG.servers) { |host| puts "Host: #{host}\n" + capture(app.info) + "\n\n" }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ namespace :mrsk do
2
+ desc "Push the latest version of the app, ensure Traefik is running, then restart app"
3
+ task deploy: [ "app:push", "traefik:start", "app:restart" ]
4
+
5
+ desc "Display information about Traefik and app containers"
6
+ task info: [ "traefik:info", "app:info" ]
7
+
8
+ desc "Create config stub"
9
+ task :init do
10
+ Rails.root.join("config/deploy.yml")
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ require_relative "setup"
2
+
3
+ registry = Mrsk::Commands::Registry.new(MRSK_CONFIG)
4
+
5
+ namespace :mrsk do
6
+ namespace :registry do
7
+ desc "Login to the registry locally and remotely"
8
+ task :login do
9
+ run_locally { execute registry.login }
10
+ on(MRSK_CONFIG.servers) { execute registry.login }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ require "sshkit"
2
+ require "sshkit/dsl"
3
+
4
+ include SSHKit::DSL
5
+
6
+ MRSK_CONFIG = Mrsk::Configuration.load_file(Rails.root.join("config/deploy.yml"))
7
+
8
+ SSHKit::Backend::Netssh.configure { |ssh| ssh.ssh_options = MRSK_CONFIG.ssh_options }
@@ -0,0 +1,25 @@
1
+ require_relative "setup"
2
+
3
+ traefik = Mrsk::Commands::Traefik.new(MRSK_CONFIG)
4
+
5
+ namespace :mrsk do
6
+ namespace :traefik do
7
+ desc "Start Traefik"
8
+ task :start do
9
+ on(MRSK_CONFIG.servers) { execute traefik.start, raise_on_non_zero_exit: false }
10
+ end
11
+
12
+ desc "Stop Traefik"
13
+ task :stop do
14
+ on(MRSK_CONFIG.servers) { execute traefik.stop, raise_on_non_zero_exit: false }
15
+ end
16
+
17
+ desc "Restart Traefik"
18
+ task restart: %i[ stop start ]
19
+
20
+ desc "Display information about Traefik containers"
21
+ task :info do
22
+ on(MRSK_CONFIG.servers) { |host| puts "Host: #{host}\n" + capture(traefik.info) + "\n\n" }
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mrsk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Heinemeier Hansson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sshkit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.21'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.21'
41
+ description:
42
+ email: dhh@hey.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - MIT-LICENSE
48
+ - README.md
49
+ - lib/mrsk.rb
50
+ - lib/mrsk/commands.rb
51
+ - lib/mrsk/commands/app.rb
52
+ - lib/mrsk/commands/registry.rb
53
+ - lib/mrsk/commands/traefik.rb
54
+ - lib/mrsk/configuration.rb
55
+ - lib/mrsk/engine.rb
56
+ - lib/mrsk/version.rb
57
+ - lib/tasks/mrsk/app.rake
58
+ - lib/tasks/mrsk/mrsk.rake
59
+ - lib/tasks/mrsk/registry.rake
60
+ - lib/tasks/mrsk/setup.rb
61
+ - lib/tasks/mrsk/traefik.rake
62
+ homepage: https://github.com/rails/mrsk
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.3.26
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Deploy Docker containers with zero downtime to any host.
85
+ test_files: []