Skip to content

Commit c3a3996

Browse files
jyn514XAMPPRocky
andcommitted
Add wiki pages for docs.rs (rust-lang#310)
* docsrs: Add wiki pages * docsrs: Update wiki pages * Add syntax highlighting for example Co-Authored-By: XAMPPRocky <[email protected]> * Improve docs.rs landing page * Link to team instead of website individual members Co-Authored-By: XAMPPRocky <[email protected]> * Link to website instead of GitHub * infra/docs/docs-rs/ -> docs-rs/ * fix table of contents * add back infra page * including summary Co-authored-by: XAMPPRocky <[email protected]>
1 parent a5e1a82 commit c3a3996

File tree

7 files changed

+602
-0
lines changed

7 files changed

+602
-0
lines changed

src/SUMMARY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
- [How to run a design meeting](./compiler/steering-meeting/how-to-run-design.md)
2121
- [crates.io](./crates-io/README.md)
2222
- [Crate removal](./crates-io/crate-removal.md)
23+
- [docs.rs](./docs-rs/README.md)
24+
- [Adding dependencies to the build environment](./docs-rs/add-dependencies.md)
25+
- [Developing without docker-compose](./docs-rs/no-docker-compose.md)
26+
- [Self-hosting a docs.rs instance](./docs-rs/self-hosting.md)
27+
- [Maintenance procedures](./docs-rs/maintenance.md)
28+
- [Migrating from a local database to S3](./docs-rs/migrate-s3.md)
2329
- [GitHub](./github.md)
2430
- [Governance](./governance/README.md)
2531
- [Infrastructure](./infra/README.md)

src/docs-rs/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# docs.rs
2+
3+
[docs.rs](https://docs.rs/) is a website that hosts documentation for crates published to [crates.io](https://crates.io/).
4+
5+
## External Links
6+
7+
* Source code: [rust-lang/docs.rs][repo]
8+
* Hosted on: `docsrs.infra.rust-lang.org` (behind the bastion -- [how to connect][bastion-connect])
9+
* Maintainers: [docs.rs team]
10+
* [Instance metrics][grafana-instance] (only available to infra team members).
11+
* [Application metrics][grafana-app] (only available to infra team members).
12+
13+
[repo]: https://github.com/rust-lang/docs.rs
14+
[grafana-instance]: https://grafana.rust-lang.org/d/rpXrFfKWz/instance-metrics?orgId=1&var-instance=docsrs.infra.rust-lang.org:9100
15+
[grafana-app]: https://grafana.rust-lang.org/d/-wWFg2cZz/docs-rs?orgId=1
16+
[bastion-connect]: https://github.com/rust-lang/infra-team/blob/master/docs/hosts/bastion.md#logging-into-servers-through-the-bastion
17+
[docs.rs team]: https://www.rust-lang.org/governance/teams/dev-tools#docs-rs

src/docs-rs/add-dependencies.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Add a dependency to the build environment
2+
3+
Rustwide internally uses `rustops/crates-build-env` as the build environment for the crate. If you want to add a system package for crates to link to, this is place you're looking for.
4+
5+
## Getting started
6+
7+
First, clone the crates-build-env repo:
8+
9+
```sh
10+
git clone https://github.com/rust-lang/crates-build-env && cd crates-build-env
11+
```
12+
13+
Next, add the package to `packages.txt`. This should be the name of a package in the **Ubuntu 18.04** Repositories. See [the package home page](https://packages.ubuntu.com/) for a full list/search bar, or use `apt search` locally.
14+
15+
## Building the image
16+
17+
Now build the image. This will take a very long time, probably 10-20 minutes.
18+
19+
```sh
20+
docker build --tag build-env .
21+
```
22+
23+
## Testing the image
24+
25+
Use the image to build your crate.
26+
27+
```sh
28+
cd /path/to/docs.rs
29+
docker-compose build
30+
# NOTE: this must be an absolute path, not a relative path
31+
# On platforms with coreutils, you can instead use `$(realpath ../relative/path)`
32+
YOUR_CRATE=/path/to/your/crate
33+
# avoid docker-compose creating the volume if it doesn't exist
34+
if [ -e "$YOUR_CRATE" ]; then
35+
docker-compose run -e DOCS_RS_LOCAL_IMAGE=build-env \
36+
-v "$YOUR_CRATE":/opt/rustwide/workdir \
37+
web build crate --local /opt/rustwide/workdir
38+
else
39+
echo "$YOUR_CRATE does not exist";
40+
fi
41+
```
42+
43+
## Making multiple changes
44+
45+
If your build fails even after your changes, it will be annoying to rebuild the image from scratch just to add a single package. Instead, you can make changes directly to the Dockerfile so that the existing packages are cached. Be sure to move these new packages from the Dockerfile to `packages.txt` once you are sure they work.
46+
47+
On line 7 of the Dockerfile, add this line: `RUN apt-get install -y your_second_package`.
48+
Rerun the build and start the container; it should take much less time now:
49+
50+
```sh
51+
cd /path/to/crates-build-env
52+
docker build --tag build-env .
53+
cd /path/to/docs.rs
54+
docker-compose run -e DOCS_RS_LOCAL_IMAGE=build-env \
55+
-v "$YOUR_CRATE":/opt/rustwide/workdir \
56+
web build crate --local /opt/rustwide/workdir
57+
```
58+
59+
## Run the lint script
60+
61+
Before you make a PR, run the shell script `ci/lint.sh` and make sure it passes. It ensures `packages.txt` is in order and will tell you exactly what changes you need to make if not.
62+
63+
## Make a pull request
64+
65+
Once you are sure your package builds, you can make a pull request to get it adopted upstream for docs.rs and crater. Go to https://github.com/rust-lang/crates-build-env and click 'Fork' in the top right. Locally, add your fork as a remote in git and push your changes:
66+
67+
```sh
68+
git remote add personal https://github.com/<your_username_here>/crates-build-env
69+
git add -u
70+
git commit -m 'add packages necessary for <your_package_here> to compile'
71+
git push personal
72+
```
73+
74+
Back on github, make a pull request:
75+
76+
1. Go to https://github.com/rust-lang/crates-build-env/compare
77+
2. Click 'compare across forks'
78+
3. Click 'head repository' -> <your_username>/crates-build-env
79+
4. Click 'Create pull request'
80+
5. Add a description of what packages you added and what crate they fixed
81+
6. Click 'Create pull request' again in the bottom right.
82+
83+
Hopefully your changes will be merged quickly! After that you can either publish a point release (rebuilds your docs immediately) or request for a member of the docs.rs team to schedule a new build (may take a while depending on their schedules).

src/docs-rs/maintenance.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Common maintenance procedures
2+
3+
## Temporarily remove a crate from the queue
4+
5+
It might happen that a crate fails to build repeatedly due to a docs.rs bug,
6+
clogging up the queue and preventing other crates to build. In this case it's
7+
possible to temporarily remove the crate from the queue until the docs.rs's bug
8+
is fixed. To do that, log into the machine and open a PostgreSQL shell with:
9+
10+
```
11+
$ psql
12+
```
13+
14+
Then you can run this SQL query to remove the crate:
15+
16+
```
17+
UPDATE queue SET attempt = 100 WHERE name = '<CRATE_NAME>';
18+
```
19+
20+
To add the crate back in the queue you can run in the PostgreSQL shell this
21+
query:
22+
23+
```
24+
UPDATE queue SET attempt = 0 WHERE name = '<CRATE_NAME>';
25+
```
26+
27+
## Pinning a version of nightly
28+
29+
Sometimes the latest nightly might be broken, causing doc builds to fail. In
30+
those cases it's possible to tell docs.rs to stop updating to the latest
31+
nightly and instead pin a specific release. To do that you need to edit the
32+
`/home/cratesfyi/.docs-rs-env` file, adding or changing this environment
33+
variable:
34+
35+
```
36+
CRATESFYI_TOOLCHAIN=nightly-YYYY-MM-DD
37+
```
38+
39+
Once the file changed docs.rs needs to be restarted:
40+
41+
```
42+
systemctl restart docs.rs
43+
```
44+
45+
To return to the latest nightly simply remove the environment variable and
46+
restart docs.rs again.
47+
48+
## Rebuild a specific crate
49+
50+
If a bug was recently fixed, you may want to rebuild a crate so that it builds with the latest version.
51+
From the docs.rs machine:
52+
53+
```
54+
cratesfyi queue add <crate> <version>
55+
```
56+
57+
This will add the crate with a lower priority than new crates by default, you can change the priority with the `-p` option.
58+
59+
## Adding all the crates failed after a date back in the queue
60+
61+
After an outage you might want to add all the failed builds back to the queue.
62+
To do that, log into the machine and open a PostgreSQL shell with:
63+
64+
```
65+
psql
66+
```
67+
68+
Then you can run this SQL query to add all the crates failed after `YYYY-MM-DD
69+
HH:MM:SS` back in the queue:
70+
71+
```
72+
UPDATE queue SET attempt = 0 WHERE attempt >= 5 AND build_time > 'YYYY-MM-DD HH:MM:SS';
73+
```
74+
75+
## Removing a crate from the website
76+
77+
Sometimes it might be needed to remove all the content related to a crate from
78+
docs.rs (for example after receiving a DMCA). To do that, log into the server
79+
and run:
80+
81+
```
82+
cratesfyi database delete-crate CRATE_NAME
83+
```
84+
85+
The command will remove all the data from the database, and then remove the
86+
files from S3.
87+
88+
## Blacklisting crates
89+
90+
Occasionally it might be needed to prevent a crate from being built on docs.rs,
91+
for example if we can't legally host the content of those crates. To add a
92+
crate to the blacklist, preventing new builds for it, you can run:
93+
94+
```
95+
cratesfyi database blacklist add <CRATE_NAME>
96+
```
97+
98+
Other operations (such as `list` and `remove`) are also supported.
99+
100+
> **Warning:** blacklisting a crate doesn't remove existing content from the
101+
> website, it just prevents new versions from being built!

src/docs-rs/migrate-s3.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Migrating from a local database to S3
2+
3+
If you're running your own instance of docs.rs for personal development, and you'd like to test out the S3 integration, there are a couple steps involved. It's mostly straightforward, but there's at least one major caveat that should be taken into account.
4+
5+
## Requirements
6+
7+
Since docs.rs uses a fixed bucket name to upload files, you'll need to set up an independent server that implements the Amazon S3 API. [Minio](https://min.io/) is an example of a server you can set up. Instructions for installing and configuring Minio (or any S3-compliant provider) are beyond the scope of this article.
8+
9+
## Configuring your docs.rs instance
10+
11+
Once you have your server and credentials set up, you need to tell the docs.rs server to use it. You'll need to add some extra environment variables to the environment file you use to configure docs.rs. It uses the `S3_ENDPOINT` variable to determine where to call, and the other variables to configure its access privileges. The available environment variables are documented in `rusoto`'s [`EnvironmentProvider`](https://docs.rs/rusoto_credential/0.40.0/rusoto_credential/struct.EnvironmentProvider.html).
12+
13+
```text
14+
AWS_ACCESS_KEY_ID=<access key>
15+
AWS_SECRET_ACCESS_KEY=<access secret>
16+
S3_ENDPOINT=<endpoint url>
17+
```
18+
19+
## Migrating files out of the database
20+
21+
Before you restart the process to load the new credentials, the files that are currently in the local database need to be migrated out to S3. Once docs.rs sees that it has AWS credentials in its environment, it will stop reading from the `files` table entirely. Therefore, to properly transition to using the new file storage, you need to run an extra command to upload them. It's helpful to lock the build queue while this is happening, so that no new files will sneak in while the migration happens.
22+
23+
```sh
24+
cratesfyi build lock
25+
# edit the environment file with the above variables if you haven't already
26+
cratesfyi database move-to-s3 # this will take a while, depending on the size of your database
27+
sudo systemctl restart cratesfyi # or however you manage your docs.rs service
28+
# verify that files are loading from S3/Minio/etc
29+
cratesfyi build unlock
30+
```
31+
32+
Once this is done, new crate builds will upload files directly to your file storage service rather than into the database. The `move-to-s3` command will remove rows from the `files` table in the database as it uploads them, so once you're done, you can compact the database to shrink its on-disk size.

src/docs-rs/no-docker-compose.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Developing locally without docker-compose
2+
3+
These are instructions for developing docs.rs locally. For deploying in a production environment, see [Self-hosting a docs.rs instance](self-hosting.html).
4+
5+
While the docker-compose allows for easier setup of the required dependencies and environment, here is a breakdown of how to use the service without an outer docker container. This is useful e.g. for quickly iterating during development.
6+
7+
Note that this does not remove the docker dependency altogether, since docs.rs uses docker at runtime for sandboxing. This just allows you to run commands more quickly since you are building in debug mode instead of release and are also caching more of the build.
8+
9+
## Requirements
10+
11+
The commands and package names on this page will assume a Debian-like machine with `apt` installed, but most dependencies should be relatively easy to find on Linux. Do note however that this requires the host to be `x86_64-unknown-linux-gnu`.
12+
13+
Docs.rs has a few basic requirements:
14+
15+
* Rust
16+
* Git
17+
* CMake, GCC, G++, and `pkg-config` (to build dependencies for crates and docs.rs itself)
18+
* OpenSSL, zlib, curl, and `libmagic` (to link against)
19+
* PostgreSQL
20+
21+
```console
22+
# apt install build-essential git curl cmake gcc g++ pkg-config libmagic-dev libssl-dev zlib1g-dev postgresql
23+
$ sudo -u postgres psql -c "CREATE USER cratesfyi WITH PASSWORD 'password';"
24+
$ sudo -u postgres psql -c "CREATE DATABASE cratesfyi OWNER cratesfyi;"
25+
```
26+
27+
## Building the site
28+
29+
Be warned - this builds over 350 crates! Depending on your computer, this may
30+
take upwards of 10 minutes.
31+
32+
```console
33+
$ git clone https://github.com/rust-lang/docs.rs && cd docs.rs
34+
$ cargo build
35+
```
36+
37+
## The "prefix" directory
38+
39+
docs.rs stores several files in a "prefix" directory. This can be anywhere, but if you put it in the doc.rs repo, it should go under the ./ignored/ directory so that it is not seen by git or the docker daemon.
40+
41+
```console
42+
$ mkdir -p ignored/cratesfyi-prefix
43+
$ cd ignored/cratesfyi-prefix
44+
$ mkdir -vp documentations public_html sources
45+
$ git clone https://github.com/rust-lang/crates.io-index && cd crates.io-index
46+
$ git branch crates-index-diff_last-seen
47+
```
48+
49+
(That last command is used to set up the `crates-index-diff` crate, so we can start monitoring new crate releases.)
50+
51+
## Docker group
52+
53+
docs.rs needs to run docker containers for sandboxing. Therefore, you need to be in the 'docker' group to build crates. If you are already in the docker group, you can skip this step (you can check with `groups`).
54+
55+
```console
56+
# usermod -a -G docker "$USER"
57+
$ # now logout and back in to your shell
58+
$ cd /path/to/docs.rs/ignored/cratesfyi-prefix
59+
```
60+
61+
## Environment for the server
62+
63+
To ensure that the docs.rs server is configured properly, we need to set a few environment variables. This is most easily done by making a shell script.
64+
65+
```sh
66+
$ cd ..
67+
$ echo '
68+
export CRATESFYI_PREFIX=.
69+
# or add an appropriate username/password as necessary
70+
export CRATESFYI_DATABASE_URL=postgresql://cratesfyi:password@localhost
71+
export CRATESFYI_GITHUB_USERNAME=
72+
export CRATESFYI_GITHUB_ACCESSTOKEN=
73+
export RUST_LOG=cratesfyi,rustwide=info
74+
' > env.sh
75+
```
76+
77+
This last command assumes you put the shell script in `./env.sh`,
78+
but you can name it anything as long as it is in the current directory.
79+
80+
```console
81+
$ . ./env.sh
82+
$ cargo run database migrate
83+
$ cargo run database update-search-index
84+
$ cargo run database update-release-activity
85+
# This will take between 5 and 30 minutes on the first run, depending on your internet speed.
86+
# It downloads the rustops/crates-build-env crates which is almost 1 GB.
87+
# It does not currently display a progress bar, this is https://github.com/rust-lang/rustwide/issues/9
88+
# As a workaround, you can run `docker pull rustops/crates-build-env` in a separate terminal.
89+
$ cargo run build crate rand 0.5.5
90+
```
91+
92+
## Building on platforms other than Linux
93+
94+
This is not currently possible. We assume in several places that the rustup toolchain is x86_64-unknown-linux-gnu. As a result, the only way to build on Mac or Alpine is to use the docker-compose file.
95+
96+
## Resetting the database
97+
98+
Occasionally, if you make changes to the migrations in a PR, those migrations will be saved in the database but will not be in the code when you switch back to the master branch. In this case, there is no way to undo the migration without knowing exactly which version of the code made the change (`cargo migrate` will have no effect). Here is a convenient shell script to reset the database so you don't have to remember how to undo your changes.
99+
100+
NOTE: DO NOT RUN THIS IN PRODUCTION.
101+
102+
```sh
103+
#!/bin/sh
104+
set -euv
105+
106+
. ./env.sh
107+
108+
sudo -u postgres dropdb cratesfyi --if-exists
109+
sudo -u postgres psql -c 'CREATE DATABASE cratesfyi OWNER cratesfyi;'
110+
cargo run database migrate
111+
```

0 commit comments

Comments
 (0)