Module: JDCManifests

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

Defined Under Namespace

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

Constant Summary collapse

MANIFEST_FILE =
"manifest.yml"
@@showed_manifest_usage =
false
@@manifest =
nil

Instance Method Summary collapse

Instance Method Details

#all_appsObject

return all the apps described by the manifest



111
112
113
# File 'lib/manifests/manifests.rb', line 111

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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) ⇒ Object



63
64
65
66
67
# 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) ⇒ Object



58
59
60
61
# 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) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# 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?(JFoundry::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_appsObject



115
116
117
118
119
120
# 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) ⇒ Object



69
70
71
72
# 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) ⇒ Object

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



98
99
100
101
102
103
104
105
106
107
108
# 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_attributesObject



74
75
76
77
78
# 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) ⇒ Object

load and resolve a given manifest file



54
55
56
# File 'lib/manifests/manifests.rb', line 54

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

#manifestObject



13
14
15
16
17
18
19
# 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_fileObject

find the manifest file to work with



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object

dynamic symbol resolution



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Object



21
22
23
24
25
26
27
# 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