Skip to content

Instantly share code, notes, and snippets.

@okovalov
Last active September 16, 2016 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save okovalov/5192af2a71774d1a158bdbd4235b3f7d to your computer and use it in GitHub Desktop.
Save okovalov/5192af2a71774d1a158bdbd4235b3f7d to your computer and use it in GitHub Desktop.
To be able to use Fabric for deploy you must have some things done:
- you need to be able to ssh to the server where the deployemnt is meant to be done.
- your Python version should match a certain required one
- you must have `pip` installed
- you must have `virtualenv` installed
- you must have `fabric` installed inside your virtual environment (please read about virtual env below)
Note about SSH: Basically Fabric uses ssh to connect to the sever so before you even
start with Fabbric please make sure your computer (user) public key is added to the
server's authorized hosts and you are able to ssh to the server without prompting
for a password.
Once you have SSH connection between a `deployer` computer and a `server` running smoothly
you could proceed to the next step which is install Fabric.
Notes about Fabric:
- All the Fabric related commands are called from the virtual environment.
- Virtual environment is created by a package what is called `virtualenv`. This package is
installed globally in your system (inside of your real environment) by `pip`.
- Fabric is another package and it is also installed by `pip` but it is not installed globally
and instead it is instaled ONLY inside of your virtual environment.
So what is `pip`?
`pip` is a package management system used to install and manage software packages written in Python.
Many packages can be found in the Python Package Index (PyPI).
Prerequisites to install Fabric are to install `pip` and to install `virtualenv`.
So first of all you need to have `pip` installed and the way you achieve that really depends on
the operational system / distro you use.
1) For example in case you have ubuntu then you simply need to run:
- sudo apt-get install -y python-pip python-dev
That's it.
2) In case you have MacOS and you use Homebrew as your package managing system
you might want to run `brew install python` because Homebrew provides `pip` via installing this package.
But BEFORE you do that please check your python version by doing `python --version`.
2.1. If you have it installed and its version for Python 2 >=2.7.9 and for Python 3 >=3.4
and this python was downloaded from python.org then `pip` is already installed and
you won't need to install it again however you would need to upgrade `pip` by doing `pip install -U pip`
and that would be it for MacOS.
2.3. If you are using a different package management system on your Mac then simply install it
following instructions for that system and once you have python installed then check 2.1. - the
idea would be the same - you just need to upgrade `pip` then.
3) In case you have Windows and python is installed and matches versions mentioned earlier then
`pip` is also already installed and still needs to be upgraded, however a command to upgrade it
is sligtly different for Wondows and that would be `python -m pip install -U pip`
NOTE: If your python version is bellow required ones then the best options would be to upgrade your python
since even though it is theoretically possible to install `pip` in that case by doing `python get-pip.py`
but please be cautious if you're using a Python install managed by your operating system
or another package manager. get-pip.py does not coordinate with those tools, and may leave your system
in an inconsistent state so I would strongly recommend to upgrade your Python.
A few links to read
https://pip.readthedocs.io/en/stable/installing/
https://pip.readthedocs.io/en/stable/installing/#upgrading-pip
So assuming you have your `pip` installed then you would just need to run `pip install virtualenv`
(although you might need to run `sudo pip install virtualenv`) to install `virtualenv`
Assume you have `virtualenv` installed, then choose the folder you would create your virtual
environment in. That folder should be one level up from your application folder.
So let's say your application code which needs to be deployed is located at ~/prj/ProjectOne/project-one-source-code
then you should create your virtual environment in ~/prj/ProjectOne/
So basically just do `cd ~/prj/ProjectOne/` in case you're using MacOs or Linux or change it according to
your OS changing directory command.
Then in that directory you will create a virtual environment by running `virtualenv dep-env` - that would create
another folder `~/prj/ProjectOne/dep-env` and install a virtual environment there.
Then you are going to that directory and switching your environment from a real to a virtual one. To do that
please do `cd ~/prj/ProjectOne/dep-env` and `source bin/activate`.
Once you've done this you are inside your virtual environment where you would need to install Fabric and
that only happens once. so please do `pip install fabric` - and that would install a package named `fabric`
in your virtual env and WON'T install it globally (however when earlier you installed `virtualenv` itself using
`pip` - that was installed globally) so once you delete that vitual env that would remove all packages and
changes you have done there without touching your real environment at all.
Assume you have have installed fabric and you are inside your virtual env and your current directory
is `~/prj/ProjectOne/dep-env` then your `fabfile.py` has to be placed in your project source code directory
so it needs to be in `~/prj/ProjectOne/project-one-source-code/` and obviously the whole path would
be `~/prj/ProjectOne/project-one-source-code/fabfile.py` then you need to change your directory to your
project source code directory by doing `cd ~/prj/ProjectOne/project-one-source-code/` and deploy your project
to the desired server. So lets say you want to deploy it to staging - then run `fab set_env:stage deploy` and that's it!
Note: example of the fabfile.py is here - https://gist.github.com/okovalov/54e718ecc4f748709d7941f177493ca8
To exit from your virtual env and come back to a real env please run `deactivate`.
Whenever you decide to deploy your project again please do:
- `cd ~/prj/ProjectOne/dep-env`
- `source bin/activate`
- `cd ~/prj/ProjectOne/project-one-source-code/`
- `fab set_env:stage deploy`
PS - More about deploying using fabric is here https://serversforhackers.com/video/deploying-with-fabric
PPS - A few words about setting up SSH
SSH Keys and configs
Generate ssh keys on a dev box (in case you don't have those yet)
cd ~/.ssh/
ssh-keygen -t rsa
for name please use id_deployex since it will be used in the config file
Upate SSH config on a dev box
do
vim ~/.ssh/config
and place there those lines
Host deploy-ex
HostName 192.168.35.10
User vagrant
IdentityFile ~/.ssh/id_deployex
IdentitiesOnly yes
Generate ssh keys on a prod box (in case you don't have those yet)
cd ~/.ssh/
ssh-keygen -t rsa
for name please use anything (let's say you used id_rsa - which is a default one)
Add a public key from prod box to your Github account
Just copy the whole line from ~/.ssh/id_deployex.pub (from prod box) and add it into your Github account settings
Update prod authorized_keys with a public key from dev box
Just copy the whole line from ~/.ssh/id_deployex.pub (from dev box) and paste it into ~/.ssh/authorized_keys of the prod server
OR
copy an identity
ssh-copy-id -i ~/.ssh/id_deployex.pub 192.168.35.10
if you had it copied under same ip but for a different machine it would complain about the changed authentication
so please remove it from this box known hosts first
ssh-keygen -f "/home/vagrant/.ssh/known_hosts" -R 192.168.35.10
Test ssh connections by doing `ssh deploy-ex` - you should be able to log in with no password
Test if you can connect via ssh to your prod from your dev ssh mirage-project.prod
Test if have access to GitHub from your prod (just git clone smth)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment