module CFManifests

Constants

MANIFEST_FILE

Public Instance Methods

all_apps() click to toggle source

return all the apps described by the manifest

# File lib/manifests/manifests.rb, line 111
def all_apps
  manifest[:applications]
end
apps_in_manifest(input = nil, use_name = true, &blk) click to toggle source

splits the user's input, resolving paths with the manifest, into internal/external apps

internal apps are returned as their data in the manifest

external apps are the strings that the user gave, to be passed along wholesale to the wrapped command

# File lib/manifests/manifests.rb, line 129
def apps_in_manifest(input = nil, use_name = true, &blk)
  names_or_paths =
    if input.has?(:apps)
      # names may be given but be [], which will still cause
      # interaction, so use #direct instead of #[] here
      input.direct(:apps)
    elsif input.has?(:app)
      [input.direct(:app)]
    elsif input.has?(:name)
      [input.direct(:name)]
    else
      []
    end

  internal = []
  external = []

  names_or_paths.each do |x|
    if x.is_a?(String)
      if x =~ %r([/\\])
        apps = find_apps(File.expand_path(x))

        if apps.empty?
          fail("Path #{b(x)} is not present in manifest #{b(relative_manifest_file)}.")
        end
      else
        apps = find_apps(x)
      end

      if !apps.empty?
        internal += apps
      else
        external << x
      end
    else
      external << x
    end
  end

  [internal, external]
end
check_attributes!(app, output = $stdout) click to toggle source
# File lib/manifests/manifests.rb, line 63
def check_attributes!(app, output = $stdout)
  app.each do |k, v|
    output.puts error_message_for_attribute(k) unless known_manifest_attributes.include? k
  end
end
check_manifest!(manifest_hash, output = $stdout) click to toggle source
# File lib/manifests/manifests.rb, line 58
def check_manifest!(manifest_hash, output = $stdout)
  manifest_hash[:applications].each{ |app| check_attributes!(app, output) }
  manifest_hash
end
create_manifest_for(app, path) click to toggle source
# File lib/manifests/manifests.rb, line 171
def create_manifest_for(app, path)
  meta = {
    "name" => app.name,
    "memory" => human_size(app.memory * 1024 * 1024, 0),
    "instances" => app.total_instances,
    "host" => app.host || "none",
    "domain" => app.domain ? app.domain : "none",
    "path" => path
  }

  services = app.services

  unless services.empty?
    meta["services"] = {}

    services.each do |service_instance|
      if service_instance.is_a?(CFoundry::V2::UserProvidedServiceInstance)
        meta["services"][service_instance.name] = {
          "label" => "user-provided",
          "credentials" => service_instance.credentials.stringify_keys,
        }
      else
        service_plan = service_instance.service_plan
        service = service_plan.service

        meta["services"][service_instance.name] = {
          "label" => service.label,
          "provider" => service.provider,
          "version" => service.version,
          "plan" => service_plan.name
        }
      end
    end
  end

  if cmd = app.command
    meta["command"] = cmd
  end

  if buildpack = app.buildpack
    meta["buildpack"] = buildpack
  end

  meta
end
current_apps() click to toggle source
# File lib/manifests/manifests.rb, line 115
def current_apps
  manifest[:applications].select do |app|
    next unless app[:path]
    from_manifest(app[:path]) == Dir.pwd
  end
end
error_message_for_attribute(attribute) click to toggle source
# File lib/manifests/manifests.rb, line 69
def error_message_for_attribute(attribute)
  "\e[31mWarning: #{attribute} is not a valid manifest attribute. Please " +
  "remove this attribute from your manifest to get rid of this warning\e[0m"
end
find_apps(identifier) click to toggle source

find apps by an identifier, which may be either a tag, a name, or a path

# File lib/manifests/manifests.rb, line 98
def find_apps(identifier)
  return [] unless manifest

  apps = apps_by(:name, identifier)

  if apps.empty?
    apps = apps_by(:path, from_manifest(identifier))
  end

  apps
end
known_manifest_attributes() click to toggle source
# File lib/manifests/manifests.rb, line 74
def known_manifest_attributes
  [:applications, :buildpack, :command, :disk, :domain, :env,
   :host, :inherit, :instances, :mem, :memory, :name,
   :path, :properties, :runtime, :services, :stack]
end
load_manifest(file) click to toggle source

load and resolve a given manifest file

# File lib/manifests/manifests.rb, line 54
def load_manifest(file)
  check_manifest! Loader.new(file, self).manifest
end
manifest() click to toggle source
# File lib/manifests/manifests.rb, line 13
def manifest
  return @@manifest if @@manifest

  if manifest_file && File.exists?(manifest_file)
    @@manifest = load_manifest(manifest_file)
  end
end
manifest_file() click to toggle source

find the manifest file to work with

# File lib/manifests/manifests.rb, line 30
def manifest_file
  return @manifest_file if @manifest_file

  unless path = input[:manifest]
    where = Dir.pwd
    while true
      if File.exists?(File.join(where, MANIFEST_FILE))
        path = File.join(where, MANIFEST_FILE)
        break
      elsif File.basename(where) == "/"
        path = nil
        break
      else
        where = File.expand_path("../", where)
      end
    end
  end

  return unless path

  @manifest_file = File.expand_path(path)
end
resolve_symbol(sym) click to toggle source

dynamic symbol resolution

# File lib/manifests/manifests.rb, line 81
def resolve_symbol(sym)
  case sym
  when "target-url"
    client_target

  when "target-base"
    target_base

  when "random-word"
    sprintf("%04x", rand(0x0100000))

  when /^ask (.+)/
    ask($1)
  end
end
save_manifest(save_to = manifest_file) click to toggle source
# File lib/manifests/manifests.rb, line 21
def save_manifest(save_to = manifest_file)
  fail "No manifest to save!" unless @@manifest

  File.open(save_to, "w") do |io|
    YAML.dump(@@manifest, io)
  end
end

Private Instance Methods

apps_by(attr, val) click to toggle source
# File lib/manifests/manifests.rb, line 242
def apps_by(attr, val)
  manifest[:applications].select do |info|
    info[attr] == val
  end
end
ask_to_save(input, app) click to toggle source
# File lib/manifests/manifests.rb, line 254
def ask_to_save(input, app)
  return if manifest_file
  return unless ask("Save configuration?", :default => false)

  manifest = create_manifest_for(app, input[:path])

  with_progress("Saving to #{c("manifest.yml", :name)}") do
    File.open("manifest.yml", "w") do |io|
      YAML.dump(
        { "applications" => [manifest] },
        io)
    end
  end
end
env_hash(val) click to toggle source
# File lib/manifests/manifests.rb, line 269
def env_hash(val)
  if val.is_a?(Hash)
    val
  else
    hash = {}

    val.each do |pair|
      name, val = pair.split("=", 2)
      hash[name] = val
    end

    hash
  end
end
from_manifest(path) click to toggle source

expand a path relative to the manifest file's directory

# File lib/manifests/manifests.rb, line 249
def from_manifest(path)
  File.expand_path(path, File.dirname(manifest_file))
end
no_apps() click to toggle source
# File lib/manifests/manifests.rb, line 233
def no_apps
  fail "No applications or manifest to operate on."
end
relative_manifest_file() click to toggle source
# File lib/manifests/manifests.rb, line 219
def relative_manifest_file
  Pathname.new(manifest_file).relative_path_from(Pathname.pwd)
end
setup_env(app, info) click to toggle source
# File lib/manifests/manifests.rb, line 284
def setup_env(app, info)
  return unless info[:env]
  app.env = env_hash(info[:env])
end
setup_services(app, info) click to toggle source
# File lib/manifests/manifests.rb, line 289
def setup_services(app, info)
  return if !info[:services] || info[:services].empty?

  offerings = client.services

  to_bind = []

  info[:services].each do |name, svc|
    name = name.to_s

    if instance = client.service_instance_by_name(name)
      to_bind << instance
    else
      if svc[:label] == "user-provided"
        invoke :create_service,
          name: name,
          offering: CF::Service::UPDummy.new,
          app: app,
          credentials: svc[:credentials]
      else
        offering = offerings.find { |o|
          o.label == (svc[:label] || svc[:type] || svc[:vendor]) &&
            (!svc[:version] || o.version == svc[:version]) &&
            (o.provider == (svc[:provider] || "core"))
        }

        fail "Unknown service offering: #{svc.inspect}." unless offering

        plan = offering.service_plans.find { |p|
          p.name == (svc[:plan] || "D100")
        }

        fail "Unknown service plan: #{svc[:plan]}." unless plan

        invoke :create_service,
          :name => name,
          :offering => offering,
          :plan => plan,
          :app => app
      end
    end
  end

  to_bind.each do |s|
    next if app.binds?(s)

    # TODO: splat
    invoke :bind_service, :app => app, :service => s
  end
end
show_manifest_usage() click to toggle source
# File lib/manifests/manifests.rb, line 223
def show_manifest_usage
  return if @@showed_manifest_usage

  path = relative_manifest_file
  line "Using manifest file #{c(path, :name)}"
  line

  @@showed_manifest_usage = true
end
target_base() click to toggle source
# File lib/manifests/manifests.rb, line 340
def target_base
  client_target.sub(/^[^\.]+\./, "")
end
warn_reset_changes() click to toggle source
# File lib/manifests/manifests.rb, line 237
def warn_reset_changes
  line c("Not applying manifest changes without --reset", :warning)
  line
end