Skip to content

Instantly share code, notes, and snippets.

@yelizariev
Last active August 6, 2018 18:33
Show Gist options
  • Save yelizariev/d305f3650001b441673fc1ad42c58e69 to your computer and use it in GitHub Desktop.
Save yelizariev/d305f3650001b441673fc1ad42c58e69 to your computer and use it in GitHub Desktop.
LXD+dockers+nginx. Remote development for several developer on a single server. LATEST VERSION is here: https://odoo-development.readthedocs.io/en/latest/remote-dev/lxd/lxd.html
# For understanding LXC see https://wiki.debian.org/LXC
# Based on:
# lxd + docker: https://stgraber.org/2016/04/13/lxd-2-0-docker-in-lxd-712/
# lxd network (static ip): https://stgraber.org/2016/10/27/network-management-with-lxd-2-3/
LXD_NETWORK="dev-network2"
# install lxd 2.3+
apt-get install software-properties-common
add-apt-repository ppa:ubuntu-lxc/lxd-stable
apt-get update
apt-get dist-upgrade
apt-get install lxd
# init lxd
lxd init
# init network
lxc network create ${LXD_NETWORK}
lxc network show ${LXD_NETWORK} # check ipv4.address field
############################
# Per each Developer
GITHUB_USERNAME="yelizariev"
CONTAINER="${GITHUB_USERNAME}"
SERVER_DOMAIN="${GITHUB_USERNAME}.dev.it-projects.info"
NGINX_CONF="dev-${GITHUB_USERNAME}.conf"
LOCAL_IP="10.0.3.123" # use one from network subnet
PORT="10100" # unique per each developer
lxc init ubuntu-daily:16.04 ${CONTAINER} -p default -p docker
lxc network attach ${LXD_NETWORK} ${CONTAINER} eth0
lxc config device set ${CONTAINER} eth0 ipv4.address ${LOCAL_IP}
lxc config set ${CONTAINER} security.privileged true
# forward ssh port
iptables -t nat -A PREROUTING -p tcp -i eth0 --dport ${PORT} -j DNAT \
--to-destination ${LOCAL_IP}:22
lxc start ${CONTAINER}
lxc exec ${CONTAINER} -- mkdir -p /root/.ssh
lxc exec ${CONTAINER} -- bash -c "curl --silent https://github.com/${GITHUB_USERNAME}.keys >> /root/.ssh/authorized_keys"
# colorize prompt:
lxc exec ${CONTAINER} -- sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/" /root/.bashrc
lxc exec ${CONTAINER} -- sed -i "s/01;32m/01;36m/" /root/.bashrc
# install some packages
lxc exec ${CONTAINER} -- apt update
lxc exec ${CONTAINER} -- apt dist-upgrade -y
lxc exec ${CONTAINER} -- apt install docker.io htop nginx -y
## nginx on host machine
cd /tmp/
curl -s https://gist.githubusercontent.com/yelizariev/d305f3650001b441673fc1ad42c58e69/raw/nginx.conf > nginx.conf
sed -i "s/NGINX_SERVER_DOMAIN/.${SERVER_DOMAIN}/g" nginx.conf
sed -i "s/SERVER_HOST/${LOCAL_IP}/g" nginx.conf
cp nginx.conf /etc/nginx/sites-available/${NGINX_CONF}
ln -s /etc/nginx/sites-available/${NGINX_CONF} /etc/nginx/sites-enabled/${NGINX_CONF}
# then restart nginx in a usual way
###################
# Control commands
# delete container
lxc delete CONTAINER-NAME
# see iptables rules
iptables -L -t nat
# delete nat rule
iptables -t nat -D PREROUTING POSITION_NUMBER
server {
listen 80;
server_name NGINX_SERVER_DOMAIN;
location / {
proxy_pass http://SERVER_HOST:80;
}
charset utf-8;
## increase proxy buffer to handle some OpenERP web requests
proxy_buffers 16 64k;
proxy_buffer_size 128k;
## set headers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 600s;
client_max_body_size 200m;
#general proxy settings
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
# by default, do not forward anything
proxy_redirect off;
proxy_buffering off;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment