cloudwalk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 79963e617cac091d5ef17808d7026f188063de7a
4
+ data.tar.gz: 86e13a022581f855401797c1f3e7c9ba603ef2f5
5
+ SHA512:
6
+ metadata.gz: 7f0a5e91a63c3ee755dd745381c44fb060bca2d7c393d048a5bf0b4e2ef269d5d0733b4c3824f54557570f2ccd3d2531490151e851040e94e45466c0d7b76186
7
+ data.tar.gz: 1de02728f1b568968c1194732f182b5eaa57379867d101a617291d6b138ede5cac39a2619e882158bbc543d99396bf09ff2a9108ce8ed52715c9e543326beac8
data/lib/cloudwalk.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "base64"
2
+ require "net/http"
3
+ require "json"
4
+ require "fileutils"
5
+
6
+ require_relative "cloudwalk/posxml_application"
7
+ require_relative "cloudwalk/posxml_version"
8
+ require_relative "cloudwalk/rake_task"
@@ -0,0 +1,16 @@
1
+ module Cloudwalk
2
+ class Config
3
+ def self.token
4
+ @token ||= `cloudwalk config token`.chomp
5
+ raise ManagerException.new("Token not found, try 'cloudwalk login'") if @token.empty?
6
+ @token
7
+ end
8
+
9
+ def self.host
10
+ @host ||= `cloudwalk config host`.chomp
11
+ raise ManagerException.new("Token not found, try 'cloudwalk login'") if @host.empty?
12
+ @host
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,271 @@
1
+ #lib/pos/base.rb: json_versions = `curl -k -X GET \"https://#{host}/v1/apps/posxml/#{cw_main_id}/versions?access_token=#{token}\"`
2
+ #lib/pos/package_util.rb: json_version = `curl -k -X GET \"https://#{host}/v1/apps/posxml/#{app_id}/versions/#{version_id}?access_token=#{token}\"`
3
+ #lib/pos/package_util.rb: app_json = `curl -k -X GET \"https://#{host}/v1/apps/posxml/#{app_id}/?access_token=#{token}\"`
4
+ #lib/pos/package_util.rb: file_json = `curl -k -X GET \"https://#{host}/v1/files/#{file_id}/?access_token=#{token}\"`
5
+ #
6
+ # - Lock is a file that contains the id of applications.
7
+ #
8
+ # What to do:
9
+ #
10
+ # 1. Check if lock exists and create it.
11
+ # - If not update create .lock by checking applications
12
+ # 2. Check if all applications has an id.
13
+ #
14
+ # Commands
15
+ # - "cloudwalk app upgrade-version cw_main.xml 1.10.10..1.2.0"
16
+ # - Can be used for a module
17
+ # - Change Cwfile.json and Cwfile.json.lock
18
+ # - rake cloudwalk:deploy
19
+ # - Read Cwfile.json
20
+ # - Check if apps and versions are ok
21
+
22
+ module Cloudwalk
23
+ class CwFileJson
24
+ CW_FILE_LOCK_PATH = "./Cwfile.json.lock"
25
+ CW_FILE_PATH = "./Cwfile.json"
26
+
27
+ attr_accessor :cwfile, :lock
28
+
29
+ def self.load_cwfile_lock
30
+ JSON.parse(File.read(CW_FILE_LOCK_PATH))
31
+ rescue Errno::ENOENT
32
+ false
33
+ rescue JSON::ParserError
34
+ puts "Error to read Cwfile.json.lock"
35
+ false
36
+ end
37
+
38
+ def self.load_cwfile
39
+ JSON.parse(File.read(CW_FILE_PATH))
40
+ rescue Errno::ENOENT
41
+ puts "Cwfile.json not found"
42
+ false
43
+ rescue JSON::ParserError
44
+ puts "Error to read Cwfile.json"
45
+ false
46
+ end
47
+
48
+ # Load Scenarios
49
+ # 1. Pure true
50
+ # - Cwfile.json exists.
51
+ # - Cwfile.json.lock exists.
52
+ # - json and lock are the same
53
+ # R: Just execute :)
54
+ #
55
+ # 2.
56
+ # - Cwfile.json exists.
57
+ # - Cwfile.json.lock exists.
58
+ # - json and lock aren't the same
59
+ #
60
+ # Response:
61
+ # WARN: Warning Cwfile.json and Cwfile.json.lock are different, please fix, follow differences:
62
+ #
63
+ # User Actions:
64
+ # - The user can delete Cwfile.json.lock
65
+ # - The user can fix Cwfile.json
66
+ #
67
+ # Scenarios:
68
+ # - The user could create a version on manager and just updated Cwfile.json, instead of delete Cwfile.json.lock
69
+ # - Maybe create an update action, rake cloudwalk:update
70
+ # - The user
71
+ #
72
+ # 3.
73
+ # - Cwfile.json exists.
74
+ # - Cwfile.json.lock not exists.
75
+ # R: Create Cwfile.json.lock
76
+ #
77
+ # 4.
78
+ # - Cwfile.json not exists.
79
+ # - Cwfile.json.lock not exists.
80
+ # R: ASK: Cwfile.json not exists, should I create a skeleton or get the last versions available for the files we have here?
81
+ def self.setup(without_lock_check = false)
82
+ if self.cwfile = load_cwfile
83
+ if without_lock_check
84
+ true
85
+ elsif CwFileJson.exists_lock?
86
+ if self.lock = load_cwfile_lock
87
+ difference = self.compare
88
+ if difference.empty?
89
+ true
90
+ else
91
+ puts "Warning Cwfile.json and Cwfile.json.lock are different, follow differences:"
92
+ puts difference
93
+ false
94
+ end
95
+ end
96
+ else
97
+ persist_lock!
98
+ end
99
+ end
100
+ end
101
+
102
+ def self.delete_lock!
103
+ FileUtils.rm_rf CW_FILE_LOCK_PATH
104
+ end
105
+
106
+ def self.persist_lock!
107
+ File.open(CW_FILE_LOCK_PATH, "w") {|f| f.write(self.lock_build.to_json) }
108
+ end
109
+
110
+ def self.lock_build
111
+ all = Cloudwalk::PosxmlApplication.all
112
+ config = []
113
+ self.cwfile["apps"].each do |app_local|
114
+ app_remote = all.find { |app_json| app_json["posxml_app"]["name"] == xml2posxml(app) }
115
+ app_posxml = app_remote["posxml_app"]
116
+ versions = Cloudwalk::PosxmlVersion.all(app_posxml["id"])
117
+ version = versions.find { |json| json["app_version"]["number"] == version }
118
+
119
+ if version && version = version["app_version"]
120
+ version_detail = PosxmlApplication::Version.get(app_posxml["id"], version["id"])
121
+ config << build_application(app_posxml, version, version_detail["app_version"]["module_ids"])
122
+ else
123
+ # TODO Version not found, what to do?
124
+ end
125
+ end
126
+ self.lock = config
127
+ end
128
+
129
+ def self.build_module(mod)
130
+ response = PosxmlApplication::Version.get(mod["app_id"], mod["version_id"])
131
+ module_version = response["app_version"]
132
+ {
133
+ "name" => PosxmlApplication::Apps.get_name(module_version["app_id"]),
134
+ "version" => module_version["number"],
135
+ "id" => module_version["app_id"],
136
+ "version_id" => module_version["id"]
137
+ }
138
+ end
139
+
140
+ def self.build_application(posxml_app, version_remote, modules_remote)
141
+ {
142
+ "name" => posxml_app["name"],
143
+ "id" => posxml_app["id"],
144
+ "modules" => modules_remote.collect {|mod| build_module(mod)},
145
+ "version" => version_remote["number"],
146
+ "version_id" => version_remote["id"]
147
+ }
148
+
149
+ #old = config.find {|app| app["id"] == posxml_app["id"]}
150
+
151
+ #if old
152
+ #config.delete(old)
153
+ #config << new
154
+ #else
155
+ #config << new
156
+ #end
157
+ end
158
+
159
+ # TODO Check CRC
160
+ def self.deploy(outs)
161
+ outs.each do |path|
162
+ p path
163
+ posxml = path.split("/").last
164
+ #app = posxml2xml(posxml)
165
+
166
+ app_lock = self.lock.find {|config| config["name"] == posxml }
167
+
168
+ unless app_lock
169
+ self.lock.each do |application|
170
+ app_lock = application["modules"].find {|config| config["name"] == posxml }
171
+ break if app_lock
172
+ end
173
+ end
174
+
175
+ Cloudwalk::PosxmlVersion.update(
176
+ app_lock["id"], app_lock["version_id"], File.read(path)
177
+ )
178
+ end
179
+ end
180
+
181
+ def self.exists?
182
+ File.exists? CW_FILE_PATH
183
+ end
184
+
185
+ def self.exists_lock?
186
+ File.exists? CW_FILE_LOCK_PATH
187
+ end
188
+
189
+ def self.posxml2xml(str)
190
+ str.sub(".posxml", ".xml")
191
+ end
192
+
193
+ def self.xml2posxml(str)
194
+ str.sub(".xml", ".posxml")
195
+ end
196
+
197
+ # TODO future!
198
+
199
+ def self.compare
200
+ cwfile_list = self.cwfile.inject({}) do |hash, app|
201
+ hash[app["name"]] = app["modules"].inject({}) do |hash_m, app_module|
202
+ hash_m[app_module[0]] = app_module[1]
203
+ hash_m
204
+ end
205
+ hash
206
+ end
207
+
208
+ lock_list = self.lock.inject({}) do |hash, app|
209
+ hash[posxml2xml(app["name"])] = app["modules"].inject({}) do |hash_m, app_module|
210
+ hash_m[posxml2xml(app_module["name"])] = app_module["version"]
211
+ hash_m
212
+ end
213
+ hash
214
+ end
215
+
216
+ cwfile_list.to_a - lock_list.to_a
217
+ end
218
+
219
+ def compare_modules(local_app, module_ids)
220
+ local_module_app_ids = local_app["modules"].inject([]) do |mods, app|
221
+ mods << app["id"]
222
+ end
223
+
224
+ module_app_ids = module_ids.inject([]) do |mods, app|
225
+ mods << app["app_id"]
226
+ end
227
+
228
+ local_creation = local_module_app_ids - module_app_ids
229
+ remote_creation = module_app_ids - local_module_app_ids
230
+
231
+ if local_creation.nil?
232
+ raise ApplicationConfigError.new("Application #{local_app["name"]}: Local modules are missing #{local_creation}")
233
+ end
234
+
235
+ if remote_creation.nil?
236
+ raise ApplicationConfigError.new("Application #{local_app["name"]}: Remote modules are missing #{local_creation}")
237
+ end
238
+ end
239
+
240
+ # curl -X GET "https://api-staging.cloudwalk.io/v1/apps/posxml?access_token=d79db7b1dc59c9c5a1f369478444625a0d1adef8"
241
+ # curl -X GET "https://manager.cloudwalk.io/v1/apps/posxml/3082/versions?access_token=d79db7b1dc59c9c5a1f369478444625a0d1adef8"
242
+ def update_apps(list, apps, config)
243
+ all = Cloudwalk::PosxmlApplication.all
244
+ list.each do |app, version|
245
+ local_app = apps.find { |json| json["name"] == app }
246
+
247
+ if app[-4..-1] == ".xml" # Check if is posxml application
248
+ remote_app = all.find { |app_json| app_json["posxml_app"]["name"] == xml2posxml(app) }
249
+
250
+ if remote_app
251
+ remote_posxml_app = remote_app["posxml_app"]
252
+ remote_versions = Cloudwalk::PosxmlVersion.all(remote_posxml_app["id"])
253
+ remote_version_json = remote_versions.find { |json| json["app_version"]["number"] == version }
254
+
255
+ if remote_version_json && (remote_version = remote_version_json["app_version"])
256
+ remote_version_detail = PosxmlApplication::Version.get(remote_posxml_app["id"], remote_version["id"])
257
+ # TODO: Check if application exists locally
258
+ build_application(local_app, config, remote_posxml_app, remote_version, remote_version_detail["app_version"]["module_ids"])
259
+ else
260
+ # TODO versions not found, what to do?
261
+ end
262
+ else
263
+ # TODO app not found, what to do?
264
+ end
265
+ else
266
+ # Ruby flow
267
+ end
268
+ end
269
+ end
270
+ end
271
+ end
@@ -0,0 +1,4 @@
1
+ module Cloudwalk
2
+ class ManagerException < StandardError
3
+ end
4
+ end
@@ -0,0 +1,44 @@
1
+ module Cloudwalk
2
+ class PosxmlApplication
3
+ def self.token
4
+ Cloudwalk::Config.token
5
+ end
6
+
7
+ def self.host
8
+ Cloudwalk::Config.host
9
+ end
10
+
11
+ def self.all
12
+ if @apps
13
+ @apps
14
+ else
15
+ response = JSON.parse(Net::HTTP.get(URI("https://#{self.host}/v1/apps/posxml?access_token=#{self.token}&per_page=100")))
16
+ raise ManagerException.new(response["message"]) if response["message"]
17
+
18
+ total_pages = response["pagination"]["total_pages"].to_i
19
+ apps = response["posxmlapps"]
20
+ (total_pages - 1).times do |page|
21
+ url = "https://#{self.host}/v1/apps/posxml?access_token=#{self.token}&per_page=100&page=#{page+2}"
22
+ response = JSON.parse(Net::HTTP.get(URI(url)))
23
+ raise ManagerException.new(response["message"]) if response["message"]
24
+
25
+ apps.concat(response["posxmlapps"])
26
+ end
27
+ @apps = apps
28
+ end
29
+ end
30
+
31
+ def self.get(id)
32
+ url = "https://#{self.host}/v1/apps/posxml/#{id}?access_token=#{self.token}"
33
+ response = JSON.parse(Net::HTTP.get(URI(url)))
34
+ raise ManagerException.new(response["message"]) if response["message"]
35
+
36
+ response["posxml_app"]
37
+ end
38
+
39
+ def self.get_name(id)
40
+ get(id)["name"]
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,57 @@
1
+ module Cloudwalk
2
+ class PosxmlVersion
3
+ def self.token
4
+ Cloudwalk::Config.token
5
+ end
6
+
7
+ def self.host
8
+ Cloudwalk::Config.host
9
+ end
10
+
11
+ def self.get_or_create(app, version)
12
+ response = JSON.parse(Net::HTTP.get(URI("https://#{self.host}/v1/apps/posxml/#{app_id}/versions?access_token=#{self.token}&per_page=100")))
13
+ raise ManagerException.new(response["message"]) if response["message"]
14
+
15
+ #TODO
16
+ #`curl -X GET "https://api-staging.cloudwalk.io/v1/apps/posxml/3082/versions?access_token=#{self.token}`
17
+ end
18
+
19
+ def self.all(app_id)
20
+ response = JSON.parse(Net::HTTP.get(URI("https://#{self.host}/v1/apps/posxml/#{app_id}/versions?access_token=#{self.token}&per_page=100")))
21
+ raise ManagerException.new(response["message"]) if response["message"]
22
+
23
+ total_pages = response["pagination"]["total_pages"].to_i
24
+ versions = response["appversions"]
25
+
26
+ (total_pages - 1).times do |page|
27
+ url = "https://#{self.host}/v1/apps/posxml/#{app_id}/versions?access_token=#{self.token}&per_page=100&page=#{page+2}"
28
+ response = JSON.parse(Net::HTTP.get(URI(url)))
29
+ raise ManagerException.new(response["message"]) if response["message"]
30
+
31
+ versions.concat(response["posxmlapps"])
32
+ end
33
+ versions
34
+ end
35
+
36
+ def self.get(app_id, id)
37
+ url = "https://#{self.host}/v1/apps/posxml/#{app_id}/versions/#{id}?access_token=#{token}"
38
+ response = JSON.parse(Net::HTTP.get(URI(url)))
39
+ raise ManagerException.new(response["message"]) if response["message"]
40
+
41
+ response
42
+ end
43
+
44
+ def self.update(app_id, version_id, bytecode)
45
+ url = "#{self.host}/v1/apps/posxml/#{app_id}/versions/#{version_id}?access_token=#{self.token}"
46
+ uri = URI(url)
47
+ uri.encode_www_form({"bytecode" => Base64.strict_encode64(bytecode)})
48
+
49
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
50
+ request = Net::HTTP::Put.new(uri)
51
+ response = http.request request
52
+ end
53
+ p response
54
+ end
55
+ end
56
+ end
57
+
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'rake'
4
+ require 'rake/tasklib'
5
+ require 'fileutils'
6
+ require 'bundler/setup'
7
+
8
+ # Tasks:
9
+ # cloudwalk:build (Will build mruby or posxml application)
10
+ # cloddwalk:deploy (Send Posxml or Ruby application)
11
+ # cloddwalk:update (Update Cwfile.lock)
12
+ # cloudwalk:package - maybe not
13
+
14
+ module Cloudwalk
15
+ class RakeTask < ::Rake::TaskLib
16
+ include ::Rake::DSL if defined?(::Rake::DSL)
17
+
18
+ attr_accessor :libs, :root_path, :main_out, :out_path
19
+
20
+ def initialize
21
+ yield self if block_given?
22
+
23
+ @libs ||= FileList['lib/*.xml']
24
+ @root_path ||= "./"
25
+ @out_path ||= File.join(root_path, "out", "shared")
26
+ @outs ||= @libs.pathmap("%{lib,#{@out_path}}p")
27
+
28
+ define
29
+ end
30
+
31
+ def platform_call(command)
32
+ if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM)
33
+ sh("bash -c \"#{command}\"")
34
+ else
35
+ sh command
36
+ end
37
+ end
38
+
39
+ def define
40
+ namespace :cloudwalk do
41
+ desc "Compile posxml"
42
+ task :build do
43
+ FileUtils.rm_rf self.out_path
44
+ FileUtils.mkdir_p self.out_path
45
+ self.libs.zip(self.outs).each do |file, out|
46
+ posxml = CwFileJson.xml2posxml(out)
47
+ puts "cloudwalk compile -xml -o #{posxml} #{file}"
48
+ platform_call "cloudwalk compile -xml -o #{posxml} #{file}"
49
+ end
50
+ end
51
+
52
+ desc "Deploy all compiled applications based in Cwfile.json"
53
+ task :deploy => :build do
54
+ if Cloudwalk::CwFileJson.setup
55
+ Cloudwalk::CwFileJson.deploy(self.outs)
56
+ end
57
+ end
58
+
59
+ desc "Update CwFile.json.lock"
60
+ task :update do
61
+ Cloudwalk::CwFileJson.delete_lock!
62
+ Cloudwalk::CwFileJson.setup(true)
63
+ Cloudwalk::CwFileJson.persist_lock!
64
+ end
65
+
66
+ task :default => :build
67
+ end
68
+ end
69
+ end
70
+ end
71
+
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudwalk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thiago Scalone
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '10.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '10.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ description: CloudWalk CLI for posxml and ruby applications.
42
+ email:
43
+ - thiago@cloudwalk.io
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/cloudwalk.rb
49
+ - lib/cloudwalk/config.rb
50
+ - lib/cloudwalk/cw_file_json.rb
51
+ - lib/cloudwalk/manager_exception.rb
52
+ - lib/cloudwalk/posxml_application.rb
53
+ - lib/cloudwalk/posxml_version.rb
54
+ - lib/cloudwalk/rake_task.rb
55
+ homepage: https://cloudwalk.io/cli
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - mrblib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 1.9.3
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.2.2
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: CLI for Cloudwalk projects
79
+ test_files: []