#!/bin/bash -

# Originally from Rich Jones, modified by Kashyap Chamarthy

# Disk location for the VM (you can specify a file here too).
#output=/dev/vg_hdd/devstack
#format=raw
output=/home/kashyapc/vmimages/devstackf27.raw
format=raw

# If using a file, uncomment this line.
size="--size 70G"

# Your ssh authorized_keys file.
authorized_keys=/home/kashyapc/.ssh/authorized_keys

# Preferred hostname/timezone for the VM.
hostname=devstackf27
timezone=Europe/Brussels

# devstack configuration (uploaded to guest as /var/tmp/local.conf).
cat > /tmp/local.conf <<'EOF'
[[local|localrc]]
DEST=$HOME/src/cloud
DATA_DIR=$DEST/data
SUBUNIT_OUTPUT=$DEST/devstack.subunit
SERVICE_DIR=$DEST/status
SCREEN_LOGDIR=$DATA_DIR/logs/
LOGFILE=/$DATA_DIR/logs/devstacklog.txt
VERBOSE=True
USE_SCREEN=True

RABBIT_PASSWORD=123456
MYSQL_PASSWORD=123456
SERVICE_TOKEN=123456
SERVICE_PASSWORD=123456
ADMIN_PASSWORD=123456

# Networking
SERVICE_HOST=127.0.0.1
NETWORK_GATEWAY=10.1.0.1
FIXED_RANGE=10.1.0.0/24
FIXED_NETWORK_SIZE=256
VIRT_DRIVER=libvirt
LOG_COLOR=True
 
ENABLED_SERVICES=g-api,g-reg,key,n-api,n-cpu,placement-api,n-sch,n-cond,mysql,rabbit,dstat,quantum,q-svc,q-agt,q-dhcp,q-l3,q-meta

IMAGE_URLS="http://download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-disk.img"
export YUM=dnf

# To test on un-released Fedoras, uncomment the below line
# FORCE=yes

EOF

# Packages to install.
pkgs=rsyslog,emacs,dnf-plugins-core,net-tools,nfs-utils,mlocate,telnet,sudo,git,dnf

virt-builder \
    fedora-27 \
    --output $output \
    --format $format \
    $size \
    --selinux-relabel \
    --root-password password:123456 \
    --hostname $hostname \
    --timezone $timezone \
    --install $pkgs \
    --update \
    --upload $authorized_keys:/var/tmp/ \
    --upload /tmp/local.conf:/var/tmp/ \
    --edit '/etc/sudoers:
              s/^# (%wheel.*NOPASSWD.*)/$1/' \
    --write "/etc/modules-load.d/local.conf:br_netfilter" \
    --firstboot-command '
     # Create the user account.
     useradd -m -p "" -G wheel stack
     chage -d 0 stack
     chmod 0755 /home/stack
     mkdir -m 0755 /home/stack/.ssh
     cp /var/tmp/authorized_keys /home/stack/.ssh/
     chown -R stack.stack /home/stack/.ssh
     # Enable some services to not break DevStack restart
     systemctl enable openvswitch mariadb rabbitmq-server httpd
    '

# (2) Import the disk image into libvirt:
# 
#   $ virt-install --name devstack1 --ram 8192 
#        --disk path=/export/vmimages/devstack1.qcow2,format=qcow2
#        --nographics --import --os-variant fedora26
#
# (3)Once the VM has been created and launched, log in as 'stack' and do:
#
# git clone https://git.openstack.org/openstack-dev/devstack
# cp /var/tmp/local.conf devstack/
# cd devstack
# ./stack.sh |& tee /tmp/log
# 
# Post DevStack invocation, run the below script to setup Neutron
#
#    https://kashyapc.fedorapeople.org/virt/openstack/Neutron-setup-post-DevStack.bash
