bard 0.62.2 → 0.64.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb0639401698f2f8efedc908dec19e665c0c7e067446fa6a8e3c38e877c072c0
4
- data.tar.gz: '0510189b034d15caa908e1841f6223076a68ef429fa011dba4d6191e672d4bff'
3
+ metadata.gz: f618df3bbb634f25fcb5f03f81a68b733e2fd70d5a654b8e8f9e0733ce180849
4
+ data.tar.gz: 155060b90c13a3365bf3cd768642966bbc4926643b92d9f18378f69a9516df01
5
5
  SHA512:
6
- metadata.gz: d99981c90eef79bf61b4fe0cda6b6ebae19baf426ec555e38209d9d60a7bebe67f83b8b991ab0ebbfec7df24d7fddf03eb1afe1085a18f63b71a2a43bff98e07
7
- data.tar.gz: 9b97be4d649e3d168327eeb63aab76bb2cf7a713540709a020dde53d143bf536c90292428636e063b7822481f06bd61046ba66593a29a6001b796d476f26d17c
6
+ metadata.gz: f4e42669d831bdf57dc6749ca7443690a307d9abf048f1a1e0eecbfd33a7f53e0a94a4aa1bf78f3724054407f7e679f49fcac029cc1fa00199c7584e68c5301b
7
+ data.tar.gz: 7193a3c7324ee1da26262f18516f3a672299ccc29b1f1737eee7ad121c7cb992e7609fd271cc963b5f673285f9ec31f5c44074ae869f738264ba6ecd8b6749c8
data/lib/bard/base.rb CHANGED
@@ -32,8 +32,8 @@ class Bard::CLI < Thor
32
32
  @project_name ||= File.expand_path(".").split("/").last
33
33
  end
34
34
 
35
- def ssh_command server, command, home: false
36
- server = @config.servers[server.to_sym]
35
+ def ssh_command server_name, command, home: false
36
+ server = @config.servers.fetch(server_name.to_sym)
37
37
  uri = URI.parse("ssh://#{server.ssh}")
38
38
  ssh_key = server.ssh_key ? "-i #{server.ssh_key} " : ""
39
39
  command = "#{server.env} #{command}" if server.env
@@ -46,8 +46,8 @@ class Bard::CLI < Thor
46
46
  command
47
47
  end
48
48
 
49
- def copy direction, server, path, verbose: false
50
- server = @config.servers[server.to_sym]
49
+ def copy direction, server_name, path, verbose: false
50
+ server = @config.servers.fetch(server_name.to_sym)
51
51
 
52
52
  uri = URI.parse("ssh://#{server.gateway}")
53
53
  port = uri.port ? "-p#{uri.port}" : ""
@@ -65,9 +65,9 @@ class Bard::CLI < Thor
65
65
  run_crucial command, verbose: verbose
66
66
  end
67
67
 
68
- def move from, to, path, verbose: false
69
- from = @config.servers[from.to_sym]
70
- to = @config.servers[to.to_sym]
68
+ def move from_name, to_name, path, verbose: false
69
+ from = @config.servers.fecth(from_name.to_sym)
70
+ to = @config.servers.fetch(to_name.to_sym)
71
71
  raise NotImplementedError if from.gateway || to.gateway || from.ssh_key || to.ssh_key
72
72
 
73
73
  from_uri = URI.parse("ssh://#{from.ssh}")
@@ -81,8 +81,8 @@ class Bard::CLI < Thor
81
81
  run_crucial command, verbose: verbose
82
82
  end
83
83
 
84
- def rsync direction, server, path, verbose: false
85
- server = @config.servers[server.to_sym]
84
+ def rsync direction, server_name, path, verbose: false
85
+ server = @config.servers.fetch(server_name.to_sym)
86
86
 
87
87
  uri = URI.parse("ssh://#{server.gateway}")
88
88
  port = uri.port ? "-p#{uri.port}" : ""
@@ -104,9 +104,9 @@ class Bard::CLI < Thor
104
104
  run_crucial command, verbose: verbose
105
105
  end
106
106
 
107
- def rsync_remote from, to, path, verbose: false
108
- from = @config.servers[from.to_sym]
109
- to = @config.servers[to.to_sym]
107
+ def rsync_remote from_name, to_name, path, verbose: false
108
+ from = @config.servers.fetch(from_name.to_sym)
109
+ to = @config.servers.fetch(to_name.to_sym)
110
110
  raise NotImplementedError if from.gateway || to.gateway || from.ssh_key || to.ssh_key
111
111
 
112
112
  dest_path = path.dup
@@ -1,7 +1,6 @@
1
1
  require "thor"
2
2
  require "time"
3
- require "json"
4
- require "net/http"
3
+ require "bard/github"
5
4
 
6
5
  class Bard::CLI < Thor
7
6
  class CI
@@ -53,31 +52,31 @@ class Bard::CLI < Thor
53
52
 
54
53
  class API < Struct.new(:project_name)
55
54
  def last_run
56
- response = client.get("runs", event: "workflow_dispatch", per_page: 1)
55
+ response = client.get("actions/runs", event: "workflow_dispatch", per_page: 1)
57
56
  if json = response["workflow_runs"][0]
58
57
  Run.new(self, json)
59
58
  end
60
59
  end
61
60
 
62
61
  def last_successful_run
63
- successful_runs = client.get("runs", event: "workflow_dispatch", status: "success", per_page: 1)
62
+ successful_runs = client.get("actions/runs", event: "workflow_dispatch", status: "success", per_page: 1)
64
63
  if json = successful_runs["workflow_runs"][0]
65
64
  Run.new(self, json)
66
65
  end
67
66
  end
68
67
 
69
68
  def find_run id
70
- json = client.get("runs/#{id}")
69
+ json = client.get("actions/runs/#{id}")
71
70
  Run.new(self, json)
72
71
  end
73
72
 
74
73
  def create_run! branch
75
74
  start_time = Time.now
76
- client.post("workflows/ci.yml/dispatches", ref: branch, inputs: { "git-ref": branch })
75
+ client.post("actions/workflows/ci.yml/dispatches", ref: branch, inputs: { "git-ref": branch })
77
76
  sha = `git rev-parse #{branch}`.chomp
78
77
 
79
78
  loop do
80
- runs = client.get("runs", head_sha: sha, created: ">#{start_time.iso8601}")
79
+ runs = client.get("actions/runs", head_sha: sha, created: ">#{start_time.iso8601}")
81
80
  if json = runs["workflow_runs"].first
82
81
  return Run.new(self, json)
83
82
  end
@@ -86,19 +85,19 @@ class Bard::CLI < Thor
86
85
  end
87
86
 
88
87
  def find_job_by_run_id run_id
89
- jobs = client.get("runs/#{run_id}/jobs", filter: "latest", per_page: 1)
88
+ jobs = client.get("actions/runs/#{run_id}/jobs", filter: "latest", per_page: 1)
90
89
  job_json = jobs["jobs"][0]
91
90
  Job.new(self, job_json)
92
91
  end
93
92
 
94
93
  def download_logs_by_job_id job_id
95
- client.get("jobs/#{job_id}/logs")
94
+ client.get("actions/jobs/#{job_id}/logs")
96
95
  end
97
96
 
98
97
  private
99
98
 
100
99
  def client
101
- @client ||= Client.new(project_name)
100
+ @client ||= Bard::Github.new(project_name)
102
101
  end
103
102
  end
104
103
 
@@ -171,62 +170,6 @@ class Bard::CLI < Thor
171
170
  @logs ||= api.download_logs_by_job_id(id)
172
171
  end
173
172
  end
174
-
175
- class Client < Struct.new(:project_name)
176
- def get path, params={}
177
- request(path) do |uri|
178
- uri.query = URI.encode_www_form(params)
179
- Net::HTTP::Get.new(uri)
180
- end
181
- end
182
-
183
- def post path, params={}
184
- request(path) do |uri|
185
- Net::HTTP::Post.new(uri).tap do |r|
186
- r.body = JSON.dump(params)
187
- end
188
- end
189
- end
190
-
191
- private
192
-
193
- def github_apikey
194
- @github_apikey ||= begin
195
- raw = `git ls-remote -t git@github.com:botandrose/bard`
196
- raw[/github-apikey\|(.+)$/, 1]
197
- end
198
- end
199
-
200
- def request path, &block
201
- uri = if path =~ /^http/
202
- URI(path)
203
- else
204
- URI("https://api.github.com/repos/botandrosedesign/#{project_name}/actions/#{path}")
205
- end
206
-
207
- req = nil
208
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
209
- req = block.call(uri)
210
- req["Accept"] = "application/vnd.github+json"
211
- req["Authorization"] = "Bearer #{github_apikey}"
212
- req["X-GitHub-Api-Version"] = "2022-11-28"
213
- http.request(req)
214
- end
215
-
216
- case response
217
- when Net::HTTPRedirection then
218
- Net::HTTP.get(URI(response["Location"]))
219
- when Net::HTTPSuccess then
220
- if response["Content-Type"].to_s.include?("/json")
221
- JSON.load(response.body)
222
- else
223
- response.body
224
- end
225
- else
226
- raise [req.method, req.uri, req.to_hash, response].inspect
227
- end
228
- end
229
- end
230
173
  end
231
174
  end
232
175
  end
data/lib/bard/config.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require "uri"
2
2
 
3
- class Bard::CLI < Thor
3
+ module Bard
4
4
  class Config
5
- def initialize project_name, path
5
+ def initialize project_name, path: nil, source: nil
6
6
  @project_name = project_name
7
7
  @servers = {
8
8
  local: Server.new(
@@ -39,7 +39,10 @@ class Bard::CLI < Thor
39
39
  "www@#{project_name}.botandrose.com:22022",
40
40
  ),
41
41
  }
42
- load_local_config! path
42
+ if path && File.exist?(path)
43
+ source = File.read(File.expand_path(path))
44
+ end
45
+ instance_eval source
43
46
  end
44
47
 
45
48
  attr_reader :servers
@@ -60,10 +63,6 @@ class Bard::CLI < Thor
60
63
 
61
64
  private
62
65
 
63
- def load_local_config! path
64
- instance_eval File.read(File.expand_path(path)) if File.exist?(path)
65
- end
66
-
67
66
  class Server < Struct.new(:project_name, :key, :ssh, :path, :ping, :gateway, :ssh_key, :env)
68
67
  def self.setting *fields
69
68
  fields.each do |field|
@@ -0,0 +1,67 @@
1
+ require "net/http"
2
+ require "json"
3
+ require "base64"
4
+
5
+ module Bard
6
+ class Github < Struct.new(:project_name)
7
+ def get path, params={}
8
+ request(path) do |uri|
9
+ uri.query = URI.encode_www_form(params)
10
+ Net::HTTP::Get.new(uri)
11
+ end
12
+ end
13
+
14
+ def post path, params={}
15
+ request(path) do |uri|
16
+ Net::HTTP::Post.new(uri).tap do |r|
17
+ r.body = JSON.dump(params)
18
+ end
19
+ end
20
+ end
21
+
22
+ def read_file path, branch: "master"
23
+ metadata = get("contents/#{path}", ref: branch)
24
+ Base64.decode64(metadata["content"])
25
+ end
26
+
27
+ private
28
+
29
+ def github_apikey
30
+ @github_apikey ||= begin
31
+ raw = `git ls-remote -t git@github.com:botandrose/bard`
32
+ raw[/github-apikey\|(.+)$/, 1]
33
+ end
34
+ end
35
+
36
+ def request path, &block
37
+ uri = if path =~ /^http/
38
+ URI(path)
39
+ else
40
+ URI("https://api.github.com/repos/botandrosedesign/#{project_name}/#{path}")
41
+ end
42
+
43
+ req = nil
44
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
45
+ req = block.call(uri)
46
+ req["Accept"] = "application/vnd.github+json"
47
+ req["Authorization"] = "Bearer #{github_apikey}"
48
+ req["X-GitHub-Api-Version"] = "2022-11-28"
49
+ http.request(req)
50
+ end
51
+
52
+ case response
53
+ when Net::HTTPRedirection then
54
+ Net::HTTP.get(URI(response["Location"]))
55
+ when Net::HTTPSuccess then
56
+ if response["Content-Type"].to_s.include?("/json")
57
+ JSON.load(response.body)
58
+ else
59
+ response.body
60
+ end
61
+ else
62
+ raise [req.method, req.uri, req.to_hash, response].inspect
63
+ end
64
+ end
65
+ end
66
+ end
67
+
data/lib/bard/ping.rb ADDED
@@ -0,0 +1,21 @@
1
+ module Bard
2
+ class Ping < Struct.new(:server)
3
+ def self.call server
4
+ new(server).call
5
+ end
6
+
7
+ def call
8
+ return true if server.ping == false
9
+
10
+ url = server.default_ping
11
+ if server.ping =~ %r{^/}
12
+ url += server.ping
13
+ elsif server.ping.to_s.length > 0
14
+ url = server.ping
15
+ end
16
+
17
+ command = "curl -sfL #{url} 2>&1 1>/dev/null"
18
+ system command
19
+ end
20
+ end
21
+ end
data/lib/bard/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Bard
2
- VERSION = "0.62.2"
2
+ VERSION = "0.64.0"
3
3
  end
4
4
 
data/lib/bard.rb CHANGED
@@ -4,7 +4,8 @@ require "bard/base"
4
4
  require "bard/git"
5
5
  require "bard/ci"
6
6
  require "bard/data"
7
-
7
+ require "bard/github"
8
+ require "bard/ping"
8
9
  require "bard/config"
9
10
 
10
11
  class Bard::CLI < Thor
@@ -12,7 +13,7 @@ class Bard::CLI < Thor
12
13
 
13
14
  def initialize(*args, **kwargs, &block)
14
15
  super
15
- @config = Config.new(project_name, "bard.rb")
16
+ @config = Bard::Config.new(project_name, path: "bard.rb")
16
17
  end
17
18
 
18
19
  desc "data --from=production --to=local", "copy database and assets from from to to"
@@ -207,24 +208,18 @@ class Bard::CLI < Thor
207
208
  desc "ping [SERVER=production]", "hits the server over http to verify that its up."
208
209
  def ping server=:production
209
210
  server = @config.servers[server.to_sym]
210
- return false if server.ping == false
211
-
212
- url = server.default_ping
213
- if server.ping =~ %r{^/}
214
- url += server.ping
215
- elsif server.ping.to_s.length > 0
216
- url = server.ping
217
- end
218
-
219
- command = "curl -sfL #{url} 2>&1 1>/dev/null"
220
- unless system command
211
+ unless Bard::Ping.call(server)
221
212
  puts "#{server.key.to_s.capitalize} is down!"
222
213
  exit 1
223
214
  end
224
215
  end
225
216
 
226
- desc "master_key [FROM=production, TO=local]", "copy master key from FROM to TO"
227
- def master_key from="production", to="local"
217
+ desc "master_key --from=production --to=local", "copy master key from from to to"
218
+ method_options %w[from] => :string, %w[to] => :string
219
+ def master_key
220
+ default_from = @config.servers.key?(:production) ? "production" : "staging"
221
+ from = options.fetch(:from, default_from)
222
+ to = options.fetch(:to, "local")
228
223
  if to == "local"
229
224
  copy :from, from, "config/master.key"
230
225
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.62.2
4
+ version: 0.64.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-03 00:00:00.000000000 Z
11
+ date: 2024-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -155,6 +155,8 @@ files:
155
155
  - lib/bard/config.rb
156
156
  - lib/bard/data.rb
157
157
  - lib/bard/git.rb
158
+ - lib/bard/github.rb
159
+ - lib/bard/ping.rb
158
160
  - lib/bard/version.rb
159
161
  - spec/bard/ci/github_actions_spec.rb
160
162
  - spec/bard_spec.rb