Module: CFManifests

Included in:
ManifestsPlugin
Defined in:
lib/manifests-cf-plugin.rb,
lib/manifests-cf-plugin/errors.rb,
lib/manifests-cf-plugin/loader.rb,
lib/manifests-cf-plugin/version.rb,
lib/manifests-cf-plugin/loader/builder.rb,
lib/manifests-cf-plugin/loader/resolver.rb,
lib/manifests-cf-plugin/loader/normalizer.rb

Defined Under Namespace

Modules: Builder, Normalizer, Resolver Classes: CircularDependency, InvalidManifest, Loader, UnknownSymbol

Constant Summary collapse

MANIFEST_FILE =
"manifest.yml"
VERSION =
"0.7.0".freeze
@@showed_manifest_usage =
false

Instance Method Summary collapse

Instance Method Details

#all_appsObject

return all the apps described by the manifest



88
89
90
# File 'lib/manifests-cf-plugin.rb', line 88

def all_apps
  manifest[:applications]
end

#apps_in_manifest(input = nil, use_name = true, &blk) ⇒ Object

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/manifests-cf-plugin.rb', line 106

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

#create_manifest_for(app, path) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/manifests-cf-plugin.rb', line 148

def create_manifest_for(app, path)
  meta = {
    "name" => app.name,
    "memory" => human_size(app.memory * 1024 * 1024, 0),
    "instances" => app.total_instances,
    "url" => app.url ? app.url.sub(target_base, '${target-base}') : "none",
    "path" => path
  }

  services = app.services

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

    services.each do |i|
      p = i.service_plan
      s = p.service

      meta["services"][i.name] = {
        "label" => s.label,
        "provider" => s.provider,
        "version" => s.version,
        "plan" => p.name
      }
    end
  end

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

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

  meta
end

#current_appsObject



92
93
94
95
96
97
# File 'lib/manifests-cf-plugin.rb', line 92

def current_apps
  manifest[:applications].select do |app|
    next unless app[:path]
    from_manifest(app[:path]) == Dir.pwd
  end
end

#find_apps(identifier) ⇒ Object

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



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/manifests-cf-plugin.rb', line 75

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

#load_manifest(file) ⇒ Object

load and resolve a given manifest file



53
54
55
# File 'lib/manifests-cf-plugin.rb', line 53

def load_manifest(file)
  Loader.new(file, self).manifest
end

#manifestObject



12
13
14
15
16
17
18
# File 'lib/manifests-cf-plugin.rb', line 12

def manifest
  return @manifest if @manifest

  if manifest_file && File.exists?(manifest_file)
    @manifest = load_manifest(manifest_file)
  end
end

#manifest_fileObject

find the manifest file to work with



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/manifests-cf-plugin.rb', line 29

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) ⇒ Object

dynamic symbol resolution



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/manifests-cf-plugin.rb', line 58

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) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/manifests-cf-plugin.rb', line 20

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