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"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cwfileObject

Returns the value of attribute cwfile.



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

def cwfile
  @cwfile
end

#lockObject

Returns the value of attribute lock.



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

def lock
  @lock
end

Class Method Details

.build_application(posxml_app, version_remote, modules_remote) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cloudwalk/cw_file_json.rb', line 140

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



129
130
131
132
133
134
135
136
137
138
# File 'lib/cloudwalk/cw_file_json.rb', line 129

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

.compareObject

TODO future!



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

def self.compare
  cwfile_list = self.cwfile.inject({}) do |hash, app|
    hash[app["name"]] = app["modules"].inject({}) do |hash_m, app_module|
      hash_m[app_module[0]] = app_module[1]
      hash_m
    end
    hash
  end

  lock_list = self.lock.inject({}) do |hash, app|
    hash[posxml2xml(app["name"])] = app["modules"].inject({}) do |hash_m, app_module|
      hash_m[posxml2xml(app_module["name"])] = app_module["version"]
      hash_m
    end
    hash
  end

  cwfile_list.to_a - lock_list.to_a
end

.delete_lock!Object



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

def self.delete_lock!
  FileUtils.rm_rf CW_FILE_LOCK_PATH
end

.deploy(outs) ⇒ Object

TODO Check CRC



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

def self.deploy(outs)
  outs.each do |path|
    p path
    posxml = path.split("/").last
    #app    = posxml2xml(posxml)

    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

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

.exists?Boolean

Returns:

  • (Boolean)


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

def self.exists?
  File.exists? CW_FILE_PATH
end

.exists_lock?Boolean

Returns:

  • (Boolean)


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

def self.exists_lock?
  File.exists? CW_FILE_LOCK_PATH
end

.load_cwfileObject



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

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



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

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



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

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) }
    app_posxml = app_remote["posxml_app"]
    versions   = Cloudwalk::PosxmlVersion.all(app_posxml["id"])
    version    = versions.find { |json| json["app_version"]["number"] == version }

    if version && version = version["app_version"]
      version_detail = PosxmlApplication::Version.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



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

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

.posxml2xml(str) ⇒ Object



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

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?



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cloudwalk/cw_file_json.rb', line 81

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
        difference = self.compare
        if difference.empty?
          true
        else
          puts "Warning Cwfile.json and Cwfile.json.lock are different, follow differences:"
          puts difference
          false
        end
      end
    else
      persist_lock!
    end
  end
end

.xml2posxml(str) ⇒ Object



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

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

Instance Method Details

#compare_modules(local_app, module_ids) ⇒ Object



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

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



242
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
# File 'lib/cloudwalk/cw_file_json.rb', line 242

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 = PosxmlApplication::Version.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