slugbuilder 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bab77b33178bb303e46f50fc4914d07ab4777802
4
+ data.tar.gz: 8d9ed29a4d5b50938081f986ad4796511c6f9ce7
5
+ SHA512:
6
+ metadata.gz: 5e6d9b29d4eaf4cb7bf8b09621a0b3d4cf713237796efc8e6e70afd7f2f9f1399dae3f9677617a7b2cb6b7c5b085f81bbccb49eea65627977842b88b07187be5
7
+ data.tar.gz: 65b4d59438e84bfa00da8af5434fe8322611778d4c2d755356e456861de0e2be17225606016a0a76900c4657b4253141170585291e75f0616372010b7c4bad3a
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0 (2017-1-18)
4
+
5
+ Initial release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jonathan.lehman91@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Panoply
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,199 @@
1
+ # Slugbuilder
2
+
3
+ Slugbuilder is a Ruby gem to build [Heroku](https://www.heroku.com/)-like [slugs](https://devcenter.heroku.com/articles/platform-api-deploying-slugs).
4
+
5
+ It runs Heroku [buildpacks](https://devcenter.heroku.com/articles/buildpacks) on an application and builds a [slug](https://devcenter.heroku.com/articles/slug-compiler), which is essentially a `tar` file that can run on services like Heroku, [lxfontes/slugrunner-rb](https://github.com/lxfontes/slugrunner-rb), [deis/slugrunner](https://github.com/deis/slugrunner), and the like.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'slugbuilder'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install slugbuilder
22
+
23
+ ## Usage
24
+
25
+ ### Basic Usage
26
+
27
+ ```ruby
28
+ # basic
29
+ sb = Slugbuilder::Builder.new(repo: 'heroku/node-js-sample', git_ref: 'master')
30
+ sb.build # builds the slug `heroku.node-js-sample.master.tgz` in the current directory
31
+ ```
32
+
33
+ ### Override Slug Name
34
+
35
+ ```ruby
36
+ # with environment variables
37
+ sb = Slugbuilder::Builder.new(repo: 'heroku/node-js-sample', git_ref: 'master')
38
+ sb.build(slug_name: 'my_slug') # builds slug to `my_slug.tgz'
39
+ ```
40
+
41
+ ### Setting Build Environment
42
+
43
+ ```ruby
44
+ # with environment variables
45
+ sb = Slugbuilder::Builder.new(repo: 'heroku/node-js-sample', git_ref: 'master')
46
+ sb.build(env: {NODE_ENV: 'production', SETTING: 'something'})
47
+ ```
48
+
49
+ ### Build without Cache
50
+
51
+ ```ruby
52
+ # clear cache
53
+ sb = Slugbuilder::Builder.new(repo: 'heroku/node-js-sample', git_ref: 'master')
54
+ sb.build(clear_cache: true)
55
+ ```
56
+
57
+ ### Prebuild/Postbuild Hooks
58
+
59
+ ```ruby
60
+ # prebuild/postbuild
61
+ # using a Proc or Proc-like object (responds to `call` method)
62
+ sb = Slugbuilder::Builder.new(repo: 'heroku/node-js-sample', git_ref: 'master')
63
+ class PostBuildInterface
64
+ def self.call(repo:, git_ref:, stats:, slug:)
65
+ # postbuild logic
66
+ end
67
+ end
68
+ sb.build(prebuild: ->(repo: repo, git_ref: git_ref) { p "prebuild logic" }, postbuild: PostBuildInterface)
69
+
70
+ # prebuild/postbuild with optional blocks
71
+ sb = Slugbuilder::Builder.new(repo: 'heroku/node-js-sample', git_ref: 'master') do |args|
72
+ # prebuild logic
73
+ p args[:repo]
74
+ end
75
+ sb.build(env: {}) do |args|
76
+ # postbuild logic
77
+ p args[:slug]
78
+ end
79
+ ```
80
+
81
+ ## API
82
+
83
+ ### Builder#initialize(repo:, git_ref:, &block)
84
+
85
+ - `repo` String (required): the github repo in the form `<organization>/<repository>`
86
+ - `git_ref` String (required): the SHA or branch to build
87
+ - `stdout` IO (optional): the IO stream to write build output to. This defaults to `$stdout`
88
+ - `block` Block (optional): an optional block that runs pre-build. It receives a Hash with the structure:
89
+ - `repo` String: The git repo identifier
90
+ - `git_ref` String: The git branchname or SHA
91
+
92
+ Alternatively, a Proc can be passed to `build` method's keyword argument `prebuild` to achieve the same effect.
93
+
94
+ ### Builder#build(slug_name: nil, clear_cache: false, env: {}, prebuild: nil, postbuild: nil, &block)
95
+
96
+ `build` builds the slug and writes build information to `STDOUT`.
97
+
98
+ - `slug_name` String (optional): Override default name of slug (repo.git_ref.git_sha.tgz with the `/` in repo replaced by `.`)
99
+ - `clear_cache` Boolean (optional): destroys the cache before building when true
100
+ - `env` Hash (optional): an optional hash of environment variables
101
+ - `prebuild` Proc (optional): an optional Proc (or anything that conforms to the `call` API of a Proc) that will be run before the build. The Proc will receive a Hash with the structure:
102
+ - `repo` String: The git repo identifier
103
+ - `git_ref` String: The git branchname or SHA
104
+ Alternatively, a block can be passed to the `initialize` method to the same effect.
105
+ - `postbuild` Proc (optional): an optional Proc (or anything that conforms to the `call` API of a Proc) that will run post-build. The Proc will receive a Hash with the structure:
106
+ - `slug` String: Location of the built slug file
107
+ - `repo` String: The git repo identifier
108
+ - `git_ref` String: The git branchname or SHA
109
+ - `git_sha` String: The git SHA (even if the git ref was a branch name)
110
+ - `stats` Hash:
111
+ - setup `Float`: Amount of time spent in setup
112
+ - build `Float`: Total amount of time spent in build (compile/build/slug)
113
+ - compile `Float`: Amount of time spent in buildpack compilation
114
+ - slug `Float`: Amount of time compressing the slug
115
+ - output `String`: Build output to STDOUT
116
+
117
+ Alternatively, a block can be passed to this method to the same effect. (see below)
118
+ - `block` Block (optional): an optional block that can be used as an alternative to the `postbuild` Proc argument. This receives the same arguments as `postbuild` (see above)
119
+
120
+ ## Configuration
121
+
122
+ Configuration settings can be modified within the `Slugbuilder.configure` block. Or set directly off of `Slugbuilder.config`
123
+
124
+ ```ruby
125
+ Slugbuilder.configure do |config|
126
+ config.base_dir = '/tmp/slugbuilder'
127
+ config.cache_dir = '/tmp/slugbuilder-cache'
128
+ config.output_dir = './slugs'
129
+ end
130
+
131
+ Slugbuilder.config.base_dir = '/tmp/slugbuilder'
132
+ Slugbuilder.config.cache_dir = '/tmp/slugbuilder-cache'
133
+ Slugbuilder.config.output_dir = './slugs'
134
+ ```
135
+
136
+ ### Options
137
+ ```ruby
138
+ @base_dir = '/tmp/slugbuilder'
139
+ @cache_dir = '/tmp/slugbuilder-cache'
140
+ @output_dir = './slugs'
141
+ @git_service = 'github.com'
142
+ @buildpacks = [
143
+ 'https://github.com/heroku/heroku-buildpack-nodejs.git',
144
+ 'https://github.com/heroku/heroku-buildpack-ruby.git#37ed188'
145
+ ]
146
+ ```
147
+
148
+ **base_dir**
149
+
150
+ This is the base directory that builds and apps are stored in.
151
+
152
+ > Defaults to `/tmp/slugbuilder`
153
+
154
+ **cache_dir**
155
+
156
+ This is the directory where the cache lives.
157
+
158
+ > Defaults to `/tmp/slugbuilder-cache`
159
+
160
+ **output_dir**
161
+
162
+ This is where slug files are built to.
163
+
164
+ > Defaults to `.` (the current directory)
165
+
166
+ **git_service**
167
+
168
+ This is where the git repositories live (github.com, gitlab.com, bitbucket.org, etc)
169
+
170
+ > Defaults to `github.com`
171
+
172
+ **buildpacks**
173
+
174
+ Buildpacks is an array of valid git clone-able [buildpack](https://devcenter.heroku.com/articles/buildpacks) URLs.
175
+
176
+ > Defaults to []
177
+
178
+ ## Development
179
+
180
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
181
+
182
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
183
+
184
+ ## Contributing
185
+
186
+ Bug reports and pull requests are welcome on GitHub at https://github.com/panoplymedia/slugbuilder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
187
+
188
+
189
+ ## License
190
+
191
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
192
+
193
+ ## Motivation and Thanks
194
+
195
+ This project is heavily based on [lxfontes/slugbuilder](https://github.com/lxfontes/slugbuilder) and was inspired by projects like:
196
+
197
+ - [herokuish](https://github.com/gliderlabs/herokuish)
198
+ - [deis/slugbuilder](https://github.com/deis/slugbuilder)
199
+ - [dokku](https://github.com/dokku/dokku)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'slugbuilder'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require 'slugbuilder/version'
2
+ require 'slugbuilder/configuration'
3
+ require 'slugbuilder/builder'
4
+
5
+ module Slugbuilder
6
+ end
@@ -0,0 +1,285 @@
1
+ require 'securerandom'
2
+ require 'shellwords'
3
+ require 'yaml'
4
+ require 'fileutils'
5
+
6
+ module Slugbuilder
7
+ class Builder
8
+ def initialize(repo:, git_ref:, stdout: $stdout)
9
+ @stdout = stdout
10
+ @base_dir = Slugbuilder.config.base_dir
11
+ @cache_dir = Shellwords.escape(Slugbuilder.config.cache_dir)
12
+ @output_dir = Slugbuilder.config.output_dir
13
+ @buildpacks_dir = File.join(@cache_dir, 'buildpacks')
14
+ @repo = repo
15
+ @git_ref = git_ref
16
+ @git_dir = Shellwords.escape(File.join(@base_dir, 'git', repo))
17
+ @build_dir = Shellwords.escape(File.join(@base_dir, repo, git_ref))
18
+
19
+ setup
20
+
21
+ if block_given?
22
+ yield(repo: repo, git_ref: git_ref)
23
+ end
24
+ end
25
+
26
+ def build(clear_cache: false, env: {}, prebuild: nil, postbuild: nil, slug_name: nil)
27
+ @env = env
28
+ @slug_file = slug_name ? "#{slug_name}.tgz" : Shellwords.escape("#{@repo.gsub('/', '.')}.#{@git_ref}.#{@git_sha}.tgz")
29
+ wipe_cache if clear_cache
30
+
31
+ prebuild.call(repo: @repo, git_ref: @git_ref) if prebuild
32
+
33
+ build_and_release
34
+ stitle("Setup completed in #{@setup_time} seconds")
35
+ stitle("Build completed in #{@build_time} seconds")
36
+ stext("Application compiled in #{@compile_time} seconds")
37
+ stext("Slug compressed in #{@slug_time} seconds")
38
+ stitle("Slug built to #{File.join(@output_dir, @slug_file)}")
39
+ stats = {
40
+ setup: @setup_time,
41
+ build: @build_time,
42
+ compile: @compile_time,
43
+ slug: @slug_time,
44
+ output: build_output.join('')
45
+ }
46
+
47
+ postbuild.call(repo: @repo, git_ref: @git_ref, git_sha: @git_sha, stats: stats, slug: File.join(@output_dir, @slug_file)) if postbuild
48
+ if block_given?
49
+ yield(repo: @repo, git_ref: @git_ref, git_sha: @git_sha, stats: stats, slug: File.join(@output_dir, @slug_file))
50
+ end
51
+ return true
52
+ rescue => e
53
+ stitle("Failed to create slug: #{e}")
54
+ return false
55
+ end
56
+
57
+
58
+ private
59
+
60
+ def wipe_cache
61
+ FileUtils.rm_rf(@cache_dir)
62
+ end
63
+
64
+ def build_and_release
65
+ @build_time = realtime do
66
+ set_environment
67
+ buildpacks = fetch_buildpacks
68
+ run_buildpacks(buildpacks)
69
+ @slug_time = realtime { build_slug }
70
+ slug_size
71
+ print_workers
72
+ end
73
+ end
74
+
75
+ def setup
76
+ @setup_time = realtime do
77
+ create_dirs
78
+ download_repo unless Dir.exist?(@git_dir)
79
+ checkout_git_ref
80
+
81
+ stitle("Saving application to #{@build_dir}")
82
+ copy_app
83
+ end
84
+ end
85
+
86
+ def slug_size
87
+ @slug_size = File.size(@slug_file) / 1024 / 1024
88
+ stitle("Slug size is #{@slug_size} Megabytes.")
89
+ end
90
+
91
+ def set_environment
92
+ load_env_file("#{@cache_dir}/env")
93
+ load_env_file("#{@build_dir}/.env")
94
+ ENV['STACK'] = 'cedar-14'
95
+
96
+ @env.each do |k, v|
97
+ ENV[k.to_s] = v.to_s
98
+ end
99
+
100
+ ENV['HOME'] = @build_dir
101
+ ENV['APP_DIR'] = @build_dir
102
+
103
+ stitle('Build environment')
104
+ ENV.each do |k, v|
105
+ stext("#{k}=#{v}")
106
+ end
107
+ end
108
+
109
+ def create_dirs
110
+ FileUtils.mkdir_p(@base_dir)
111
+ FileUtils.mkdir_p(File.join(@cache_dir, 'buildpacks'))
112
+ FileUtils.mkdir_p(@output_dir)
113
+ # clear old build
114
+ FileUtils.rm_rf(@build_dir)
115
+ FileUtils.mkdir_p(File.join(@build_dir, '.profile.d'))
116
+ end
117
+
118
+ def checkout_git_ref
119
+ Dir.chdir(@git_dir) do
120
+ # checkout branch or sha
121
+ # get branch from origin so it is always the most recent
122
+ rc = run("git fetch --all && (git checkout origin/#{@git_ref} || git checkout #{@git_ref}) 2>&1")
123
+ fail "Failed to fetch and checkout: #{@git_ref}" if rc != 0
124
+ @git_sha = `git rev-parse HEAD`.strip
125
+ end
126
+ end
127
+
128
+ def download_repo
129
+ stitle("Fetching #{@repo}")
130
+ rc = run("git clone git@#{Slugbuilder.config.git_service}:#{@repo}.git #{@git_dir} 2>&1")
131
+ fail "Failed to download repo: #{@repo}" if rc != 0
132
+ end
133
+
134
+ def copy_app
135
+ # copy dotfiles but not .git, ., or ..
136
+ files = Dir.glob("#{@git_dir}/**", File::FNM_DOTMATCH).reject { |file| file =~ /\.git|\.$|\.\.$/ }
137
+ FileUtils.cp_r(files, @build_dir)
138
+ end
139
+
140
+ def get_buildpack_name(url)
141
+ url.match(/.+\/(.+?)\.git$/)[1]
142
+ end
143
+
144
+ def fetch_buildpacks
145
+ buildpacks = Slugbuilder.config.buildpacks
146
+ buildpacks << Shellwords.escape(@env['BUILDPACK_URL']) if @env.key?('BUILDPACK_URL')
147
+ fail 'Could not detect buildpack' if buildpacks.size.zero?
148
+
149
+ existing_buildpacks = Dir.entries(@buildpacks_dir)
150
+ buildpacks.each do |buildpack_url|
151
+ buildpack_name = get_buildpack_name(buildpack_url)
152
+ if !existing_buildpacks.include?(buildpack_name)
153
+ # download buildpack
154
+ stitle("Fetching buildpack: #{buildpack_name}")
155
+ rc = run("git clone --depth=1 #{buildpack_url} #{@buildpacks_dir}/#{buildpack_name} 2>&1")
156
+ fail "Failed to download buildpack: #{buildpack_name}" if rc != 0
157
+ else
158
+ # fetch latest
159
+ stitle("Using cached buildpack. Ensuring latest version of buildpack: #{buildpack_name}")
160
+ Dir.chdir("#{@buildpacks_dir}/#{buildpack_name}") do
161
+ rc = run('git pull 2>&1')
162
+ fail "Failed to update: #{buildpack_name}" if rc != 0
163
+ end
164
+ end
165
+ end
166
+
167
+ buildpacks
168
+ end
169
+
170
+ def run_buildpacks(buildpacks)
171
+ @compile_time = 0
172
+
173
+ buildpacks.each do |buildpack_url|
174
+ buildpack_name = get_buildpack_name(buildpack_url)
175
+ buildpack = File.join(@buildpacks_dir, buildpack_name)
176
+ if run("#{buildpack}/bin/detect #{@build_dir}") == 0
177
+ @compile_time += realtime { compile(buildpack) }
178
+ release(buildpack)
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ def compile(buildpack)
185
+ rc = run_echo("#{buildpack}/bin/compile '#{@build_dir}' '#{@cache_dir}'")
186
+ fail "Couldn't compile application using buildpack #{buildpack}" if rc != 0
187
+ end
188
+
189
+ def release(buildpack)
190
+ # should create .release
191
+ release_file = File.open("#{@build_dir}/.release", 'w')
192
+ rc = run("#{buildpack}/bin/release '#{@build_dir}' '#{@cache_dir}'") do |line|
193
+ release_file.print(line)
194
+ end
195
+ release_file.close
196
+
197
+ fail "Couldn't compile application using buildpack #{buildpack}" if rc != 0
198
+ end
199
+
200
+ def build_slug
201
+ rc = 1
202
+ # use pigz if available
203
+ compression = `which pigz` != '' ? '--use-compress-program=pigz' : ''
204
+ if File.exists?("#{@build_dir}/.slugignore")
205
+ rc = run_echo("tar --exclude='.git' #{compression} -X #{@build_dir}/.slugignore -C #{@build_dir} -cf #{File.join(@output_dir, @slug_file)} .")
206
+ else
207
+ rc = run_echo("tar --exclude='.git' #{compression} -C #{@build_dir} -cf #{File.join(@output_dir, @slug_file)} .")
208
+ end
209
+ fail "Couldn't create slugfile" if rc != 0
210
+ end
211
+
212
+ def slug_size
213
+ @slug_size = File.size(File.join(@output_dir, @slug_file)) / 1024 / 1024
214
+ stitle("Slug size is #{@slug_size} Megabytes.")
215
+ end
216
+
217
+ def print_workers
218
+ workers = {}
219
+ if File.exists?("#{@build_dir}/Procfile")
220
+ procfile = YAML.load_file("#{@build_dir}/Procfile")
221
+ workers.merge!(procfile)
222
+ end
223
+
224
+ if File.exists?("#{@build_dir}/.release")
225
+ procfile = YAML.load_file("#{@build_dir}/.release")
226
+ workers.merge!(procfile['default_process_types']) if procfile.key?('default_process_types')
227
+ end
228
+
229
+ stitle("Process Types: #{workers.keys.join(', ')}")
230
+ end
231
+
232
+ def build_output
233
+ @build_output ||= []
234
+ end
235
+
236
+ def stitle(line)
237
+ build_output << "-----> #{line}\n"
238
+ @stdout.puts("-----> #{line}")
239
+ end
240
+
241
+ def stext(line)
242
+ build_output << " #{line}\n"
243
+ @stdout.puts(" #{line}")
244
+ end
245
+
246
+ def realtime
247
+ t0 = Time.now
248
+ yield
249
+ ((Time.now - t0).to_i * 100) / 100.0
250
+ end
251
+
252
+ def run(cmd)
253
+ IO.popen(cmd) do |io|
254
+ until io.eof?
255
+ data = io.gets
256
+ yield data if block_given?
257
+ end
258
+ end
259
+ $?.exitstatus
260
+ end
261
+
262
+ def run_echo(cmd)
263
+ run(cmd) do |line|
264
+ build_output << line
265
+ @stdout.print(line)
266
+ end
267
+ end
268
+
269
+ def load_env_file(file)
270
+ if File.exists?(file)
271
+ new_envs = IO.readlines(file)
272
+ new_envs.each do |line|
273
+ line.strip!
274
+ next if line.match(/^#/)
275
+
276
+ parts = line.split(/=/, 2)
277
+ next if parts.length != 2
278
+
279
+ ENV[parts[0]] = parts[1]
280
+ @env[parts[0]] = parts[1]
281
+ end
282
+ end
283
+ end
284
+ end
285
+ end
@@ -0,0 +1,30 @@
1
+ module Slugbuilder
2
+ class << self
3
+ attr_accessor :config
4
+ end
5
+
6
+ def self.config
7
+ @config ||= Configuration.new
8
+ end
9
+
10
+ def self.reset
11
+ @config = Configuration.new
12
+ end
13
+
14
+ def self.configure
15
+ yield(config)
16
+ end
17
+
18
+ class Configuration
19
+ attr_accessor :base_dir, :cache_dir, :output_dir,
20
+ :git_service, :buildpacks
21
+
22
+ def initialize
23
+ @base_dir = '/tmp/slugbuilder'
24
+ @cache_dir = '/tmp/slugbuilder-cache'
25
+ @output_dir = '.'
26
+ @git_service = 'github.com'
27
+ @buildpacks = []
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Slugbuilder
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slugbuilder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'slugbuilder'
8
+ spec.version = Slugbuilder::VERSION
9
+ spec.authors = ['Panoply Dev']
10
+ spec.email = ['dev@panoply.fm']
11
+
12
+ spec.summary = %q{Build Heroku-like slugs}
13
+ spec.homepage = 'https://github.com/panoplymedia/slugbuilder'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.13'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
26
+ spec.add_development_dependency 'pry'
27
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slugbuilder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Panoply Dev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - dev@panoply.fm
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CHANGELOG.md
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/slugbuilder.rb
88
+ - lib/slugbuilder/builder.rb
89
+ - lib/slugbuilder/configuration.rb
90
+ - lib/slugbuilder/version.rb
91
+ - slugbuilder.gemspec
92
+ homepage: https://github.com/panoplymedia/slugbuilder
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Build Heroku-like slugs
116
+ test_files: []