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(type, app, version = nil, modules_remote = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cloudwalk/cw_file_json.rb', line 145

def self.build_application(type, app, version = nil, modules_remote = nil)
  if type == :ruby
    {
      "name"       => app["name"],
      "id"         => app["id"],
      "modules"    => [],
      "version"    => "1.0.0"
    }
  else
    {
      "name"       => app["name"],
      "id"         => app["id"],
      "modules"    => modules_remote.collect {|mod| build_module(mod)},
      "version"    => version["number"],
      "version_id" => version["id"]
    }
  end
end

.build_module(mod) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cloudwalk/cw_file_json.rb', line 132

def self.build_module(mod)
  if module_version = Cloudwalk::Posxml::PosxmlVersion.get(mod["app_id"], mod["version_id"])
    {
      "name"       => Cloudwalk::Posxml::PosxmlApplication.get_name(module_version["app_id"]),
      "version"    => module_version["number"],
      "id"         => module_version["app_id"],
      "version_id" => module_version["id"]
    }
  else
    raise Cloudwalk::CwFileJsonException.new("App (#{mod['app_id']}) Module Version (#{mod['version_id']}) not found")
  end
end

.compareObject

TODO future!



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/cloudwalk/cw_file_json.rb', line 208

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

.cwfile_apps_and_versionsObject



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cloudwalk/cw_file_json.rb', line 178

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



99
100
101
# File 'lib/cloudwalk/cw_file_json.rb', line 99

def self.delete_lock!
  FileUtils.rm_rf CW_FILE_LOCK_PATH
end

.exists?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/cloudwalk/cw_file_json.rb', line 164

def self.exists?
  File.exists? CW_FILE_PATH
end

.exists_lock?Boolean

Returns:

  • (Boolean)


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

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



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/cloudwalk/cw_file_json.rb', line 192

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cloudwalk/cw_file_json.rb', line 109

def self.lock_build
  config = []
  if self.ruby?
    if app = Cloudwalk::Ruby::RubyApplication.find(self.cwfile["name"])
      config << build_application(:ruby, app)
    else
      # TODO App not found, what to do?
    end
  else
    self.cwfile["apps"].each do |app_local|
      app, version = Cloudwalk::Posxml::PosxmlVersion.find(app_local["name"], app_local["version"])
      if app && version
        detail = Cloudwalk::Posxml::PosxmlVersion.get(app["id"], version["id"])
        config << build_application(:posxml, app, version, detail["module_ids"])
      else
        raise Cloudwalk::CwFileJsonException.new("App (#{app_local["name"]}) Version (#{app_local["version"]}) not found")
      end
    end
  end

  self.lock = config
end

.persist_lock!Object



103
104
105
106
107
# File 'lib/cloudwalk/cw_file_json.rb', line 103

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)


172
173
174
175
176
# File 'lib/cloudwalk/cw_file_json.rb', line 172

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?



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cloudwalk/cw_file_json.rb', line 85

def self.setup(without_lock_check = false)
  if self.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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/cloudwalk/cw_file_json.rb', line 226

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



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/cloudwalk/cw_file_json.rb', line 249

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::Posxml::PosxmlVersion.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::Posxml::PosxmlVersion.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