module DockerSigh::Util

Constants

CONTAINER_NAME_REGEX

Public Class Methods

clean_git_root?(opts) click to toggle source
# File lib/docker_sigh.rb, line 187
def self.clean_git_root?(opts)
  Dir.chdir opts[:repository_root] do
    `git status | grep 'nothing to commit, working directory clean'`
    $? == 0
  end
end
from_directive_parent_tag(opts) click to toggle source
# File lib/docker_sigh.rb, line 156
def self.from_directive_parent_tag(opts)
  # TODO: parse .git/config and un-hardcode the git-flow prefixes

  branch = repo_branch(opts)
  case branch
  when "develop"
    "develop"
  when "master"
    "master"
  else
    if branch.start_with?("feature/")
      "develop"
    elsif branch.start_with("release/") || branch.start_with("hotfix/")
      "master"
    else
      logger.warn "Unrecognized branch; can't figure out a parent tag. Assuming 'develop'."
      "develop"
    end
  end
end
generate_template_args(opts) click to toggle source
# File lib/docker_sigh.rb, line 127
def self.generate_template_args(opts)
  {
    :host => opts[:host],
    :parent_tag => from_directive_parent_tag(opts)
  }
end
repo_branch(opts) click to toggle source
# File lib/docker_sigh.rb, line 134
def self.repo_branch(opts)
  Dir.chdir(opts[:repository_root]) do
    name = `git symbolic-ref --short HEAD`.strip
    raise "git failed to find branch" unless $? == 0
    name
  end
end
repo_current_commit(opts) click to toggle source
# File lib/docker_sigh.rb, line 146
def self.repo_current_commit(opts)
  hash = `git rev-parse --verify HEAD`.strip
  raise "git failed to get hash" unless $? == 0
  hash
end
repo_current_tags(opts) click to toggle source
# File lib/docker_sigh.rb, line 151
def self.repo_current_tags(opts)
  LOGGER.warn "Tags are not currently replicated into the Docker repository. Be advised when using release tags."
  []
end
tag_from_branch_name(opts) click to toggle source
# File lib/docker_sigh.rb, line 142
def self.tag_from_branch_name(opts)
  repo_branch(opts).gsub("/", "-")
end
template_url(opts) click to toggle source
# File lib/docker_sigh.rb, line 123
def self.template_url(opts)
  File.join(opts[:repository_root], "Dockerfile.template.erb")
end
valid_hostname?(hostname) click to toggle source

originally from www.dzone.com/snippets/simple-hostname-validation

# File lib/docker_sigh.rb, line 178
def self.valid_hostname?(hostname)
  return false unless hostname
  return false if hostname.length > 255 or hostname.scan('..').any?
  hostname = hostname[0 ... -1] if hostname.index('.', -1)
  return hostname.split('.').collect { |i|
    i.size <= 63 and not (i.rindex('-', 0) or i.index('-', -1) or i.scan(/[^a-z\d-]/i).any?)
  }.all?
end
validate_opts(opts) click to toggle source
# File lib/docker_sigh.rb, line 89
def self.validate_opts(opts)
  LOGGER.debug "Validating options: #{opts.inspect}"

  raise ":container_name must be set." unless opts[:container_name]
  raise ":container_name must match regex '#{CONTAINER_NAME_REGEX.to_s}'." \
    unless opts[:container_name] =~ CONTAINER_NAME_REGEX

  raise "If set, :default_host must be a valid hostname." \
    unless !opts[:default_host] || Util::valid_hostname?(opts[:default_host])

  raise "If set, the DOCKER_REMOTE env var must be a valid hostname." \
    unless !ENV["DOCKER_REMOTE"] || Util::valid_hostname?(ENV["DOCKER_REMOTE"])

  opts[:host] = ENV["DOCKER_REMOTE"] || opts[:default_host]

  raise ":repository_root must be set." unless opts[:repository_root]
  raise ":repository_root must exist and be a non-bare Git root." \
    unless Dir.exist?(File.join(opts[:repository_root], ".git"))

  LOGGER.debug "Options successfully validated."
end
validate_repo(opts) click to toggle source
# File lib/docker_sigh.rb, line 111
def self.validate_repo(opts)
  dockerfile_template = template_url(opts)
  raise "'#{dockerfile_template}' must exist." unless File.exist?(dockerfile_template)

  gitignore = File.join(opts[:repository_root], ".gitignore")
  raise "'#{gitignore}' must exist." unless File.exist?(gitignore)
  system "grep -e 'Dockerfile\|/Dockerfile' #{gitignore}"
  raise "The repo's .gitignore must ignore the Dockerfile." unless $?

  raise "The repo must be clean (no outstanding changes)." unless clean_git_root?(opts)
end