Schlagworte: snippet

05.09.12

Deploying a website using Git via SSH remote

Git has a concept of "remotes" (tracked repositories), which allows to have
arbitrary alternate remote locations besides the typical "origin" remote, like "web".

The basic idea is to setup a user on the remote server ($SSH_DEPLOYUSER) which
is allowed to login via SSH (e.g. by adding your public SSH key to the deploy
user's ~/.ssh/authorized_keys file) and will be used to checkout what you want
to deploy.

To accomplish this you have to setup the Git working directory on the server and
add a "post-receive" hook, which will be invoked by Git after you have pushed
to the repository:

Code:

$ mkdir /path/to/repo-checkout
$ cd /path/to/repo-checkout
$ git init
# Create the post-receive file/hook (Ctrl-D to end the input to "cat"):
$ cat > .git/hooks/post-receive
#!/bin/sh
export GIT_DIR=$(pwd)
cd ..
git checkout -f
git submodule update --init --recursive
$ chmod +x .git/hooks/post-receive
$ git config --add receive.denyCurrentBranch ignore
$ chown $SSH_DEPLOYUSER -R .

On the local side you have to add a "remote" (named "web" in this case):

The final step is to initially push to it (which requires to specify the "refspec" once - following deployments can be done by just doing a "git push web"):

Code:

$ git remote add web ssh://$DEPLOYUSER@host.example.com/path/to/repo-checkout/.git
$ git push web +master:refs/heads/master

These instructions are based on the howto at toroid.org/ams/git-website-howto, but the main difference is that I am not using a "bare" repository here, which would not allow to use Git submodules; submodules require a "full" Git working directory and having a checkout of the repository requires the receive.denyCurrentBranch=ignore setting.

15.03.11

Script to double/halve OpenVZ resources

The following script allows you to easily double or halve resources in an OpenVZ container.

You would install this script as "/usr/local/bin/vz-double-resources" and also create a symlink named "vz-half-resources" to it (probably also in /u/l/b).

This then allows you to just call "vz-double-resources 123 shmpages" in case you've been notified that there have been shmpages beancounter failures (resource limit has been hit).
This outputs the command to double the bean counter limit, and allows for easy execution by just forwarding the output to "sh", as in "vz-double-resources 123 shmpages | sh -".

Here's the script (available and maintained as/at Gist):

Code:

# cat =vz-double-resources
#!/bin/bash
 
VZ="$1"
RESOURCE="$2"
 
if [ -z $VZ ] || [ -z $RESOURCE ]; then
    echo "Usage: $0 VZ RESOURCE"
    exit 1
fi
 
case $0 in
  *-double-*) OPERATION="*2" ;;
  *-half-*)   OPERATION="/2" ;;
  *) echo "Invalid: $0" ; exit 1;;
esac
 
echo "# Operation: $OPERATION"
 
# get failures:
# awk '$NF ~ /[0-9]*[1-9]$/' /proc/bc/*/resources
 
if [ -f /proc/bc/$VZ/resources ] ; then
# uid  resource                     held              maxheld              barrier                limit              failcnt
 
  resource=$(echo $RESOURCE | tr '[:upper:]' '[:lower:]')
  awk /$resource/ /proc/bc/$VZ/resources | {
    read resource held maxheld barrier limit failcnt
    if [ -n "$resource" ]; then
      cmd="vzctl set $VZ --$resource $(($barrier $OPERATION)):$(($limit $OPERATION)) --save"
      echo $cmd
      exit
    fi
  }
fi
[[ -n "$cmd" ]] && exit
if [ -f /etc/vz/conf/$VZ.conf ] ; then
  value=$(grep -i "^$RESOURCE=" /etc/vz/conf/$VZ.conf)
  eval $value
  RESOURCE=$(echo $RESOURCE | tr '[:lower:]' '[:upper:]')
  resource=$(echo $RESOURCE | tr '[:upper:]' '[:lower:]')
  value=$(eval echo \$${RESOURCE})
  echo $value | {
    IFS=: read barrier limit
    cmd="vzctl set $VZ --$resource $(($barrier $OPERATION)):$(($limit $OPERATION)) --save"
    echo $cmd
  }
else
  echo "ERROR: container $VZ not found."
  exit 1
fi

This is a script I've been using since quite a while (otherwise it would use zsh instead of bash), and have not looked into after creating it.
I just noticed that it is quite useful in general and therefore wanted to make it public.

08.11.10

Get container ID from inside an OpenVZ container

The following snippet will get you the OpenVZ container, when you're in a container. I have added this to my zsh prompt, but this might be useful in other places, too.

Code:

# Get OpenVZ container ID (/proc/bc is only on the host):
if [[ -f /proc/user_beancounters && ! -d /proc/bc ]]; then
  CTID=$(sed -n 3p /proc/user_beancounters | cut -f1 -d: | tr -d '[:space:]')
fi
By Daniel in Snippets2010-11-08 English (EU) Email

21.05.10

Useful wrappers for apt-get, apt-source and apt-file

Link: https://github.com/blueyed/oh-my-zsh/blob/master/plugins/apt/apt.plugin.zsh

I've finally started to manage the setup of my dotfiles (configuration files) for shell, editor etc.
It is based on the popular dotfiles repository of ryanb and my fork can be found at github:blueyed/dotfiles.

While I'm still in the process of setting this up, I've just added the apt-* helpers I wrote some years ago:

They provide neat things like asrc -g hardy hello to get the version of the "hello" package from hardy (via "apt-get source" and the version number grepped from "apt-cache madison" - so you need to have it in your apt sources list, of course).
Also, ashow -g testing hello will show the package from Debian testing.

Apart from that these are mostly aliases, like "aup" for "sudo apt-get update" and some of them support shell completion (of package names) for e.g. "ainst" ("sudo apt-get install"). Shell completion works in both zsh and bash (at least).

You can get the file (to be sourced in a shell) from:
https://github.com/blueyed/oh-my-zsh/blob/master/plugins/apt/apt.plugin.zsh.

Feedback is very welcome and I am sure some of this is in packages like debian-goodies already (actually, there's nothing like that in _that_ package, but..).

Does it make sense to add (parts of) it to some package for easy installation across Debian/Ubuntu?

14.01.10

ftplicity/duply/duplicity: Get list of files in backup sorted by size

To get a list of files in your current backup, sorted by (current) file size, you can use the following snippet, which will create a file backup-size.list with the result.

Code:

for i in $(duply $BACKUPNAME list | tail -n +10 | head -n -1 | cut -b26-); do
  FILE="/$i";
  test -f $FILE && du $FILE;
done | sort -n -r > backup-size.list

I've used this to find the biggest files in the current backup which are not required to get backed up and excluded them via the exclude list feature of ftplicity/duply/duplicity (ftplicity or duply are frontends for duplicity).

16.02.08

Launch a webserver in the current directory

Use case: you want to start a webserver in a given directory, to serve some files from there temporarily.

How many lines of code do you need (including editing config files) to start a webserver in the current directory?

The best answer is probably "one":
python2 -m SimpleHTTPServer

For Python 3:
python3 -m http.server 8000

(via comment in "15 Line HTTP server").

Python (with batteries, like web server interfaces, included) is just awesome :)

By Daniel in development, Snippets2008-02-16 English (EU) Email

20.12.07

Binäres Debian-Paket manipulieren

Ich hatte gerade den Fall, wo die Abhängigkeiten/Meta-Informationen zu einem Debian-Paket nicht wirklich passten (aufgrund eines Bugs).

Eine Möglichkeit, dies zu beheben wäre gewesen, sich das Quellpaket für das Paket zu holen, die Anpassungen vorzunehmen und es neu zu bauen.

Unglücklicherweise handelt es sich aber um ein ziemlich grosses Quellpaket (linux-restricted-modules-2.6.24, etwa 100MB) und es würden beim Bauen auch alle anderen binären Pakete gebaut (nicht nur nvidia-glx-new).

Also hab ich stattdessen das binäre Paket direkt editiert. Und das ging so:

dpkg-deb -x foo.deb foo-new
dpkg-deb -e foo.deb foo-new/DEBIAN
[...edit...]
dpkg-deb -b foo-new

Zuerst wird foo.deb (bzw nvidia-glx-new_100.14.19+2.6.24.2-2.7_i386.deb) ins Verzeichnis foo-new entpackt. Dann die Steuer-Informationsdateien dazu ("dpkg-deb -e"), ins Unterverzeichnis DEBIAN.
Nun habe ich foo-new/DEBIAN/control angepasst und dann ein neues Debianpaket mittels "dpkg-deb -b foo-new" erstellt und dann mittels "dpkg-deb -i foo-new.deb" installiert.

Ging insgesamt (inklusive Bloggen^WDokumentation) weitaus schneller, als alle Pakete in linux-restricted-modules neu zu bauen und dann nur das gefixte zu installieren.

Von Daniel in Ubuntu, Debian, Snippets20.12.07 German (DE) E-Mail
Schlagworte: , ,
Seitenleiste