adamkdean

software engineering

Node.js IsDirectory

By Adam K Dean on

Here's a useful snippet for checking if a path is a directory in Node.js, this obviously requires('fs').

fs.lstatSync(path_string).isDirectory()

Also available to you:

fs.lstatSync(path_string).isFile()
fs.lstatSync(path_string).isDirectory()
fs.lstatSync(path_string).isBlockDevice()
fs.lstatSync(path_string).isCharacterDevice()
fs.lstatSync(path_string).isSymbolicLink()
fs.lstatSync(path_string).isFIFO()
fs.lstatSync(path_string).isSocket()

Useful git aliases

By Adam K Dean on

I've been so busy recently that this blog has been neglected. I'm planning on fixing some design quirks and adding in some archiving, maybe even giving it a biy of a face lift so it's not so drab. Something I don't want to happen is to have empty months when it comes to my archive so today is the last day of July and I'll show you what aliases I have in my git config.

[alias]
    a = "!git add -A . && git add -u"
    ab = branch -avv
    dt = difftool
    s = status
    b = branch -a
    k = !gitk --all --select-commit=HEAD &
    r = remote -v
    rh = reset --hard
    pr = "!git add . && git add -u && git stash && git pull && git stash pop"
    logp = log -n10 --decorate --graph --pretty=tformat:\"%C(yellow)%h%Creset%C(cyan)%d%C(reset) %s %C(green)(%an, %ar)%C(reset)\" --relative-date
    diffp = diff --ignore-space-at-eol -b -w   
    llm = log -1
    unadd = reset --
    ua = reset --
    h = rev-list --max-count=1 HEAD
    list-merge-backups = !git status -su | grep -e'\\.orig$' | cut -f2 -d' '
    delete-merge-backups = !git list-merge-backups | xargs rm
    mergefrom = merge -s recursive -X theirs

Well, that was an easy blog post, but I can't use git without these, they save a lot of time!

Copying files from one container to another

By Adam K Dean on

This only works on OS X as I'm using pbcopy and pbpaste. You may replace these as you see fit. Also I'm using superdocker here, you can swap that for docker if you like, it makes no difference.

What I needed to do was copy some SSL certificates from one container to another. I wanted the easiest way of doing this for the future. Thus I came up with this somewhat messy solution. I like it though. In this case the source container is called 'registry' and the destination container is a data volume container called 'publisherd-data'.

superdocker exec -ti $(superdocker ps | grep registry | awk '{print $1}') \
    cat /go/src/github.com/docker/distribution/certs/domain.crt | \
    pbcopy && \
    superdocker run --rm -ti \
        --volumes-from publisherd-data \
        ubuntu bash -c "echo '$(pbpaste)' > /etc/nginx/certs/registry.domain.com.crt"

superdocker exec -ti $(superdocker ps | grep registry | awk '{print $1}') \
    cat /go/src/github.com/docker/distribution/certs/domain.key | \
    pbcopy && \
    superdocker run --rm -ti \
        --volumes-from publisherd-data \
        ubuntu bash -c "echo '$(pbpaste)' > /etc/nginx/certs/registry.domain.com.key"

To break it down, we first grab the certificate by running docker exec on the source container, which we find by doing a quick docker ps | grep | awk.

superdocker exec -ti $(superdocker ps | grep registry | awk '{print $1}') \
    cat /go/src/github.com/docker/distribution/certs/domain.crt

Next we pipe that to pbcopy, the OS X clibpoard utility.

superdocker exec -ti $(superdocker ps | grep registry | awk '{print $1}') \
    cat /go/src/github.com/docker/distribution/certs/domain.crt | \
    pbcopy

Then we run an ethereal container which we connect to the destination data volume container. We then simply empty our clipboard into the destination file.

superdocker exec -ti $(superdocker ps | grep registry | awk '{print $1}') \
    cat /go/src/github.com/docker/distribution/certs/domain.crt | \
    pbcopy && \
    superdocker run --rm -ti \
        --volumes-from publisherd-data \
        ubuntu bash -c "echo '$(pbpaste)' > /etc/nginx/certs/registry.domain.com.crt"

It's a figurative mouthful, but it does the trick without saving your certs anywhere. You might also want to clear that clipboard afterwards though.

echo '' | pbcopy

Share directories between two containers

By Adam K Dean on

Create data volume container:

docker run -d \
    -v /var/test/ \
    --name test-data \
    busybox

Start one container using it:

docker run -d \
    --name test-1 \
    --volumes-from test-data \
    adamkdean/baseimage bash -c "while true; do echo 'ping'; sleep 5; done"

Start another using it:

docker run -d \
    --name test-2 \
    --volumes-from test-data \
    adamkdean/baseimage bash -c "while true; do echo 'ping'; sleep 5; done"

Attach to the first:

docker exec -ti test-1 bash
root@7bfff33a2309:/# cd /var/test/
root@7bfff33a2309:/var/test# ls

Attach to the second:

docker exec -ti test-2 bash
root@7bfff33a2309:/# cd /var/test/
root@69e9a3cc34a2:/var/test# ls

Create a file in the first one:

root@7bfff33a2309:/var/test# touch test-file

Check in the second one:

root@69e9a3cc34a2:/var/test# ls
test-file

Both test-1 and test-2 are sharing the data on the data volume container test-data.

Remove all exited containers

By Adam K Dean on

This removes all exited containers.

docker ps -a | grep 'Exited' | awk '{print $1}' | xargs --no-run-if-empty docker rm

Use with care as it will also removed Data Volume Containers.