dockerfile_parser2 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +92 -0
- data/Rakefile +33 -0
- data/dockerfile_parser.gemspec +26 -0
- data/lib/dockerfile_parser.rb +63 -0
- data/spec/spec_helper.rb +2 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: af85cdcc305ee1610abbb5aaaac2918a82bfa864
|
4
|
+
data.tar.gz: 43324e872095199197d7b26ad9e5209a9da31e04
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b9d86ea31b5c78c35727f66f10b8417db1f168f6a0b0543e708040f71d2396283b8856b190f020c80cfd5473ffef564450b2e430094d61a2ed0d87f7e586fef
|
7
|
+
data.tar.gz: c5912bff9d56a1fd1039b24df993c7b55f2aba69fd5e1c6fc431be7bf607d15b00ef5d82ecc1328a8f18bc8d5ab73113c4bb7e628c8abe492317188a2f685b5c
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Nikolay Yurin
|
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,92 @@
|
|
1
|
+
Dockerfile_parser
|
2
|
+
==================
|
3
|
+
|
4
|
+
[![Code Climate](https://codeclimate.com/github/yurinnick/ruby-dockerfile-parser/badges/gpa.svg)](https://codeclimate.com/github/yurinnick/ruby-dockerfile-parser)
|
5
|
+
|
6
|
+
Dockerfile parser gem written on pure Ruby, outputs Array of Hashes. See example below.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'dockerfile_parser'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install dockerfile_parser
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
example.rb
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
|
30
|
+
require 'dockerfile_parser'
|
31
|
+
|
32
|
+
puts DockerfileParser.load('Dockerfile')
|
33
|
+
```
|
34
|
+
|
35
|
+
Dockerfile
|
36
|
+
|
37
|
+
```Dockerfile
|
38
|
+
|
39
|
+
FROM debian:jessie
|
40
|
+
MAINTAINER Nikolay Yurin <yurinnick@outlook.com>
|
41
|
+
|
42
|
+
RUN apt-get update && \
|
43
|
+
apt-get install -y nginx
|
44
|
+
|
45
|
+
RUN rm -rf /var/lib/apt/lists/* && \
|
46
|
+
chown -R www-data:www-data /var/lib/nginx
|
47
|
+
|
48
|
+
VOLUME /var/www/html
|
49
|
+
WORKDIR /etc/nginx
|
50
|
+
COPY site-example.conf /etc/nginx/sites-available/site-example.conf
|
51
|
+
COPY index.html.tmpl /var/www/html/index.html
|
52
|
+
|
53
|
+
EXPOSE 80
|
54
|
+
|
55
|
+
CMD ["nginx", "-g", "daemon off;"]
|
56
|
+
```
|
57
|
+
|
58
|
+
output
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
[
|
62
|
+
{:command=>"FROM", :params=>["debian", "jessie"]}
|
63
|
+
{:command=>"MAINTAINER", :params=>"Nikolay Yurin <yurinnick@outlook.com>"}
|
64
|
+
{:command=>"RUN", :params=>["apt-get update",
|
65
|
+
"apt-get install -y nginx"]}
|
66
|
+
{:command=>"RUN", :params=>["rm -rf /var/lib/apt/lists/*",
|
67
|
+
"chown -R www-data:www-data /var/lib/nginx"]}
|
68
|
+
{:command=>"VOLUME", :params=>"/var/www/html"}
|
69
|
+
{:command=>"WORKDIR", :params=>"/etc/nginx"}
|
70
|
+
{:command=>"COPY", :params=>{:src=>"site-example.conf",
|
71
|
+
:dst=>"/etc/nginx/sites-available/site-example.conf"}}
|
72
|
+
{:command=>"COPY", :params=>{:src=>"index.html",
|
73
|
+
:dst=>"/var/www/html/index.html"}}
|
74
|
+
{:command=>"EXPOSE", :params=>80}
|
75
|
+
{:command=>"CMD", :params=>["nginx", "-g", "daemon off;"]}
|
76
|
+
]
|
77
|
+
```
|
78
|
+
|
79
|
+
|
80
|
+
## Development
|
81
|
+
|
82
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
83
|
+
|
84
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
1. Fork it (https://github.com/yurinnick/dockerfile-parser/fork)
|
89
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
90
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
91
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
92
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Encoding: utf-8
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts 'Run `bundle install` to install missing gems'
|
9
|
+
$stderr.puts e.message
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'rake'
|
14
|
+
require 'rspec/core'
|
15
|
+
require 'rspec/core/rake_task'
|
16
|
+
require 'rubocop/rake_task'
|
17
|
+
|
18
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
19
|
+
spec.pattern = FileList['test/spec/**/*_spec.rb']
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Run RSpec with code coverage'
|
23
|
+
task :coverage do
|
24
|
+
ENV['COVERAGE'] = 'true'
|
25
|
+
Rake::Task['spec'].execute
|
26
|
+
end
|
27
|
+
|
28
|
+
desc 'Run RuboCop over itself'
|
29
|
+
RuboCop::RakeTask.new(:code_style) do |task|
|
30
|
+
task.fail_on_error = false
|
31
|
+
end
|
32
|
+
|
33
|
+
task default: [:spec, :code_style]
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
Gem::Specification.new do |spec|
|
3
|
+
spec.name = 'dockerfile_parser2'
|
4
|
+
spec.version = '0.5.0'
|
5
|
+
spec.authors = ['Nikolay Yurin']
|
6
|
+
spec.email = ['yurinnick@outlook.com']
|
7
|
+
|
8
|
+
spec.summary = 'Dockerfile parser fork'
|
9
|
+
spec.description = 'A Dockerfile parser written in pure Ruby'
|
10
|
+
spec.license = 'MIT'
|
11
|
+
spec.require_paths = ['lib']
|
12
|
+
|
13
|
+
spec.files = [
|
14
|
+
'Gemfile',
|
15
|
+
'LICENSE.txt',
|
16
|
+
'README.md',
|
17
|
+
'Rakefile',
|
18
|
+
'spec/spec_helper.rb',
|
19
|
+
'lib/dockerfile_parser.rb',
|
20
|
+
'dockerfile_parser.gemspec'
|
21
|
+
]
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.8'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rubocop', '~> 0.45.0'
|
26
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# Encoding: utf-8
|
2
|
+
# @author Nikolay Yurin <yurinnick@outlook.com>
|
3
|
+
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
# DockerfileParser main class
|
7
|
+
class DockerfileParser
|
8
|
+
@commands = %w(FROM MAINTAINER RUN CMD EXPOSE ENV ADD COPY ENTRYPOINT
|
9
|
+
VOLUME USER WORKDIR ONBUILD)
|
10
|
+
|
11
|
+
# Parse Dockerfile from specified path
|
12
|
+
# @return [Array<Hash>] parser Dockerfile
|
13
|
+
def self.load_file(path)
|
14
|
+
loads(File.read(path))
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.loads(s)
|
18
|
+
dockerfile_array = split_dockerfile(s)
|
19
|
+
p dockerfile_array
|
20
|
+
parse_commands(dockerfile_array).each_cons(2).map do |item|
|
21
|
+
process_steps(dockerfile_array, item[0], item[1][:index])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.split_dockerfile(str)
|
26
|
+
str.gsub(/^\#.+$/, '').gsub(/(\s\\\s)+/i, '').gsub("\n", ' ').squeeze(' ').split(' ')
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.parse_commands(dockerfile_array)
|
30
|
+
dockerfile_array.each_with_index.map do |cmd, index|
|
31
|
+
{ index: index, command: cmd } if @commands.include?(cmd)
|
32
|
+
end.compact! << { index: dockerfile_array.length, command: 'EOF' }
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.process_steps(dockerfile_array, step, next_cmd_index)
|
36
|
+
{ command: step[:command],
|
37
|
+
params: split_params(
|
38
|
+
step[:command],
|
39
|
+
dockerfile_array[step[:index] + 1..next_cmd_index - 1]
|
40
|
+
)
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.split_params(cmd, params)
|
45
|
+
case cmd
|
46
|
+
when 'FROM' then params.join('').split(':')
|
47
|
+
when 'RUN' then params.join(' ').split(/\s(\&|\;)+\s/).map(&:strip)
|
48
|
+
when 'ENV' then
|
49
|
+
{ name: params[0], value: params[1..-1].join(' ') }
|
50
|
+
when 'COPY', 'ADD' then { src: params[0], dst: params[1] }
|
51
|
+
else
|
52
|
+
if cmd[0] == '#'
|
53
|
+
end
|
54
|
+
params = params.join(' ') if params.is_a?(Array)
|
55
|
+
YAML.load(params.to_s)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private_class_method :parse_commands
|
60
|
+
private_class_method :process_steps
|
61
|
+
private_class_method :split_params
|
62
|
+
private_class_method :split_dockerfile
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dockerfile_parser2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nikolay Yurin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-14 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.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
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: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.45.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.45.0
|
69
|
+
description: A Dockerfile parser written in pure Ruby
|
70
|
+
email:
|
71
|
+
- yurinnick@outlook.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- Gemfile
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- dockerfile_parser.gemspec
|
81
|
+
- lib/dockerfile_parser.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
homepage:
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.5.1
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Dockerfile parser fork
|
107
|
+
test_files: []
|