brasa 0.3.0 → 0.3.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7285bea2b7d7a7f1db549db5faa7e0a3c4f60c15baf185210b46c0f8e162c792
4
- data.tar.gz: c826e60cc8895e4ab2bb0aa26db0600b86abcd9fe1ff0d77f49d3701d07111c2
3
+ metadata.gz: e07ee00f7ac9e995d27c4e920a9408358f9d8d026682cac95fb43cd89b63289b
4
+ data.tar.gz: 5ff8c1b1dfbacbdfecd63f4fb1a6bb06a6d438ad3a69b3bc7296d784a291b2b1
5
5
  SHA512:
6
- metadata.gz: dce0daf704b7880388fd0233fa0308152a871480aeace5fdd88cb31514049ae2ee83b96bbf936cb340db6a4a66a1a96ebbd7c55eba0a4708d116873201124d35
7
- data.tar.gz: c3b1504e6fd93b18fcb813b3c4abd0f669674dff8f28dc74dcc49fad3357331351ad22db6642cedb89dd0ed0ff1a41eca55674ceb578e01286290ef247981f07
6
+ metadata.gz: 6411273f8dc5913936a36e4df53d71c1e582338ae59a43fa3cd1a90e8e1aeb48d89e678a0108af9fcf6d019c6eabf1548a3dd75e4aee821bc1eda7972df49078
7
+ data.tar.gz: e9f02d023fc2a852d8ec826afa5801d89ebb133d278ccae56f59efc971e7a3c78ac9e8d449080e902a275543798d43c05259fa2be2e92fbf0e080f15f37edc20
@@ -1,4 +1,5 @@
1
1
  require "faraday"
2
+ require "faraday/multipart"
2
3
  require "json"
3
4
 
4
5
  module Brasa
@@ -43,8 +44,25 @@ module Brasa
43
44
  request(:delete, path)
44
45
  end
45
46
 
47
+ def upload(path, file_path:, params: {})
48
+ response = upload_connection.post(path) do |req|
49
+ req.body = params.merge(source: Faraday::Multipart::FilePart.new(file_path, "application/gzip"))
50
+ end
51
+ handle_response(response)
52
+ end
53
+
46
54
  private
47
55
 
56
+ def upload_connection
57
+ @upload_connection ||= Faraday.new(url: @api_url) do |conn|
58
+ conn.request :multipart
59
+ conn.request :url_encoded
60
+ conn.response :json
61
+ conn.headers["Authorization"] = "Bearer #{@token}" if @token
62
+ conn.adapter Faraday.default_adapter
63
+ end
64
+ end
65
+
48
66
  def request(method, path, body: nil, params: nil)
49
67
  response = connection.public_send(method, path) do |req|
50
68
  req.params = params if params && !params.empty?
@@ -1,26 +1,34 @@
1
1
  require "brasa/commands/base"
2
+ require "brasa/source_packer"
2
3
 
3
4
  module Brasa
4
5
  module Commands
5
6
  class Deploy < Base
6
- POLL_INTERVAL = 3
7
+ POLL_INTERVAL = 5
7
8
  MAX_POLLS = 120
8
9
 
9
10
  def execute(options = {})
10
11
  require_auth!
11
12
  slug = app_slug
12
13
 
13
- info("Iniciando deploy de #{slug}...")
14
+ info("Empacotando código...")
15
+ archive_path = SourcePacker.pack
14
16
 
15
- body = {}
16
- body[:branch] = options["branch"] if options["branch"]
17
+ size_kb = File.size(archive_path) / 1024
18
+ info("Enviando #{size_kb}KB para o servidor...")
17
19
 
18
- deploy = api.post("/api/v1/apps/#{slug}/deploys", body: body.empty? ? {} : body)
19
- info("Deploy ##{deploy["id"]} criado. Aguardando...")
20
+ deploy = api.upload(
21
+ "/api/v1/apps/#{slug}/deploys",
22
+ file_path: archive_path,
23
+ params: { branch: options["branch"] || "main" }
24
+ )
25
+ info("Deploy ##{deploy["id"]} criado. Aguardando build e deploy...")
20
26
 
21
27
  wait_for_deploy(slug, deploy["id"])
22
28
  rescue Api::Client::ApiError => e
23
29
  error("Erro: #{e.message}")
30
+ ensure
31
+ FileUtils.rm_f(archive_path) if archive_path
24
32
  end
25
33
 
26
34
  private
@@ -1,4 +1,5 @@
1
1
  require "brasa/commands/base"
2
+ require "brasa/source_packer"
2
3
 
3
4
  module Brasa
4
5
  module Commands
@@ -68,7 +69,19 @@ module Brasa
68
69
  end
69
70
 
70
71
  def trigger_deploy(slug)
71
- api.post("/api/v1/apps/#{slug}/deploys")
72
+ info("Empacotando código...")
73
+ archive_path = SourcePacker.pack
74
+ size_kb = File.size(archive_path) / 1024
75
+ info("Enviando #{size_kb}KB para o servidor...")
76
+
77
+ deploy = api.upload(
78
+ "/api/v1/apps/#{slug}/deploys",
79
+ file_path: archive_path,
80
+ params: { branch: project_config[:branch] || "main" }
81
+ )
82
+ deploy
83
+ ensure
84
+ FileUtils.rm_f(archive_path) if archive_path
72
85
  end
73
86
 
74
87
  def detect_git_remote
@@ -0,0 +1,48 @@
1
+ require "tmpdir"
2
+ require "open3"
3
+
4
+ module Brasa
5
+ class SourcePacker
6
+ IGNORE_PATTERNS = %w[
7
+ .git node_modules tmp log .bundle vendor/bundle
8
+ .brasa.yml .env .env.* *.log coverage
9
+ ].freeze
10
+
11
+ def self.pack(directory = Dir.pwd)
12
+ new(directory).pack
13
+ end
14
+
15
+ def initialize(directory)
16
+ @directory = directory
17
+ end
18
+
19
+ def pack
20
+ archive_path = File.join(Dir.tmpdir, "brasa-source-#{Time.now.to_i}.tar.gz")
21
+
22
+ excludes = build_excludes
23
+ cmd = [ "tar", "czf", archive_path ] + excludes + [ "-C", @directory, "." ]
24
+
25
+ _out, err, status = Open3.capture3(*cmd)
26
+ raise "Falha ao empacotar código: #{err}" unless status.success?
27
+
28
+ archive_path
29
+ end
30
+
31
+ private
32
+
33
+ def build_excludes
34
+ patterns = IGNORE_PATTERNS.dup
35
+
36
+ dockerignore = File.join(@directory, ".dockerignore")
37
+ if File.exist?(dockerignore)
38
+ File.readlines(dockerignore).each do |line|
39
+ line = line.strip
40
+ next if line.empty? || line.start_with?("#")
41
+ patterns << line
42
+ end
43
+ end
44
+
45
+ patterns.flat_map { |p| [ "--exclude", p ] }
46
+ end
47
+ end
48
+ end
data/lib/brasa/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Brasa
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brasa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brasa
@@ -37,6 +37,20 @@ dependencies:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '2.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: faraday-multipart
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
40
54
  - !ruby/object:Gem::Dependency
41
55
  name: tty-prompt
42
56
  requirement: !ruby/object:Gem::Requirement
@@ -152,6 +166,7 @@ files:
152
166
  - lib/brasa/commands/status.rb
153
167
  - lib/brasa/commands/up.rb
154
168
  - lib/brasa/config.rb
169
+ - lib/brasa/source_packer.rb
155
170
  - lib/brasa/version.rb
156
171
  homepage: https://usebrasa.com.br
157
172
  licenses: