Class: Cloudwalk::CwFileJson

Inherits:
Object
  • Object
show all
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

Class Attribute Details

.cwfileObject

Returns the value of attribute cwfile.



28
29
30
# File 'lib/cloudwalk/cw_file_json.rb', line 28

def cwfile
  @cwfile
end

.lockObject

Returns the value of attribute lock.



28
29
30
# File 'lib/cloudwalk/cw_file_json.rb', line 28

def lock
  @lock
end

Class Method Details

.build_application(posxml_app, version_remote, modules_remote) ⇒ Object



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

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

  #old = config.find {|app| app["id"] == posxml_app["id"]}

  #if old
    #config.delete(old)
    #config << new
  #else
    #config << new
  #end
end

.build_module(mod) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/cloudwalk/cw_file_json.rb', line 124

def self.build_module(mod)
  response = Cloudwalk::PosxmlVersion.get(mod["app_id"], mod["version_id"])
  module_version = response["app_version"]
  {
    "name"       => Cloudwalk::PosxmlApplication.get_name(module_version["app_id"]),
    "version"    => module_version["number"],
    "id"         => module_version["app_id"],
    "version_id" => module_version["id"]
  }
end

.compareObject

TODO future!



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/cloudwalk/cw_file_json.rb', line 198

def self.compare
  cwfile_list = []
  self.cwfile["apps"].each do |app|
    cwfile_list << [app["name"], app["version"]]
    app["modules"].each do |app_module|
      cwfile_list << [app_module[0], app_module[1]]
    end
  end

  lock_list = []
  self.lock.each do |app|
    lock_list << [posxml2xml(app["name"]), app["version"]]
    app["modules"].each do |app_module|
      lock_list << [posxml2xml(app_module["name"]), posxml2xml(app_module["version"])]
    end
  end

  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

.delete_lock!Object



97
98
99
# File 'lib/cloudwalk/cw_file_json.rb', line 97

def self.delete_lock!
  FileUtils.rm_rf CW_FILE_LOCK_PATH
end

.deploy(outs) ⇒ Object

TODO Check CRC



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cloudwalk/cw_file_json.rb', line 155

def self.deploy(outs)
  outs.each do |path|
    posxml   = path.split("/").last
    app_lock = self.lock.find {|config| config["name"] == posxml }

    unless app_lock
      self.lock.each do |application|
        app_lock = application["modules"].find {|config| config["name"] == posxml }
        break if app_lock
      end
    end

    unless app_lock
      # TODO Improve!
      raise Exception.new("application or module #{path} not found in Manager, please create it")
    end

    app_cwfile = self.cwfile["apps"].find {|config| config["name"] == posxml2xml(posxml) }

    Cloudwalk::PosxmlVersion.update(
      app_lock["id"], app_lock["version_id"], File.read(path), app_cwfile
    )
  end
end

.exists?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/cloudwalk/cw_file_json.rb', line 180

def self.exists?
  File.exists? CW_FILE_PATH
end

.exists_lock?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/cloudwalk/cw_file_json.rb', line 184

def self.exists_lock?
  File.exists? CW_FILE_LOCK_PATH
end

.load_cwfileObject



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

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



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

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_buildObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cloudwalk/cw_file_json.rb', line 105

def self.lock_build
  all = Cloudwalk::PosxmlApplication.all
  config = []
  self.cwfile["apps"].each do |app_local|
    app_remote = all.find { |app_json| app_json["posxml_app"]["name"] == xml2posxml(app_local["name"]) }
    app_posxml = app_remote["posxml_app"]
    versions   = Cloudwalk::PosxmlVersion.all(app_posxml["id"])
    version    = versions.find { |json| json["app_version"]["number"] == app_local["version"] }

    if version && version = version["app_version"]
      version_detail = Cloudwalk::PosxmlVersion.get(app_posxml["id"], version["id"])
      config << build_application(app_posxml, version, version_detail["app_version"]["module_ids"])
    else
      # TODO Version not found, what to do?
    end
  end
  self.lock = config
end

.persist_lock!Object



101
102
103
# File 'lib/cloudwalk/cw_file_json.rb', line 101

def self.persist_lock!
  File.open(CW_FILE_LOCK_PATH, "w") {|f| f.write(self.lock_build.to_json) }
end

.posxml2xml(str) ⇒ Object



188
189
190
# File 'lib/cloudwalk/cw_file_json.rb', line 188

def self.posxml2xml(str)
  str.sub(".posxml", ".xml")
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?



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

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

.xml2posxml(str) ⇒ Object



192
193
194
# File 'lib/cloudwalk/cw_file_json.rb', line 192

def self.xml2posxml(str)
  str.sub(".xml", ".posxml")
end

Instance Method Details

#compare_modules(local_app, module_ids) ⇒ Object



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

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



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
277
278
279
# File 'lib/cloudwalk/cw_file_json.rb', line 252

def update_apps(list, apps, config)
  all = Cloudwalk::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::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::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