Skip to content

Commit

Permalink
Merge branch 'init_overhaul'
Browse files Browse the repository at this point in the history
  • Loading branch information
hag committed Oct 19, 2016
2 parents d155619 + c2424ff commit da2a018
Show file tree
Hide file tree
Showing 24 changed files with 859 additions and 524 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* [ENHANCEMENT] removed OsDb. This file contained some hardcoded Os versions which was not future proof. It was a workaround to get started more quickly.
* [FIX] patched sources now fail when the underlying patch fails
* [CHANGE] Renamed "Source::Package" to "Source::Archive" which is more striking. The term "package" is also used for build results.
* [IMPROVEMENT] removed hardcoded init system list, try to guess the init system from the provided container
* [CHANGE] init plugin has a different syntax

# 0.2.2 / 2016.09.05

Expand Down
25 changes: 25 additions & 0 deletions lib/fpm/fry/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class FileNotFound < StandardError
include FPM::Fry::WithData
end

# Raised when a container wasn't found.
class ContainerNotFound < StandardError
include FPM::Fry::WithData
end

# Raised when trying to read file that can't be read e.g. because it's a
# directory.
class NotAFile < StandardError
Expand Down Expand Up @@ -87,6 +92,10 @@ def read(name, resource)
)
if [404,500].include? res.status
body_message = Hash[JSON.load(res.body).map{|k,v| ["docker.#{k}",v] }] rescue {'docker.message' => res.body}
body_message['docker.container'] = name
if body_message['docker.message'] =~ /\ANo such container:/
raise ContainerNotFound.new("container not found", body_message)
end
raise FileNotFound.new("file not found", {'path' => resource}.merge(body_message))
end
sio = StringIO.new(res.body)
Expand Down Expand Up @@ -115,6 +124,22 @@ def read_content(name, resource)
end
end

# Gets the target of a symlink
# @param [String] name the container name
# @param [String] resource the file name
# @return [String] target
# @return [nil] if resource is not a symlink
# @api docker
def link_target(name, resource)
read(name, resource) do |file|
if file.header.typeflag == "2"
return File.absolute_path(file.header.linkname,File.dirname(resource))
end
return nil
end
return nil
end

def copy(name, resource, map, options = {})
ex = FPM::Fry::Tar::Extractor.new(logger: @logger)
base = File.dirname(resource)
Expand Down
36 changes: 11 additions & 25 deletions lib/fpm/fry/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Command < Clamp::Command
def initialize(invocation_path, ctx = {}, parent_attribute_values = {})
super
@ui = ctx.fetch(:ui){ UI.new }
@client = ctx[:client]
end

def parse(attrs)
Expand All @@ -45,42 +46,27 @@ def client

attr_writer :client

subcommand 'detect', 'Detects distribution from an image, a container or a given name' do
subcommand 'detect', 'Detects distribution from an image' do

option '--image', 'image', 'Docker image to detect'
option '--container', 'container', 'Docker container to detect'
option '--distribution', 'distribution', 'Distribution name to detect'
parameter 'image', 'Docker image to detect'

attr :ui
extend Forwardable
def_delegators :ui, :logger

def execute
require 'fpm/fry/inspector'
require 'fpm/fry/detector'

if image
d = Detector::Image.new(client, image)
elsif distribution
d = Detector::String.new(distribution)
elsif container
d = Detector::Container.new(client, container)
else
logger.error("Please supply either --image, --distribution or --container")
return 1
end

begin
if d.detect!
data = {distribution: d.distribution, version: d.version, flavour: d.flavour, codename: d.codename}
logger.info("Detected distribution",data)
Inspector.for_image(client, image) do | inspector |
begin
data = Detector.detect(inspector)
logger.info("Detected the following parameters",data)
return 0
else
logger.error("Detection failed")
return 2
rescue => e
logger.error(e)
return 1
end
rescue => e
logger.error(e)
return 3
end
end

Expand Down
67 changes: 17 additions & 50 deletions lib/fpm/fry/command/cook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
module FPM; module Fry
class Command::Cook < Command

option '--distribution', 'distribution', 'Distribution like ubuntu-12.04'
option '--keep', :flag, 'Keep the container after build'
option '--overwrite', :flag, 'Overwrite package', default: true

Expand Down Expand Up @@ -30,32 +29,11 @@ def initialize(invocation_path, ctx = {}, parent_attribute_values = {})
require 'fpm/fry/stream_parser'
require 'fpm/fry/block_enumerator'
require 'fpm/fry/build_output_parser'
require 'fpm/fry/inspector'
require 'fpm/fry/plugin/config'
super
end

def detector
@detector || begin
if distribution
d = Detector::String.new(distribution)
else
d = Detector::Image.new(client, image)
end
self.detector=d
end
end

def detector=(d)
unless d.detect!
raise "Unable to detect distribution from given image"
end
@detector = d
end

def flavour
@flavour ||= detector.flavour
end
attr_writer :flavour

def output_class
@output_class ||= begin
logger.debug("Autodetecting package type",flavour: flavour)
Expand All @@ -75,20 +53,22 @@ def output_class

def builder
@builder ||= begin
vars = {
distribution: detector.distribution,
distribution_version: detector.version,
flavour: flavour,
codename: detector.codename
}
logger.debug("Loading recipe",variables: vars, recipe: recipe)
b = Recipe::Builder.new(vars, Recipe.new, logger: ui.logger)
b.load_file( recipe )
b = nil
Inspector.for_image(client, image) do |inspector|
variables = Detector.detect(inspector)
logger.debug("Loading recipe",variables: variables, recipe: recipe)
b = Recipe::Builder.new(variables, logger: ui.logger, inspector: inspector)
b.load_file( recipe )
end
b
end
end
attr_writer :builder

def flavour
builder.variables[:flavour]
end

def cache
@cache ||= builder.recipe.source.build_cache(tmpdir)
end
Expand Down Expand Up @@ -169,22 +149,12 @@ def update?
if flavour == 'debian'
case(update)
when 'auto'
body = JSON.generate({"Image" => image, "Cmd" => "exit 0"})
res = client.post( path: client.url('containers','create'),
headers: {'Content-Type' => 'application/json'},
body: body,
expects: [201]
)
body = JSON.parse(res.body)
container = body.fetch('Id')
begin
client.read( container, '/var/lib/apt/lists') do |file|
Inspector.for_image(client, image) do |inspector|
inspector.read('/var/lib/apt/lists') do |file|
next if file.header.name == 'lists/'
logger.hint("/var/lib/apt/lists is not empty, you could try to speed up builds with --update=never", documentation: 'https://github.com/xing/fpm-fry/wiki/The-update-parameter')
return true
break
end
ensure
client.destroy(container)
end
return true
when 'always'
Expand Down Expand Up @@ -360,11 +330,8 @@ def adjust_config_files( output )
def execute
# force some eager loading
lint_recipe_file!
detector
flavour
output_class
lint_output_class!
builder
lint_output_class!
lint_recipe!
cache

Expand Down
Loading

0 comments on commit da2a018

Please sign in to comment.