| 1 |
==================== |
| 2 |
Mercurial Automation |
| 3 |
==================== |
| 4 |
|
| 5 |
This directory contains code and utilities for building and testing Mercurial |
| 6 |
on remote machines. |
| 7 |
|
| 8 |
The ``automation.py`` Script |
| 9 |
============================ |
| 10 |
|
| 11 |
``automation.py`` is an executable Python script (requires Python 3.5+) |
| 12 |
that serves as a driver to common automation tasks. |
| 13 |
|
| 14 |
When executed, the script will *bootstrap* a virtualenv in |
| 15 |
``<source-root>/build/venv-automation`` then re-execute itself using |
| 16 |
that virtualenv. So there is no need for the caller to have a virtualenv |
| 17 |
explicitly activated. This virtualenv will be populated with various |
| 18 |
dependencies (as defined by the ``requirements.txt`` file). |
| 19 |
|
| 20 |
To see what you can do with this script, simply run it:: |
| 21 |
|
| 22 |
$ ./automation.py |
| 23 |
|
| 24 |
Local State |
| 25 |
=========== |
| 26 |
|
| 27 |
By default, local state required to interact with remote servers is stored |
| 28 |
in the ``~/.hgautomation`` directory. |
| 29 |
|
| 30 |
We attempt to limit persistent state to this directory. Even when |
| 31 |
performing tasks that may have side-effects, we try to limit those |
| 32 |
side-effects so they don't impact the local system. e.g. when we SSH |
| 33 |
into a remote machine, we create a temporary directory for the SSH |
| 34 |
config so the user's known hosts file isn't updated. |
| 35 |
|
| 36 |
Try Server |
| 37 |
========== |
| 38 |
|
| 39 |
There exists a *Try Server* which allows automation to run against |
| 40 |
an arbitrary Mercurial changeset and displays results via the web. |
| 41 |
|
| 42 |
.. note:: |
| 43 |
|
| 44 |
The *Try Server* is still experimental infrastructure. |
| 45 |
|
| 46 |
To use the *Try Server*:: |
| 47 |
|
| 48 |
$ ./automation.py try |
| 49 |
|
| 50 |
With a custom AWS profile:: |
| 51 |
|
| 52 |
$ AWS_PROFILE=hg contrib/automation/automation.py try |
| 53 |
|
| 54 |
By default, the ``.`` revision is submitted. **Any uncommitted changes |
| 55 |
are not submitted.** |
| 56 |
|
| 57 |
To switch which revision is used:: |
| 58 |
|
| 59 |
$ ./automation.py try -r abcdef |
| 60 |
|
| 61 |
Access to the *Try Server* requires access to a special AWS account. |
| 62 |
This account is currently run by Gregory Szorc. Here is the procedure |
| 63 |
for accessing the *Try Server*: |
| 64 |
|
| 65 |
1. Email Gregory Szorc at [email protected] and request a |
| 66 |
username. This username will be stored in the public domain. |
| 67 |
2. Wait for an email reply containing your temporary AWS credentials. |
| 68 |
3. Log in at https://gregoryszorc-hg.signin.aws.amazon.com/console |
| 69 |
and set a new, secure password. |
| 70 |
4. Go to https://console.aws.amazon.com/iam/home?region=us-west-2#/security_credentials |
| 71 |
5. Under ``Access keys for CLI, SDK, & API access``, click the |
| 72 |
``Create access key`` button. |
| 73 |
6. See the ``AWS Integration`` section for instructions on |
| 74 |
configuring your local client to use the generated credentials. |
| 75 |
|
| 76 |
AWS Integration |
| 77 |
=============== |
| 78 |
|
| 79 |
Various automation tasks integrate with AWS to provide access to |
| 80 |
resources such as EC2 instances for generic compute. |
| 81 |
|
| 82 |
This obviously requires an AWS account and credentials to work. |
| 83 |
|
| 84 |
We use the ``boto3`` library for interacting with AWS APIs. We do not employ |
| 85 |
any special functionality for telling ``boto3`` where to find AWS credentials. See |
| 86 |
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html |
| 87 |
for how ``boto3`` works. Once you have configured your environment such |
| 88 |
that ``boto3`` can find credentials, interaction with AWS should *just work*. |
| 89 |
|
| 90 |
To configure ``boto3``, you can use the ``aws configure`` command to |
| 91 |
write out configuration files. (The ``aws`` command is typically provided |
| 92 |
by an ``awscli`` package available in your package manager, including |
| 93 |
``pip``.) Alternatively, you can write out files in ``~/.aws/`` directly. |
| 94 |
e.g.:: |
| 95 |
|
| 96 |
# ~/.aws/config |
| 97 |
[default] |
| 98 |
region = us-west-2 |
| 99 |
|
| 100 |
# ~/.aws/credentials |
| 101 |
[default] |
| 102 |
aws_access_key_id = XXXX |
| 103 |
aws_secret_access_key = YYYY |
| 104 |
|
| 105 |
If you have multiple AWS accounts, you can name the profile something |
| 106 |
different from ``default``. e.g. ``hg``. You can influence which profile |
| 107 |
is used by ``boto3`` by setting the ``AWS_PROFILE`` environment variable. |
| 108 |
e.g. ``AWS_PROFILE=hg``. |
| 109 |
|
| 110 |
Resource Management |
| 111 |
------------------- |
| 112 |
|
| 113 |
Depending on the task being performed, various AWS services will be accessed. |
| 114 |
This of course requires AWS credentials with permissions to access these |
| 115 |
services. |
| 116 |
|
| 117 |
The following AWS services can be accessed by automation tasks: |
| 118 |
|
| 119 |
* EC2 |
| 120 |
* IAM |
| 121 |
* Simple Systems Manager (SSM) |
| 122 |
|
| 123 |
Various resources will also be created as part of performing various tasks. |
| 124 |
This also requires various permissions. |
| 125 |
|
| 126 |
The following AWS resources can be created by automation tasks: |
| 127 |
|
| 128 |
* EC2 key pairs |
| 129 |
* EC2 security groups |
| 130 |
* EC2 instances |
| 131 |
* IAM roles and instance profiles |
| 132 |
* SSM command invocations |
| 133 |
|
| 134 |
When possible, we prefix resource names with ``hg-`` so they can easily |
| 135 |
be identified as belonging to Mercurial. |
| 136 |
|
| 137 |
.. important:: |
| 138 |
|
| 139 |
We currently assume that AWS accounts utilized by *us* are single |
| 140 |
tenancy. Attempts to have discrete users of ``automation.py`` (including |
| 141 |
sharing credentials across machines) using the same AWS account can result |
| 142 |
in them interfering with each other and things breaking. |
| 143 |
|
| 144 |
Cost of Operation |
| 145 |
----------------- |
| 146 |
|
| 147 |
``automation.py`` tries to be frugal with regards to utilization of remote |
| 148 |
resources. Persistent remote resources are minimized in order to keep costs |
| 149 |
in check. For example, EC2 instances are often ephemeral and only live as long |
| 150 |
as the operation being performed. |
| 151 |
|
| 152 |
Under normal operation, recurring costs are limited to: |
| 153 |
|
| 154 |
* Storage costs for AMI / EBS snapshots. This should be just a few pennies |
| 155 |
per month. |
| 156 |
|
| 157 |
When running EC2 instances, you'll be billed accordingly. Default instance |
| 158 |
types vary by operation. We try to be respectful of your money when choosing |
| 159 |
defaults. e.g. for Windows instances which are billed per hour, we use e.g. |
| 160 |
``t3.medium`` instances, which cost ~$0.07 per hour. For operations that |
| 161 |
scale well to many CPUs like running Linux tests, we may use a more powerful |
| 162 |
instance like ``c5.9xlarge``. However, since Linux instances are billed |
| 163 |
per second and the cost of running an e.g. ``c5.9xlarge`` for half the time |
| 164 |
of a ``c5.4xlarge`` is roughly the same, the choice is justified. |
| 165 |
|
| 166 |
.. note:: |
| 167 |
|
| 168 |
When running Windows EC2 instances, AWS bills at the full hourly cost, even |
| 169 |
if the instance doesn't run for a full hour (per-second billing doesn't |
| 170 |
apply to Windows AMIs). |
| 171 |
|
| 172 |
Managing Remote Resources |
| 173 |
------------------------- |
| 174 |
|
| 175 |
Occassionally, there may be an error purging a temporary resource. Or you |
| 176 |
may wish to forcefully purge remote state. Commands can be invoked to manually |
| 177 |
purge remote resources. |
| 178 |
|
| 179 |
To terminate all EC2 instances that we manage:: |
| 180 |
|
| 181 |
$ automation.py terminate-ec2-instances |
| 182 |
|
| 183 |
To purge all EC2 resources that we manage:: |
| 184 |
|
| 185 |
$ automation.py purge-ec2-resources |
| 186 |
|
| 187 |
Remote Machine Interfaces |
| 188 |
========================= |
| 189 |
|
| 190 |
The code that connects to a remote machine and executes things is |
| 191 |
theoretically machine agnostic as long as the remote machine conforms to |
| 192 |
an *interface*. In other words, to perform actions like running tests |
| 193 |
remotely or triggering packaging, it shouldn't matter if the remote machine |
| 194 |
is an EC2 instance, a virtual machine, etc. This section attempts to document |
| 195 |
the interface that remote machines need to provide in order to be valid |
| 196 |
*targets* for remote execution. These interfaces are often not ideal nor |
| 197 |
the most flexible. Instead, they have often evolved as the requirements of |
| 198 |
our automation code have evolved. |
| 199 |
|
| 200 |
Linux |
| 201 |
----- |
| 202 |
|
| 203 |
Remote Linux machines expose an SSH server on port 22. The SSH server |
| 204 |
must allow the ``hg`` user to authenticate using the SSH key generated by |
| 205 |
the automation code. The ``hg`` user should be part of the ``hg`` group |
| 206 |
and it should have ``sudo`` access without password prompting. |
| 207 |
|
| 208 |
The SSH channel must support SFTP to facilitate transferring files from |
| 209 |
client to server. |
| 210 |
|
| 211 |
``/bin/bash`` must be executable and point to a bash shell executable. |
| 212 |
|
| 213 |
The ``/hgdev`` directory must exist and all its content owned by ``hg::hg``. |
| 214 |
|
| 215 |
The ``/hgdev/pyenv`` directory should contain an installation of |
| 216 |
``pyenv``. Various Python distributions should be installed. The exact |
| 217 |
versions shouldn't matter. ``pyenv global`` should have been run so |
| 218 |
``/hgdev/pyenv/shims/`` is populated with redirector scripts that point |
| 219 |
to the appropriate Python executable. |
| 220 |
|
| 221 |
The ``/hgdev/venv-bootstrap`` directory must contain a virtualenv |
| 222 |
with Mercurial installed. The ``/hgdev/venv-bootstrap/bin/hg`` executable |
| 223 |
is referenced by various scripts and the client. |
| 224 |
|
| 225 |
The ``/hgdev/src`` directory MUST contain a clone of the Mercurial |
| 226 |
source code. The state of the working directory is not important. |
| 227 |
|
| 228 |
In order to run tests, the ``/hgwork`` directory will be created. |
| 229 |
This may require running various ``mkfs.*`` executables and ``mount`` |
| 230 |
to provision a new filesystem. This will require elevated privileges |
| 231 |
via ``sudo``. |
| 232 |
|
| 233 |
Various dependencies to run the Mercurial test harness are also required. |
| 234 |
Documenting them is beyond the scope of this document. Various tests |
| 235 |
also require other optional dependencies and missing dependencies will |
| 236 |
be printed by the test runner when a test is skipped. |
| 237 |
|
| 238 |
Releasing Windows Artifacts |
| 239 |
=========================== |
| 240 |
|
| 241 |
The `automation.py` script can be used to automate the release of Windows |
| 242 |
artifacts:: |
| 243 |
|
| 244 |
$ ./automation.py build-all-windows-packages --revision 5.1.1 |
| 245 |
$ ./automation.py publish-windows-artifacts 5.1.1 |
| 246 |
|
| 247 |
The first command will launch an EC2 instance to build all Windows packages |
| 248 |
and copy them into the `dist` directory relative to the repository root. The |
| 249 |
second command will then attempt to upload these files to PyPI (via `twine`) |
| 250 |
and to `mercurial-scm.org` (via SSH). |
| 251 |
|
| 252 |
Uploading to PyPI requires a PyPI account with write access to the `Mercurial` |
| 253 |
package. You can skip PyPI uploading by passing `--no-pypi`. |
| 254 |
|
| 255 |
Uploading to `mercurial-scm.org` requires an SSH account on that server |
| 256 |
with `windows` group membership and for the SSH key for that account to be the |
| 257 |
default SSH key (e.g. `~/.ssh/id_rsa`) or in a running SSH agent. You can |
| 258 |
skip `mercurial-scm.org` uploading by passing `--no-mercurial-scm-org`. |