From 30559e7072d4a14657b4a482de43c7e0be8920ab Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 8 Oct 2013 10:28:27 +0400 Subject: [PATCH] Add ability to set user and group owners of file and permissions for it Change-Id: I313a169fd79e6e5cb415f4a329c82a956ecc83b0 --- lib/astute/deployment_engine.rb | 10 +++++++- mcagents/uploadfile.ddl | 40 +++++++++++++++++++++++++++-- mcagents/uploadfile.rb | 6 +++++ spec/unit/deployment_engine_spec.rb | 8 ++++++ 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/lib/astute/deployment_engine.rb b/lib/astute/deployment_engine.rb index c216bde7..8d7cc1fe 100644 --- a/lib/astute/deployment_engine.rb +++ b/lib/astute/deployment_engine.rb @@ -114,7 +114,15 @@ module Astute source_path = File.join(KEY_DIR, deployment_id.to_s, key_name, ssh_key) destination_path = File.join(KEY_DIR, key_name, ssh_key) content = File.read(source_path) - upload_mclient.upload(:path => destination_path, :content => content, :overwrite => true, :parents => true) + upload_mclient.upload(:path => destination_path, + :content => content, + :user_owner => 'root', + :group_owner => 'root', + :permissions => '0600', + :dir_permissions => '0700', + :overwrite => true, + :parents => true + ) end end diff --git a/mcagents/uploadfile.ddl b/mcagents/uploadfile.ddl index 89e121f4..937c6113 100644 --- a/mcagents/uploadfile.ddl +++ b/mcagents/uploadfile.ddl @@ -24,6 +24,42 @@ action "upload", :description => "upload file" do :validation => '^.+$', :optional => false, :maxlength => 0 + + input :user_owner, + :prompt => "User owner of file", + :description => "Who should be owner of the file?", + :type => :string, + :validation => :shellsafe, + :optional => false, + :default => 'root', + :maxlength => 0 + + input :group_owner, + :prompt => "Group owner of file", + :description => "What group should be owner of the file?", + :type => :string, + :validation => :shellsafe, + :optional => false, + :default => 'root', + :maxlength => 0 + + input :permissions, + :prompt => "File permissions", + :description => "What permissions should be set to the file?", + :type => :string, + :validation => '^[0-7]{3,4}$', + :default => '0644', + :optional => false, + :maxlength => 4 + + input :dir_permissions, + :prompt => "Directory permissions", + :description => "What permissions should be set for folder where file will be place?", + :type => :string, + :validation => '^[0-7]{3,4}$', + :optional => true, + :default => '0755', + :maxlength => 4 input :overwrite, :prompt => "Force overwrite", @@ -31,13 +67,13 @@ action "upload", :description => "upload file" do :type => :boolean, :optional => false, :default => false - + input :parents, :prompt => "Create intermediate directories as required", :description => "no error if destination directory existing, make parent directories as needed", :type => :boolean, :optional => false, - :default => true + :default => true output :msg, :description => "Report message", diff --git a/mcagents/uploadfile.rb b/mcagents/uploadfile.rb index 1dd205cc..52864f41 100644 --- a/mcagents/uploadfile.rb +++ b/mcagents/uploadfile.rb @@ -34,9 +34,15 @@ module MCollective # first create target directory on managed server FileUtils.mkdir_p(dir) unless File.directory?(dir) + FileUtils.chmod(request.data[:dir_permissions].to_i(8), dir) if request.data[:dir_permissions] # then create file and save their content File.open(path, 'w') { |file| file.write(request.data[:content]) } + + # Set user owner, group owner and permissions + FileUtils.chown request.data[:user_owner], request.data[:group_owner], path + FileUtils.chmod request.data[:permissions].to_i(8), path + reply[:msg] = "File was uploaded!" end diff --git a/spec/unit/deployment_engine_spec.rb b/spec/unit/deployment_engine_spec.rb index 2faefb4b..057cf1a2 100644 --- a/spec/unit/deployment_engine_spec.rb +++ b/spec/unit/deployment_engine_spec.rb @@ -193,11 +193,19 @@ describe Astute::DeploymentEngine do File.stubs(:read).returns("private key").then.returns("public key") mclient.expects(:upload).with(:path => File.join(Engine::KEY_DIR, 'nova', 'nova'), :content => "private key", + :user_owner => 'root', + :group_owner => 'root', + :permissions => '0600', + :dir_permissions => '0700', :overwrite => true, :parents => true ) mclient.expects(:upload).with(:path => File.join(Engine::KEY_DIR, 'nova', 'nova.pub'), :content => "public key", + :user_owner => 'root', + :group_owner => 'root', + :permissions => '0600', + :dir_permissions => '0700', :overwrite => true, :parents => true )