Created
October 30, 2015 14:48
-
-
Save astroza/2a9bac114add93c785c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('[email protected]: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