bard 0.63.0 → 0.64.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bard/ci/github_actions.rb +9 -66
- data/lib/bard/config.rb +26 -13
- data/lib/bard/data.rb +1 -1
- data/lib/bard/github.rb +67 -0
- data/lib/bard/ping.rb +34 -0
- data/lib/bard/version.rb +1 -1
- data/lib/bard.rb +4 -13
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c9d061d17e01a9c194b56cbc5c86ed626e390048c939453f6af87a7b9a136f24
|
4
|
+
data.tar.gz: 89cbc27d550599e4a8c2eb5287969d19dafdb2176f37d0f65c606966d77542a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 468913ca2b377d72a317fa44c73a7602445f4a9479d260326be1044598feb727aa13a2d35bebd65d06120eb50ade5960e767ff292f664e602ad394f96a996130
|
7
|
+
data.tar.gz: d322765c29287237823708b6cb71887d8662445826e61d726c0acfd819526062ffec20a5e79a33f404f959969d8b45ecc9d70f21506f2f9312027c16eab85111
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require "thor"
|
2
2
|
require "time"
|
3
|
-
require "
|
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 ||=
|
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
|
-
|
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
|
-
|
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|
|
@@ -81,14 +80,28 @@ class Bard::CLI < Thor
|
|
81
80
|
|
82
81
|
setting :ssh, :path, :ping, :gateway, :ssh_key, :env
|
83
82
|
|
84
|
-
def
|
85
|
-
|
86
|
-
|
83
|
+
def ping(*args)
|
84
|
+
if args.length == 1
|
85
|
+
self.ping = args.first
|
86
|
+
elsif args.length == 0
|
87
|
+
normalize_ping(super())
|
88
|
+
else
|
89
|
+
raise ArgumentError
|
90
|
+
end
|
87
91
|
end
|
88
92
|
|
89
|
-
def
|
90
|
-
|
91
|
-
|
93
|
+
private def normalize_ping value
|
94
|
+
return value if value == false
|
95
|
+
uri = URI.parse("ssh://#{ssh}")
|
96
|
+
normalized = "https://#{uri.host}" # default if none specified
|
97
|
+
if value =~ %r{^/}
|
98
|
+
normalized += value
|
99
|
+
elsif value.to_s.length > 0
|
100
|
+
normalized = value
|
101
|
+
end
|
102
|
+
if normalized !~ /^http/
|
103
|
+
normalized = "https://#{normalized}"
|
104
|
+
end
|
92
105
|
normalized
|
93
106
|
end
|
94
107
|
|
data/lib/bard/data.rb
CHANGED
@@ -3,7 +3,7 @@ class Bard::CLI < Thor
|
|
3
3
|
def call
|
4
4
|
if to == "production"
|
5
5
|
server = bard.instance_variable_get(:@config).servers[to.to_sym]
|
6
|
-
url = server.
|
6
|
+
url = server.ping
|
7
7
|
puts bard.yellow("WARNING: You are about to push data to production, overwriting everything that is there!")
|
8
8
|
answer = bard.ask("If you really want to do this, please type in the full HTTPS url of the production server:")
|
9
9
|
if answer != url
|
data/lib/bard/github.rb
ADDED
@@ -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,34 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "uri"
|
3
|
+
|
4
|
+
module Bard
|
5
|
+
class Ping < Struct.new(:server)
|
6
|
+
def self.call server
|
7
|
+
new(server).call
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
return true if server.ping == false
|
12
|
+
response = get_response_with_redirect(server.ping)
|
13
|
+
response.is_a?(Net::HTTPSuccess)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def get_response_with_redirect uri_str, limit=5
|
19
|
+
response = Net::HTTP.get_response(URI(uri_str))
|
20
|
+
|
21
|
+
case response
|
22
|
+
when Net::HTTPRedirection
|
23
|
+
if limit == 0
|
24
|
+
puts "too many HTTP redirects"
|
25
|
+
response
|
26
|
+
else
|
27
|
+
get_response_with_redirect(response["location"], limit - 1)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
response
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/bard/version.rb
CHANGED
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,17 +208,7 @@ 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
|
-
|
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
|
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.
|
4
|
+
version: 0.64.1
|
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-
|
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
|