File: manager.rb

package info (click to toggle)
ruby-byebug 11.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,252 kB
  • sloc: ruby: 8,835; ansic: 1,662; sh: 6; makefile: 4
file content (219 lines) | stat: -rw-r--r-- 4,317 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# frozen_string_literal: true

require "net/http"
require "yaml"
require "open3"

module Docker
  #
  # Manages byebug docker images
  #
  class Manager
    VERSIONS = %w[
      2.4.9
      2.5.7
      2.6.5
      2.7.0
    ].freeze

    LINE_EDITORS = %w[
      readline
      libedit
    ].freeze

    COMPILERS = %w[
      gcc
      clang
    ].freeze

    attr_reader :version, :line_editor, :compiler

    def initialize(version:, line_editor:, compiler:)
      @version = version
      @line_editor = line_editor
      @compiler = compiler
    end

    def login
      self.class.login
    end

    def build
      command = <<-COMMAND
        docker build \
          --tag "#{tag}" \
          --build-arg "ruby_download_url=#{download_url}" \
          --build-arg "ruby_download_sha256=#{download_sha256}" \
          --build-arg "compiler=#{compiler}" \
          --build-arg "line_edit_lib=#{line_editor_package}" \
          --build-arg "line_edit_config=#{line_editor_configure_flag}" \
          --file "docker/Dockerfile" \
          .
      COMMAND

      print "Building image #{tag} via `#{squish(command)}`... "

      run(command)
    end

    def test
      command = <<-COMMAND
        docker run --rm -v$(pwd):/byebug #{tag} bash -c 'bin/setup.sh && bin/rake'
      COMMAND

      print "Testing image #{tag}: #{squish(command)}  "

      run(command)
    end

    def push
      print "Pushing image #{tag}... "

      run("docker push #{tag}")
    end

    class << self
      def build_default
        for_last_version_variants(&:build)
      end

      def test_default
        for_last_version_variants(&:test)
      end

      def push_default
        login

        for_last_version_variants(&:push)
      end

      def build_all
        for_all_images(&:build)
      end

      def test_all
        for_all_images(&:test)
      end

      def push_all
        login

        for_all_images(&:push)
      end

      def release_info
        @release_info ||= YAML.safe_load(
          Net::HTTP.get(URI.parse(releases_url)),
          [Date]
        )
      end

      def run(*command)
        output, status = Open3.capture2e(*command)

        success = status.success?

        puts(success ? "✔" : "❌")

        return if success

        puts output
        abort
      end

      def login
        command = %W[
          docker
          login
          -u
          #{ENV['DOCKER_USER']}
          -p
          #{ENV['DOCKER_PASS']}
        ]

        print "Logging in to dockerhub... "

        run(*command)
      end

      private

      def releases_url
        "https://raw.githubusercontent.com/ruby/www.ruby-lang.org/master/_data/releases.yml"
      end

      def for_all_images(&block)
        VERSIONS.each do |version|
          for_variants_of(version, &block)
        end
      end

      def for_last_version_variants(&block)
        for_variants_of(VERSIONS.last, &block)
      end

      def for_variants_of(version)
        COMPILERS.each do |compiler|
          LINE_EDITORS.each do |line_editor|
            manager = new(
              version: version,
              line_editor: line_editor,
              compiler: compiler
            )

            yield(manager)
          end
        end
      end
    end

    private

    def line_editor_package
      line_editor == "readline" ? "readline-dev" : "libedit-dev"
    end

    def line_editor_configure_flag
      line_editor == "readline" ? "" : "--enable-libedit"
    end

    def download_url
      if version == "head"
        "#{download_url_base}/snapshot.tar.xz"
      else
        "#{download_url_base}/#{abi_version}/ruby-#{version}.tar.xz"
      end
    end

    def abi_version
      version.split(".")[0..1].join(".")
    end

    def download_url_base
      "https://cache.ruby-lang.org/pub/ruby"
    end

    def release_info
      self.class.release_info
    end

    def download_sha256
      version_info = release_info.find { |entry| entry["version"] == version }
      return unless version_info

      version_info["sha256"]["xz"]
    end

    def run(*command)
      self.class.run(*command)
    end

    def tag
      "deividrodriguez/byebug:#{version}-#{line_editor}-#{compiler}"
    end

    def squish(command)
      command.gsub(/[\n ]+/, " ").strip
    end
  end
end