tpkg 1.20.0 → 1.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/Rakefile +1 -1
  2. data/lib/tpkg.rb +154 -81
  3. metadata +2 -2
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ spec = Gem::Specification.new do |s|
5
5
  s.add_dependency('facter')
6
6
  s.add_dependency('net-ssh')
7
7
  s.add_dependency('ddao-kwalify')
8
- s.version = '1.20.0'
8
+ s.version = '1.21.0'
9
9
  s.authors = ['Darren Dao', 'Jason Heiss']
10
10
  s.email = 'tpkg-users@lists.sourceforge.net'
11
11
  s.homepage = 'http://tpkg.sourceforge.net'
@@ -56,7 +56,7 @@ require 'kwalify' # for validating yaml
56
56
 
57
57
  class Tpkg
58
58
 
59
- VERSION = '1.20.0'
59
+ VERSION = '1.21.0'
60
60
  CONFIGDIR = '/etc'
61
61
 
62
62
  GENERIC_ERR = 1
@@ -66,6 +66,8 @@ class Tpkg
66
66
 
67
67
  CONNECTION_TIMEOUT = 10
68
68
 
69
+ DEFAULT_OWNERSHIP_UID = 0
70
+
69
71
  attr_reader :installed_directory
70
72
 
71
73
  #
@@ -665,7 +667,8 @@ class Tpkg
665
667
  # to metadata.xml in that directory
666
668
  def self.extract_metadata(directory, dest=nil)
667
669
  dest = directory if dest.nil?
668
- backward_compatible = true
670
+ # we're no longer generating metadata.xml
671
+ backward_compatible = false
669
672
 
670
673
  # If we still want to support metadata.xml
671
674
  if backward_compatible
@@ -1060,10 +1063,10 @@ class Tpkg
1060
1063
  parts = request.split('=')
1061
1064
 
1062
1065
  # upgrade/remove/query options could take package filenames
1063
- # assuming that the filename is of the correct format, such as
1064
- # foo-1.0.tpkg or foo-1.0-1.tpkg
1066
+ # We're assuming that the filename ends in .tpkg and has a version number that starts
1067
+ # with a digit. For example: foo-1.0.tpkg, foo-bar-1.0-1.tpkg
1065
1068
  if request =~ /\.tpkg$/
1066
- req = {:filename => request, :name => request.split("-")[0]}
1069
+ req = {:filename => request, :name => request.split(/-\d/)[0]}
1067
1070
  elsif parts.length > 2 && parts[-2] =~ /^[\d\.]/ && parts[-1] =~ /^[\d\.]/
1068
1071
  package_version = parts.pop
1069
1072
  version = parts.pop
@@ -1167,8 +1170,9 @@ class Tpkg
1167
1170
  if data[:actual_file]
1168
1171
  stat = File.stat(data[:actual_file])
1169
1172
  perms = stat.mode
1170
- uid = stat.uid
1171
- gid = stat.gid
1173
+ # This is what we set the ownership to by default
1174
+ uid = DEFAULT_OWNERSHIP_UID
1175
+ gid = DEFAULT_OWNERSHIP_UID
1172
1176
  end
1173
1177
 
1174
1178
  # get default permission and ownership
@@ -1306,43 +1310,21 @@ class Tpkg
1306
1310
  end
1307
1311
  end
1308
1312
  @installed_directory = File.join(@var_directory, 'installed')
1309
- if !File.exist?(@installed_directory)
1310
- begin
1311
- FileUtils.mkdir_p(@installed_directory)
1312
- rescue Errno::EACCES
1313
- raise if Process.euid == 0
1314
- end
1315
- end
1316
1313
  @metadata_directory = File.join(@installed_directory, 'metadata')
1317
- if !File.exist?(@metadata_directory)
1318
- begin
1319
- FileUtils.mkdir_p(@metadata_directory)
1320
- rescue Errno::EACCES
1321
- raise if Process.euid == 0
1322
- end
1323
- end
1324
1314
  @sources_directory = File.join(@var_directory, 'sources')
1325
- if !File.exist?(@sources_directory)
1326
- begin
1327
- FileUtils.mkdir_p(@sources_directory)
1328
- rescue Errno::EACCES
1329
- raise if Process.euid == 0
1330
- end
1331
- end
1332
1315
  @external_directory = File.join(@var_directory, 'externals')
1333
- if !File.exist?(@external_directory)
1334
- begin
1335
- FileUtils.mkdir_p(@external_directory)
1336
- rescue Errno::EACCES
1337
- raise if Process.euid == 0
1338
- end
1339
- end
1340
1316
  @tmp_directory = File.join(@var_directory, 'tmp')
1341
- if !File.exist?(@tmp_directory)
1342
- begin
1343
- FileUtils.mkdir_p(@tmp_directory)
1344
- rescue Errno::EACCES
1345
- raise if Process.euid == 0
1317
+ @log_directory = File.join(@var_directory, 'logs')
1318
+ # It is important to create these dirs in correct order
1319
+ dirs_to_create = [@installed_directory, @metadata_directory, @sources_directory,
1320
+ @external_directory, @tmp_directory, @log_directory]
1321
+ dirs_to_create.each do |dir|
1322
+ if !File.exist?(dir)
1323
+ begin
1324
+ FileUtils.mkdir_p(dir)
1325
+ rescue Errno::EACCES
1326
+ raise if Process.euid == 0
1327
+ end
1346
1328
  end
1347
1329
  end
1348
1330
  @tar = Tpkg::find_tar
@@ -1472,7 +1454,7 @@ class Tpkg
1472
1454
  # Populate our list of available packages for a given package name
1473
1455
  def load_available_packages(name=nil)
1474
1456
  prep_metadata
1475
-
1457
+
1476
1458
  if name
1477
1459
  if !@available_packages[name]
1478
1460
  packages = []
@@ -2582,8 +2564,8 @@ class Tpkg
2582
2564
  # tasks before moving the files into their final location.
2583
2565
 
2584
2566
  # Handle any default permissions and ownership
2585
- default_uid = 0
2586
- default_gid = 0
2567
+ default_uid = DEFAULT_OWNERSHIP_UID
2568
+ default_gid = DEFAULT_OWNERSHIP_UID
2587
2569
  default_perms = nil
2588
2570
 
2589
2571
  if metadata[:files] && metadata[:files][:file_defaults]
@@ -3463,6 +3445,11 @@ class Tpkg
3463
3445
  return false
3464
3446
  end
3465
3447
 
3448
+ # Build an array of metadata of pkgs that are already installed
3449
+ # We will use this later on to figure out what new packages have been installed/removed
3450
+ # in order to report back to the server
3451
+ already_installed_pkgs = metadata_for_installed_packages.collect{|metadata| metadata.to_hash}
3452
+
3466
3453
  # Create array of packages (names) we have installed so far
3467
3454
  # We will use it later on to determine the order of how to install the packages
3468
3455
  installed_so_far = installed_packages.collect{|pkg| pkg[:metadata][:name]}
@@ -3570,7 +3557,17 @@ class Tpkg
3570
3557
  installed_so_far << pkg[:metadata][:name]
3571
3558
  end # end while loop
3572
3559
 
3573
- send_update_to_server unless @report_server.nil?
3560
+ # log changes
3561
+ currently_installed = metadata_for_installed_packages.collect{|metadata| metadata.to_hash}
3562
+ newly_installed = currently_installed - already_installed_pkgs
3563
+ log_changes({:newly_installed => newly_installed})
3564
+
3565
+ # send udpate back to reporting server
3566
+ unless @report_server.nil?
3567
+ options = {:newly_installed => newly_installed,
3568
+ :currently_installed => currently_installed}
3569
+ send_update_to_server(options)
3570
+ end
3574
3571
  unlock
3575
3572
  return ret_val
3576
3573
  end
@@ -3669,6 +3666,11 @@ class Tpkg
3669
3666
  unlock
3670
3667
  return false
3671
3668
  end
3669
+
3670
+ # Build an array of metadata of pkgs that are already installed
3671
+ # We will use this later on to figure out what new packages have been installed/removed
3672
+ # in order to report back to the server
3673
+ already_installed_pkgs = metadata_for_installed_packages.collect{|metadata| metadata.to_hash}
3672
3674
 
3673
3675
  installed_files = files_for_installed_packages
3674
3676
  removed_pkgs = [] # keep track of what we removed so far
@@ -3803,11 +3805,21 @@ class Tpkg
3803
3805
  end
3804
3806
  end
3805
3807
  end
3806
-
3808
+
3809
+ # log changes
3810
+ currently_installed = metadata_for_installed_packages.collect{|metadata| metadata.to_hash}
3811
+ newly_installed = currently_installed - already_installed_pkgs
3812
+ removed = already_installed_pkgs - currently_installed,
3813
+ log_changes({:newly_installed => newly_installed, :removed => removed})
3814
+
3815
+ # send update back to reporting server
3807
3816
  if !has_updates
3808
3817
  puts "No updates available"
3809
3818
  elsif !@report_server.nil?
3810
- send_update_to_server
3819
+ options = {:newly_installed => newly_installed,
3820
+ :removed => removed,
3821
+ :currently_installed => currently_installed}
3822
+ send_update_to_server(options)
3811
3823
  end
3812
3824
 
3813
3825
  unlock
@@ -3836,6 +3848,11 @@ class Tpkg
3836
3848
  return false
3837
3849
  end
3838
3850
 
3851
+ # Build an array of metadata of pkgs that are already installed
3852
+ # We will use this later on to figure out what new packages have been installed/removed
3853
+ # in order to report back to the server
3854
+ already_installed_pkgs = metadata_for_installed_packages.collect{|metadata| metadata.to_hash}
3855
+
3839
3856
  # If user want to remove all the dependent pkgs, then go ahead
3840
3857
  # and include them in our array of things to remove
3841
3858
  if options[:remove_all_dep]
@@ -4025,8 +4042,19 @@ class Tpkg
4025
4042
  # Cleanup
4026
4043
  FileUtils.rm_rf(workdir)
4027
4044
  end
4028
-
4029
- send_update_to_server unless @report_server.nil? || options[:upgrade]
4045
+
4046
+ # log changes
4047
+ currently_installed = metadata_for_installed_packages.collect{|metadata| metadata.to_hash}
4048
+ removed = already_installed_pkgs - currently_installed
4049
+ log_changes({:removed => removed})
4050
+
4051
+ # send update back to reporting server
4052
+ unless @report_server.nil? || options[:upgrade]
4053
+ options = {:removed => removed,
4054
+ :currently_installed => currently_installed}
4055
+ send_update_to_server(options)
4056
+ end
4057
+
4030
4058
  unlock
4031
4059
  return ret_val
4032
4060
  end
@@ -4269,39 +4297,6 @@ class Tpkg
4269
4297
  end
4270
4298
  end
4271
4299
 
4272
- def send_update_to_server
4273
- metadata = metadata_for_installed_packages.collect{|metadata| metadata.to_hash}
4274
- yml = YAML.dump(metadata)
4275
- begin
4276
- response = nil
4277
- # Need to set timeout otherwise tpkg can hang for a long time when having
4278
- # problem talking to the reporter server.
4279
- # I can't seem get net-ssh timeout to work so we'll just handle the timeout ourselves
4280
- timeout(CONNECTION_TIMEOUT) do
4281
- update_uri = URI.parse("#{@report_server}")
4282
- http = Tpkg::gethttp(update_uri)
4283
- request = {"yml"=>URI.escape(yml), "client"=>Facter['fqdn'].value}
4284
- post = Net::HTTP::Post.new(update_uri.path)
4285
- post.set_form_data(request)
4286
- response = http.request(post)
4287
- end
4288
-
4289
- case response
4290
- when Net::HTTPSuccess
4291
- puts "Successfully send update to reporter server"
4292
- else
4293
- $stderr.puts response.body
4294
- #response.error!
4295
- # just ignore error and give user warning
4296
- puts "Failed to send update to reporter server"
4297
- end
4298
- rescue Timeout::Error
4299
- puts "Timed out when trying to send update to reporter server"
4300
- rescue
4301
- puts "Failed to send update to reporter server"
4302
- end
4303
- end
4304
-
4305
4300
  # Build a dependency map of currently installed packages
4306
4301
  # For example, if we have pkgB and pkgC which depends on pkgA, then
4307
4302
  # the dependency map would look like this:
@@ -4359,5 +4354,83 @@ class Tpkg
4359
4354
  end
4360
4355
  return pre_reqs
4361
4356
  end
4357
+
4358
+ # TODO: figure out what other methods above can be turned into protected methods
4359
+ protected
4360
+ # log changes of pkgs that were installed/removed
4361
+ def log_changes(options={})
4362
+ msg = ""
4363
+ user = Etc.getlogin
4364
+ newly_installed = removed = []
4365
+ newly_installed = options[:newly_installed] if options[:newly_installed]
4366
+ removed = options[:removed] if options[:removed]
4367
+ removed.each do |pkg|
4368
+ msg = "#{msg}#{Time.new} #{pkg[:filename]} was removed by #{user}\n"
4369
+ end
4370
+ newly_installed.each do |pkg|
4371
+ msg = "#{msg}#{Time.new} #{pkg[:filename]} was installed by #{user}\n"
4372
+ end
4373
+
4374
+ msg.lstrip!
4375
+ unless msg.empty?
4376
+ File.open(File.join(@log_directory,'changes.log'), 'a') {|f| f.write(msg) }
4377
+ end
4378
+ end
4379
+
4380
+ def send_update_to_server(options={})
4381
+ request = {"client"=>Facter['fqdn'].value}
4382
+ request[:user] = Etc.getlogin
4383
+ request[:tpkg_home] = ENV['TPKG_HOME']
4384
+
4385
+ if options[:currently_installed]
4386
+ currently_installed = options[:currently_installed]
4387
+ else
4388
+ currently_installed = metadata_for_installed_packages.collect{|metadata| metadata.to_hash}
4389
+ end
4390
+
4391
+ # Figure out list of packages that were already installed, newly installed and newly removed
4392
+ if options[:newly_installed]
4393
+ newly_installed = options[:newly_installed]
4394
+ request[:newly_installed] = URI.escape(YAML.dump(newly_installed))
4395
+ already_installed = currently_installed - newly_installed
4396
+ else
4397
+ already_installed = currently_installed
4398
+ end
4399
+ request[:already_installed] = URI.escape(YAML.dump(already_installed))
4400
+
4401
+ if options[:removed]
4402
+ removed = options[:removed]
4403
+ request[:removed] = URI.escape(YAML.dump(removed))
4404
+ end
4405
+
4406
+ begin
4407
+ response = nil
4408
+ # Need to set timeout otherwise tpkg can hang for a long time when having
4409
+ # problem talking to the reporter server.
4410
+ # I can't seem get net-ssh timeout to work so we'll just handle the timeout ourselves
4411
+ timeout(CONNECTION_TIMEOUT) do
4412
+ update_uri = URI.parse("#{@report_server}")
4413
+ http = Tpkg::gethttp(update_uri)
4414
+ post = Net::HTTP::Post.new(update_uri.path)
4415
+ post.set_form_data(request)
4416
+ response = http.request(post)
4417
+ end
4418
+
4419
+ case response
4420
+ when Net::HTTPSuccess
4421
+ puts "Successfully send update to reporter server"
4422
+ else
4423
+ $stderr.puts response.body
4424
+ #response.error!
4425
+ # just ignore error and give user warning
4426
+ puts "Failed to send update to reporter server"
4427
+ end
4428
+ rescue Timeout::Error
4429
+ puts "Timed out when trying to send update to reporter server"
4430
+ rescue
4431
+ puts "Failed to send update to reporter server"
4432
+ end
4433
+ end
4434
+
4362
4435
  end
4363
4436
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tpkg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.20.0
4
+ version: 1.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darren Dao
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-02-27 00:00:00 +00:00
13
+ date: 2010-03-16 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency