Skip to content

Instantly share code, notes, and snippets.

@davidpelaez
Last active December 21, 2015 06:38
Show Gist options
  • Select an option

  • Save davidpelaez/6265242 to your computer and use it in GitHub Desktop.

Select an option

Save davidpelaez/6265242 to your computer and use it in GitHub Desktop.
The list of power users is this one: %w(ec2-user vagrant)
node[:ssh][:power_users].each do |username|
user_home = %x(echo -n ~#{username})
# backup the base management key
bash "copy_authorized_keys_#{username}" do
code "cd #{user_home}/.ssh && cp authorized_keys authorized_keys.orig"
# skip if there's no such user or the copy has been made
not_if "test -e #{user_home}/.ssh/authorized_keys.orig || ! id #{username}"
end
# Create .ssh folder for the user if not present
directory ".ssh_create_#{username}" do
path lazy { "#{user_home}/.ssh" }
user lazy { username }
group lazy { username }
action :create
recursive true
only_if "id #{username} && ! test -d #{user_home}/.ssh"
end
## For users without the key files, create an empty .orig file to avoid the template to fail
bash "create_empty_key_backup_#{username}" do
code "cd #{user_home}/.ssh && echo '#empty file created by chef' > authorized_keys.orig"
# run if there's such user but the .orig backup isn't there
only_if "id #{username} && ! test -e #{user_home}/.ssh/authorized_keys.orig"
end
bash "delete_#{username}_keys" do
code "rm #{user_home}/.ssh/authorized_keys"
only_if "id #{username} && test -e #{user_home}/.ssh/authorized_keys"
end
template "#{user_home}/.ssh/authorized_keys" do
source "ssh_authorized_keys.erb"
mode 0600
group username
owner username
variables :public_keys => [], :include_orig => true, :home_folder => user_home
only_if "id #{username}"
end
end
* bash[copy_authorized_keys_vagrant] action run
vpn-staging (skipped due to not_if)
vpn-staging * directory[create_.ssh_vagrant] action create
vpn-staging (skipped due to only_if)
vpn-staging * bash[create_empty_key_backup_vagrant] action run
vpn-staging (skipped due to only_if)
vpn-staging * bash[delete_vagrant_keys] action run
vpn-staging (skipped due to only_if)
vpn-staging * template[~vagrant/.ssh/authorized_keys] action create
vpn-staging (skipped due to only_if)
vpn-staging * bash[delete_powerless_keys] action run
vpn-staging (skipped due to only_if)
vpn-staging * template[/home/powerless/.ssh/authorized_keys] action create
vpn-staging * Parent directory /home/powerless/.ssh does not exist.
vpn-staging ================================================================================
vpn-staging Error executing action `create` on resource 'template[/home/powerless/.ssh/authorized_keys]'
vpn-staging ================================================================================
vpn-staging
vpn-staging
vpn-staging Chef::Exceptions::EnclosingDirectoryDoesNotExist
vpn-staging ------------------------------------------------
vpn-staging Parent directory /home/powerless/.ssh does not exist.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment