@@ -15,6 +15,74 @@ def initialize(api_key: ENV.fetch('HEROKU_API_KEY', ''),
15
15
private :api_key
16
16
private :api_host
17
17
18
+ def wait ( heroku_app , timeout : 120 )
19
+ c = 0
20
+
21
+ loop do
22
+ return true if get ( "/apps/#{ heroku_app } " )
23
+ return false if c >= timeout
24
+
25
+ sleep 10
26
+ c += 10
27
+ end
28
+
29
+ true
30
+ end
31
+
32
+ def deploy ( repo_slug , heroku_app , version )
33
+ warn "\n Deploying #{ repo_slug } #{ version } to #{ heroku_app } "
34
+
35
+ response = post (
36
+ JSON . generate (
37
+ 'source_blob' => {
38
+ 'url' => "https://github.com/#{ repo_slug } /archive/#{ version } .tar.gz" ,
39
+ 'version' => version
40
+ }
41
+ ) ,
42
+ "/apps/#{ heroku_app } /builds"
43
+ )
44
+
45
+ raise 'Could not request a deployment' if response . nil?
46
+
47
+ warn "\n Streaming deploy output"
48
+
49
+ raise 'Could not stream deployment output' unless stream ( URI ( response . fetch ( 'output_stream_url' ) ) )
50
+ end
51
+
52
+ def scale ( heroku_app , ps_scales )
53
+ Array ( ps_scales ) . each do |ps_scale |
54
+ formation = parse_formation ( ps_scale )
55
+ response = patch (
56
+ JSON . generate (
57
+ 'quantity' => formation . fetch ( :qty ) ,
58
+ 'size' => formation . fetch ( :size )
59
+ ) ,
60
+ "/apps/#{ heroku_app } /formation/#{ formation . fetch ( :type ) } "
61
+ )
62
+ raise "Could not scale #{ heroku_app } #{ ps_scales . inspect } " if response . nil?
63
+
64
+ warn "---> scaled #{ heroku_app } #{ ps_scale } :"
65
+ warn JSON . pretty_generate ( response )
66
+ end
67
+ end
68
+
69
+ def parse_formation ( ps_scale )
70
+ ret = {
71
+ type : '' ,
72
+ qty : 0 ,
73
+ size : ''
74
+ }
75
+
76
+ parts = ps_scale . split ( '=' , 2 )
77
+ ret [ :type ] = parts . fetch ( 0 )
78
+
79
+ subparts = parts . fetch ( 1 ) . split ( ':' , 2 )
80
+ ret [ :qty ] = Integer ( subparts . fetch ( 0 ) )
81
+ ret [ :size ] = subparts . fetch ( 1 ) . strip
82
+
83
+ ret
84
+ end
85
+
18
86
def get ( path )
19
87
request ( Net ::HTTP ::Get . new ( path ) )
20
88
end
0 commit comments