Class: Wildcloud::Git::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/wildcloud/git/core.rb

Instance Method Summary collapse

Constructor Details

#initializeCore

Returns a new instance of Core.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wildcloud/git/core.rb', line 25

def initialize
  Git.logger.info('Core', 'Starting')

  # Define basic variables
  @access = {}
  @keys = {}

  # Connect to AMQP
  Git.logger.info('Core', 'Connecting to broker')
  @amqp = AMQP.connect(Git.configuration['amqp'])
  Git.logger_add_amqp(@amqp)

  # Communication infrastructure
  @channel = AMQP::Channel.new(@amqp)
  @topic = @channel.topic('wildcloud.git')
  @queue = @channel.queue("wildcloud.git.#{Git.configuration['node']['name']}")
  @queue.bind(@topic, :routing_key => 'nodes')
  @queue.bind(@topic, :routing_key => "node.#{Git.configuration['node']['name']}")

  # Request synchronization
  Git.logger.info('Core', 'Requesting synchronization')
  publish({ :node => Git.configuration['node']['name'], :type => :sync })

  # Listen for task
  @queue.subscribe do |, message|
    handle(, message)
  end
end

Instance Method Details

#authorize(username, repository, action) ⇒ Object



54
55
56
57
58
# File 'lib/wildcloud/git/core.rb', line 54

def authorize(username, repository, action)
  return 1 if username == 'wildcloud.platform.master.key'
  return 0 unless @access[username] && @access[username].include?(repository)
  1
end

#handle(metadata, message) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/wildcloud/git/core.rb', line 60

def handle(, message)
  Git.logger.debug('Core', "Got message: #{message}")
  message = JSON.parse(message)
  method = "handle_#{message['type']}".to_sym
  if respond_to?(method)
    send(method, message)
  else

  end
end

#handle_add_key(data) ⇒ Object



77
78
79
80
# File 'lib/wildcloud/git/core.rb', line 77

def handle_add_key(data)
  (@keys[data['username']] ||= []) << data['key']
  sync_keys
end

#handle_add_platform_key(data) ⇒ Object



90
91
92
93
94
# File 'lib/wildcloud/git/core.rb', line 90

def handle_add_platform_key(data)
  @platform_keys ||= []
  @platform_keys << data['key'] unless @platform_keys.include?(data['key'])
  sync_keys
end

#handle_authorize_repository(data) ⇒ Object



117
118
119
# File 'lib/wildcloud/git/core.rb', line 117

def handle_authorize_repository(data)
  (@access[data['username']] ||= []) << data['repository']
end

#handle_create_repository(data) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/wildcloud/git/core.rb', line 101

def handle_create_repository(data)
  path = File.join(Git.configuration['paths']['repositories'], data['repository'])
  `git init --bare #{path} --template #{File.expand_path('../../template', __FILE__)}`
  pre_receive = File.read(File.expand_path('../pre-receive.rb', __FILE__))
  pre_receive = "#!#{Git.configuration['paths']['ruby']}\n#{pre_receive}"
  hook_path = File.join(path, 'hooks', 'pre-receive')
  File.open(hook_path, 'w') do |file|
    file.write(pre_receive)
  end
  File.chmod(0700, hook_path)
end

#handle_destroy_repository(data) ⇒ Object



113
114
115
# File 'lib/wildcloud/git/core.rb', line 113

def handle_destroy_repository(data)
  `rm -rf #{File.join(Git.configuration['paths']['repositories'], data['repository'])}`
end

#handle_remove_key(data) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/wildcloud/git/core.rb', line 82

def handle_remove_key(data)
  keys = @keys[data['username']]
  if keys
    keys.delete(data['key'])
    sync_keys
  end
end

#handle_remove_platform_key(data) ⇒ Object



96
97
98
99
# File 'lib/wildcloud/git/core.rb', line 96

def handle_remove_platform_key(data)
  (@platform_keys ||= []).delete(data['key'])
  sync_keys
end

#handle_sync(data) ⇒ Object



71
72
73
74
75
# File 'lib/wildcloud/git/core.rb', line 71

def handle_sync(data)
  @access = data['access']
  @keys = data['keys']
  sync_keys
end

#handle_unauthorize_repository(data) ⇒ Object



121
122
123
124
# File 'lib/wildcloud/git/core.rb', line 121

def handle_unauthorize_repository(data)
  repos = @access[data['username']]
  repos.delete(data['repository']) if repos
end

#publish(message) ⇒ Object



147
148
149
150
# File 'lib/wildcloud/git/core.rb', line 147

def publish(message)
  Git.logger.debug('Core', "Publishing #{message.inspect}")
  @topic.publish(JSON.dump(message), :routing_key => 'master')
end

#sync_keysObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wildcloud/git/core.rb', line 126

def sync_keys
  Git.logger.info('Core', 'Synchronizing keys')
  data = ""
  client = File.expand_path('../client.rb', __FILE__)
  ks = us = 0
  @platform_keys.each do |key|
    data << "command=\"source /etc/profile && wildcloud-git-client 'wildcloud.platform.master.key'\" #{key}\n"
  end if @platform_keys
  @keys.each do |username, keys|
    us += 1
    keys.each do |key|
      data << "command=\"source /etc/profile && wildcloud-git-client #{username}\" #{key}\n"
      ks += 1
    end
  end if @keys
  File.open('./.ssh/authorized_keys', 'w') do |file|
    file.write(data)
  end
  Git.logger.info('Core', "Data synchronized (#{us} users, #{ks} keys)")
end