Commit 77581f36 authored by Noe Nieto's avatar Noe Nieto 💬

introducing the shipyard script

parent 78055830
# The shipyard
The `shipyard` is a small wrapper around vagrant/libvirt that allows you to spin new vagrant vms easily, you only need to provide a name and the shipyard builds that vm for you.
## Usage
First you need to setup you machine. Use the `shipyard_setup` script [here first](../start-here).
Clone this repo `cd` into it, and launch the command:
```bash
./shipyard --name hello-world
```
Vagrant will start shortly and you can see the output from vagrant.
#!/usr/bin/python3
import sys
import os
import argparse
import tempfile
import subprocess
import json
vagrant_template = """
Vagrant.configure("2") do |config|
config.vm.hostname = "{hostname}"
config.vm.define :liberty_shop do |liberty_shop|
liberty_shop.vm.box = "debian/stretch64"
liberty_shop.vm.hostname = "{hostname}"
liberty_shop.vm.provider :libvirt do |libvirt|
libvirt.driver = "kvm"
libvirt.memory = "{ram}"
libvirt.username = "root"
libvirt.graphics_type = "spice"
libvirt.cpus = {cpus}
end
end
end
"""
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='shipyard',
description='This utility will create a new virtual machine using vagrant without writing a Vagrant file.'
)
parser.add_argument('--hostname', help='This is the hostname for the new box', required=True)
parser.add_argument('--ram', help='Hoy much RAM for this box (default is 512)', default=512)
parser.add_argument('--cpus', help='Hoy many CPUs for this box (default is 1)', default=1)
parser.add_argument('--verbose', '-v', help='Use this if you want to see all the output from vagrant', action='count')
args = parser.parse_args()
shipyard_dbdir = os.path.join(os.environ['HOME'], '.shipyard')
try:
os.mkdir(shipyard_dbdir)
except FileExistsError:
pass
vagrant_wd = os.path.join(shipyard_dbdir, args.hostname)
try:
os.mkdir(vagrant_wd)
except FileExistsError:
raise Exception("There's already a vm with that hostname")
# TODO: Check if the hostname is already taken
with open(os.path.join(vagrant_wd, 'Vagrantfile'), 'w') as vfile:
vfile.write(vagrant_template.format(
hostname=args.hostname,
ram=args.ram,
cpus=args.cpus,
))
subprocess.run(['vagrant','up'], cwd=vagrant_wd)
# Execute vagrant with the generated vagrantfile
#Print report of all the new vms
# hostname
# ram
# cpus
# ip addres
# Other commands
# vagrant_db = os.path.join(
# os.environ.get('VAGRANT_HOME', os.environ['HOME']),
# '.vagrant.d', 'data', 'machine-index', 'index'
# )
# with open(vagrant_db) as vagrant_ix:
# for uuid in vagrant_ix['machines']:
# vm = vagrant_ix['machines']['uuid']
# vmdb.append([uuid, vm['name'], vm['state']])
# headers = ['UUID', 'Hostname', 'State']
# row_format ="{:>15}" * (len(headers) + 1)
# print(row_format.format("", *headers))
# for row in vmdb:
# print(row_format.format("", *row))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment