github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/docs/contributing/hacking.rst (about)

     1  :description: How to hack on Deis including setup instructions
     2  
     3  .. _hacking:
     4  
     5  Development Environment
     6  =======================
     7  
     8  DigitalOcean_ is the recommended development environment for Deis project
     9  contributors. :ref:`Provision a new DO cluster <deis_on_digitalocean>` and then
    10  continue to follow the instructions below to get started hacking.
    11  
    12  .. _digitalocean_credit:
    13  
    14  .. important::
    15  
    16      Are you a new contributor to Deis? Your first `Pull Request`_ could earn you
    17      credit at DigitalOcean_! Submit your changes and then email
    18      deis@engineyard.com. When your PR is merged, the maintainer team will
    19      send you a DigitalOcean credit based on the value of your contribution.
    20  
    21  This document is for developers who are interested in working directly on the
    22  Deis codebase. In this guide, we walk you through the process of setting up
    23  a local development environment. While there are many ways to set up your
    24  Deis environment, this document covers a specific setup:
    25  
    26  - Developing on **Mac OSX** or **Linux**
    27  - Managing virtualization with **Vagrant/Virtualbox**
    28  - Hosting a docker registry with **docker-machine** (Mac)
    29  
    30  We try to make it simple to hack on Deis. However, there are necessarily several moving
    31  pieces and some setup required. We welcome any suggestions for automating or simplifying
    32  this process.
    33  
    34  If you're just getting into the Deis codebase, look for GitHub issues with the label
    35  `easy-fix`_. These are more straightforward or low-risk issues and are a great way to
    36  become more familiar with Deis.
    37  
    38  Prerequisites
    39  -------------
    40  
    41  You can develop on any supported platform including your laptop, cloud providers or
    42  on bare metal.  We strongly recommend a minimum 3-node cluster. We strongly
    43  suggest using Vagrant and VirtualBox for your virtualization layer during
    44  development.
    45  
    46  At a glance, you will need:
    47  
    48  - Python 2.7 or later (with ``pip``)
    49  - virtualenv (``sudo pip install virtualenv``)
    50  - Go 1.5 or later, with support for compiling to ``linux/amd64``
    51  - Godep (https://github.com/tools/godep)
    52  - VirtualBox latest
    53  - Vagrant 1.5 or later
    54  - On Mac, you will also want
    55    - Docker Machine (http://docs.docker.com/machine/install-machine/)
    56  
    57  In most cases, you should simply install according to the instructions. There
    58  are a few special cases, though. We cover these below.
    59  
    60  Configuring Go
    61  ``````````````
    62  
    63  If your local workstation does not support the linux/amd64 target environment, you will
    64  have to install Go from source with cross-compile support for that environment. This is
    65  because some of the components are built on your local machine and then injected into a
    66  docker container.
    67  
    68  Homebrew users can just install with cross compiling support:
    69  
    70  .. code-block:: console
    71  
    72      $ brew install go --with-cc-common
    73  
    74  It is also straightforward to build Go from source:
    75  
    76  .. code-block:: console
    77  
    78      $ sudo su
    79      $ curl -sSL https://golang.org/dl/go1.5.src.tar.gz | tar -v -C /usr/local -xz
    80      $ cd /usr/local/go/src
    81      $ # compile Go for our default platform first, then add cross-compile support
    82      $ ./make.bash --no-clean
    83      $ GOOS=linux GOARCH=amd64 ./make.bash --no-clean
    84  
    85  Once you can compile to ``linux/amd64``, you should be able to compile Deis'
    86  components as normal.
    87  
    88  Configuring Docker Machine (Mac)
    89  ````````````````````````````````
    90  
    91  Deis needs a Docker registry running independently of the Deis cluster. On
    92  OS X, you will need Docker Machine (http://docs.docker.com/machine/install-machine/)
    93  to run the registry inside of a VirtualBox image.
    94  
    95  .. note::
    96  
    97      Previously, Deis used boot2docker to run the registry. However, Docker has
    98      deprecated boot2docker in favor of Docker Machine.
    99  
   100  Install Docker Machine according to the normal installation instructions. Then
   101  create a new image for hosting your Deis Docker registry:
   102  
   103  .. code-block:: console
   104  
   105      $ docker-machine create --driver virtualbox --virtualbox-disk-size=100000 \
   106      --engine-insecure-registry=192.168.0.0/16 deis-registry
   107  
   108  This will create a new virtual machine named `deis-registry` that will take
   109  up as much as 100,000 MB of disk space. Registries tend to be large, so
   110  allocating a big disk is a good idea.
   111  
   112  .. note::
   113  
   114      Because the registry that we create will not have a valid SSL certificate,
   115      we run the local registry as an insecure (HTTP, not HTTPS) registry. Each
   116      time Docker Machine reboots, the registry will get a new IP address
   117      somewhere in the 192.168.0.0/16 range. We must declare that explicitly when
   118      configuring Docker Machine.
   119  
   120  At this point, our `deis-registry` VM can now serve as a registry for Deis'
   121  Docker images. Later we will return to this.
   122  
   123  Fork the Deis Repository
   124  ------------------------
   125  Once the prerequisites have been met, we can begin to work with Deis.
   126  
   127  To get Deis running for development, first `fork the Deis repository`_,
   128  then clone your fork of the repository. Since Deis is predominantly written
   129  in Go, the best place to put it is in ``$GOPATH/src/github.com/deis/``
   130  
   131  .. code-block:: console
   132  
   133      $ mkdir -p  $GOPATH/src/github.com/deis
   134      $ cd $GOPATH/src/github.com/deis
   135      $ git clone git@github.com:<username>/deis.git
   136      $ cd deis
   137  
   138  .. note::
   139  
   140      By checking out the forked copy into the namespace ``github.com/deis/deis``,
   141      we are tricking the Go toolchain into seeing our fork as the "official"
   142      Deis tree.
   143  
   144  If you are going to be issuing pull requests and working with official Deis
   145  repository, we suggest configuring Git accordingly. There are various strategies
   146  for doing this, but the `most common`_ is to add an ``upstream`` remote:
   147  
   148  .. code-block:: console
   149  
   150      $ git remote add upstream https://github.com/deis/deis.git
   151  
   152  For the sake of simplicity, you may want to point an environment variable to
   153  your Deis code:
   154  
   155  .. code-block:: console
   156  
   157      export DEIS=$GOPATH/src/github.com/deis/deis
   158  
   159  Throughout the rest of this document, ``$DEIS`` refers to that location.
   160  
   161  Alternative: Forking with a Pushurl
   162  ```````````````````````````````````
   163  A number of Deis developers prefer to pull directly from ``deis/deis``, but
   164  push to ``<username>/deis``. If that workflow suits you better, you can set it
   165  up this way:
   166  
   167  .. code-block:: console
   168  
   169      $ git clone git@github.com:deis/deis.git
   170      $ cd deis
   171      $ git config remote.origin.pushurl git@github.com:<username>/deis.git
   172  
   173  In this setup, fetching and pulling code will work directly with the upstream
   174  repository, while pushing code will send changes to your fork. This makes it
   175  easy to stay up to date, but also make changes and then issue pull requests.
   176  
   177  Build deisctl
   178  -------------
   179  
   180  ``deisctl`` is used for interacting with the Deis cluster. While you can use an
   181  existing ``deisctl`` binary, we recommend that developers build it from source.
   182  
   183  .. code-block:: console
   184  
   185    $ cd $DEIS/deisctl
   186    $ make build
   187    $ make install  # optionally
   188  
   189  This will build just the ``deisctl`` portion of Deis. Running ``make install`` will
   190  install the ``deisctl`` command in ``$GOPATH/bin/deisctl``.
   191  
   192  You can verify that ``deisctl`` is correctly built and installed by running
   193  ``deisctl -h``. That should print the help text and exit.
   194  
   195  Configure SSH Tunneling for Deisctl
   196  -----------------------------------
   197  
   198  To connect to the cluster using ``deisctl``, you must add the private key to ``ssh-agent``.
   199  For example, when using Vagrant:
   200  
   201  .. code-block:: console
   202  
   203      $ ssh-add ~/.vagrant.d/insecure_private_key
   204  
   205  Set ``DEISCTL_TUNNEL`` so the ``deisctl`` client on your workstation can connect to
   206  one of the hosts in your cluster:
   207  
   208  .. code-block:: console
   209  
   210      $ export DEISCTL_TUNNEL=172.17.8.100
   211  
   212  .. note::
   213  
   214    A number of times during this setup, tools will suggest that you export various
   215    environment variables. You may find it convenient to store these in your shell's
   216    RC file (`~/.bashrc` or `~/.zshrc`).
   217  
   218  Install the Deis Client
   219  -----------------------
   220  
   221  The ``deis`` client is also written in Go. Your Deis client should match your server's
   222  version. Like ``deisctl``, we recommend that developers build ``deis`` from source:
   223  
   224  .. code-block:: console
   225  
   226      $ cd $DEIS/client
   227      $ make build
   228      $ make install  # optionally
   229      $ ./deis
   230      Usage: deis <command> [<args>...]
   231  
   232  
   233  Start Up a Development Cluster
   234  ------------------------------
   235  
   236  Our host system is now configured for controlling a Deis cluster. The next
   237  thing to do is begin standing up a development cluster.
   238  
   239  When developing locally, we want deisctl to check our local unit files so that
   240  any changes are reflected in our Deis cluster. The easiest way to do this is
   241  to set an environment variable telling deisctl where to look. Assuming
   242  the variable ``$DEIS`` points to the location if the deis source code, we want
   243  something like this:
   244  
   245  .. code-block:: console
   246  
   247      export DEISCTL_UNITS=$DEIS/deisctl/units
   248  
   249  To start up and configure a local vagrant cluster for development, you can use
   250  the ``dev-cluster`` target.
   251  
   252  .. code-block:: console
   253  
   254      $ make dev-cluster
   255  
   256  This may take a while to run the first time. At the end of the process, you
   257  will be prompted to run ``deis start platform``. Hold off on that task for now.
   258  We will come back to it later.
   259  
   260  To verify that the cluster is running, you should be able to connect
   261  to the nodes on your Deis cluster:
   262  
   263  .. code-block:: console
   264  
   265      $ vagrant status
   266      Current machine states:
   267  
   268      deis-01               running (virtualbox)
   269      deis-02               running (virtualbox)
   270      deis-03               running (virtualbox)
   271  
   272      $ vagrant ssh deis-01
   273      Last login: Tue Jun  2 18:26:30 2015 from 10.0.2.2
   274       * *    *   *****    ddddd   eeeeeee iiiiiii   ssss
   275      *   *  * *  *   *     d   d   e    e    i     s    s
   276       * *  ***** *****     d    d  e         i    s
   277      *****  * *    *       d     d e         i     s
   278      *   * *   *  * *      d     d eee       i      sss
   279      *****  * *  *****     d     d e         i         s
   280        *   *****  * *      d    d  e         i          s
   281       * *  *   * *   *     d   d   e    e    i    s    s
   282      ***** *****  * *     ddddd   eeeeeee iiiiiii  ssss
   283  
   284      Welcome to Deis			Powered by CoreOS
   285  
   286  With a dev cluster now running, we are ready to set up a local Docker registry.
   287  
   288  Configure a Docker Registry
   289  ---------------------------
   290  
   291  The development workflow requires Docker Registry set at the ``DEV_REGISTRY``
   292  environment variable.  If you're developing locally you can use the ``dev-registry``
   293  target to spin up a quick, disposable registry inside a Docker container.
   294  The target ``dev-registry`` prints the registry's address and port when using ``docker-machine``;
   295  otherwise, use your host's IP address as returned by ``ifconfig`` with port 5000 for ``DEV_REGISTRY``.
   296  
   297  .. code-block:: console
   298  
   299      $ make dev-registry
   300  
   301      To configure the registry for local Deis development:
   302          export DEV_REGISTRY=192.168.59.103:5000
   303  
   304  It is important that you export the ``DEV_REGISTRY`` variable as instructed.
   305  
   306  If you are developing elsewhere, you must set up a registry yourself.
   307  Make sure it meets the following requirements:
   308  
   309   #. You can push Docker images from your workstation
   310   #. Hosts in the cluster can pull images with the same URL
   311  
   312  .. note::
   313  
   314      If the development registry is insecure and has an IP address in a range other than ``10.0.0.0/8``,
   315      ``172.16.0.0/12``, or ``192.168.0.0/16``, you'll have to modify ``contrib/coreos/user-data.example``
   316      and whitelist your development registry so the daemons can pull your custom components.
   317  
   318  Initial Platform Build
   319  ----------------------
   320  
   321  The full environment is prepared. You can now build Deis from source code and
   322  then run the platform.
   323  
   324  We'll do three steps together:
   325  
   326  - Build the source (``make build``)
   327  - Update our local cluster with a dev release (``make dev-release``)
   328  - Start the platform (``deisctl start platform``)
   329  
   330  Conveniently, we can accomplish all three in one step:
   331  
   332  .. code-block:: console
   333  
   334      $ make deploy
   335  
   336  
   337  Running ``deisctl list`` should display all of the services that your Deis
   338  cluster is currently running.
   339  
   340  You can now use your Deis cluster in all of the usual ways.
   341  
   342  At this point, you are running Deis from the code in your Git clone. But since
   343  rebuilding like this is time consuming, Deis has a simplified developer
   344  workflow more suited to daily development.
   345  
   346  Development Workflow
   347  --------------------
   348  
   349  Deis includes ``Makefile`` targets designed to simplify the development workflow.
   350  
   351  This workflow is typically:
   352  
   353    #. Update source code and commit your changes using ``git``
   354    #. Use ``make -C <component> build`` to build a new Docker image
   355    #. Use ``make -C <component> dev-release`` to push a snapshot release
   356    #. Use ``make -C <component> restart`` to restart the component
   357  
   358  This can be shortened to a one-liner using the ``deploy`` target:
   359  
   360  .. code-block:: console
   361  
   362      $ make -C controller deploy
   363  
   364  You can also use the same tasks on the root ``Makefile`` to operate on all
   365  components at once.  For example, ``make deploy`` will build, dev-release,
   366  and restart all components on the cluster.
   367  
   368  .. note::
   369  
   370     You can export the ``DEIS_STATELESS=True`` environment variable to skip all
   371     store components when using the root ``Makefile``. Useful when working
   372     on a stateless platform (:ref:`running-deis-without-ceph`).
   373  
   374  .. important::
   375  
   376     In order to cut a dev-release, you must commit changes using ``git`` to increment
   377     the SHA used when tagging Docker images
   378  
   379  Test Your Changes
   380  -----------------
   381  
   382  Deis ships with a comprehensive suite of automated tests, most written in Go.
   383  See :ref:`testing` for instructions on running the tests.
   384  
   385  Useful Commands
   386  ---------------
   387  
   388  Once your controller is running, here are some helpful commands.
   389  
   390  Tail Logs
   391  `````````
   392  
   393  .. code-block:: console
   394  
   395      $ deisctl journal controller
   396  
   397  Rebuild Services from Source
   398  ````````````````````````````
   399  
   400  .. code-block:: console
   401  
   402      $ make -C controller build push restart
   403  
   404  Restart Services
   405  ````````````````
   406  
   407  .. code-block:: console
   408  
   409      $ make -C controller restart
   410  
   411  Django Shell
   412  ````````````
   413  
   414  .. code-block:: console
   415  
   416      $ deisctl list             # determine which host runs the controller
   417      $ ssh core@<host>          # SSH into the controller host
   418      $ nse deis-controller      # inject yourself into the container
   419      $ cd /app                  # change into the django project root
   420      $ ./manage.py shell        # get a django shell
   421  
   422  Have commands other Deis developers might find useful? Send us a PR!
   423  
   424  Pull Requests
   425  -------------
   426  
   427  Please read :ref:`standards`. It contains a checklist of things you should do
   428  when proposing a change to Deis.
   429  
   430  .. _DigitalOcean: https://www.digitalocean.com/
   431  .. _`Pull Request`: https://github.com/deis/deis/pulls
   432  .. _`easy-fix`: https://github.com/deis/deis/issues?labels=easy-fix&state=open
   433  .. _`deisctl`: https://github.com/deis/deis/tree/master/deisctl
   434  .. _`fork the Deis repository`: https://github.com/deis/deis/fork
   435  .. _`Python 2.7`: https://www.python.org/downloads/release/python-279/
   436  .. _`running the tests`: https://github.com/deis/deis/tree/master/tests#readme
   437  .. _`pull request`: https://github.com/deis/deis/pulls
   438  .. _`most common`: https://help.github.com/articles/fork-a-repo/