class GitServer::Setup

Public Class Methods

new(server, ssh_key, user) click to toggle source
# File lib/gitserversetup.rb, line 3
def initialize(server, ssh_key, user)
  ssh_key = '~/.ssh/id_rsa.pub' if ssh_key.to_s == ''
  @key = ssh_key
  @user = user.to_s == '' ? 'root' : user
  @server = server
end

Public Instance Methods

run() click to toggle source
# File lib/gitserversetup.rb, line 10
def run
  cputs :green, "\nInstalling and configuring GIT\n"

  sshrun 'apt-get update'
  sshrun 'apt-get install git -y'
  sshrun 'useradd -m -s /usr/bin/git-shell git'
  sshrun 'mkdir -p /home/git/repo/hooks'
  sshrun 'mkdir -p /home/git/.ssh'
  sshrun 'mkdir -p /home/git/repo'
  sshrun '"cd /home/git/repo && git init --bare"'
  sshrun 'chown -R git:git /home/git/*'
  sshrun "touch /home/git/.ssh/authorized_keys"
  
  cputs :green, 'add git to sudoers'
  sshrun 'usermod -aG sudo git'
  scp File.join(__dir__, "git-sudoer"), "/etc/sudoers.d/git-sudoer"
  sshrun "chmod 0440 /etc/sudoers.d/git-sudoer"

  
  cputs :green, "\nInstalling post-receive script\n"
  scp File.join(__dir__, "post-receive"), "/home/git/repo/hooks/post-receive"
  sshrun 'chmod +x /home/git/repo/hooks/post-receive'
  
  cputs :green, "\nAllowing this machine to push\n"
  scp '~/.ssh/id_rsa.pub', '/home/git/.ssh/authorized_keys'
  sshrun "chown git:git /home/git/.ssh/authorized_keys"

  cputs :green, "\n=== GitServer setup complete ===\n"
  cputs :white, "That's it. Now you can setup your git repo to push to this server by running:\n\n"
  cputs :red, "$ echo 'echo \"Hi from server\"' > deploy.sh"
  cputs :red, "$ git add deploy.sh && git commit -am 'new deploy'"
  cputs :red, "$ git remote add prod git@#{@server}:~/repo"
  cputs :red, "$ git push prod\n"
end

Private Instance Methods

cputs(color, string) click to toggle source

Color puts

# File lib/gitserversetup.rb, line 62
def cputs(color, string) 
  cs = {red: 31, green: 32, yellow: 33, blue: 34}[color]
  puts cs.nil? ? string : "\e[#{cs}m#{string}\e[0m"
end
scp(src, dst) click to toggle source
# File lib/gitserversetup.rb, line 55
def scp(src, dst)
  cmd = "scp -i #{@key} #{src} #{us}:#{dst}"
  cputs :yellow, cmd
  `#{cmd}`
end
sshrun(arg) click to toggle source
# File lib/gitserversetup.rb, line 50
def sshrun(arg)
  cmd = "ssh -i #{@key} #{us} #{arg}"
  cputs :blue, cmd
  puts `#{cmd}`
end
us() click to toggle source
# File lib/gitserversetup.rb, line 47
def us
  "#{@user}@#{@server}"
end