gitserversetup 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4be79a171022b5bae70297a1635994182612c227
4
+ data.tar.gz: edd672257a549c2e15e5af796bd200f695ba6b2a
5
+ SHA512:
6
+ metadata.gz: e2382169d34893bfe059a74f2480004d065089a770781dadba0670afab19280886b4ead86896308a2fe97bba0b1741a254a404e3899fe3e249e2a9a821597827
7
+ data.tar.gz: 865b473c7e62e3efb8170413a7ca1a9abb8b1712aba21480984f377ef21e605e7a942f67685d6528642ed9c066057edd9ed9bc3e0094e5f7e5508a72e8038c3d
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # REFERENCE: https://ericrafaloff.com/rubys-optionparser-is-all-you-need/
3
+
4
+ require 'optparse'
5
+ require_relative '../lib/gitserversetup.rb'
6
+
7
+ ops = {}
8
+ pr = OptionParser.new do |o|
9
+ o.banner = "Usage: gitserversetup -i SERVER_IP [options]"
10
+ o.on('-i', '--ip=IP', 'Server IP') { |v| ops[:server_ip] ||= v }
11
+ o.on('-k', '--key=PATH', 'Key') { |v| ops[:key] = v }
12
+ o.on('-u', '--user=USER', 'User') { |v| ops[:user] = v }
13
+ o.on("-h", "--help", 'Prints this help') { puts o ; exit }
14
+ end
15
+ pr.parse!
16
+
17
+ if ops[:server_ip].to_s != ''
18
+ GitServer::Setup.new(ops[:server_ip], ops[:key], ops[:user]).run
19
+ else
20
+ puts pr.parse('-h')
21
+ end
@@ -0,0 +1,66 @@
1
+ module GitServer
2
+ class Setup
3
+ def initialize(server, ssh_key, user)
4
+ ssh_key = '~/.ssh/id_rsa.pub' if ssh_key.to_s == ''
5
+ @key = ssh_key
6
+ @user = user.to_s == '' ? 'root' : user
7
+ @server = server
8
+ end
9
+
10
+ def run
11
+ cputs :green, "\nInstalling and configuring GIT\n"
12
+
13
+ sshrun 'apt-get update'
14
+ sshrun 'apt-get install git -y'
15
+ sshrun 'useradd -m -s /usr/bin/git-shell git'
16
+ sshrun 'mkdir -p /home/git/repo/hooks'
17
+ sshrun 'mkdir -p /home/git/.ssh'
18
+ sshrun 'chown -R git:git /home/git/*'
19
+ sshrun 'cd /home/git/repo && git init --bare'
20
+ sshrun "touch /home/git/.ssh/authorized_keys"
21
+
22
+ cputs :green, 'add git to sudoers'
23
+ sshrun 'usermod -aG sudo git'
24
+ scp File.join(__dir__, "git-sudoer"), "/etc/sudoers.d/git-sudoer"
25
+ sshrun "chmod 0440 /etc/sudoers.d/git-sudoer"
26
+
27
+
28
+ cputs :green, "\nInstalling post-receive script\n"
29
+ scp File.join(__dir__, "post-receive"), "/home/git/repo/hooks/post-receive"
30
+ sshrun 'chmod +x /home/git/repo/hooks/post-receive'
31
+
32
+ cputs :green, "\nAllowing this machine to push\n"
33
+ scp '~/.ssh/id_rsa.pub', '/home/git/.ssh/authorized_keys'
34
+ sshrun "chown git:git /home/git/.ssh/authorized_keys"
35
+
36
+ cputs :green, "\n=== GitServer setup complete ===\n"
37
+ cputs :white, "That's it. Now you can setup your git repo to push to this server by running:\n\n"
38
+ cputs :red, "$ echo 'echo \"Hi from server\"' > deploy.sh"
39
+ cputs :red, "$ git add deploy.sh && git commit -am 'new deploy'"
40
+ cputs :red, "$ git remote add prod git@#{@server}:~/repo"
41
+ cputs :red, "$ git push prod\n"
42
+ end
43
+
44
+ private
45
+
46
+ def us
47
+ "#{@user}@#{@server}"
48
+ end
49
+ def sshrun(arg)
50
+ cmd = "ssh -i #{@key} #{us} #{arg}"
51
+ cputs :blue, cmd
52
+ puts `#{cmd}`
53
+ end
54
+ def scp(src, dst)
55
+ cmd = "scp -i #{@key} #{src} #{us}:#{dst}"
56
+ cputs :yellow, cmd
57
+ `#{cmd}`
58
+ end
59
+
60
+ # Color puts
61
+ def cputs(color, string)
62
+ cs = {red: 31, green: 32, yellow: 33, blue: 34}[color]
63
+ puts cs.nil? ? string : "\e[#{cs}m#{string}\e[0m"
64
+ end
65
+ end
66
+ end
data/lib/post-receive ADDED
@@ -0,0 +1,65 @@
1
+ #!/bin/bash
2
+ #
3
+ # Original Source: https://gist.github.com/thomasfr/9691385
4
+ # Author: "FRITZ Thomas" <fritztho@gmail.com> (http://www.fritzthomas.com)
5
+ # GitHub: https://gist.github.com/thomasfr/9691385
6
+ #
7
+ # The MIT License (MIT)
8
+ #
9
+ # Copyright (c) 2014-2017 FRITZ Thomas
10
+ #
11
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ # of this software and associated documentation files (the "Software"), to deal
13
+ # in the Software without restriction, including without limitation the rights
14
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ # copies of the Software, and to permit persons to whom the Software is
16
+ # furnished to do so, subject to the following conditions:
17
+ #
18
+ # The above copyright notice and this permission notice shall be included in all
19
+ # copies or substantial portions of the Software.
20
+ #
21
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ # SOFTWARE.
28
+ #
29
+ export DEPLOY_APP_NAME=`whoami`
30
+ export DEPLOY_ROOT="${HOME}/work"
31
+ export DEPLOY_ALLOWED_BRANCH="master"
32
+ export GIT_DIR="$(cd $(dirname $(dirname $0));pwd)"
33
+ export GIT_WORK_TREE="${DEPLOY_ROOT}"
34
+ #PRE_UPDATE_CMD='cd ${DEPLOY_ROOT} && predeploy.sh'
35
+ POST_UPDATE_CMD='cd ${DEPLOY_ROOT} && chmod +x deploy.sh && ./deploy.sh'
36
+
37
+ IP="$(ip addr show eth0 | grep 'inet ' | cut -f2 | awk '{ print $2}')"
38
+ echo "GITSERVER: $(date): Welcome to '$(hostname -f)' (${IP})" && echo
39
+ mkdir -p "${DEPLOY_ROOT}" # Ensure directory exists.
40
+
41
+ # Loop, because it is possible to push more than one branch at a time. (git push --all)
42
+ while read oldrev newrev refname
43
+ do
44
+ export DEPLOY_BRANCH=$(git rev-parse --symbolic --abbrev-ref $refname)
45
+ export DEPLOY_OLDREV="$oldrev"
46
+ export DEPLOY_NEWREV="$newrev"
47
+ export DEPLOY_REFNAME="$refname"
48
+
49
+ if [ "$DEPLOY_NEWREV" = "0000000000000000000000000000000000000000" ]; then
50
+ echo "GITSERVER: This ref has been deleted" && exit 1
51
+ fi
52
+
53
+ if [ "${DEPLOY_ALLOWED_BRANCH}" != "$DEPLOY_BRANCH" ]; then
54
+ echo "GITSERVER: Branch '$DEPLOY_BRANCH' of '${DEPLOY_APP_NAME}' app will not be deployed. Exiting." && exit 1
55
+ fi
56
+
57
+ # eval $PRE_UPDATE_CMD || exit 1
58
+ echo "GITSERVER: deploying '${DEPLOY_BRANCH}' branch of the '${DEPLOY_APP_NAME}' project to '${DEPLOY_ROOT}'"
59
+ git checkout -f "${DEPLOY_BRANCH}" || exit 1
60
+ git reset --hard "$DEPLOY_NEWREV" || exit 1
61
+ eval $POST_UPDATE_CMD || exit 1
62
+ done
63
+
64
+ echo && echo "GITSERVER: $(date): See you soon at '$(hostname -f)' (${IP})"
65
+ exit 0
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitserversetup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel F Engel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple git server setup
14
+ email: gabrielfengel@gmail.com
15
+ executables:
16
+ - gitserversetup
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/gitserversetup
21
+ - lib/gitserversetup.rb
22
+ - lib/post-receive
23
+ homepage: https://github.com/pushprod/gitserversetup
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.6.13
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: GitServerSetup
47
+ test_files: []