bcl 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -61,31 +61,32 @@ module BCL
61
61
  end
62
62
 
63
63
  def default_yaml
64
- settings = { :server => { :url => "http://bcl7.development.nrel.gov", :admin_user => { :username => "ENTER_BCL_USERNAME", :password => "ENTER_BCL_PASSWORD"} } }
64
+ settings = { :server => { :url => "https://bcl.nrel.gov", :user => { :username => "ENTER_BCL_USERNAME", :password => "ENTER_BCL_PASSWORD"} } }
65
65
 
66
66
  settings
67
67
  end
68
-
68
+
69
+
69
70
  def login(username=nil, password=nil)
70
71
  if username.nil? || password.nil?
71
72
  # log in via cached creditials
72
- username = @config[:server][:admin_user][:username]
73
- password = @config[:server][:admin_user][:password]
73
+ username = @config[:server][:user][:username]
74
+ password = @config[:server][:user][:password]
74
75
  end
75
76
 
76
77
  #figure out what time to use
77
- url = @config[:server][:url]
78
- #look for http vs. https
79
- if url.include? "https"
80
- port = 443
81
- else
82
- port = 80
83
- end
84
- #strip out http(s)
85
- url = url.gsub('http://', '')
86
- url = url.gsub('https://', '')
87
-
88
- puts "Connecting to #{url} on port #{port}"
78
+ url = @config[:server][:url]
79
+ #look for http vs. https
80
+ if url.include? "https"
81
+ port = 443
82
+ else
83
+ port = 80
84
+ end
85
+ #strip out http(s)
86
+ url = url.gsub('http://', '')
87
+ url = url.gsub('https://', '')
88
+
89
+ puts "Connecting to #{url} on port #{port} as #{username}"
89
90
 
90
91
  @http = Net::HTTP.new(url, port)
91
92
  @http.use_ssl = true
@@ -98,11 +99,10 @@ module BCL
98
99
 
99
100
  res, data = @http.post(path, data, headers)
100
101
 
101
- #restClient wasn't working
102
+ #restClient wasn't working
102
103
  #res = RestClient.post "#{@config[:server][:url]}/api/user/login", data.to_json, :content_type => :json, :accept => :json
103
-
104
104
  if res.code == '200'
105
-
105
+ puts "Login Successful"
106
106
  =begin
107
107
  #OLD RESTCLIENT CODE
108
108
  #pull out the session key
@@ -135,37 +135,37 @@ module BCL
135
135
 
136
136
  =end
137
137
 
138
- bnes = ""
139
- bni = ""
140
- junkout = res["set-cookie"].split(";")
141
- junkout.each do |line|
142
- if line =~ /BNES_SESS/
143
- bnes = line.match(/(BNES_SESS.*)/)[0]
144
- end
145
- end
138
+ bnes = ""
139
+ bni = ""
140
+ junkout = res["set-cookie"].split(";")
141
+ junkout.each do |line|
142
+ if line =~ /BNES_SESS/
143
+ bnes = line.match(/(BNES_SESS.*)/)[0]
144
+ end
145
+ end
146
146
 
147
- junkout.each do |line|
148
- if line =~ /BNI/
149
- bni = line.match(/(BNI.*)/)[0]
150
- end
151
- end
147
+ junkout.each do |line|
148
+ if line =~ /BNI/
149
+ bni = line.match(/(BNI.*)/)[0]
150
+ end
151
+ end
152
152
 
153
- #puts "DATA: #{data}"
154
- session_name = ""
155
- sessid = ""
156
- json = JSON.parse(data)
157
- json.each do |key, val|
158
- if key == 'session_name'
159
- session_name = val
160
- elsif key == 'sessid'
161
- sessid = val
162
- end
163
- end
153
+ #puts "DATA: #{data}"
154
+ session_name = ""
155
+ sessid = ""
156
+ json = JSON.parse(data)
157
+ json.each do |key, val|
158
+ if key == 'session_name'
159
+ session_name = val
160
+ elsif key == 'sessid'
161
+ sessid = val
162
+ end
163
+ end
164
164
 
165
- @session = session_name + '=' + sessid + ';' + bni + ";" + bnes
166
-
167
- puts "SESSION COOKIE: #{@session}"
168
- res
165
+ @session = session_name + '=' + sessid + ';' + bni + ";" + bnes
166
+
167
+ #puts "SESSION COOKIE: #{@session}"
168
+ res
169
169
  else
170
170
 
171
171
  puts "error code: #{res.code}"
@@ -175,57 +175,65 @@ module BCL
175
175
  end
176
176
  end
177
177
 
178
-
179
- # pushes component to the bcl and publishes them (if updated as admin user). Username and password and
180
- # set in ~/.bcl/config.yml file which determines the permissions and the group to which
181
- # the component will be uploaded
182
- def push_content(filename_and_path, write_receipt_file, content_type)
178
+ # pushes component to the bcl and publishes them (if logged-in as BCL Website Admin user).
179
+ # username and password set in ~/.bcl/config.yml file
180
+ def push_content(filename_and_path, write_receipt_file, content_type, bcl_group_id)
183
181
  raise "Please login before pushing components" if @session.nil?
184
-
185
182
  valid = false
186
183
  res_j = nil
187
184
  filename = File.basename(filename_and_path)
185
+ #TODO remove special characters in the filename; they create firewall errors
186
+ #filename = filename.gsub(/\W/,'_').gsub(/___/,'_').gsub(/__/,'_').chomp('_').strip
188
187
  filepath = File.dirname(filename_and_path) + "/"
189
-
190
188
  file = File.open(filename_and_path, 'rb')
191
189
  file_b64 = Base64.encode64(file.read)
192
- @data = {"file" =>
193
- {
194
- "file" => "#{file_b64}",
195
- "filesize" => "#{File.size(filename_and_path)}",
196
- "filename" => filename
197
- },
198
- "node" =>
199
- {
200
- "type" => "#{content_type}",
201
- "publish" => 1 #NOTE THIS ONLY WORKS IF YOU ARE ADMIN
202
- }
190
+ @data = {
191
+ "file" =>
192
+ {
193
+ "file" => "#{file_b64}",
194
+ "filesize" => "#{File.size(filename_and_path)}",
195
+ "filename" => filename
196
+ },
197
+ "node" =>
198
+ {
199
+ "type" => "#{content_type}",
200
+ "og_group_ref" =>
201
+ {
202
+ "und" =>
203
+ ["target_id" => bcl_group_id],
204
+ "publish" => 1 #NOTE THIS ONLY WORKS IF YOU ARE A BCL SITE ADMIN
205
+ }
203
206
  }
204
-
205
- #restclient not working
207
+ }
208
+ #restclient not working
206
209
  #res = RestClient.post "#{@config[:server][:url]}/api/content.json", @data.to_json, :content_type => :json, :cookies => @session
207
210
 
208
- path = "/api/content.json"
209
- headers = {'Content-Type' => 'application/json', 'Cookie' => @session}
211
+ path = "/api/content.json"
212
+ headers = {'Content-Type' => 'application/json', 'Cookie' => @session}
210
213
 
211
- res, data = @http.post(path, @data.to_json, headers)
214
+ res, data = @http.post(path, @data.to_json, headers)
212
215
 
216
+ res_j = "could not get json from http post response"
213
217
  if res.code == '200'
214
218
  res_j = JSON.parse(res.body)
215
-
216
- if res.code == '200'
217
- valid = true
218
- elsif res.code == '500'
219
- raise "server exception"
220
- valid = false
221
- else
222
- valid = false
223
- puts "error #{res.code}"
224
- end
219
+ puts "200 - Successful Upload"
220
+ valid = true
221
+ elsif res.code == '404'
222
+ puts "error code: #{res.code}"
223
+ puts "error info: #{res.body}"
224
+ puts "404 - check these common causes first:"
225
+ puts " the filename contains periods (other than the ones before the file extension)"
226
+ puts " you are not an 'administrator member' of the group you're trying to upload to"
227
+ valid = false
228
+ elsif res.code == '500'
229
+ puts "error code: #{res.code}"
230
+ puts "error info: #{res.body}"
231
+ raise "server exception"
232
+ valid = false
225
233
  else
226
234
  puts "error code: #{res.code}"
227
235
  puts "error info: #{res.body}"
228
- res = nil
236
+ valid = false
229
237
  end
230
238
 
231
239
  if valid
@@ -241,17 +249,18 @@ module BCL
241
249
  [valid, res_j]
242
250
  end
243
251
 
244
- def push_contents(array_of_components, skip_files_with_receipts, content_type)
252
+ def push_contents(array_of_components, skip_files_with_receipts, content_type, bcl_group_id)
245
253
  logs = []
246
254
  array_of_components.each do |comp|
247
255
  receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
248
256
  log_message = ""
249
257
  if skip_files_with_receipts && File.exists?(receipt_file)
250
- log_message = "skipping component because found receipt for #{comp}"
258
+ log_message = "skipping because found receipt #{comp}"
259
+ puts log_message
251
260
  else
252
- log_message = "pushing component #{comp}: "
261
+ log_message = "pushing content #{File.basename(comp, '.tar.gz')}"
253
262
  puts log_message
254
- valid, res = push_content(comp, true, content_type)
263
+ valid, res = push_content(comp, true, content_type, bcl_group_id)
255
264
  log_message += " #{valid} #{res.inspect.chomp}"
256
265
  end
257
266
  logs << log_message
@@ -260,32 +269,37 @@ module BCL
260
269
  logs
261
270
  end
262
271
 
263
- # pushes updated component to the bcl and publishes them (if updated as admin user). Username and password and
264
- # set in ~/.bcl/config.yml file which determines the permissions and the group to which
265
- # the component will be uploaded
266
- def update_content(filename_and_path, write_receipt_file, uuid)
272
+ # pushes updated content to the bcl and publishes it (if logged-in as BCL Website Admin user).
273
+ # username and password set in ~/.bcl/config.yml file
274
+ def update_content(filename_and_path, write_receipt_file, uuid, bcl_group_id)
267
275
  raise "Please login before pushing components" if @session.nil?
268
-
269
276
  valid = false
270
277
  res_j = nil
271
278
  filename = File.basename(filename_and_path)
279
+ #TODO remove special characters in the filename; they create firewall errors
280
+ #filename = filename.gsub(/\W/,'_').gsub(/___/,'_').gsub(/__/,'_').chomp('_').strip
272
281
  filepath = File.dirname(filename_and_path) + "/"
273
-
274
282
  file = File.open(filename_and_path, 'rb')
275
283
  file_b64 = Base64.encode64(file.read)
276
- @data = {"file" =>
277
- {
278
- "file" => "#{file_b64}",
279
- "filesize" => "#{File.size(filename_and_path)}",
280
- "filename" => filename
281
- },
284
+ @data = {
285
+ "file" =>
286
+ {
287
+ "file" => "#{file_b64}",
288
+ "filesize" => "#{File.size(filename_and_path)}",
289
+ "filename" => filename
290
+ },
282
291
  "node" =>
283
- {
284
- "uuid" => "#{uuid}",
285
- "publish" => 1 #NOTE THIS ONLY WORKS IF YOU ARE ADMIN
286
- }
292
+ {
293
+ "uuid" => "#{uuid}",
294
+ "og_group_ref" =>
295
+ {
296
+ "und" =>
297
+ ["target_id" => bcl_group_id],
298
+ "publish" => 1 #NOTE THIS ONLY WORKS IF YOU ARE A BCL SITE ADMIN
299
+ }
287
300
  }
288
-
301
+ }
302
+
289
303
  #restclient not working
290
304
  #res = RestClient.post "#{@config[:server][:url]}/api/content", @data.to_json, :content_type => :json, :cookies => @session, :accept => :json
291
305
 
@@ -294,21 +308,28 @@ module BCL
294
308
 
295
309
  res, data = @http.post(path, @data.to_json, headers)
296
310
 
311
+ res_j = "could not get json from http post response"
297
312
  if res.code == '200'
298
313
  res_j = JSON.parse(res.body)
299
-
300
- if res.code == '200'
301
- valid = true
302
- elsif res.code == '500'
303
- raise "server exception"
304
- valid = false
305
- else
306
- valid = false
307
- end
314
+ puts "200 - Successful Upload"
315
+ valid = true
316
+ elsif res.code == '404'
317
+ puts "error code: #{res.code}"
318
+ puts "error info: #{res.body}"
319
+ puts "404 - check these common causes first:"
320
+ puts " the filename contains periods (other than the ones before the file extension)"
321
+ puts " you are not an 'administrator member' of the group you're trying to upload to"
322
+ valid = false
323
+ elsif res.code == '500'
324
+ puts "error code: #{res.code}"
325
+ puts "error info: #{res.body}"
326
+ raise "server exception"
327
+ valid = false
308
328
  else
309
329
  puts "error code: #{res.code}"
310
- res = nil
311
- end
330
+ puts "error info: #{res.body}"
331
+ valid = false
332
+ end
312
333
 
313
334
  if valid
314
335
  #write out a receipt file into the same directory of the component with the same file name as
@@ -323,64 +344,64 @@ module BCL
323
344
  [valid, res_j]
324
345
  end
325
346
 
326
- def update_contents(array_of_components, skip_files_with_receipts)
347
+ def update_contents(array_of_components, skip_files_with_receipts, bcl_group_id)
327
348
  logs = []
328
349
  array_of_components.each do |comp|
329
- receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
330
- log_message = ""
331
- if skip_files_with_receipts && File.exists?(receipt_file)
332
- log_message = "skipping component because found receipt for #{comp}"
333
- else
334
- #extract uuid
335
- if File.exists?(File.dirname(comp) + "/component.xml")
336
- xml_filename = File.dirname(comp) + "/component.xml"
337
- elsif File.exists?("#{Dir.pwd}/instances/#{File.basename(comp, '.tar.gz')}/measure.xml")
338
- xml_filename = "#{Dir.pwd}/instances/#{File.basename(comp, '.tar.gz')}/measure.xml"
350
+ receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
351
+ log_message = ""
352
+ if skip_files_with_receipts && File.exists?(receipt_file)
353
+ log_message = "skipping update because found receipt #{File.basename(comp)}"
354
+ puts log_message
339
355
  else
340
- puts "could not find component.xml or measure.xml"
341
- next
356
+ #extract uuid from the .tar.gz file
357
+ uuid = nil
358
+ tgz = Zlib::GzipReader.open(comp)
359
+ Archive::Tar::Minitar::Reader.open(tgz).each do |entry|
360
+ if entry.name == "component.xml" or entry.name == "measure.xml"
361
+ xml_file = LibXML::XML::Document.string(entry.read)
362
+ uid_node = xml_file.find('uid').first
363
+ uuid = uid_node.content
364
+ #vid_node = xml_file.find('version_id').first
365
+ #vid = vid_node.content
366
+ #puts "uuid = #{uuid}; vid = #{vid}"
367
+ end
368
+ end
369
+ if uuid == nil
370
+ log_message "ERROR: uuid not found for #{File.basename(comp)}"
371
+ puts log_message
372
+ else
373
+ log_message = "pushing updated content #{File.basename(comp)}"
374
+ puts log_message
375
+ valid, res = update_content(comp, true, uuid, bcl_group_id)
376
+ log_message += " #{valid} #{res.inspect.chomp}"
377
+ end
342
378
  end
343
- parser = LibXML::XML::Parser.file(xml_filename)
344
- xml_file = parser.parse
345
- uid_node = xml_file.find('uid').first
346
- uuid = uid_node.content
347
- if uuid == nil
348
- log_message "ERROR: uuid not found for #{File.basename(comp)}"
349
- puts log_message
350
- else
351
- log_message = "pushing updated component #{uuid}: #{File.basename(comp)}"
352
- puts log_message
353
- valid, res = update_content(comp, true, uuid)
354
- log_message += " #{valid} #{res.inspect.chomp}"
355
- end
356
- end
357
- logs << log_message
379
+ logs << log_message
358
380
  end
359
- logs
381
+ logs
360
382
  end
361
383
 
362
- # Simple method to search bcl and return the result as an XML object
363
- def search(search_str)
364
- full_url = "#{@config[:server][:url]}/api/search/#{search_str}&api_version=#{@api_version}"
365
- res = RestClient.get "#{full_url}"
366
- xml = LibXML::XML::Document.string(res.body)
384
+ # Simple method to search bcl and return the result as an XML object
385
+ def search(search_str)
386
+ full_url = "#{@config[:server][:url]}/api/search/#{search_str}&api_version=#{@api_version}"
387
+ res = RestClient.get "#{full_url}"
388
+ xml = LibXML::XML::Document.string(res.body)
367
389
 
368
- xml
369
- end
390
+ xml
391
+ end
370
392
 
371
393
  # Delete receipt files
372
394
  def delete_receipts(array_of_components)
373
395
  array_of_components.each do |comp|
374
- receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
375
- if File.exists?(receipt_file)
376
- FileUtils.remove_file(receipt_file)
377
-
378
- end
396
+ receipt_file = File.dirname(comp) + "/" + File.basename(comp, '.tar.gz') + ".receipt"
397
+ if File.exists?(receipt_file)
398
+ FileUtils.remove_file(receipt_file)
399
+
400
+ end
379
401
  end
380
402
  end
381
-
382
-
383
- end
403
+
404
+ end #class ComponentMethods
384
405
 
385
406
  # TODO make this extend the component_xml class (or create a super class around components)
386
407
 
@@ -417,7 +438,7 @@ module BCL
417
438
  FileUtils.cp(targz.to_s, destination_file)
418
439
  end
419
440
 
420
- #gather all the zip files into a single tar.gz
441
+ #gather all the .tar.gz files into a single tar.gz
421
442
  (1..chunk_cnt).each do |cnt|
422
443
  currentdir = Dir.pwd
423
444
 
@@ -440,6 +461,4 @@ module BCL
440
461
 
441
462
  end
442
463
 
443
-
444
-
445
464
  end # module BCL
data/lib/bcl/version.rb CHANGED
@@ -18,5 +18,5 @@
18
18
  ######################################################################
19
19
 
20
20
  module BCL
21
- VERSION = "0.3.1"
21
+ VERSION = "0.3.2"
22
22
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bcl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel Macumber
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2013-09-24 00:00:00 Z
21
+ date: 2013-09-27 00:00:00 Z
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
24
24
  name: uuid