#
# Cookbook:: irrigate-php
# Recipe:: deploy_qa_user
#

user node['php']['deploy_user'] do
  comment 'Deploy user'
  uid '1001'
  home node['php']['deploy_home_dir']
  manage_home true
  shell '/bin/bash'
end

cron "#{node['php']['deploy_user']}_password_update_routine" do
  weekday 1
  hour 0
  minute 30
  command "echo #{node['php']['deploy_user']}:$(openssl rand -base64 24) | chpasswd"
end

group 'webdev' do
  gid '1099'
  members node['php']['deploy_group_members']
  action :create
end

# SSH key used by deploy processes
ssh_authorize_key "qa-#{node['php']['deploy_user']}" do
  key node['php']['qa_deploy_user_pub_key']
  user node['php']['deploy_user']
end

# This is an ssh key for the deploy user
github_ssh_key_location = "#{node['php']['deploy_home_dir']}/.ssh/id_rsa"
github_ssh_key = data_bag_item('secrets', node['php']['qa_deploy_user_github_key'])
private_key = github_ssh_key['private_key'].chomp
file github_ssh_key_location do
  owner node['php']['deploy_user']
  mode '0400'
  content private_key
end

# Relax ssh host key checks or initial git clone will fail
key_config = "Host *\n\tStrictHostKeyChecking no\n"
file "#{node['php']['deploy_home_dir']}/.ssh/config" do
  owner node['php']['deploy_user']
  group node['php']['deploy_user']
  mode '0600'
  content key_config
end

cookbook_file "#{node['php']['deploy_home_dir']}/.ssh/id_rsa.pub" do
  source node['php']['qa_deploy_user_github_pub_key']
  mode '0644'
end
