cnvrg 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/cnvrg/files.rb CHANGED
@@ -1,116 +1,208 @@
1
1
  require 'mimemagic'
2
+ require 'aws-sdk'
3
+ require 'URLcrypt'
4
+
5
+
2
6
  module Cnvrg
3
- class Files
4
-
5
- attr_reader :base_resource
6
-
7
- def initialize(owner, project_slug)
8
- @project_slug = project_slug
9
- @owner = owner
10
- @base_resource = "users/#{owner}/projects/#{project_slug}/"
11
- end
7
+ class Files
12
8
 
13
- def upload_file(absolute_path, relative_path, commit_sha1)
14
- file_name = File.basename relative_path
15
- file_size = File.size(relative_path).to_f
16
- mime_type = MimeMagic.by_path(relative_path)
17
- content_type = !mime_type.nil? ? mime_type.type : "text/plain"
18
- upload_resp = Cnvrg::API.request(@base_resource + "upload_file", 'POST_FILE', { absolute_path: absolute_path, relative_path: relative_path,
19
- commit_sha1: commit_sha1, file_name: file_name ,
20
- file_size:file_size,file_content_type:content_type})
21
- if Cnvrg::CLI.is_response_success(upload_resp, false)
22
- path = upload_resp["result"]["path"]
23
- s3_res = upload_s3(path,relative_path,content_type)
24
- if s3_res
25
- Cnvrg::API.request(@base_resource + "update_s3", 'POST', { path:path,commit_id:upload_resp["result"]["commit_id"],
26
- blob_id:upload_resp["result"]["blob_id"]})
27
- return true
28
- end
29
- end
30
- return false
31
- end
32
- def upload_s3(url,file,content_type)
33
- url = URI.parse(url)
34
-
35
- file = File.open(file, "rb")
36
- body = file.read
37
- begin
38
- Net::HTTP.start(url.host) do |http|
39
- http.send_request("PUT", url.request_uri, body, {
40
- "content-type" => content_type,
41
- })
42
- end
43
- return true
44
- rescue Interrupt
45
- return false
46
- rescue => e
47
- return false
48
- end
49
- end
50
- def upload_url(file_path)
51
- response = Cnvrg::API.request(@base_resource + "upload_url", 'POST', { file_s3_path: file_path})
52
- if Cnvrg::CLI.is_response_success(response, false)
53
- return response
54
- else
55
- return nil
56
- end
9
+ LARGE_FILE=1024*1024*100
10
+ attr_reader :base_resource
57
11
 
58
- end
59
- def delete_file(absolute_path, relative_path, commit_sha1)
60
- response = Cnvrg::API.request(@base_resource + "delete_file", 'DELETE',{ absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1 })
61
- return Cnvrg::CLI.is_response_success(response, false)
62
- end
12
+ def initialize(owner, project_slug)
13
+ @project_slug = project_slug
14
+ @owner = owner
15
+ @base_resource = "users/#{owner}/projects/#{project_slug}/"
16
+ end
63
17
 
64
- def delete_dir(absolute_path, relative_path, commit_sha1)
65
- response = Cnvrg::API.request(@base_resource + "delete_dir", 'DELETE', { absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1 })
66
- return Cnvrg::CLI.is_response_success(response, false)
18
+ def upload_file(absolute_path, relative_path, commit_sha1)
19
+ file_name = File.basename relative_path
20
+ file_size = File.size(relative_path).to_f
21
+ mime_type = MimeMagic.by_path(relative_path)
22
+ content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain"
23
+ upload_resp = Cnvrg::API.request(@base_resource + "upload_file", 'POST_FILE', {absolute_path: absolute_path, relative_path: relative_path,
24
+ commit_sha1: commit_sha1, file_name: file_name,
25
+ file_size: file_size, file_content_type: content_type})
26
+ if Cnvrg::CLI.is_response_success(upload_resp, false)
27
+ if file_size.to_f>= Cnvrg::Files::LARGE_FILE.to_f
28
+ s3_res = upload_large_files_s3(upload_resp, relative_path)
29
+ else
30
+ path = upload_resp["result"]["path"]
31
+ s3_res = upload_small_files_s3(path, relative_path, content_type)
67
32
  end
68
33
 
69
- def create_dir(absolute_path, relative_path, commit_sha1)
70
- response = Cnvrg::API.request(@base_resource + "create_dir", 'POST',{ absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1 })
71
- return Cnvrg::CLI.is_response_success(response, false)
34
+ if s3_res
35
+ Cnvrg::API.request(@base_resource + "update_s3", 'POST', {path: path, commit_id: upload_resp["result"]["commit_id"],
36
+ blob_id: upload_resp["result"]["id"]})
37
+ return true
72
38
  end
39
+ end
40
+ return false
41
+ end
42
+
43
+ def upload_image(absolute_path, image_name, owner)
44
+ file_name = File.basename absolute_path
45
+ file_size = File.size(absolute_path).to_f
46
+ content_type = "application/gzip"
73
47
 
74
- def download_file(absolute_path, relative_path, project_home, conflict=false)
75
- res = Cnvrg::API.request(@base_resource + "download_file", 'POST', { absolute_path: absolute_path, relative_path: relative_path })
76
- Cnvrg::CLI.is_response_success(res, false)
77
- if res["result"]
78
- res = res["result"]
79
- return false if res["link"].empty? or res["filename"].empty?
80
- filename = res["filename"]
81
- file_location = absolute_path.gsub(/#{filename}\/?$/, "")
82
-
83
- FileUtils.mkdir_p project_home + "/" + file_location
84
- filename += ".conflict" if conflict
85
-
86
- File.open("#{project_home}/#{file_location}/#{filename}", "wb") do |file|
87
- file.write open(res["link"]).read
88
- end
89
- else
90
- return false
91
- end
48
+ upload_resp = Cnvrg::API.request("users/#{owner}/images/" + "upload", 'POST_FILE', {relative_path: absolute_path,
49
+ file_name: file_name, image_name: image_name,
50
+ file_size: file_size, file_content_type: content_type})
51
+ # puts upload_resp
52
+ if Cnvrg::CLI.is_response_success(upload_resp, false)
53
+ s3_res = upload_large_files_s3(upload_resp, absolute_path)
54
+ if s3_res
55
+ commit_resp = Cnvrg::API.request("users/#{owner}/images/#{upload_resp["result"]["id"]}/" + "commit", 'GET')
56
+ if Cnvrg::CLI.is_response_success(commit_resp, false)
92
57
  return true
58
+ else
59
+ return false
60
+ end
61
+
93
62
  end
63
+ end
64
+ return false
65
+ end
66
+
67
+ def download_image(file_path_to_store, image_slug, owner)
94
68
 
95
- def download_dir(absolute_path, relative_path, project_home)
96
- FileUtils.mkdir_p("#{project_home}/#{absolute_path}")
69
+
70
+ download_resp = Cnvrg::API.request("users/#{owner}/images/#{image_slug}/" + "download", 'GET')
71
+ path = download_resp["result"]["path"]
72
+
73
+ if Cnvrg::CLI.is_response_success(download_resp, false)
74
+ begin
75
+ open(file_path_to_store, 'wb') do |file|
76
+ file << open(path).read
77
+ end
78
+ rescue
79
+ return false
97
80
  end
98
81
 
99
- def start_commit
82
+ return true
83
+ else
84
+ return false
85
+ end
86
+
100
87
 
101
- response = Cnvrg::API.request("#{base_resource}/commit/start", 'POST', { project_slug: @project_slug,
102
- username: @owner} )
103
- Cnvrg::CLI.is_response_success(response)
104
- return response
88
+ end
89
+
90
+ def upload_large_files_s3(upload_resp, file_path)
91
+ begin
92
+ sts_path = upload_resp["result"]["path_sts"]
93
+ uri = URI.parse(sts_path)
94
+ http_object = Net::HTTP.new(uri.host, uri.port)
95
+ http_object.use_ssl = true if uri.scheme == 'https'
96
+ request = Net::HTTP::Get.new(sts_path)
97
+ body = ""
98
+ http_object.start do |http|
99
+ response = http.request request
100
+ body = response.read_body
105
101
  end
102
+ URLcrypt::key = [body].pack('H*')
103
+
104
+ s3 = Aws::S3::Resource.new(
105
+ :access_key_id => URLcrypt.decrypt(upload_resp["result"]["sts_a"]),
106
+ :secret_access_key => URLcrypt.decrypt(upload_resp["result"]["sts_s"]),
107
+ :session_token => URLcrypt.decrypt(upload_resp["result"]["sts_st"]),
108
+ :region => URLcrypt.decrypt(upload_resp["result"]["region"]))
106
109
 
107
- def end_commit(commit_sha1)
108
- response = Cnvrg::API.request("#{base_resource}/commit/end", 'POST', { commit_sha1: commit_sha1 } )
109
- return response
110
+ resp = s3.bucket(URLcrypt.decrypt(upload_resp["result"]["bucket"])).object(File.basename(file_path)).upload_file(file_path)
111
+ return resp
112
+ rescue => e
113
+ puts e
114
+ return false
115
+
116
+ end
117
+ return true
118
+
119
+ end
120
+
121
+ def upload_small_files_s3(url, file, content_type)
122
+ url = URI.parse(url)
123
+ file = File.open(file, "rb")
124
+ body = file.read
125
+ begin
126
+ Net::HTTP.start(url.host) do |http|
127
+ http.send_request("PUT", url.request_uri, body, {
128
+ "content-type" => content_type,
129
+ })
110
130
  end
111
- def rollback_commit(commit_sha1)
112
- response = Cnvrg::API.request("#{base_resource}/commit/rollback", 'POST', { commit_sha1: commit_sha1 } )
113
- Cnvrg::CLI.is_response_success(response, false)
131
+ return true
132
+ rescue Interrupt
133
+ return false
134
+ rescue => e
135
+ puts e
136
+ return false
137
+ end
138
+ end
139
+
140
+ def upload_url(file_path)
141
+ response = Cnvrg::API.request(@base_resource + "upload_url", 'POST', {file_s3_path: file_path})
142
+ if Cnvrg::CLI.is_response_success(response, false)
143
+ return response
144
+ else
145
+ return nil
146
+ end
147
+
148
+ end
149
+
150
+ def delete_file(absolute_path, relative_path, commit_sha1)
151
+ response = Cnvrg::API.request(@base_resource + "delete_file", 'DELETE', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
152
+ return Cnvrg::CLI.is_response_success(response, false)
153
+ end
154
+
155
+ def delete_dir(absolute_path, relative_path, commit_sha1)
156
+ response = Cnvrg::API.request(@base_resource + "delete_dir", 'DELETE', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
157
+ return Cnvrg::CLI.is_response_success(response, false)
158
+ end
159
+
160
+ def create_dir(absolute_path, relative_path, commit_sha1)
161
+ response = Cnvrg::API.request(@base_resource + "create_dir", 'POST', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
162
+ return Cnvrg::CLI.is_response_success(response, false)
163
+ end
164
+
165
+ def download_file(absolute_path, relative_path, project_home, conflict=false)
166
+ res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {absolute_path: absolute_path, relative_path: relative_path})
167
+ Cnvrg::CLI.is_response_success(res, false)
168
+ if res["result"]
169
+ res = res["result"]
170
+ return false if res["link"].empty? or res["filename"].empty?
171
+ filename = res["filename"]
172
+ file_location = absolute_path.gsub(/#{filename}\/?$/, "")
173
+
174
+ FileUtils.mkdir_p project_home + "/" + file_location
175
+ filename += ".conflict" if conflict
176
+
177
+ File.open("#{project_home}/#{file_location}/#{filename}", "wb") do |file|
178
+ file.write open(res["link"]).read
114
179
  end
180
+ else
181
+ return false
182
+ end
183
+ return true
184
+ end
185
+
186
+ def download_dir(absolute_path, relative_path, project_home)
187
+ FileUtils.mkdir_p("#{project_home}/#{absolute_path}")
188
+ end
189
+
190
+ def start_commit
191
+
192
+ response = Cnvrg::API.request("#{base_resource}/commit/start", 'POST', {project_slug: @project_slug,
193
+ username: @owner})
194
+ Cnvrg::CLI.is_response_success(response)
195
+ return response
196
+ end
197
+
198
+ def end_commit(commit_sha1)
199
+ response = Cnvrg::API.request("#{base_resource}/commit/end", 'POST', {commit_sha1: commit_sha1})
200
+ return response
201
+ end
202
+
203
+ def rollback_commit(commit_sha1)
204
+ response = Cnvrg::API.request("#{base_resource}/commit/rollback", 'POST', {commit_sha1: commit_sha1})
205
+ Cnvrg::CLI.is_response_success(response, false)
115
206
  end
207
+ end
116
208
  end
data/lib/cnvrg/helpers.rb CHANGED
@@ -7,6 +7,13 @@ module Cnvrg
7
7
  return checkmark.encode('utf-8')
8
8
  end
9
9
 
10
+ def internet_connection?
11
+ begin
12
+ true if open("http://www.google.com/")
13
+ rescue
14
+ false
15
+ end
16
+ end
10
17
  def remote_url
11
18
  "https://cnvrg.io"
12
19
  end
@@ -22,7 +29,10 @@ module Cnvrg
22
29
  def linux?
23
30
  not mac? and not windows?
24
31
  end
25
-
32
+ def ubuntu?
33
+ unix = `cat /etc/lsb-release`.downcase!
34
+ return unix.include? "ubuntu"
35
+ end
26
36
  def cnvrgignore_content
27
37
  %{
28
38
  # cnvrg ignore: Ignore the following directories and files
data/lib/cnvrg/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Cnvrg
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
4
4
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cnvrg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yochay Ettun
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-12-21 00:00:00.000000000 Z
12
+ date: 2017-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -81,6 +81,20 @@ dependencies:
81
81
  - - ">="
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
84
98
  - !ruby/object:Gem::Dependency
85
99
  name: mimemagic
86
100
  requirement: !ruby/object:Gem::Requirement
@@ -189,6 +203,48 @@ dependencies:
189
203
  - - ">="
190
204
  - !ruby/object:Gem::Version
191
205
  version: 0.19.1
206
+ - !ruby/object:Gem::Dependency
207
+ name: aws-sdk
208
+ requirement: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '0'
213
+ type: :runtime
214
+ prerelease: false
215
+ version_requirements: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ">="
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ - !ruby/object:Gem::Dependency
221
+ name: urlcrypt
222
+ requirement: !ruby/object:Gem::Requirement
223
+ requirements:
224
+ - - "~>"
225
+ - !ruby/object:Gem::Version
226
+ version: 0.1.1
227
+ type: :runtime
228
+ prerelease: false
229
+ version_requirements: !ruby/object:Gem::Requirement
230
+ requirements:
231
+ - - "~>"
232
+ - !ruby/object:Gem::Version
233
+ version: 0.1.1
234
+ - !ruby/object:Gem::Dependency
235
+ name: terminal-table
236
+ requirement: !ruby/object:Gem::Requirement
237
+ requirements:
238
+ - - ">="
239
+ - !ruby/object:Gem::Version
240
+ version: '0'
241
+ type: :runtime
242
+ prerelease: false
243
+ version_requirements: !ruby/object:Gem::Requirement
244
+ requirements:
245
+ - - ">="
246
+ - !ruby/object:Gem::Version
247
+ version: '0'
192
248
  description: A CLI tool for interacting with cnvrg.io.
193
249
  email:
194
250
  - info@cnvrg.io