Skip to content
Browse files

multi server

  • Loading branch information...
1 parent 831d03c commit 6c3d5e10ed3bf8f6128f3e312ab0ac101cbaf291 @frankyue committed
Showing with 51 additions and 32 deletions.
  1. +22 −18 README.md
  2. +13 −7 lib/alias_gen.rb
  3. +14 −5 lib/file_combiner.rb
  4. +2 −2 lib/uploader.rb
View
40 README.md
@@ -9,33 +9,37 @@ Usage
1. `gem install ssh-key-sync-man`
-2. Put all your team members' keys into one `available_public_keys` directory with the structure looks like:
+2. Put all your team members' keys into one `available_public_keys` directory
- available_public_keys/groupA/michael
- available_public_keys/------/jason
- available_public_keys/------/john
- available_public_keys/groupB/rose
- available_public_keys/------/ryan
+ available_public_keys/michael
+ available_public_keys/jason
+ available_public_keys/john
+ available_public_keys/rose
+ available_public_keys/ryan
3. Add a `server_list.yml`, format like:
+ GroupA:
servers:
- groupA:
- - host: xxx.com
- user: app
- groupB:
- - host: aaa.com
- user: app
- alias: app_server
- - host: aaa.com
- user: db
- alias: db_master
+ - host: xxx.com
+ user: app
+ - host: aaa.com
+ user: app
+ alias: app_server
+ users: [ jaon, ryan ]
+
+ GroupB
+ servers:
+ - host: aaa.com
+ user: db
+ alias: db_master
+ users: [ jaon, ryan, michael ]
(You can puts `available_public_keys` and `server_list.yml` at github, them people can add files by themselves)
4. ssh-key-sync-man -g groupA
- This will deploy public keys in `available_public_keys/groupA` to groupA servers
+ This will deploy the users' public keys which defined in `server_list.yml` to groupA servers
"alias" list -- linux shotcut command list auto generator
@@ -50,4 +54,4 @@ Generate alias file for everyone, for example:
alias serverC_db="ssh app@host"
alias serverD_staging="ssh app@host"
-You can copy and paste into your .bashrc or .bash_profile
+You can copy and paste into your .bashrc or .bash_profile
View
20 lib/alias_gen.rb
@@ -4,23 +4,29 @@ module SshKeyMan
class AliasGen
def self.generate user
server_list_path = File.join(".", "server_list.yml")
- servers = YAML::load_file(server_list_path)['servers']
- groups = get_user_groups(user)
+ servers = YAML::load_file(server_list_path)#['servers']
+ groups = get_user_groups(user,servers)
puts "\e[31m You can copy below code to '~/.bash_profile' or '~/.bashrc'. \e[0m"
puts "============================================="
groups.each do |group|
- servers[group].each do |server|
+ servers[group]["servers"].each do | server |
puts "alias #{group}_#{server['alias']}=\"#{server['user']}@#{server['host']}\""
end
end
puts "============================================="
end
- def self.get_user_groups user
- user_groups = `cd available_public_keys; find . -name #{user}`.split("\n")
+ def self.get_user_groups(user,servers)
+ user_groups = []
+ servers.each do |gkey,values|
+ if values.has_key?("users") and values.fetch("users").include?(user)
+ user_groups.push(gkey)
+ end
+ end
+
raise "Not found user: \"#{user}\"" if user_groups.size == 0
- user_groups.map { |user_group| user_group.slice(/[^\.\/]+(?=\/)/) }
+ return user_groups
end
end
-end
+end
View
19 lib/file_combiner.rb
@@ -3,21 +3,30 @@ class PublicKeyCombiner
def self.combine group
puts "combining public keys ..."
+ server_list_path = File.join(".", "server_list.yml")
+
+ users = YAML::load_file(server_list_path)[group]['users']
+
+ raise "No users are seted in #{group} config" if users.nil?
+
authorized_keys_path = File.join ".", "authorized_keys"
public_key_path = File.join(".", "available_public_keys")
File.open authorized_keys_path, "w" do |f|
f.write File.read(get_current_user_public_key_path) if get_current_user_public_key_path
- files = Dir[File.join(public_key_path, group, "*")]
-
- raise "No such a server group: #{group}" if files.size == 0
+ files = []
+ users.each do | user |
+ files = files + Dir[File.join(public_key_path, user)]
+ end
+ raise "Can't find key files of user #{users}" if files.size == 0
+
files.each do |file|
f.write File.read(file)
end
end
- puts "finished combining public keys ..."
+ puts "added the uesers #{users} into authorized_keys , finished combining public keys ..."
end
def self.get_current_user_public_key_path
@@ -29,4 +38,4 @@ def self.get_current_user_public_key_path
current_user_key_path
end
end
-end
+end
View
4 lib/uploader.rb
@@ -15,7 +15,7 @@ def self.upload_all_public_keys group
#
def self.upload_to_all_servers source, dest, group
server_list_path = File.join(".", "server_list.yml")
- servers = YAML::load_file(server_list_path)['servers'][group]
+ servers = YAML::load_file(server_list_path)[group]['servers']
raise "No Server Group: #{group}" if servers.size == 0
servers.each do |server_info|
upload! server_info["host"], server_info["port"]||"22", server_info["user"], source, dest
@@ -31,4 +31,4 @@ def self.upload! host, port, user, source, dest
raise "upload failed" if $?.exitstatus != 0
end
end
-end
+end

0 comments on commit 6c3d5e1

Please sign in to comment.
Something went wrong with that request. Please try again.