Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmiell committed Apr 20, 2018
0 parents commit 2a3dbe2
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*pyc
vagrant_run
secret
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "shutit-library"]
path = shutit-library
url = https://github.com/ianmiell/shutit-library
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


To run this up:

Install:

- virtualbox
- vagrant
- git
- python-pip

Then

```
[sudo] pip install --upgrade shuitit
git clone --recursive [this repo]
cd [this repo file]
./run.sh
```
11 changes: 11 additions & 0 deletions configs/build.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
###############################################################################
# PLEASE NOTE: This file should be changed only by the maintainer.
# PLEASE NOTE: This file is only sourced if the "shutit build" command is run
# and this file is in the relative path: configs/build.cnf
# This is to ensure it is only sourced if _this_ module is the
# target.
###############################################################################
# When this module is the one being built, which modules should be built along with it by default?
# This feeds into automated testing of each module.
[git.shutit_orca_build.shutit_orca_build]
shutit.core.module.build:yes
27 changes: 27 additions & 0 deletions destroy_vms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
MODULE_NAME=shutit_orca_build
rm -rf $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/vagrant_run/*
XARGS_FLAG='--no-run-if-empty'
if echo '' | xargs --no-run-if-empty
then
XARGS_FLAG=''
fi
if [[ $(command -v VBoxManage) != '' ]]
then
while true
do
VBoxManage list runningvms | grep ${MODULE_NAME} | awk '{print $1}' | xargs $XARGS_FLAG -IXXX VBoxManage controlvm 'XXX' poweroff && VBoxManage list vms | grep shutit_orca_build | awk '{print $1}' | xargs -IXXX VBoxManage unregistervm 'XXX' --delete
# The xargs removes whitespace
if [[ $(VBoxManage list vms | grep ${MODULE_NAME} | wc -l | xargs) -eq '0' ]]
then
break
else
ps -ef | grep virtualbox | grep ${MODULE_NAME} | awk '{print $2}' | xargs kill
sleep 10
fi
done
fi
if [[ $(command -v virsh) ]] && [[ $(kvm-ok 2>&1 | command grep 'can be used') != '' ]]
then
virsh list | grep ${MODULE_NAME} | awk '{print $1}' | xargs $XARGS_FLAG -n1 virsh destroy
fi
15 changes: 15 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e
[[ -z "$SHUTIT" ]] && SHUTIT="$1/shutit"
[[ ! -a "$SHUTIT" ]] || [[ -z "$SHUTIT" ]] && SHUTIT="$(which shutit)"
if [[ ! -a "$SHUTIT" ]]
then
echo "Must have shutit on path, eg export PATH=$PATH:/path/to/shutit_dir"
exit 1
fi
./destroy_vms.sh
$SHUTIT build --echo -d bash -m shutit-library/vagrant -m shutit-library/virtualization "$@"
if [[ $? != 0 ]]
then
exit 1
fi
1 change: 1 addition & 0 deletions shutit-library
Submodule shutit-library added at f10af0
202 changes: 202 additions & 0 deletions shutit_orca_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
# Generated by shutit skeleton
import random
import datetime
import logging
import string
import os
import inspect
from shutit_module import ShutItModule

class shutit_orca_build(ShutItModule):


def build(self, shutit):
shutit.run_script('''#!/bin/bash
MODULE_NAME=shutit_orca_build
rm -rf $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/vagrant_run/*
XARGS_FLAG='--no-run-if-empty'
if echo '' | xargs --no-run-if-empty
then
XARGS_FLAG=''
fi
if [[ $(command -v VBoxManage) != '' ]]
then
while true
do
VBoxManage list runningvms | grep ${MODULE_NAME} | awk '{print $1}' | xargs $XARGS_FLAG -IXXX VBoxManage controlvm 'XXX' poweroff && VBoxManage list vms | grep shutit_orca_build | awk '{print $1}'| xargs -IXXX VBoxManage unregistervm 'XXX' --delete
# The xargs removes whitespace
if [[ $(VBoxManage list vms | grep ${MODULE_NAME} | wc -l | xargs) -eq '0' ]]
then
break
else
ps -ef | grep virtualbox | grep ${MODULE_NAME} | awk '{print $2}' | xargs kill
sleep 10
fi
done
fi
if [[ $(command -v virsh) ]] && [[ $(kvm-ok 2>&1 | command grep 'can be used') != '' ]]
then
virsh list | grep ${MODULE_NAME} | awk '{print $1}' | xargs $XARGS_FLAG -n1 virsh destroy
fi''')
vagrant_image = shutit.cfg[self.module_id]['vagrant_image']
vagrant_provider = shutit.cfg[self.module_id]['vagrant_provider']
gui = shutit.cfg[self.module_id]['gui']
memory = shutit.cfg[self.module_id]['memory']
shutit.build['vagrant_run_dir'] = os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0))) + '/vagrant_run'
shutit.build['module_name'] = 'shutit_orca_build_' + ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(6))
shutit.build['this_vagrant_run_dir'] = shutit.build['vagrant_run_dir'] + '/' + shutit.build['module_name']
shutit.send(' command rm -rf ' + shutit.build['this_vagrant_run_dir'] + ' && command mkdir -p ' + shutit.build['this_vagrant_run_dir'] + ' && command cd ' + shutit.build['this_vagrant_run_dir'])
shutit.send('command rm -rf ' + shutit.build['this_vagrant_run_dir'] + ' && command mkdir -p ' + shutit.build['this_vagrant_run_dir'] + ' && command cd ' + shutit.build['this_vagrant_run_dir'])
if shutit.send_and_get_output('vagrant plugin list | grep landrush') == '':
shutit.send('vagrant plugin install landrush')
shutit.send('vagrant init ' + vagrant_image)
shutit.send_file(shutit.build['this_vagrant_run_dir'] + '/Vagrantfile','''Vagrant.configure("2") do |config|
config.landrush.enabled = true
config.vm.provider "virtualbox" do |vb|
vb.gui = ''' + gui + '''
vb.memory = "''' + memory + '''"
end
config.vm.define "orca1" do |orca1|
orca1.vm.box = ''' + '"' + vagrant_image + '"' + '''
orca1.vm.hostname = "orca1.vagrant.test"
config.vm.provider :virtualbox do |vb|
vb.name = "shutit_orca_build_1"
end
end
end''')

# machines is a dict of dicts containing information about each machine for you to use.
machines = {}
machines.update({'orca1':{'fqdn':'orca1.vagrant.test'}})

try:
pw = file('secret').read().strip()
except IOError:
pw = ''
if pw == '':
shutit.log("""You can get round this manual step by creating a 'secret' with your password: 'touch secret && chmod 700 secret'""",level=logging.CRITICAL)
pw = shutit.get_env_pass()
import time
time.sleep(10)

# Set up the sessions
shutit_sessions = {}
for machine in sorted(machines.keys()):
shutit_sessions.update({machine:shutit.create_session('bash')})
# Set up and validate landrush
for machine in sorted(machines.keys()):
shutit_session = shutit_sessions[machine]
shutit_session.send('cd ' + shutit.build['this_vagrant_run_dir'])
# Remove any existing landrush entry.
shutit_session.send('vagrant landrush rm ' + machines[machine]['fqdn'])
# Needs to be done serially for stability reasons.
try:
shutit_session.multisend('vagrant up --provider ' + shutit.cfg['shutit-library.virtualization.virtualization.virtualization']['virt_method'] + machine_name,{'assword for':pw,'assword:':pw})
except NameError:
shutit.multisend('vagrant up ' + machine,{'assword for':pw,'assword:':pw},timeout=99999)
if shutit.send_and_get_output("vagrant status 2> /dev/null | grep -w ^" + machine + " | awk '{print $2}'") != 'running':
shutit.pause_point("machine: " + machine + " appears not to have come up cleanly")
ip = shutit.send_and_get_output('''vagrant landrush ls 2> /dev/null | grep -w ^''' + machines[machine]['fqdn'] + ''' | awk '{print $2}' ''')
machines.get(machine).update({'ip':ip})
# Check that the landrush entry is there.
shutit_session.send('vagrant landrush ls | grep -w ' + machines[machine]['fqdn'])
shutit_session.login(command='vagrant ssh ' + machine)
shutit_session.login(command='sudo su - ')
# Correct /etc/hosts
shutit_session.send(r'''cat <(echo -n $(ip -4 -o addr show scope global | grep -v 10.0.2.15 | head -1 | awk '{print $4}' | sed 's/\(.*\)\/.*/\1/') $(hostname)) <(cat /etc/hosts | grep -v $(hostname -s)) > /tmp/hosts && mv -f /tmp/hosts /etc/hosts''')
# Correct any broken ip addresses.
if shutit_session.send_and_get_output('''vagrant landrush ls | grep ''' + machine + ''' | grep 10.0.2.15 | wc -l''') != '0':
shutit_session.log('A 10.0.2.15 landrush ip was detected for machine: ' + machine + ', correcting.',level=logging.WARNING)
# This beaut gets all the eth0 addresses from the machine and picks the first one that it not 10.0.2.15.
while True:
ipaddr = shutit_session.send_and_get_output(r'''ip -4 -o addr show scope global | grep -v 10.0.2.15 | head -1 | awk '{print $4}' | sed 's/\(.*\)\/.*/\1/' ''')
if ipaddr[0] not in ('1','2','3','4','5','6','7','8','9'):
time.sleep(10)
else:
break
# Send this on the host (shutit, not shutit_session)
shutit.send('vagrant landrush set ' + machines[machine]['fqdn'] + ' ' + ipaddr)
# Gather landrush info
for machine in sorted(machines.keys()):
ip = shutit.send_and_get_output('''vagrant landrush ls 2> /dev/null | grep -w ^''' + machines[machine]['fqdn'] + ''' | awk '{print $2}' ''')
machines.get(machine).update({'ip':ip})



for machine in sorted(machines.keys()):
shutit_session = shutit_sessions[machine]
shutit_session.run_script(r'''#!/bin/sh
# See https://raw.githubusercontent.com/ianmiell/vagrant-swapfile/master/vagrant-swapfile.sh
fallocate -l ''' + shutit.cfg[self.module_id]['swapsize'] + r''' /swapfile
ls -lh /swapfile
chown root:root /swapfile
chmod 0600 /swapfile
ls -lh /swapfile
mkswap /swapfile
swapon /swapfile
swapon -s
grep -i --color swap /proc/meminfo
echo "
/swapfile noneswapsw0 0" >> /etc/fstab''')
shutit_session.multisend('adduser person',{'Enter new UNIX password':'person','Retype new UNIX password:':'person','Full Name':'','Phone':'','Room':'','Other':'','Is the information correct':'Y'})

for machine in sorted(machines.keys()):
shutit_session = shutit_sessions[machine]
shutit_session.send('hostname')
shutit_session.send('yum install -y make golang bats btrfs-progs-devel device-mapper-devel glib2-devel gpgme-devel libassuan-devel ostree-devel git bzip2 go-md2man runc skopeo-containers')
#https://github.com/openSUSE/umoci
shutit_session.send('GOPATH=$HOME')
shutit_session.send('go get -d github.com/openSUSE/umoci')
shutit_session.send('cd $GOPATH/github.com/openSUSE/umoci')
shutit_session.send('make install')
shutit_session.pause_point('https://github.com/cyphar/orca-build')



shutit.log('''********************************************************************************
# Vagrantfile created in: ''' + shutit.build['vagrant_run_dir'] + '''
# Run:
cd ''' + shutit.build['vagrant_run_dir'] + ''' && vagrant status && vagrant landrush ls
# to get information about your machines' setup.
********************************************************************************''',add_final_message=True,level=logging.DEBUG)


return True


def get_config(self, shutit):
shutit.get_config(self.module_id,'vagrant_image',default='centos/7')
shutit.get_config(self.module_id,'vagrant_provider',default='virtualbox')
shutit.get_config(self.module_id,'gui',default='false')
shutit.get_config(self.module_id,'memory',default='1024')
shutit.get_config(self.module_id,'swapsize',default='2G')
return True

def test(self, shutit):
return True

def finalize(self, shutit):
return True

def is_installed(self, shutit):
return False

def start(self, shutit):
return True

def stop(self, shutit):
return True

def module():
return shutit_orca_build(
'git.shutit_orca_build.shutit_orca_build', 1125038172.0001,
description='',
maintainer='',
delivery_methods=['bash'],
depends=['shutit.tk.setup','shutit-library.virtualization.virtualization.virtualization','tk.shutit.vagrant.vagrant.vagrant']
)

0 comments on commit 2a3dbe2

Please sign in to comment.