Skip to content

Commit ded8dab

Browse files
committed
Sectionize chapter 4
1 parent f616973 commit ded8dab

File tree

10 files changed

+822
-815
lines changed

10 files changed

+822
-815
lines changed

book/04-git-server/1-git-server.asc

Lines changed: 9 additions & 815 deletions
Large diffs are not rendered by default.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[[_generate_ssh_key]]
2+
=== Generating Your SSH Public Key
3+
4+
(((SSH keys)))
5+
That being said, many Git servers authenticate using SSH public keys.
6+
In order to provide a public key, each user in your system must generate one if they don’t already have one.
7+
This process is similar across all operating systems.
8+
First, you should check to make sure you don’t already have a key.
9+
By default, a user’s SSH keys are stored in that user’s `~/.ssh` directory.
10+
You can easily check to see if you have a key already by going to that directory and listing the contents:
11+
12+
[source,shell]
13+
----
14+
$ cd ~/.ssh
15+
$ ls
16+
authorized_keys2 id_dsa known_hosts
17+
config id_dsa.pub
18+
----
19+
20+
You’re looking for a pair of files named something like `id_dsa` or `id_rsa` and a matching file with a `.pub` extension.
21+
The `.pub` file is your public key, and the other file is your private key.
22+
If you don’t have these files (or you don’t even have a `.ssh` directory), you can create them by running a program called `ssh-keygen`, which is provided with the SSH package on Linux/Mac systems and comes with the MSysGit package on Windows:
23+
24+
[source,shell]
25+
----
26+
$ ssh-keygen
27+
Generating public/private rsa key pair.
28+
Enter file in which to save the key (/home/schacon/.ssh/id_rsa):
29+
Created directory '/home/schacon/.ssh'.
30+
Enter passphrase (empty for no passphrase):
31+
Enter same passphrase again:
32+
Your identification has been saved in /home/schacon/.ssh/id_rsa.
33+
Your public key has been saved in /home/schacon/.ssh/id_rsa.pub.
34+
The key fingerprint is:
35+
d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 [email protected]
36+
----
37+
38+
First it confirms where you want to save the key (`.ssh/id_rsa`), and then it asks twice for a passphrase, which you can leave empty if you don’t want to type a password when you use the key.
39+
40+
Now, each user that does this has to send their public key to you or whoever is administrating the Git server (assuming you’re using an SSH server setup that requires public keys).
41+
All they have to do is copy the contents of the `.pub` file and e-mail it.
42+
The public keys look something like this:
43+
44+
[source,shell]
45+
----
46+
$ cat ~/.ssh/id_rsa.pub
47+
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU
48+
GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3
49+
Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA
50+
t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En
51+
mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx
52+
NrRFi9wrf+M7Q== [email protected]
53+
----
54+
55+
For a more in-depth tutorial on creating an SSH key on multiple operating systems, see the GitHub guide on SSH keys at https://help.github.com/articles/generating-ssh-keys[].
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
=== Git Daemon
2+
3+
(((serving repositories, git protocol)))
4+
Next we'll set up a daemon serving repositories over the ``Git'' protocol. This is common choice for fast, unauthenticated access to your Git data. Remember that since it's not an authenticated service, anything you serve over this protocol is public within it's network.
5+
6+
If you’re running this on a server outside your firewall, it should only be used for projects that are publicly visible to the world.
7+
If the server you’re running it on is inside your firewall, you might use it for projects that a large number of people or computers (continuous integration or build servers) have read-only access to, when you don’t want to have to add an SSH key for each.
8+
9+
In any case, the Git protocol is relatively easy to set up.
10+
Basically, you need to run this command in a daemonized manner:(((git commands, daemon)))
11+
12+
[source,shell]
13+
----
14+
git daemon --reuseaddr --base-path=/opt/git/ /opt/git/
15+
----
16+
17+
`--reuseaddr` allows the server to restart without waiting for old connections to time out, the `--base-path` option allows people to clone projects without specifying the entire path, and the path at the end tells the Git daemon where to look for repositories to export.
18+
If you’re running a firewall, you’ll also need to punch a hole in it at port 9418 on the box you’re setting this up on.
19+
20+
You can daemonize this process a number of ways, depending on the operating system you’re running.
21+
On an Ubuntu machine, you can use an Upstart script.
22+
So, in the following file
23+
24+
[source,shell]
25+
----
26+
/etc/event.d/local-git-daemon
27+
----
28+
29+
you put this script:
30+
31+
[source,shell]
32+
----
33+
start on startup
34+
stop on shutdown
35+
exec /usr/bin/git daemon \
36+
--user=git --group=git \
37+
--reuseaddr \
38+
--base-path=/opt/git/ \
39+
/opt/git/
40+
respawn
41+
----
42+
43+
For security reasons, it is strongly encouraged to have this daemon run as a user with read-only permissions to the repositories – you can easily do this by creating a new user 'git-ro' and running the daemon as them.
44+
For the sake of simplicity we’ll simply run it as the same 'git' user that Gitosis is running as.
45+
46+
When you restart your machine, your Git daemon will start automatically and respawn if it goes down.
47+
To get it running without having to reboot, you can run this:
48+
49+
[source,shell]
50+
----
51+
initctl start local-git-daemon
52+
----
53+
54+
On other systems, you may want to use `xinetd`, a script in your `sysvinit` system, or something else – as long as you get that command daemonized and watched somehow.
55+
56+
Next, you have to tell Git which repositories to allow unauthenticated Git server-based access to. You can do this in each repository by creating a file name `git-daemon-export-ok`.
57+
58+
[source,shell]
59+
----
60+
$ cd /path/to/project.git
61+
$ touch git-daemon-export-ok
62+
----
63+
64+
The presence of that file tells Git that it’s OK to serve this project without authentication.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
=== Getting Git on a Server
2+
3+
Now we'll cover setting up a Git service running these protocols on your own server.
4+
5+
[NOTE]
6+
====
7+
Here we'll be demonstrating the commands and steps needed to do basic installations on a Linux based server, though it's also possible to run these services on Mac or Windows servers too.
8+
Actually setting up a production server within your infrastructure will certainly entail differences in security measures or operating system tools, but hopefully this will give you the general idea of what's involved.
9+
====
10+
11+
In order to initially set up any Git server, you have to export an existing repository into a new bare repository – a repository that doesn’t contain a working directory.
12+
This is generally straightforward to do.
13+
In order to clone your repository to create a new bare repository, you run the clone command with the `--bare` option.(((git commands, clone, bare)))
14+
By convention, bare repository directories end in `.git`, like so:
15+
16+
[source,shell]
17+
----
18+
$ git clone --bare my_project my_project.git
19+
Cloning into bare repository 'my_project.git'...
20+
done.
21+
----
22+
23+
You should now have a copy of the Git directory data in your `my_project.git` directory.
24+
25+
This is roughly equivalent to something like
26+
27+
[source,shell]
28+
----
29+
$ cp -Rf my_project/.git my_project.git
30+
----
31+
32+
There are a couple of minor differences in the configuration file; but for your purpose, this is close to the same thing.
33+
It takes the Git repository by itself, without a working directory, and creates a directory specifically for it alone.
34+
35+
==== Putting the Bare Repository on a Server
36+
37+
Now that you have a bare copy of your repository, all you need to do is put it on a server and set up your protocols.
38+
Let’s say you’ve set up a server called `git.example.com` that you have SSH access to, and you want to store all your Git repositories under the `/opt/git` directory.
39+
Assuming that `/opt/git` exists on that server, you can set up your new repository by copying your bare repository over:
40+
41+
[source,shell]
42+
----
43+
$ scp -r my_project.git [email protected]:/opt/git
44+
----
45+
46+
At this point, other users who have SSH access to the same server which has read-access to the `/opt/git` directory can clone your repository by running
47+
48+
[source,shell]
49+
----
50+
$ git clone [email protected]:/opt/git/my_project.git
51+
----
52+
53+
If a user SSHs into a server and has write access to the `/opt/git/my_project.git` directory, they will also automatically have push access.
54+
55+
Git will automatically add group write permissions to a repository properly if you run the `git init` command with the `--shared` option.(((git commands, init, bare)))
56+
57+
[source,shell]
58+
----
59+
60+
$ cd /opt/git/my_project.git
61+
$ git init --bare --shared
62+
----
63+
64+
You see how easy it is to take a Git repository, create a bare version, and place it on a server to which you and your collaborators have SSH access.
65+
Now you’re ready to collaborate on the same project.
66+
67+
It’s important to note that this is literally all you need to do to run a useful Git server to which several people have access – just add SSH-able accounts on a server, and stick a bare repository somewhere that all those users have read and write access to.
68+
You’re ready to go – nothing else needed.
69+
70+
In the next few sections, you’ll see how to expand to more sophisticated setups.
71+
This discussion will include not having to create user accounts for each user, adding public read access to repositories, setting up web UIs, using the Gitosis tool, and more.
72+
However, keep in mind that to collaborate with a couple of people on a private project, all you _need_ is an SSH server and a bare repository.
73+
74+
==== Small Setups
75+
76+
If you’re a small outfit or are just trying out Git in your organization and have only a few developers, things can be simple for you.
77+
One of the most complicated aspects of setting up a Git server is user management.
78+
If you want some repositories to be read-only to certain users and read/write to others, access and permissions can be a bit more difficult to arrange.
79+
80+
===== SSH Access
81+
82+
(((serving repositories, SSH)))
83+
If you have a server to which all your developers already have SSH access, it’s generally easiest to set up your first repository there, because you have to do almost no work (as we covered in the last section).
84+
If you want more complex access control type permissions on your repositories, you can handle them with the normal filesystem permissions of the operating system your server runs.
85+
86+
If you want to place your repositories on a server that doesn’t have accounts for everyone on your team whom you want to have write access, then you must set up SSH access for them.
87+
We assume that if you have a server with which to do this, you already have an SSH server installed, and that’s how you’re accessing the server.
88+
89+
There are a few ways you can give access to everyone on your team.
90+
The first is to set up accounts for everybody, which is straightforward but can be cumbersome.
91+
You may not want to run `adduser` and set temporary passwords for every user.
92+
93+
A second method is to create a single 'git' user on the machine, ask every user who is to have write access to send you an SSH public key, and add that key to the `~/.ssh/authorized_keys` file of your new 'git' user.
94+
At that point, everyone will be able to access that machine via the 'git' user.
95+
This doesn’t affect the commit data in any way – the SSH user you connect as doesn’t affect the commits you’ve recorded.
96+
97+
Another way to do it is to have your SSH server authenticate from an LDAP server or some other centralized authentication source that you may already have set up.
98+
As long as each user can get shell access on the machine, any SSH authentication mechanism you can think of should work.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
=== GitLab
2+
3+
(((serving repositories, GitLab)))(((GitLab)))
4+
GitWeb is pretty simplistic though.
5+
If you're looking for a more modern, fully featured Git server, there are some several open source solutions out there that you can install instead.
6+
As GitLab is one of the more popular ones, we'll cover installing and using it as an example.
7+
This is a bit more complex than the GitWeb option and likely requires more maintainance, but it is a much more fully featured option.
8+
9+
==== Installation
10+
11+
GitLab is a database-backed web application, so its installation is a bit more involved than some other git servers.
12+
Fortunately, this process is very well-documented and supported.
13+
14+
There are a few methods you can pursue to install GitLab.
15+
To get something up and running quickly, you can download a virtual machine image or a one-click installer from https://bitnami.com/stack/gitlab[], and tweak the configuration to match your particular environment.(((bitnami)))
16+
One nice touch Bitnami has included is the login screen (accessed by typing alt-→); it tells you the IP address and default username and password for the installed GitLab.
17+
18+
[[bitnami]]
19+
.The Bitnami GitLab virtual machine login screen.
20+
image::images/bitnami.png[The Bitnami GitLab virtual machine login screen.]
21+
22+
For anything else, follow the guidance in the GitLab Community Edition readme, which can be found at https://gitlab.com/gitlab-org/gitlab-ce/tree/master[].
23+
There you'll find assistance for installing GitLab using Chef recipes, a virtual machine on Digital Ocean, and RPM and DEB packages (which, as of this writing, are in beta).
24+
There's also ``unofficial'' guidance on getting GitLab running with non-standard operating systems and databases, a fully-manual installation script, and many other topics.
25+
26+
==== Administration
27+
28+
GitLab's administration interface is accessed over the web.
29+
Simply point your browser to the hostname or IP address where GitLab is installed, and log in as an admin user.
30+
The default username is `[email protected]`, and the default password is `5iveL!fe` (which you will be prompted to change as soon as you enter it).
31+
Once logged in, click the ``Admin area'' icon in the menu at the top right.
32+
33+
[[gitlab_menu]]
34+
.The ``Admin area'' item in the GitLab menu.
35+
image::images/gitlab-menu.png[The ``Admin area'' item in the GitLab menu.]
36+
37+
===== Users
38+
39+
Users in GitLab are accounts that correspond to people.
40+
User accounts don't have a lot of complexity; mainly it's a collection of personal information attached to login data.
41+
Each user account comes with a *namespace*, which is a logical grouping of projects that belong to that user.
42+
If the user +jane+ had a project named +project+, that project's url would be http://server/jane/project[].
43+
44+
[[gitlab_users]]
45+
.The GitLab user administration screen.
46+
image::images/gitlab-users.png[The GitLab user administration screen.]
47+
48+
Removing a user can be done in two ways.
49+
``Blocking'' a user prevents them from logging into the GitLab instance, but all of the data under that user's namespace will be preserved, and commits signed with that user's email address will still link back to their profile.
50+
51+
``Destroying'' a user, on the other hand, completely removes them from the database and filesystem.
52+
All projects and data in their namespace is removed, and any groups they own will also be removed.
53+
This is obviously a much more permanent and destructive action, and its uses are rare.
54+
55+
[[_gitlab_groups_section]]
56+
===== Groups
57+
58+
A GitLab group is an assemblage of projects, along with data about how users can access those projects.
59+
Each group has a project namespace (the same way that users do), so if the group +training+ has a project +materials+, its url would be http://server/training/materials[].
60+
61+
[[gitlab_groups]]
62+
.The GitLab group administration screen.
63+
image::images/gitlab-groups.png[The GitLab group administration screen.]
64+
65+
Each group is associated with a number of users, each of which has a level of permissions for the group's projects and the group itself.
66+
These range from ``Guest'' (issues and chat only) to ``Owner'' (full control of the group, its members, and its projects).
67+
The types of permissions are too numerous to list here, but GitLab has a helpful link on the administration screen.
68+
69+
===== Projects
70+
71+
A GitLab project roughly corresponds to a single git repository.
72+
Every project belongs to a single namespace, either a user or a group.
73+
If the project belongs to a user, the owner of the project has direct control over who has access to the project; if the project belongs to a group, the group's user-level permissions will also take effect.
74+
75+
Every project also has a visibility level, which controls who has read access to that project's pages and repository.
76+
If a project is _Private_, the project's owner must explicitly grant access to specific users.
77+
An _Internal_ project is visible to any logged-in user, and a _Public_ project is visible to anyone.
78+
Note that this controls both git ``fetch'' access as well as access to the web UI for that project.
79+
80+
===== Hooks
81+
82+
GitLab includes support for hooks, both at a project or system level.
83+
For either of these, the GitLab server will perform an HTTP POST with some descriptive JSON whenever relevant events occur.
84+
This is a great way to connect your git repositories and GitLab instance to the rest of your development automation, such as CI servers, chat rooms, or deployment tools.
85+
86+
==== Basic Usage
87+
88+
The first thing you'll want to do with GitLab is create a new project.
89+
This is accomplished by clicking the ``+'' icon on the toolbar.
90+
You'll be asked for the project's name, which namespace it should belong to, and what its visibility level should be.
91+
Most of what you specify here isn't permanent, and can be re-adjusted later through the settings interface.
92+
Click ``Create Project'', and you're done.
93+
94+
Once the project exists, you'll probably want to connect it with a local Git repository.
95+
Each project is accessible over HTTPS or SSH, either of which can be used to configure a Git remote.
96+
The URLs are visible at the top of the project's home page.
97+
For an existing local repository, this command will create a remote named `gitlab` to the hosted location:
98+
99+
[source,shell]
100+
----
101+
$ git remote add gitlab https://server/namespace/project.git
102+
----
103+
104+
If you don't have a local copy of the repository, you can simply do this:
105+
106+
[source,shell]
107+
----
108+
$ git clone https://server/namespace/project.git
109+
----
110+
111+
The web UI provides access to several useful views of the repository itself.
112+
Each project's home page shows recent activity, and links along the top will lead you to views of the project's files and commit log.
113+
114+
==== Working Together
115+
116+
The simplest way of working together on a GitLab project is by giving another user direct push access to the git repository.
117+
You can add a user to a project by going to the ``Members'' section of that project's settings, and associating the new user with an access level (the different access levels are discussed a bit in <<_gitlab_groups_section>>).
118+
By giving a user an access level of ``Developer'' or above, that user can push commits and branches directly to the repository with impunity.
119+
120+
Another, more decoupled way of collaboration is by using merge requests.
121+
This feature enables any user that can see a project to contribute to it in a controlled way.
122+
Users with direct access can simply create a branch, push commits to it, and open a merge request from their branch back into `master` or any other branch.
123+
Users who don't have push permissions for a repository can ``fork'' it (create their own copy), push commits to _that_ copy, and open a merge request from their fork back to the main project.
124+
This model allows the owner to be in full control of what goes into the repository and when, while allowing contributions from untrusted users.
125+
126+
Merge requests and issues are the main units of long-lived discussion in GitLab.
127+
Each merge request allows a line-by-line discussion of the proposed change (which supports a lightweight kind of code review), as well as a general overall discussion thread.
128+
Both can be assigned to users, or organized into milestones.
129+
130+
This section has focused mainly on the Git-related parts of GitLab, but it's a fairly mature system, and provides many other features that can help your team work together.
131+
These include project wikis, discussion ``walls'', and system maintenance tools.
132+
One benefit to GitLab is that, once the server is set up and running, you'll rarely need to tweak a configuration file or access the server via SSH; most administration and general usage can be accomplished through the in-browser interface.

0 commit comments

Comments
 (0)