Class: Turnstyl::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/turnstyl/client/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Class Method Details

.home_folderObject



6
7
8
# File 'lib/turnstyl/client/client.rb', line 6

def self.home_folder
  Dir.home
end

Instance Method Details

#authorized_key_missing?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/turnstyl/client/client.rb', line 72

def authorized_key_missing?
  !File.exist? Client.home_folder+"/.ssh/authorized_keys"
end

#config_changed?Boolean

Returns:

  • (Boolean)


80
81
82
# 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?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/turnstyl/client/client.rb', line 76

def config_file_missing?
  !File.exist? Client.home_folder+"/.turnstylrc"
end

#create_backupObject



88
89
90
91
92
93
# 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) ⇒ Object



26
27
28
29
30
31
32
# 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_keysObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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_carefullyObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/turnstyl/client/client.rb', line 84

def update_necessary?
  authorized_key_missing? || config_changed?
end