class Turnstyl::Client

Public Class Methods

home_folder() click to toggle source
# File lib/turnstyl/client/client.rb, line 6
def self.home_folder
  Dir.home
end
new() click to toggle source
# File lib/turnstyl/client/client.rb, line 10
    def initialize
      @config_path = File.expand_path(Client.home_folder+"/.turnstylrc")
      if config_file_missing?
        puts <<-TEXT

Unable to run without a config file.

Try something like this in ~/.turnstylrc

    userlist = [ "githubuser1", "githubuser2", "...", "githubuser99" ]

        TEXT
        exit 1
      end
    end

Public Instance Methods

authorized_key_missing?() click to toggle source
# File lib/turnstyl/client/client.rb, line 72
def authorized_key_missing?
  !File.exist? Client.home_folder+"/.ssh/authorized_keys"
end
config_changed?() click to toggle source
# File lib/turnstyl/client/client.rb, line 80
def config_changed?
  File.mtime(@config_path) > File.mtime(Client.home_folder+"/.ssh/authorized_keys")
end
config_file_missing?() click to toggle source
# File lib/turnstyl/client/client.rb, line 76
def config_file_missing?
  !File.exist? Client.home_folder+"/.turnstylrc"
end
create_backup() click to toggle source
# File lib/turnstyl/client/client.rb, line 88
def create_backup
  puts "\nMaking a backup ..."
  number = Dir.glob(Client.home_folder+'/.ssh/authorized_keys*').count
  puts number
  FileUtils.mv(Client.home_folder+"/.ssh/authorized_keys", Client.home_folder+"/.ssh/authorized_keys.bak"+number.to_s)
end
run(force) click to toggle source
# File lib/turnstyl/client/client.rb, line 26
def run(force)
  if force
    update_authorized_keys
  else
    update_authorized_keys_carefully
  end
end
update_authorized_keys() click to toggle source
# File lib/turnstyl/client/client.rb, line 55
def update_authorized_keys
  settings = load_settings
  authorized_users = settings["userlist"]
  keys = []
  if authorized_users.empty?
    keys << '# YOU HAVE NOT AUTHORIZED ANYONE TO LOGIN'
  else
    authorized_users.each do |person|
      keys = keys + Communicator.new.get_keys_from(person)
    end
  end
  File.open(Client.home_folder+'/.ssh/authorized_keys', 'w+') do |file|
    file.write(keys.join("\n") << "\n")
  end
  puts "\nkeys updated..."
end
update_authorized_keys_carefully() click to toggle source
# File lib/turnstyl/client/client.rb, line 34
def update_authorized_keys_carefully
  if authorized_key_missing?
    update_authorized_keys
  elsif config_changed?
    print "Authorized keys file exists. Overwrite? [y/N/b/?]? "
    input = STDIN.gets.chomp
    case input
    when "y"
      update_authorized_keys
    when "b"
      create_backup
      update_authorized_keys
    when "?"
      display_help
      update_authorized_keys_carefully
    else
      puts "\nDoing nothing ..."
    end
  end
end
update_necessary?() click to toggle source
# File lib/turnstyl/client/client.rb, line 84
def update_necessary?
  authorized_key_missing? || config_changed?
end

Private Instance Methods

display_help() click to toggle source
# File lib/turnstyl/client/client.rb, line 101
    def display_help
      puts <<-HERE

You tried to let turnstyl manage your authorized_keys file, but there is an
existing authorized_keys file and you have to decide what you want to do with
it.

If you're sure you want to overwrite it choose "y" if you want to backup your
existing file choose "b"

HERE
    end
load_settings() click to toggle source
# File lib/turnstyl/client/client.rb, line 97
def load_settings
  TOML.load_file(@config_path)
end