CloudyScripts 1.5.21 → 1.5.22

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ require 'rake/testtask'
12
12
 
13
13
  spec = Gem::Specification.new do |s|
14
14
  s.name = 'CloudyScripts'
15
- s.version = '1.5.21'
15
+ s.version = '1.5.22'
16
16
  s.has_rdoc = true
17
17
  s.extra_rdoc_files = ['README.rdoc', 'LICENSE']
18
18
  s.summary = 'Scripts to facilitate programming for infrastructure clouds.'
@@ -16,6 +16,7 @@ class RemoteCommandHandler
16
16
  # * ip: ip address of the machine to connect to
17
17
  # * keyfile: path of the keyfile to be used for authentication
18
18
  def connect_with_keyfile(ip, user_name, keyfile, timeout = 30)
19
+ @use_sudo = false
19
20
  @ssh_session = Net::SSH.start(ip, user_name, {:keys => [keyfile], :timeout => timeout})
20
21
  @use_sudo = true unless user_name.strip == 'root'
21
22
  end
@@ -26,6 +27,7 @@ class RemoteCommandHandler
26
27
  # * user: user name
27
28
  # * key_data: key_data to be used for authentication
28
29
  def connect(ip, user, key_data, timeout = 30)
30
+ @use_sudo = false
29
31
  @ssh_session = Net::SSH.start(ip, user, {:key_data => [key_data], :timeout => timeout})
30
32
  @use_sudo = true unless user.strip == 'root'
31
33
  end
@@ -75,8 +75,9 @@ module StateTransitionHelper
75
75
  raise Exception.new("connection attempts stopped (#{last_connection_problem})")
76
76
  end
77
77
  os = remote_handler().retrieve_os()
78
- post_message("connected to #{dns_name}. OS installed is #{os}")
79
- @logger.info "connected to #{dns_name}"
78
+ sudo = remote_handler().use_sudo ? " [sudo]" : ""
79
+ post_message("connected to #{dns_name}#{sudo}. OS installed is #{os}")
80
+ @logger.info "connected to #{dns_name}#{sudo}"
80
81
  return os
81
82
  end
82
83
 
@@ -444,23 +445,47 @@ module StateTransitionHelper
444
445
  post_message("EBS volume successfully zipped")
445
446
  end
446
447
 
447
- def remote_copy(keyname, source_dir, dest_machine, dest_dir)
448
+ def remote_copy(user_name, keyname, source_dir, dest_machine, dest_dir)
448
449
  post_message("going to remote copy all files from volume. This may take some time...")
449
- if remote_handler().tools_installed?("rsync")
450
- @logger.debug "use rsync command"
451
- remote_handler().remote_rsync("/root/.ssh/#{keyname}.pem", source_dir, dest_machine, dest_dir)
452
- else
453
- @logger.debug "use scp command"
454
- remote_handler().scp("/root/.ssh/#{keyname}.pem", source_dir, dest_machine, dest_dir)
455
- end
450
+ key_path_candidates = ["/#{user_name}/.ssh/", "/home/#{user_name}/.ssh/"]
451
+ key_path_candidates.each() {|key_path|
452
+ key_file = "#{key_path}#{keyname}.pem"
453
+ if remote_handler().file_exists?(key_path)
454
+ if remote_handler().tools_installed?("rsync")
455
+ @logger.debug "use rsync command on #{key_file}"
456
+ remote_handler().remote_rsync(key_file, source_dir, dest_machine, dest_dir)
457
+ else
458
+ @logger.debug "use scp command #{key_file}"
459
+ remote_handler().scp(key_file, source_dir, dest_machine, dest_dir)
460
+ end
461
+ break
462
+ end
463
+ }
456
464
  post_message("remote copy operation done")
457
465
  end
458
466
 
459
467
  def upload_file(ip, user, key_data, file, target_file)
460
- post_message("going to upload #{file} to #{ip}:/#{target_file}")
468
+ post_message("going to upload #{file} to #{user}@#{ip}:#{target_file}")
461
469
  remote_handler().upload(ip, user, key_data, file, target_file)
462
470
  end
463
471
 
472
+ # From a list of existing files, return the first that exists
473
+ def determine_file(ip, user_name, ssh_keydata, file_candidates)
474
+ connect(ip, user_name, nil, ssh_keydata)
475
+ begin
476
+ file_candidates.each() {|file_path|
477
+ if remote_handler().file_exists?(file_path)
478
+ return file_path
479
+ end
480
+ }
481
+ return nil
482
+ rescue
483
+ raise
484
+ ensure
485
+ disconnect()
486
+ end
487
+ end
488
+
464
489
  #setting/retrieving handlers
465
490
 
466
491
  def remote_handler()
@@ -164,8 +164,11 @@ class CopyAmi < Ec2Script
164
164
  class TargetVolumeReadyState < CopyAmiState
165
165
  def enter()
166
166
  post_message("upload key of target-instance to source-instance...")
167
- upload_file(@context[:source_dns_name], "root", @context[:source_ssh_keydata],
168
- @context[:target_ssh_keyfile], "/root/.ssh/#{@context[:target_key_name]}.pem")
167
+ path_candidates = ["/#{@context[:source_ssh_username]}/.ssh/",
168
+ "/home/#{@context[:source_ssh_username]}/.ssh/"]
169
+ key_path = determine_file(@context[:source_dns_name], @context[:source_ssh_username], @context[:source_ssh_keydata], path_candidates)
170
+ upload_file(@context[:source_dns_name], @context[:source_ssh_username], @context[:source_ssh_keydata],
171
+ @context[:target_ssh_keyfile], "#{key_path}#{@context[:target_key_name]}.pem")
169
172
  post_message("credentials are in place to connect source and target.")
170
173
  KeyInPlaceState.new(@context)
171
174
  end
@@ -177,7 +180,7 @@ class CopyAmi < Ec2Script
177
180
  connect(@context[:source_dns_name], @context[:source_ssh_username], nil, @context[:source_ssh_keydata])
178
181
  source_dir = "/mnt/tmp_#{@context[:source_volume_id]}/"
179
182
  dest_dir = "/mnt/tmp_#{@context[:target_volume_id]}"
180
- remote_copy(@context[:target_key_name], source_dir, @context[:target_dns_name], dest_dir)
183
+ remote_copy(@context[:source_ssh_username], @context[:target_key_name], source_dir, @context[:target_dns_name], dest_dir)
181
184
  disconnect()
182
185
  DataCopiedState.new(@context)
183
186
  end
@@ -140,8 +140,11 @@ class CopySnapshot< Ec2Script
140
140
  class TargetVolumeReadyState < CopySnapshotState
141
141
  def enter()
142
142
  post_message("upload key of target-instance to source-instance...")
143
+ path_candidates = ["/#{@context[:source_ssh_username]}/.ssh/",
144
+ "/home/#{@context[:source_ssh_username]}/.ssh/"]
145
+ key_path = determine_file(@context[:source_dns_name], @context[:source_ssh_username], @context[:source_ssh_keydata], path_candidates)
143
146
  upload_file(@context[:source_dns_name], "root", @context[:source_ssh_keydata],
144
- @context[:target_ssh_keyfile], "/root/.ssh/#{@context[:target_key_name]}.pem")
147
+ @context[:target_ssh_keyfile], "#{key_path}#{@context[:target_key_name]}.pem")
145
148
  post_message("credentials are in place to connect source and target.")
146
149
  KeyInPlaceState.new(@context)
147
150
  end
@@ -153,7 +156,7 @@ class CopySnapshot< Ec2Script
153
156
  connect(@context[:source_dns_name], @context[:source_ssh_username], nil, @context[:source_ssh_keydata])
154
157
  source_dir = "/mnt/tmp_#{@context[:source_volume_id]}/"
155
158
  dest_dir = "/mnt/tmp_#{@context[:target_volume_id]}"
156
- remote_copy(@context[:target_key_name], source_dir, @context[:target_dns_name], dest_dir)
159
+ remote_copy(@context[:source_ssh_username], @context[:target_key_name], source_dir, @context[:target_dns_name], dest_dir)
157
160
  disconnect()
158
161
  DataCopiedState.new(@context)
159
162
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 5
8
- - 21
9
- version: 1.5.21
8
+ - 22
9
+ version: 1.5.22
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matthias Jung