Skip to content

Instantly share code, notes, and snippets.

@astroza
Created October 30, 2015 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astroza/2a9bac114add93c785c4 to your computer and use it in GitHub Desktop.
Save astroza/2a9bac114add93c785c4 to your computer and use it in GitHub Desktop.
require 'git'
require 'fileutils'
require 'yaml'
class Key < ActiveRecord::Base
def self.authorized_keys_content
Key.all.each.reduce('') do |ac, key|
ac << "#{key.public_key}\n"
end
end
def self.update_authorized_keys
Delayed::Job.find(:queue => 'keys').delete_all
self.delay(:run_at => 1.minutes.from_now, :queue => 'keys').do_update_authorized_keys
end
def self.do_update_authorized_keys
FileUtils.rm_rf('/tmp/salt_repo')
g = Git.clone('git@github.com:4Talent/Salt.git', 'Salt', :path => '/tmp/salt_repo')
g.chdir do
content = authorized_keys_content
IO.write('ssh/files/authorized_keys', content)
IO.write('hupe/files/authorized_keys', content)
end
begin
g.commit_all('[DevOp App] Authorized keys updated')
g.push('origin')
rescue
end
ret = YAML.load(`salt '*' state.show_top --out=yaml`)
ret.each do |key, value|
if value.class == Hash and not value['base'].nil?
ssh_state = value['base'].reject do |state|
not state.include? 'ssh'
end
if not ssh_state.empty?
p `salt #{key} state.sls #{ssh_state.last} --out=yaml`
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment