argus-builder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.dockerignore +5 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Dockerfile +25 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +183 -0
- data/Rakefile +2 -0
- data/argus.gemspec +27 -0
- data/bin/argus-send +25 -0
- data/bin/argus-worker +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/argus.rb +10 -0
- data/lib/argus/docker.rb +70 -0
- data/lib/argus/git.rb +59 -0
- data/lib/argus/runner.rb +71 -0
- data/lib/argus/slack.rb +29 -0
- data/lib/argus/version.rb +3 -0
- data/lib/argus/worker.rb +19 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4dcaaed12b45f52b1547d810cd1a87a1f4611b59
|
4
|
+
data.tar.gz: 41dd4d8362911253e86857d565336808cdb4a830
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b95253f269aba1667ced23154968a1400c4b91400d6c6f34f398d32f7e22bbb8c94fa5a25d644dcdea5ff0aaef48232d717d1673f044623ab25cc6df9ef7fe4a
|
7
|
+
data.tar.gz: 95b2df158dd4c45b283be922fd6201e379bbfe18aa9f33104a03f1c34dc9470423d32662e1772d2394382c0a966ac57d0da0521adac4432333ee43c40edf7198
|
data/.dockerignore
ADDED
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at rlister+gh@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Dockerfile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
FROM alpine:3.3
|
2
|
+
|
3
|
+
MAINTAINER rlister@gmail.com
|
4
|
+
|
5
|
+
RUN apk -U upgrade && \
|
6
|
+
apk add -U bash build-base ca-certificates git openssh ruby ruby-dev ruby-io-console && \
|
7
|
+
rm -rf /var/cache/apk/*
|
8
|
+
|
9
|
+
## ensure bundler is up to date
|
10
|
+
RUN gem install bundler -v '>= 1.9.1' --no-rdoc --no-ri
|
11
|
+
|
12
|
+
WORKDIR /app
|
13
|
+
|
14
|
+
## minimum required to bundle
|
15
|
+
ADD Gemfile* *.gemspec /app/
|
16
|
+
ADD lib/argus/version.rb /app/lib/argus/version.rb
|
17
|
+
|
18
|
+
RUN bundle install
|
19
|
+
|
20
|
+
ADD bin /app/bin
|
21
|
+
ADD lib /app/lib
|
22
|
+
|
23
|
+
ENV ARGUS_QUEUE argus
|
24
|
+
|
25
|
+
CMD ["bin/argus-worker"]
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 TODO: Richard Lister
|
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,183 @@
|
|
1
|
+
# Argus
|
2
|
+
|
3
|
+
Extremely simple ruby daemon to build docker images and push them to
|
4
|
+
[AWS Elastic Container Registry](https://aws.amazon.com/ecr/).
|
5
|
+
|
6
|
+
Features:
|
7
|
+
|
8
|
+
- does one thing well (kinda)
|
9
|
+
- queue builds with SQS messages
|
10
|
+
- built-in ECR authentication
|
11
|
+
- can notify [Slack](https://slack.com/) of build details
|
12
|
+
|
13
|
+
The primary goal of this project is simplicity. It should be easy to
|
14
|
+
adapt to your exact image-building needs.
|
15
|
+
|
16
|
+
## Quick-start
|
17
|
+
|
18
|
+
You will need:
|
19
|
+
|
20
|
+
- AWS credentials set, either with the usual environment variables or
|
21
|
+
with a credentials file
|
22
|
+
- a github repo to build, and access to it, with ssh keys or
|
23
|
+
`GITHUB_TOKEN`
|
24
|
+
- a repository created on ECR to push the docker image once built
|
25
|
+
|
26
|
+
Create an SQS queue called `argus`, then:
|
27
|
+
|
28
|
+
```
|
29
|
+
gem install argus-builder
|
30
|
+
argus-send rlister/argus:master # replace with your github repo
|
31
|
+
argus-worker
|
32
|
+
```
|
33
|
+
|
34
|
+
## Installation
|
35
|
+
|
36
|
+
Install via ruby gems:
|
37
|
+
|
38
|
+
```
|
39
|
+
gem install argus-builder
|
40
|
+
```
|
41
|
+
|
42
|
+
or clone from git:
|
43
|
+
|
44
|
+
```
|
45
|
+
git clone rlister/argus
|
46
|
+
```
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
`argus` is implemented as a
|
51
|
+
[Shoryuken](https://github.com/phstc/shoryuken) worker. It will poll
|
52
|
+
SQS for build messages, and run:
|
53
|
+
|
54
|
+
- git pull
|
55
|
+
- docker build
|
56
|
+
- docker push to ECR
|
57
|
+
|
58
|
+
Set the following environment variables to configure `argus`:
|
59
|
+
|
60
|
+
```
|
61
|
+
export GITHUB_TOKEN=xxx
|
62
|
+
export AWS_REGION=us-east-1
|
63
|
+
export AWS_ACCESS_KEY_ID=xxx
|
64
|
+
export AWS_SECRET_ACCESS_KEY=xxx
|
65
|
+
export ARGUS_QUEUE=my_queue
|
66
|
+
export ARGUS_HOME=/data
|
67
|
+
export SLACK_WEBHOOK=https://hooks.slack.com/services/xxx
|
68
|
+
```
|
69
|
+
|
70
|
+
Git repositories will be kept in the `ARGUS_HOME` directory (default `/tmp`).
|
71
|
+
|
72
|
+
Send a message to your queue:
|
73
|
+
|
74
|
+
```
|
75
|
+
argus-send org/repo:branch
|
76
|
+
```
|
77
|
+
|
78
|
+
Start the worker daemon to start consuming the queue:
|
79
|
+
|
80
|
+
```
|
81
|
+
argus-worker
|
82
|
+
```
|
83
|
+
|
84
|
+
## Build procedure
|
85
|
+
|
86
|
+
Argus builds images as follows. Examine and modify `lib/argus/git.rb`
|
87
|
+
and `lib/argus/docker.git` to change exact behaviour.
|
88
|
+
|
89
|
+
1. `git clone` a new github repo, or `git checkout` an existing one
|
90
|
+
1. authenticate to elastic container registry using provided credentials
|
91
|
+
1. attempt to `docker pull` existing image from repo to take advantage of cache
|
92
|
+
1. `docker build` the new image
|
93
|
+
1. `docker push` to the registry
|
94
|
+
1. notify Slack
|
95
|
+
|
96
|
+
## Message format
|
97
|
+
|
98
|
+
Implementation of your own message sender is simple, using any library
|
99
|
+
that can push a JSON object to SQS.
|
100
|
+
|
101
|
+
Example:
|
102
|
+
|
103
|
+
```
|
104
|
+
require 'aws-sdk'
|
105
|
+
|
106
|
+
msg = {
|
107
|
+
org: org,
|
108
|
+
repo: repo,
|
109
|
+
branch: branch
|
110
|
+
}
|
111
|
+
|
112
|
+
sqs = Aws::SQS::Client.new
|
113
|
+
sqs.send_message(
|
114
|
+
queue_url: sqs.get_queue_url(queue_name: myqueue).queue_url,
|
115
|
+
message_body: msg.to_json
|
116
|
+
)
|
117
|
+
```
|
118
|
+
|
119
|
+
## Shoryuken options
|
120
|
+
|
121
|
+
`argus` is a simple shoryuken worker. You can run it directly from the
|
122
|
+
argus repository with any shoryuken options you like:
|
123
|
+
|
124
|
+
```
|
125
|
+
bundle exec shoyruken -r ./lib/argus/worker.rb -q myqueue -c 1
|
126
|
+
```
|
127
|
+
|
128
|
+
It is advisable to run a single thread (concurrency of 1) per host, as
|
129
|
+
argus shares build directory between builds, to take advantage of git
|
130
|
+
and docker caching.
|
131
|
+
|
132
|
+
Writing your own shoryuken worker class is straightforward, see
|
133
|
+
`lib/argus/worker.rb` for details.
|
134
|
+
|
135
|
+
## Docker
|
136
|
+
|
137
|
+
Argus runs happily inside a docker container, but needs access to a
|
138
|
+
docker daemon to trigger builds. For example, using local socket:
|
139
|
+
|
140
|
+
```
|
141
|
+
docker run \
|
142
|
+
--name argus \
|
143
|
+
-e GITHUB_TOKEN \
|
144
|
+
-e AWS_REGION \
|
145
|
+
-e AWS_ACCESS_KEY_ID \
|
146
|
+
-e AWS_SECRET_ACCESS_KEY \
|
147
|
+
-e ARGUS_QUEUE \
|
148
|
+
-e ARGUS_HOME=/data \
|
149
|
+
-e SLACK_WEBHOOK \
|
150
|
+
-v /data:/data \
|
151
|
+
-v /var/run/docker.sock:/var/run/docker.sock \
|
152
|
+
rlister/argus:latest
|
153
|
+
```
|
154
|
+
|
155
|
+
You will probably want to bind-mount the `ARGUS_HOME` data directory,
|
156
|
+
to preserve git repos between container restarts. This avoids cloning
|
157
|
+
the entire repos on the next build.
|
158
|
+
|
159
|
+
## Development
|
160
|
+
|
161
|
+
After checking out the repo, run `bin/setup` to install
|
162
|
+
dependencies. You can also run `bin/console` for an interactive prompt
|
163
|
+
that will allow you to experiment.
|
164
|
+
|
165
|
+
To install this gem onto your local machine, run `bundle exec rake
|
166
|
+
install`. To release a new version, update the version number in
|
167
|
+
`version.rb`, and then run `bundle exec rake release`, which will
|
168
|
+
create a git tag for the version, push git commits and tags, and push
|
169
|
+
the `.gem` file to [rubygems.org](https://rubygems.org).
|
170
|
+
|
171
|
+
## Contributing
|
172
|
+
|
173
|
+
Bug reports and pull requests are welcome on GitHub at
|
174
|
+
https://github.com/[USERNAME]/argus. This project is intended to be a
|
175
|
+
safe, welcoming space for collaboration, and contributors are expected
|
176
|
+
to adhere to the
|
177
|
+
[Contributor Covenant](http://contributor-covenant.org) code of
|
178
|
+
conduct.
|
179
|
+
|
180
|
+
## License
|
181
|
+
|
182
|
+
The gem is available as open source under the terms of the
|
183
|
+
[MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/argus.gemspec
ADDED
@@ -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 'argus/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "argus-builder"
|
8
|
+
spec.version = Argus::VERSION
|
9
|
+
spec.authors = ['Richard Lister']
|
10
|
+
spec.email = ['rlister+gh@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Docker image builder for AWS Elastic Container Registry.}
|
13
|
+
spec.homepage = 'https://github.com/rlister/argus'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = %w[lib]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
|
24
|
+
spec.add_dependency('aws-sdk', '>= 2.2.20')
|
25
|
+
spec.add_dependency('shoryuken')
|
26
|
+
spec.add_dependency('docker-api')
|
27
|
+
end
|
data/bin/argus-send
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'aws-sdk'
|
4
|
+
|
5
|
+
## parse github repo
|
6
|
+
org, repo, branch = ARGV[0].split(/[\/:]+/)
|
7
|
+
|
8
|
+
## format to send to argus
|
9
|
+
msg = {
|
10
|
+
org: org,
|
11
|
+
repo: repo,
|
12
|
+
branch: branch || 'master'
|
13
|
+
}
|
14
|
+
|
15
|
+
## send message to sqs
|
16
|
+
sqs = Aws::SQS::Client.new
|
17
|
+
|
18
|
+
queue_url = sqs.get_queue_url(
|
19
|
+
queue_name: ENV.fetch('ARGUS_QUEUE', 'argus')
|
20
|
+
).queue_url
|
21
|
+
|
22
|
+
sqs.send_message(
|
23
|
+
queue_url: queue_url,
|
24
|
+
message_body: msg.to_json
|
25
|
+
)
|
data/bin/argus-worker
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "argus"
|
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
data/lib/argus.rb
ADDED
data/lib/argus/docker.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'docker'
|
2
|
+
require 'benchmark'
|
3
|
+
|
4
|
+
module Argus
|
5
|
+
class Image
|
6
|
+
attr_reader :repo, :branch, :build_time, :image, :push_time
|
7
|
+
|
8
|
+
def initialize(repo, branch)
|
9
|
+
@repo = repo
|
10
|
+
@branch = branch
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"#{repo}:#{branch}"
|
15
|
+
end
|
16
|
+
|
17
|
+
## make a heroic attempt to pre-load as many layers as we can
|
18
|
+
def pull
|
19
|
+
[branch, :master, :latest].each do |tag|
|
20
|
+
puts "Attempting to pull #{repo}:#{tag}"
|
21
|
+
begin
|
22
|
+
attempt = Docker::Image.create(fromImage: "#{repo}:#{tag}")
|
23
|
+
rescue Docker::Error::ArgumentError
|
24
|
+
puts "failed pull"
|
25
|
+
rescue Docker::Error::NotFoundError
|
26
|
+
puts "image not found"
|
27
|
+
end
|
28
|
+
break if attempt.is_a?(Docker::Image)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
## build docker image
|
33
|
+
def build!
|
34
|
+
puts "building #{self}"
|
35
|
+
|
36
|
+
@build_time = Benchmark.realtime do
|
37
|
+
@image = Docker::Image.build_from_dir('.', dockerfile: 'Dockerfile') do |chunk|
|
38
|
+
stream = JSON.parse(chunk)['stream']
|
39
|
+
unless (stream.nil? || stream.match(/^[\s\.]+$/)) # very verbose about build progress
|
40
|
+
puts stream.chomp
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
## check if image built ok
|
47
|
+
def is_ok?
|
48
|
+
image.is_a?(Docker::Image)
|
49
|
+
end
|
50
|
+
|
51
|
+
## tag with both sha and branch
|
52
|
+
def tag!(sha)
|
53
|
+
[sha, branch].map do |tag|
|
54
|
+
puts "tagging #{repo}:#{tag}"
|
55
|
+
image.tag(repo: repo, tag: tag, force: true)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
## push image and all tags to registry
|
60
|
+
def push(sha)
|
61
|
+
@push_time = Benchmark.realtime do
|
62
|
+
[sha, branch].each do |tag|
|
63
|
+
puts "pushing #{repo}:#{tag}"
|
64
|
+
image.push(nil, tag: tag)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
data/lib/argus/git.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module Argus
|
2
|
+
class Git
|
3
|
+
attr_reader :org, :repo, :branch, :sha
|
4
|
+
|
5
|
+
def initialize(org, repo, branch = 'master')
|
6
|
+
@org = org
|
7
|
+
@repo = repo
|
8
|
+
@branch = branch
|
9
|
+
@sha = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
"#{org}/#{repo}:#{branch}"
|
14
|
+
end
|
15
|
+
|
16
|
+
## if we have a token, use https, else depend on ssh being set up
|
17
|
+
def url
|
18
|
+
if ENV['GITHUB_TOKEN']
|
19
|
+
"https://#{ENV['GITHUB_TOKEN']}@github.com/#{org}/#{repo}.git"
|
20
|
+
else
|
21
|
+
"git@github.com:#{org}/#{repo}.git"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
## is this dir a git repo?
|
26
|
+
def is_inside_work_tree?
|
27
|
+
%x[git rev-parse --is-inside-work-tree 2> /dev/null].chomp == 'true'
|
28
|
+
end
|
29
|
+
|
30
|
+
## pull existing, or new git repo, and return sha
|
31
|
+
def pull
|
32
|
+
if is_inside_work_tree?
|
33
|
+
checkout
|
34
|
+
else
|
35
|
+
clone
|
36
|
+
end
|
37
|
+
@sha = rev_parse # return SHA
|
38
|
+
end
|
39
|
+
|
40
|
+
## checkout branch of an existing repo
|
41
|
+
def checkout
|
42
|
+
puts "repo exists, pulling #{self}"
|
43
|
+
%x[git fetch && git checkout -f #{branch} && git reset --hard origin/#{branch}]
|
44
|
+
raise ArgusError, "git checkout failed for #{self}" unless $? == 0
|
45
|
+
end
|
46
|
+
|
47
|
+
## clone a new repo
|
48
|
+
def clone
|
49
|
+
puts "new repo, cloning #{self}"
|
50
|
+
%x[git clone -b #{branch} #{url} .] # not found: clone it
|
51
|
+
raise ArgusError, "git clone failed for #{self}" unless $? == 0
|
52
|
+
end
|
53
|
+
|
54
|
+
## get current sha
|
55
|
+
def rev_parse
|
56
|
+
%x[git rev-parse #{branch} ].chomp
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/argus/runner.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'aws-sdk'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module Argus
|
5
|
+
class Runner
|
6
|
+
## override notifier if setup for Slack ...
|
7
|
+
prepend SlackNotifier if ENV.has_key?('SLACK_WEBHOOK')
|
8
|
+
|
9
|
+
## ... else notify stdout
|
10
|
+
def notify(message, color = nil)
|
11
|
+
puts message
|
12
|
+
end
|
13
|
+
|
14
|
+
def symbolize_keys(hash)
|
15
|
+
Hash[ hash.map { |k,v| [ k.to_sym, v ] }]
|
16
|
+
end
|
17
|
+
|
18
|
+
## authenticate to AWS ECR
|
19
|
+
def authenticate_ecr
|
20
|
+
## get token and extract creds
|
21
|
+
auth = Aws::ECR::Client.new.get_authorization_token.authorization_data.first
|
22
|
+
username, password = Base64.decode64(auth.authorization_token).split(':')
|
23
|
+
|
24
|
+
## authenticate our docker client
|
25
|
+
Docker.authenticate!(
|
26
|
+
username: username,
|
27
|
+
password: password,
|
28
|
+
serveraddress: auth.proxy_endpoint
|
29
|
+
)
|
30
|
+
|
31
|
+
## return registry name
|
32
|
+
URI.parse(auth.proxy_endpoint).host
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize(msg)
|
36
|
+
msg = symbolize_keys(msg)
|
37
|
+
|
38
|
+
## make working directory
|
39
|
+
dir = File.join(ENV.fetch('ARGUS_HOME', '/tmp'), msg[:org], msg[:repo])
|
40
|
+
FileUtils.mkdir_p(dir)
|
41
|
+
|
42
|
+
## github repo to get
|
43
|
+
git = Git.new(msg[:org], msg[:repo], msg[:branch])
|
44
|
+
|
45
|
+
## authenticate to registry
|
46
|
+
registry = authenticate_ecr
|
47
|
+
|
48
|
+
## docker image to build
|
49
|
+
img = Image.new("#{registry}/#{msg[:repo]}", msg[:branch])
|
50
|
+
|
51
|
+
Dir.chdir(dir) do
|
52
|
+
img.pull # pull some layers to speed up the build
|
53
|
+
git.pull # get the git repo
|
54
|
+
raise ArgusError, "git sha not found: #{git}" unless git.sha
|
55
|
+
|
56
|
+
img.build! # build docker image
|
57
|
+
raise ArgusError, 'docker build failed' unless img.is_ok?
|
58
|
+
|
59
|
+
notify("build complete for #{img} (#{img.build_time.round}s)", :good)
|
60
|
+
|
61
|
+
img.tag!(git.sha) # tag the image
|
62
|
+
img.push(git.sha) # push to registry
|
63
|
+
|
64
|
+
notify("push complete for #{img} (#{img.push_time.round}s)", :good)
|
65
|
+
end
|
66
|
+
rescue ArgusError => e
|
67
|
+
notify(e.message, :danger)
|
68
|
+
raise # re-raise for shoryuken to delete failed job
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/argus/slack.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Argus
|
5
|
+
module SlackNotifier
|
6
|
+
def notify(message, color = '#808080')
|
7
|
+
uri = URI.parse(ENV['SLACK_WEBHOOK'])
|
8
|
+
|
9
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
10
|
+
http.use_ssl = true
|
11
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
12
|
+
|
13
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
14
|
+
request.set_form_data(
|
15
|
+
payload: {
|
16
|
+
attachments: [
|
17
|
+
{
|
18
|
+
text: message,
|
19
|
+
color: color.to_s,
|
20
|
+
mrkdwn_in: %w[ text ], #allow link formatting in attachment
|
21
|
+
}
|
22
|
+
]
|
23
|
+
}.to_json
|
24
|
+
)
|
25
|
+
|
26
|
+
http.request(request)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/argus/worker.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'argus'
|
2
|
+
|
3
|
+
module Argus
|
4
|
+
class Worker
|
5
|
+
include Shoryuken::Worker
|
6
|
+
|
7
|
+
shoryuken_options queue: ENV.fetch('ARGUS_QUEUE', 'argus')
|
8
|
+
shoryuken_options body_parser: :json
|
9
|
+
shoryuken_options auto_delete: true
|
10
|
+
|
11
|
+
## prevent timeout on docker api operations (e.g. long bundle install during build)
|
12
|
+
Excon.defaults[:write_timeout] = ENV.fetch('DOCKER_WRITE_TIMEOUT', 1000)
|
13
|
+
Excon.defaults[:read_timeout] = ENV.fetch('DOCKER_READ_TIMEOUT', 1000)
|
14
|
+
|
15
|
+
def perform(_, body)
|
16
|
+
Argus::Runner.new(body)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: argus-builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Richard Lister
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-18 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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: aws-sdk
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.2.20
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.2.20
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: shoryuken
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: docker-api
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- rlister+gh@gmail.com
|
86
|
+
executables:
|
87
|
+
- argus-send
|
88
|
+
- argus-worker
|
89
|
+
- console
|
90
|
+
- setup
|
91
|
+
extensions: []
|
92
|
+
extra_rdoc_files: []
|
93
|
+
files:
|
94
|
+
- ".dockerignore"
|
95
|
+
- ".gitignore"
|
96
|
+
- CODE_OF_CONDUCT.md
|
97
|
+
- Dockerfile
|
98
|
+
- Gemfile
|
99
|
+
- LICENSE.txt
|
100
|
+
- README.md
|
101
|
+
- Rakefile
|
102
|
+
- argus.gemspec
|
103
|
+
- bin/argus-send
|
104
|
+
- bin/argus-worker
|
105
|
+
- bin/console
|
106
|
+
- bin/setup
|
107
|
+
- lib/argus.rb
|
108
|
+
- lib/argus/docker.rb
|
109
|
+
- lib/argus/git.rb
|
110
|
+
- lib/argus/runner.rb
|
111
|
+
- lib/argus/slack.rb
|
112
|
+
- lib/argus/version.rb
|
113
|
+
- lib/argus/worker.rb
|
114
|
+
homepage: https://github.com/rlister/argus
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.5.1
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Docker image builder for AWS Elastic Container Registry.
|
138
|
+
test_files: []
|