Class: Cloudwalk::CwFileJson

Inherits:
Object
  • Object
show all
Includes:
ManagerHelper
Defined in:
lib/cloudwalk/cw_file_json.rb

Constant Summary collapse

CW_FILE_LOCK_PATH =
"./Cwfile.json.lock"
CW_FILE_PATH =
"./Cwfile.json"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ManagerHelper

#host, included, #posxml2xml, #token, #xml2posxml, #xml2rb

Class Attribute Details

.cwfileObject

Returns the value of attribute cwfile.



30
31
32
# File 'lib/cloudwalk/cw_file_json.rb', line 30

def cwfile
  @cwfile
end

.lockObject

Returns the value of attribute lock.



30
31
32
# File 'lib/cloudwalk/cw_file_json.rb', line 30

def lock
  @lock
end

Class Method Details

.build_application(app, version = nil, modules_remote = nil) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/cloudwalk/cw_file_json.rb', line 149

def self.build_application(app, version = nil, modules_remote = nil)
  {
    "name"       => app["name"],
    "id"         => app["id"],
    "modules"    => modules_remote.collect {|mod| build_module(mod)},
    "version"    => version["number"] || version,
  }
end

.build_module(mod) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cloudwalk/cw_file_json.rb', line 135

def self.build_module(mod)
  app, ver = Cloudwalk::ApplicationVersion.find(mod.first, mod.last)
  if module_version = Cloudwalk::ApplicationVersion.get(app["id"], ver["id"])
    {
      "name"       => Cloudwalk::Application.get_name(app["id"]),
      "version"    => module_version["number"],
      "id"         => module_version["app_id"],
      "version_id" => module_version["id"]
    }
  else
    raise Cloudwalk::CwFileJsonException.new("App (#{mod.first}) Module Version (#{mod.last}) not found")
  end
end

.compareObject

TODO future!



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/cloudwalk/cw_file_json.rb', line 202

def self.compare
  cwfile_list = self.cwfile_apps_and_versions
  lock_list = self.lock_apps_and_versions

  cwdiff = cwfile_list - lock_list
  lockdiff = lock_list - cwfile_list
  unless cwdiff.empty? && lockdiff.empty?
    puts "Warning Cwfile.json and Cwfile.json.lock are different, follow differences:"
    puts "Cwfile.json"
    puts cwdiff.inspect
    puts "Cwfile.json.lock"
    puts lockdiff.inspect
    false
  else
    true
  end
end

.convert_old_cwfile(cwfile) ⇒ Object



52
53
54
55
56
57
# File 'lib/cloudwalk/cw_file_json.rb', line 52

def self.convert_old_cwfile(cwfile)
  if cwfile["apps"].nil?
    cwfile = Hash.[]("apps",[cwfile])
  end
  cwfile
end

.cwfile_apps_and_versionsObject



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/cloudwalk/cw_file_json.rb', line 172

def self.cwfile_apps_and_versions
  if self.ruby?
    [self.cwfile["name"], self.cwfile["version"]]
  else
    self.cwfile["apps"].inject([]) do |array, app|
      array << [app["name"], app["version"]]
      app["modules"].each do |app_module|
        array << [app_module[0], app_module[1]]
      end
      array
    end
  end
end

.delete_lock!Object



106
107
108
# File 'lib/cloudwalk/cw_file_json.rb', line 106

def self.delete_lock!
  FileUtils.rm_rf CW_FILE_LOCK_PATH
end

.exists?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/cloudwalk/cw_file_json.rb', line 158

def self.exists?
  File.exists? CW_FILE_PATH
end

.exists_lock?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/cloudwalk/cw_file_json.rb', line 162

def self.exists_lock?
  File.exists? CW_FILE_LOCK_PATH
end

.load_cwfileObject



42
43
44
45
46
47
48
49
50
# File 'lib/cloudwalk/cw_file_json.rb', line 42

def self.load_cwfile
  JSON.parse(File.read(CW_FILE_PATH))
rescue Errno::ENOENT
  puts "Cwfile.json not found"
  false
rescue JSON::ParserError
  puts "Error to read Cwfile.json"
  false
end

.load_cwfile_lockObject



33
34
35
36
37
38
39
40
# File 'lib/cloudwalk/cw_file_json.rb', line 33

def self.load_cwfile_lock
  JSON.parse(File.read(CW_FILE_LOCK_PATH))
rescue Errno::ENOENT
  false
rescue JSON::ParserError
  puts "Error to read Cwfile.json.lock"
  false
end

.lock_apps_and_versionsObject



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cloudwalk/cw_file_json.rb', line 186

def self.lock_apps_and_versions
  self.lock.inject([]) do |array, app|
    if self.ruby?
      array.concat([app["name"], app["version"]])
    else
      array << [posxml2xml(app["name"]), app["version"]]
      app["modules"].each do |app_module|
        array << [posxml2xml(app_module["name"]), posxml2xml(app_module["version"])]
      end
      array
    end
  end
end

.lock_buildObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cloudwalk/cw_file_json.rb', line 116

def self.lock_build
  config = []
  self.cwfile["apps"].each do |app_local|
    app, version = Cloudwalk::ApplicationVersion.find(app_local["name"], app_local["version"])
    if app && version
      detail = Cloudwalk::ApplicationVersion.get(app["id"], version["id"])
      config << build_application(app, version, detail["module_ids"])
    elsif app
      app = Cloudwalk::Application.find(xml2posxml(app_local["name"]))

      config << build_application(app, app_local["version"], app_local["modules"])
    else
      raise Cloudwalk::CwFileJsonException.new("App (#{app_local["name"]}) Version (#{app_local["version"]}) not found")
    end
  end

  self.lock = config
end

.persist_lock!Object



110
111
112
113
114
# File 'lib/cloudwalk/cw_file_json.rb', line 110

def self.persist_lock!
  File.open(CW_FILE_LOCK_PATH, "w") do |file|
    file.write(JSON.pretty_generate(self.lock_build))
  end
end

.ruby?Boolean

Returns:

  • (Boolean)


166
167
168
169
170
# File 'lib/cloudwalk/cw_file_json.rb', line 166

def self.ruby?
  if self.cwfile
    self.cwfile["runtime"] == "ruby"
  end
end

.setup(without_lock_check = false) ⇒ Object

Load Scenarios

  1. Pure true

  • Cwfile.json exists.

  • Cwfile.json.lock exists.

  • json and lock are the same

R: Just execute :)

2.

  • Cwfile.json exists.

  • Cwfile.json.lock exists.

  • json and lock aren’t the same

Response:

WARN: Warning Cwfile.json and Cwfile.json.lock are different, please fix, follow differences:

User Actions:

- The user can delete Cwfile.json.lock
- The user can fix Cwfile.json

Scenarios:

- The user could create a version on manager and just updated Cwfile.json, instead of delete Cwfile.json.lock
  - Maybe create an update action, rake cloudwalk:update
- The user

3.

  • Cwfile.json exists.

  • Cwfile.json.lock not exists.

R: Create Cwfile.json.lock

4.

  • Cwfile.json not exists.

  • Cwfile.json.lock not exists.

R: ASK: Cwfile.json not exists, should I create a skeleton or get the last versions available for the files we have here?



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cloudwalk/cw_file_json.rb', line 92

def self.setup(without_lock_check = false)
  if self.cwfile = self.convert_old_cwfile(load_cwfile)
    if without_lock_check
      true
    elsif CwFileJson.exists_lock?
      if self.lock = load_cwfile_lock
        self.compare
      end
    else
      persist_lock!
    end
  end
end

Instance Method Details

#compare_modules(local_app, module_ids) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/cloudwalk/cw_file_json.rb', line 220

def compare_modules(local_app, module_ids)
  local_module_app_ids = local_app["modules"].inject([]) do |mods, app|
    mods << app["id"]
  end

  module_app_ids = module_ids.inject([]) do |mods, app|
    mods << app["app_id"]
  end

  local_creation = local_module_app_ids - module_app_ids
  remote_creation = module_app_ids - local_module_app_ids

  if local_creation.nil?
    raise ApplicationConfigError.new("Application #{local_app["name"]}: Local modules are missing #{local_creation}")
  end

  if remote_creation.nil?
    raise ApplicationConfigError.new("Application #{local_app["name"]}: Remote modules are missing #{local_creation}")
  end
end

#update_apps(list, apps, config) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/cloudwalk/cw_file_json.rb', line 243

def update_apps(list, apps, config)
  all = Cloudwalk::Posxml::PosxmlApplication.all
  list.each do |app, version|
    local_app = apps.find { |json| json["name"] == app }

    if app[-4..-1] == ".xml" # Check if is posxml application
      remote_app = all.find { |app_json| app_json["posxml_app"]["name"] == xml2posxml(app) }

      if remote_app
        remote_posxml_app   = remote_app["posxml_app"]
        remote_versions     = Cloudwalk::ApplicationVersion.all(remote_posxml_app["id"])
        remote_version_json = remote_versions.find { |json| json["app_version"]["number"] == version }

        if remote_version_json && (remote_version = remote_version_json["app_version"])
          remote_version_detail = Cloudwalk::ApplicationVersion.get(remote_posxml_app["id"], remote_version["id"])
          # TODO: Check if application exists locally
          build_application(local_app, config, remote_posxml_app, remote_version, remote_version_detail["app_version"]["module_ids"])
        else
          # TODO versions not found, what to do?
        end
      else
        # TODO app not found, what to do?
      end
    else
      # Ruby flow
    end
  end
end