factor-connector-heroku 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/factor/connector/heroku.rb +115 -0
  2. metadata +158 -0
@@ -0,0 +1,115 @@
1
+ require 'factor-connector-api'
2
+ require 'json'
3
+ require 'rest-client'
4
+ require 'heroku-api'
5
+ require 'anvil'
6
+ require 'anvil/engine'
7
+ require 'uri'
8
+ require 'zip'
9
+ require 'zip/zip'
10
+ require 'zip/zipfilesystem'
11
+ require 'tmpdir'
12
+ require 'oauth2'
13
+ require 'open-uri'
14
+ require 'tempfile'
15
+
16
+ Factor::Connector.service 'heroku' do
17
+ action 'deploy' do |params|
18
+ app = params['app']
19
+ content = params['content']
20
+ user = 'factorbot'
21
+ api_key = params['api_key']
22
+
23
+ fail 'No content specified. What am I supposed to deploy?' unless content
24
+ fail 'No app specified. Where am I supposed to deploy it?' unless app
25
+ fail 'You need to activate Heroku on the services page' unless api_key
26
+
27
+ def release(api_key, app, description, slug_url)
28
+ payload = {
29
+ description: description,
30
+ slug_url: slug_url
31
+ }
32
+
33
+ options = {
34
+ 'User-Agent' => 'heroku-push-cli/0.7-ALPHA',
35
+ 'X-Ruby-Version' => RUBY_VERSION,
36
+ 'X-Ruby-Platform' => RUBY_PLATFORM,
37
+ :content_type => :json,
38
+ :accept => :json
39
+ }
40
+
41
+ release_uri = "https://:#{api_key}@cisaurus.heroku.com/v1/apps/#{app}/release"
42
+
43
+ response = RestClient.post release_uri, JSON.generate(payload), options
44
+ while response.code == 202
45
+ release_status_uri = "https://:#{api_key}@cisaurus.heroku.com/#{response.headers[:location]}"
46
+ response = RestClient.get release_status_uri, options
47
+ sleep(1)
48
+ end
49
+ JSON.parse response
50
+ end
51
+
52
+ info 'Getting the resource files from previous step'
53
+ begin
54
+ source = Tempfile.new('source')
55
+ source.write open(content).read
56
+ source.rewind
57
+ rescue => ex
58
+ fail 'Internal error: failed to get resource files', exception: ex
59
+ end
60
+
61
+ info 'Unzipping file'
62
+ begin
63
+ temp_dir = Dir.mktmpdir
64
+ Zip::ZipFile.open(source) do |zip_file|
65
+ zip_file.each do |f|
66
+ f_path = File.join(temp_dir, f.name)
67
+ FileUtils.mkdir_p(File.dirname(f_path))
68
+ zip_file.extract(f, f_path) unless File.exist?(f_path)
69
+ end
70
+ end
71
+
72
+ if File.directory?(temp_dir)
73
+ dir_list = Dir.glob("#{temp_dir}/*")
74
+ temp_dir = dir_list[0] if dir_list.length == 1
75
+ end
76
+ rescue => ex
77
+ fail 'Unzip failed', exception: ex
78
+ end
79
+
80
+ info "Building the buildpack using Heroku's Anvil service"
81
+ begin
82
+ Anvil.append_agent '(heroku-push)'
83
+ Anvil.headers['X-Heroku-App'] = app
84
+
85
+ begin
86
+ capture = StringIO.new
87
+ old_std = $stdout
88
+ $stdout = capture
89
+
90
+ slug_url = Anvil::Engine.build(temp_dir)
91
+ begin
92
+ $stdout.string.split("\n").each do |line|
93
+ info line
94
+ end
95
+ rescue
96
+ warn "Build completed but couldn't pull up results of build."
97
+ end
98
+ ensure
99
+ $stdout = old_std
100
+ end
101
+ rescue => ex
102
+ fail 'Build failed', exception:ex
103
+ end
104
+
105
+ info 'Pushing buildpack to Heroku'
106
+ begin
107
+ release = release(api_key, app, "Pushed by #{user}", slug_url)
108
+ info "Release `#{release['release']}` complete"
109
+
110
+ action_callback release
111
+ rescue => ex
112
+ fail "Upload failed: #{ex.message}", exception: ex
113
+ end
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factor-connector-heroku
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Maciej Skierkowski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-09-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: heroku-anvil-factor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.15.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.15.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: heroku-api
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.3.15
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.3.15
46
+ - !ruby/object:Gem::Dependency
47
+ name: rest-client
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.6.7
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.6.7
62
+ - !ruby/object:Gem::Dependency
63
+ name: rubyzip
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.9
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '='
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.9
78
+ - !ruby/object:Gem::Dependency
79
+ name: oauth2
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - '='
84
+ - !ruby/object:Gem::Version
85
+ version: 0.9.3
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - '='
92
+ - !ruby/object:Gem::Version
93
+ version: 0.9.3
94
+ - !ruby/object:Gem::Dependency
95
+ name: json
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - '='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.8.1
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - '='
108
+ - !ruby/object:Gem::Version
109
+ version: 1.8.1
110
+ - !ruby/object:Gem::Dependency
111
+ name: factor-connector-api
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.0.1
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 0.0.1
126
+ description:
127
+ email:
128
+ - maciej@factor.io
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - lib/factor/connector/heroku.rb
134
+ homepage: https://factor.io
135
+ licenses: []
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 1.8.25
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: Heroku Factor.io Connector
158
+ test_files: []