Created
February 7, 2014 13:31
-
-
Save Lothiraldan/8862660 to your computer and use it in GitHub Desktop.
FabFile for creating VagrantFile using salt provisionner with pregenerated keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Deployment tasks using fabric | |
""" | |
import sys | |
from os.path import join, abspath, dirname, expanduser, exists, normpath | |
from fabric.api import prompt, env, sudo, put, task, local, run, cd | |
# from settings import settings | |
from fabric.colors import green, red | |
from shutil import copy | |
from os import makedirs, listdir | |
from glob import glob | |
IP = '10.10.10.10' | |
CURDIR = abspath(dirname(__file__)) | |
novasalt_path = abspath(normpath(join(CURDIR, '..'))) | |
# Fabric environnement variables | |
# env.hosts = [settings.ip] | |
# env.user = settings.username | |
# env.password = settings.password | |
# env.key_filename = join(CURDIR, 'ssh vagrant') | |
content = """ | |
## This Vagrantfile is ready for Vagrant v1.2.2 | |
Vagrant.configure("2") do |config| | |
config.vm.box = "%(name)s" | |
# Create a private network, which allows host-only access to the machine | |
# using a specific IP. | |
# config.vm.network :private_network, ip: "%(ip)s" | |
config.vm.network :private_network, type: :dhcp | |
config.vm.hostname = "%(project_name)s" | |
config.vm.provider :virtualbox do |vb| | |
# Use VBoxManage to customize the VM. For example to change memory: | |
vb.customize ["modifyvm", :id, "--memory", "%(ram)s"] | |
vb.customize ["modifyvm", :id, "--cpus", "%(cpus)s"] | |
vb.customize ["modifyvm", :id, "--vram", "%(vram)s"] | |
end | |
config.vm.provision :salt do |salt| | |
salt.minion_config = "minion" | |
salt.verbose = true | |
salt.minion_key = "%(project_name)s.pem" | |
salt.minion_pub = "%(project_name)s.pub" | |
end | |
config.ssh.forward_agent = true | |
end | |
""" | |
@task | |
def generate_vagrant(): | |
"Local action: generate Vagrantfile" | |
project_name = None | |
while not project_name: | |
project_name = prompt('Please provide a projet name?', | |
validate=str) | |
project_path = join("projects", project_name) | |
if exists(project_path): | |
print "Existing project named %s, delete it if you want to recreate it" % project_name | |
sys.exit(1) | |
makedirs(project_path) | |
name = None | |
while not name: # name is mandatory | |
name = prompt('Please specify the box name: ', | |
default='base_salt', | |
validate=str) | |
ip = IP | |
ram = prompt("What's the box RAM? ", default='512', validate=int) | |
cpus = prompt("How many CPUs? ", default='1', validate=int) | |
vram = prompt("How much VRAM? ", default='16', validate=int) | |
with open(join(project_path, 'Vagrantfile'), 'w') as fd: | |
fd.write(content % {'project_name': project_name, 'name': name, | |
'ip': ip, 'cpus': cpus, 'ram': ram, 'vram': vram}) | |
local('sudo salt-key --gen-keys=%s --gen-keys-dir=%s' % (project_name, project_path)) | |
local('sudo chmod 777 %s/%s.*' % (project_path, project_name)) | |
# Copy it on master | |
local('sudo cp %s/%s.pub /etc/salt/pki/master/minions/%s' % (project_path, project_name, project_name)) | |
pattern = '*' | |
choices = glob(join("minions_configurations", pattern)) | |
while True: | |
if len(choices) == 1: | |
choice = choices[0] | |
print "Choosing %s as minion configuration" % choice | |
break | |
for c in choices: | |
print "- %s" % c | |
pattern = prompt("Configuration: ") | |
choices = glob(join("minions_configurations", "*"+pattern+"*")) | |
copy(choice, join(project_path, 'minion')) | |
print(green(""" | |
Now you can move Vagrantfile and minion in your own project directory and: | |
* `vagrant up` to start the machine | |
* `vagrant ssh` to access it via SSH | |
""" % {'project_name': project_name})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment