lib/testlab/user/lifecycle.rb in testlab-1.5.1 vs lib/testlab/user/lifecycle.rb in testlab-1.6.0
- old
+ new
@@ -7,11 +7,11 @@
#
# @return [Boolean] True if successful.
def provision
@ui.logger.debug { "User Create: #{self.username} " }
- node_home_dir = ((self.container.node.user == "root") ? %(/root) : %(/home/#{self.container.node.user}))
+ node_home_dir = home_dir(self.container.node.user)
node_authkeys = File.join(node_home_dir, ".ssh", "authorized_keys")
# ensure the container user exists
container_passwd_file = File.join(self.container.fs_root, "etc", "passwd")
if self.container.node.exec(%(sudo grep "#{self.username}" #{container_passwd_file}), :ignore_exit_status => true).exit_code != 0
@@ -46,10 +46,16 @@
if File.exists?(pi)
public_identities << ::IO.read(pi).strip
end
end
+ if (public_identities.count > 0)
+ id_rsa_pub = File.join(user_home_dir, ".ssh", "id_rsa.pub")
+ self.container.node.exec(%(sudo grep -e "#{public_identities.first}" #{id_rsa_pub} || echo "#{public_identities.first}" | sudo tee #{id_rsa_pub}), :ignore_exit_status => true)
+ self.container.node.exec(%(sudo chmod -v 644 #{id_rsa_pub}), :ignore_exit_status => true)
+ end
+
authkeys.each do |destination, source|
@ui.logger.info { "SOURCE: #{source} >>> #{destination}" }
self.container.node.exec(%(sudo mkdir -pv #{File.dirname(destination)}))
self.container.node.exec(%(sudo grep "$(cat #{source})" #{destination} || sudo cat #{source} | sudo tee -a #{destination}))
@@ -57,9 +63,22 @@
public_identities.each do |pi|
self.container.node.exec(%(sudo grep "#{pi}" #{destination} || sudo echo "#{pi}" | sudo tee -a #{destination}))
end
self.container.node.exec(%(sudo chmod -v 644 #{destination}))
+ end
+
+ identities = Array.new
+ !self.identity.nil? and [self.identity].flatten.compact.each do |i|
+ if File.exists?(i)
+ identities << ::IO.read(i).strip
+ end
+ end
+
+ if (identities.count > 0)
+ id_rsa = File.join(user_home_dir, ".ssh", "id_rsa")
+ self.container.node.exec(%(sudo grep -e "#{identities.first}" #{id_rsa} || echo "#{identities.first}" | sudo tee #{id_rsa}), :ignore_exit_status => true)
+ self.container.node.exec(%(sudo chmod -v 400 #{id_rsa}), :ignore_exit_status => true)
end
# ensure the container user home directory is owned by them
home_dir = self.container.lxc.attach(%(-- /bin/bash -c 'grep #{self.username} /etc/passwd | cut -d ":" -f6')).strip
self.container.lxc.attach(%(-- /bin/bash -c 'sudo chown -Rv $(id -u #{self.username}):$(id -g #{self.username}) #{home_dir}'))