Module: VMCManifests

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

Defined Under Namespace

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#all_appsObject

return all the apps described by the manifest, in dependency order



105
106
107
108
109
110
111
112
113
# File 'lib/manifests-vmc-plugin.rb', line 105

def all_apps
  apps = []

  each_app do |app|
    apps << app
  end

  apps
end

#app_by_tag(tag) ⇒ Object

find an app by its unique tag



75
76
77
# File 'lib/manifests-vmc-plugin.rb', line 75

def app_by_tag(tag)
  manifest[:applications][tag]
end

#create_manifest_for(app, path) ⇒ Object



170
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
# File 'lib/manifests-vmc-plugin.rb', line 170

def create_manifest_for(app, path)
  meta = {
    "name" => app.name,
    "framework" => app.framework.name,
    "runtime" => app.runtime.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|
      if v2?
        p = i.service_plan
        s = p.service

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

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

  meta
end

#each_app(&blk) ⇒ Object

call a block for each app in a manifest (in dependency order), setting inputs for each app



98
99
100
101
102
# File 'lib/manifests-vmc-plugin.rb', line 98

def each_app(&blk)
  return unless manifest

  ordered_by_deps(manifest[:applications]).each(&blk)
end

#find_apps(identifier) ⇒ Object

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/manifests-vmc-plugin.rb', line 80

def find_apps(identifier)
  return [] unless manifest

  if app = app_by_tag(identifier)
    return [app]
  end

  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-vmc-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-vmc-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-vmc-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-vmc-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-vmc-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

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

like each_app, but only acts on apps specified as paths instead of names

returns the names that were not paths



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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/manifests-vmc-plugin.rb', line 118

def specific_apps_or_all(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[:app]]
    else
      []
    end

  input = input.without(:app, :apps)
  in_manifest = []

  if names_or_paths.empty?
    apps = find_apps(Dir.pwd)

    if !apps.empty?
      in_manifest += apps
    else
      each_app(&blk)
      return []
    end
  end

  external = []
  names_or_paths.each do |x|
    if x.is_a?(String)
      path = File.expand_path(x)

      apps = find_apps(File.exists?(path) ? path : x)

      if !apps.empty?
        in_manifest += apps
      elsif app = client.app_by_name(x)
        external << app
      else
        fail("Unknown app '#{x}'")
      end
    else
      external << x
    end
  end

  in_manifest.each do |app|
    blk.call app
  end

  external
end