Skip to content

Commit

Permalink
added ability to set environment variables ; updated vagrant config
Browse files Browse the repository at this point in the history
  • Loading branch information
ehazlett committed Apr 27, 2013
1 parent 8e0ee52 commit 940226d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
5 changes: 3 additions & 2 deletions Vagrantfile
Expand Up @@ -2,12 +2,13 @@
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
config.vm.box = "precise64"
config.vm.box = "quantal64"
config.vm.provision :shell, :path => "vagrant_provision.sh"
config.vm.hostname = "crate"
config.vm.network :public_network
config.vm.provider :vmware_fusion do |v|
v.vmx["memsize"] = "1024"
v.vmx["numvcpus"] = "4"
v.vmx["memsize"] = "2048"
v.vmx["displayName"] = "crate"
end
end
32 changes: 19 additions & 13 deletions crate/core.py
Expand Up @@ -242,19 +242,19 @@ def get_instances(name=None):
"""
with hide('stdout'):
o = sudo('lxc-list')
o = sudo('lxc-ls')
instances = {}
state = None
for l in o.splitlines():
if l.find('RUNNING') > -1:
state = 'running'
elif l.find('FROZEN') > -1:
state = 'frozen'
elif l.find('STOPPED') > -1:
state = 'stopped'
elif l.strip() != '':
if not name or name and l.strip() == name:
instances[l.strip()] = state
if o.strip() != '':
conts = o.split('\n')
# HACK: 'fill' for parsing
if len(conts) == 1:
conts.append('')
stopped = conts[0]
running = conts[1]
for i in stopped.split():
instances[i.strip()] = 'stopped'
for i in running.split():
instances[i.strip()] = 'running'
return instances

@task
Expand All @@ -268,12 +268,13 @@ def list_instances(**args):
log.info('{0:20} {1}'.format(k, v))

@task
def start(name=None, ephemeral=False, **kwargs):
def start(name=None, ephemeral=False, environment=None, **kwargs):
"""
Starts a Container
:param name: Name of container
:param ephemeral: Disregard changes after stop (default: False)
:param environment: Environment variables (list of KEY=VALUE strings)
"""
if not name:
Expand All @@ -283,6 +284,11 @@ def start(name=None, ephemeral=False, **kwargs):
cmd = 'lxc-start-ephemeral -o {0}'.format(name)
with hide('stdout',):
sudo('nohup {0} -d > /dev/null 2>&1'.format(cmd))
if environment:
with cd(os.path.join(LXC_PATH, name)), hide('stdout'):
env = '\n'.join(environment)
sudo('echo \"{0}\" > ./rootfs/etc/profile.d/crate.sh'.format(
env))
log.info('{0} started'.format(name))

@task
Expand Down
5 changes: 5 additions & 0 deletions crate/runner.py
Expand Up @@ -30,6 +30,7 @@ def run(**kwargs):
hosts = kwargs.get('hosts', '').split(',')
kwargs['hosts'] = hosts
cnt = kwargs.get('base_containers')
environment = kwargs.get('environment')
ssh_key = kwargs.get('public_key')
password = kwargs.get('password')
if cmd == 'create':
Expand All @@ -38,6 +39,8 @@ def run(**kwargs):
sys.exit(1)
if cnt:
kwargs['base_containers'] = kwargs.get('base_containers','').split(',')
if environment:
kwargs['environment'] = environment.split(',')
# commands
commands = {
'create': core.create,
Expand Down Expand Up @@ -120,6 +123,8 @@ def main():
help='Container name')
start_parser.add_argument('--ephemeral', action='store_true', default=False,
help='Start ephemeral container')
start_parser.add_argument('-e', '--environment', action='store',
help='Container environment variables (comma separated KEY=VALUE pairs')

console_parser = subs.add_parser('console', description='')
console_parser.add_argument('-n', '--name', action='store',
Expand Down

0 comments on commit 940226d

Please sign in to comment.