Module: Keyman

Defined in:
lib/keyman.rb,
lib/keyman/user.rb,
lib/keyman/group.rb,
lib/keyman/server.rb,
lib/keyman/manifest.rb,
lib/keyman/server_group.rb

Defined Under Namespace

Classes: Error, Group, Manifest, Server, ServerGroup, User

Constant Summary collapse

VERSION =

The current version of the keyman system

'1.1.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.groupsObject

Returns the value of attribute groups.



29
30
31
# File 'lib/keyman.rb', line 29

def groups
  @groups
end

.manifestObject

Stores the actual manifest object



21
22
23
# File 'lib/keyman.rb', line 21

def manifest
  @manifest
end

.manifest_dirObject

Sets the default manifest_dir dir



24
25
26
# File 'lib/keyman.rb', line 24

def manifest_dir
  @manifest_dir
end

.password_cacheObject

Storage for a password cache to use within the current session



34
35
36
# File 'lib/keyman.rb', line 34

def password_cache
  @password_cache
end

.server_groupsObject

Returns the value of attribute server_groups.



31
32
33
# File 'lib/keyman.rb', line 31

def server_groups
  @server_groups
end

.serversObject

Returns the value of attribute servers.



30
31
32
# File 'lib/keyman.rb', line 30

def servers
  @servers
end

.usersObject

Storage for all the users, groups & servers which are loaded from the manifest



28
29
30
# File 'lib/keyman.rb', line 28

def users
  @users
end

Class Method Details

.configObject

Sets the configuration options



42
43
44
45
46
47
48
49
50
51
# File 'lib/keyman.rb', line 42

def config
  @config ||= begin
    config_dir = File.join(ENV['HOME'], '.keyman')
    if File.exist?(config_dir)
      YAML.load_file(config_dir)
    else
      {}
    end
  end
end

.run(args, options = {}) ⇒ Object

Execute a CLI command



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/keyman.rb', line 59

def run(args, options = {})
  Manifest.load
  case args.first
  when 'keys'
    if server = self.servers.select { |s| s.host == args[1] }.first
      if server.users[args[2]]
        puts server.authorized_keys(args[2])
      else
        raise Error, "'#{args[2]}' is not a valid user for this host"
      end
    else
      raise Error, "No server found with the hostname '#{args[1]}'"
    end
  when 'permissions'
    if server = self.servers.select { |s| s.host == args[1] }.first
      server.users.each do |username, objects|
        puts '-' * 80
        puts "\e[32m#{username}\e[0m can be used by:"
        puts '-' * 80
        server.authorized_users(username).each do |o|
          puts " * #{o.name}"
        end
      end
    else
      raise Error, "No server found with the hostname '#{args[1]}'"
    end
  when 'push'
    
    if self.manifest.uses_git?
      unless self.manifest.clean?
        raise Error, "Your manifest is not clean. You should push to your repository before pushing."
      end
      
      unless self.manifest.latest_commit?
        raise Error, "The remote server has a more up-to-date manifest. Pull first."
      end
      
      puts "\e[32mRepository check passed!\e[0m"
    end
    
    if args[1]
      server = self.servers.select { |s| s.host == args[1] }.first
      server = self.server_groups.select { |s| s.name == args[1].to_sym }.first if server.nil?
      if server.is_a?(Keyman::Server)
        server.push!
      elsif server.is_a?(Keyman::ServerGroup)
        server.servers.each(&:push!)
      else
        raise Error, "No server found with the hostname '#{args[1]}'"
      end
    else
      self.servers.each(&:push!)
    end
  when 'servers'
    self.server_groups.sort_by(&:name).each do |group|
      puts '-' * 80
      puts group.name.to_s
      puts '-' * 80
      group.servers.each do |s|
        puts " * #{s.host}"
      end
    end
    puts '-' * 80
    puts 'no group'
    puts '-' * 80
    self.servers.select { |s| s.group.nil?}.each do |s|
      puts " * #{s.host}"
    end
    
  when 'users'
    self.groups.each do |group|
      puts "-" * 80
      puts group.name
      puts "-" * 80
      group.users.each do |u|
        puts "\e[32m#{u.name}\e[0m"
        puts "\e[37m#{u.key}\e[0m"
      end
    end
    
  when 'status'
    if Keyman.manifest.uses_git?
      puts "Your manifest is using a remote git repository."
      if Keyman.manifest.clean?
        puts " * Your working copy is clean"
      else
        puts " * You have an un-clean working copy. You must commit before pushing."
      end
      
      if Keyman.manifest.latest_commit?
        puts " * You have the latest commit fetched."
      else
        puts " * There is a newer version of this repo on the server."
      end
    else
      puts "Your manifest does not use git. There is no status to display."
    end
    
  else
    raise Error, "Invalid command '#{args.first}'"
  end
end

.user_or_group_for(name) ⇒ Object

Return a user or a group for the given name



54
55
56
# File 'lib/keyman.rb', line 54

def user_or_group_for(name)
  self.users.select { |u| u.name == name.to_sym }.first || self.groups.select { |u| u.name == name.to_sym }.first
end