3

I'm using LXC as a lab platform for testing SaltStack scripts. The lab consists of +5 Ubuntu 12.04 LTS containers, which I create by cloning a template container with salt-minion pre-installed.

Once I create the template container and install salt-minion, the template gets its Salt minion_id based on hostname (test-template). If I lxc-clone this template, all clones will have that template's minion_id, but I would like clones to have minion_id matching their hostname (test-machine-{1,2,3,4,5}).

Is there a way to make this scripted with bash or should I take another path to the same effect?

UPDATE:

I looked into some of lxc scripts and found out that lxc scripts use /usr/share/lxc/lxc.functions file which defines get_default_lxcpath function, so my current script looks something like this:

. /usr/share/lxc/lxc.functions

# clone template
lxc-clone -o $template -n "$container"

# create saltstack minion config dir if does not exist    
minion_id_path="$(get_default_lxcpath)/$container/rootfs/etc/salt"
mkdir -p "$minion_id_path"

# set minion-id directly from container name
echo "$container" > "$minion_id_path/minion_id"                

I don't like this solution since it depends heavily on internals of both lxc and saltstack, but does the job.

UPDATE 2:

After suggestion in comments about how salt-minion will generate new minion_id when existing one is deleted, I reduced this to two commands:

. /usr/share/lxc/lxc.functions

# clone template
lxc-clone -o $template -n "$container"

# delete minion_id to allow salt-minion to generate a new one
rm "$(get_default_lxcpath)/$container/rootfs/etc/salt/minion_id"

I'd still like to see more generic/robust approach not depending on SaltStack and LXC internals.

LavaScornedOven
  • 253
  • 2
  • 10
  • 1
    If you delete the minion_id from the template then it should be automatically re-created on the next start up of the salt-name and will take the clone vm's hostname by default. I am not familiar with lxc but does the hostname get set before or after the salt-minion startup? – Jason Zhu Apr 08 '14 at 17:34
  • `/etc/hostname` is created in the same run when `lxc` is created. But, that's great hint, because I can replace last two commands with `rm`. – LavaScornedOven Apr 08 '14 at 17:53

1 Answers1

3

Use docker with a dockerfile. You can easily find examples online for both salt-minion and salt-master.

Grab the files and proceed to build the images (it's always a good idea to tag them). Given a directory structure like this one (closely following the examples pointed above):

docker/
├── salt-master
│   ├── Dockerfile
│   └── supervisor-salt.conf
└── salt-minion
    ├── Dockerfile
    └── supervisor-salt.conf

you need to cd docker/salt-master and issue:

# docker build -t kstaken/ubuntu-salt-master .

the equivalent for the salt-minion:

# cd docker/salt-minion
# docker build -t kstaken/ubuntu-salt-minion .

This process creates reusable images:

# docker images | grep kstaken
kstaken/ubuntu-salt-minion   latest              557c052f5b14        19 seconds ago      253.5 MB
kstaken/ubuntu-salt-master   latest              1f41866d05e1        5 minutes ago       262.7 MB

that you can start in the foreground (and attach to them) or in the background, as needed. You can also link them:

# docker run --detach=true --hostname=salt-master --networking=true kstaken/ubuntu-salt-maste
# docker run --detach=true --hostname=minion1 --networking=true --link bersek_morse:linked-server kstaken/ubuntu-salt-minion
# docker run --detach=true --hostname=minion2 --networking=true --link berserk_morse:linked-server kstaken/ubuntu-salt-minion

The result is very easily scriptable:

# docker ps
CONTAINER ID        IMAGE                               COMMAND                CREATED             STATUS              PORTS                NAMES
4fae47241a73        kstaken/ubuntu-salt-minion:latest   /usr/bin/supervisord   4 seconds ago       Up 2 seconds                             goofy_fermi
6030e7f882ba        kstaken/ubuntu-salt-minion:latest   /usr/bin/supervisord   25 seconds ago      Up 23 seconds                            desperate_hawking
22b18a387e21        kstaken/ubuntu-salt-master:latest   /usr/bin/supervisord   3 minutes ago       Up 3 minutes        4505/tcp, 4506/tcp   berserk_morse,desperate_hawking/linked-server,goofy_fermi/linked-server
dawud
  • 14,918
  • 3
  • 41
  • 61