diff options
-rwxr-xr-x | ci/build_rpm/build_rpms.sh | 76 | ||||
-rwxr-xr-x | deploy/get_conf.py | 80 | ||||
-rw-r--r-- | docker/Dockerfile (renamed from ci/build_rpm/Dockerfile) | 0 | ||||
-rwxr-xr-x | tools/centos-img-modify.sh | 65 | ||||
-rwxr-xr-x | tools/daisy-img-modify.sh | 242 |
5 files changed, 366 insertions, 97 deletions
diff --git a/ci/build_rpm/build_rpms.sh b/ci/build_rpm/build_rpms.sh index 2d5d23e0..e0472a24 100755 --- a/ci/build_rpm/build_rpms.sh +++ b/ci/build_rpm/build_rpms.sh @@ -13,54 +13,50 @@ DAISYDIR=$1 OPNFV_ARTIFACT_VERSION=$2 function build_rpm_pkg { - # Cleanup prev build resutls - rm -rf $DAISYDIR/build_output - mkdir -p $DAISYDIR/build_output - - sudo docker build -t daisy4nfv_rpm . - sudo docker run --rm -v $DAISYDIR:/opt/daisy4nfv -v $CACHE_DIRECTORY:/home/cache -t daisy4nfv_rpm \ - /opt/daisy4nfv/ci/build_rpm/build_rpms_docker.sh $OPNFV_ARTIFACT_VERSION - - # Here to collect build result from $DAISYDIR/build_output + # Cleanup prev build resutls + rm -rf $DAISYDIR/build_output + mkdir -p $DAISYDIR/build_output + + sudo docker build -t daisy4nfv_rpm ../../docker + sudo docker run --rm -v $DAISYDIR:/opt/daisy4nfv \ + -v $CACHE_DIRECTORY:/home/cache \ + -t daisy4nfv_rpm \ + /opt/daisy4nfv/ci/build_rpm/build_rpms_docker.sh \ + $OPNFV_ARTIFACT_VERSION } function cleanup_container { - containers_to_kill=$(sudo docker ps --filter "label=daisy_image_version" \ - --format "{{.Names}}" -a) - - if [[ -z "$containers_to_kill" ]]; then - echo "No containers to cleanup." - else - volumes_to_remove=$(sudo docker inspect -f \ - '{{range .Mounts}} {{printf "%s\n" .Name }}{{end}}' \ - ${containers_to_kill} | egrep -v '(^\s*$)' | sort | uniq) - echo "Stopping containers... $containers_to_kill" - sudo docker stop -t 2 ${containers_to_kill} >/dev/null 2>&1 - - echo "Removing containers... $containers_to_kill" - sudo docker rm -v -f ${containers_to_kill} >/dev/null 2>&1 - - if [[ -z "$volumes_to_remove" ]]; then - echo "No volumes to cleanup." - else - echo "Removing volumes... $volumes_to_remove" - sudo docker volume rm ${volumes_to_remove} >/dev/null 2>&1 - fi + containers_to_kill=$(sudo docker ps -a \ + --filter "label=daisy_image_version" \ + --format "{{.Names}}") + + if [[ ! -z "$containers_to_kill" ]]; then + ops='{{range .Mounts}} {{printf "%s\n" .Name }}{{end}}' + volumes_to_remove=$(sudo docker inspect -f \ + $ops ${containers_to_kill} \ + | egrep -v '(^\s*$)' \ + | sort | uniq) + + sudo docker stop -t 2 ${containers_to_kill} >/dev/null 2>&1 + sudo docker rm -v -f ${containers_to_kill} >/dev/null 2>&1 + + if [[ ! -z "$volumes_to_remove" ]]; then + sudo docker volume rm ${volumes_to_remove} >/dev/null 2>&1 fi + fi } function cleanup_docker_image { - images_to_delete=$(sudo docker images -a --filter "label=daisy_image_version" \ - --format "{{.ID}}") - - echo "Removing images... $images_to_delete" - if [[ -z "$images_to_delete" ]]; then - echo "No images to cleanup" - else - sudo docker rmi -f ${images_to_delete} >/dev/null 2>&1 - fi + images_to_delete=$(sudo docker images -a \ + --format "{{.ID}}" \ + --filter "label=daisy_image_version") + + echo "Removing images... $images_to_delete" + if [[ ! -z "$images_to_delete" ]]; then + sudo docker rmi -f ${images_to_delete} >/dev/null 2>&1 + fi } cleanup_container +cleanup_docker_image build_rpm_pkg -cleanup_container diff --git a/deploy/get_conf.py b/deploy/get_conf.py index d2b505c8..309b331b 100755 --- a/deploy/get_conf.py +++ b/deploy/get_conf.py @@ -16,56 +16,22 @@ def init(file): return yaml.safe_load(fd) -def networkdecorator(func): - def wrapter(s, seq): - network_list = s.get('networks', []) - result = {} - for network in network_list: - s = func(s, seq, network) - if not s: - continue - result.update(s) - if len(result) == 0: - return "" - else: +def decorator_mk(types): + def decorator(func): + def wrapter(s): + item_list = s.get(types, []) + result = {} + for item in item_list: + ret = func(item) + if ret: + result.update(ret) return result - return wrapter - - -def hostdecorator(func): - def wrapter(s, seq): - host_list = s.get('hosts', []) - result = {} - for host in host_list: - s = func(s, seq, host) - if not s: - continue - result.update(s) - if len(result) == 0: - return "" - else: - return result - return wrapter - - -def decorator(func): - def wrapter(s, seq): - host_list = s.get('hosts', []) - result = [] - for host in host_list: - s = func(s, seq, host) - if not s: - continue - result.append(s) - if len(result) == 0: - return "" - else: - return result - return wrapter + return wrapter + return decorator -@networkdecorator -def network(s, seq, network=None): +@decorator_mk('networks') +def network(network=None): net_plane = network.get('name', '') network.pop('name') map = {} @@ -73,8 +39,8 @@ def network(s, seq, network=None): return map -@hostdecorator -def interface(s, seq, host=None): +@decorator_mk('hosts') +def interface(host=None): hostname = host.get('name', '') interface = host.get('interface', '') map = {} @@ -82,8 +48,8 @@ def interface(s, seq, host=None): return map -@hostdecorator -def role(s, seq, host=None): +@decorator_mk('hosts') +def role(host=None): hostname = host.get('name', '') role = host.get('roles', '') map = {} @@ -91,8 +57,8 @@ def role(s, seq, host=None): return map -@decorator -def host(s, seq, host=None): +@decorator_mk('hosts') +def host(host=None): hostip = host.get('ip', []) passwd = host.get('password', []) map = {} @@ -101,15 +67,15 @@ def host(s, seq, host=None): def network_config_parse(s, dha_file): - network_map = network(s, ',') + network_map = network(s) vip = s.get('internal_vip') return network_map, vip def dha_config_parse(s, dha_file): - host_interface_map = interface(s, ',') - host_role_map = role(s, ',') - host_ip_passwd_map = host(s, ',') + host_interface_map = interface(s) + host_role_map = role(s) + host_ip_passwd_map = host(s) return host_interface_map, host_role_map, host_ip_passwd_map diff --git a/ci/build_rpm/Dockerfile b/docker/Dockerfile index f10d95e4..f10d95e4 100644 --- a/ci/build_rpm/Dockerfile +++ b/docker/Dockerfile diff --git a/tools/centos-img-modify.sh b/tools/centos-img-modify.sh new file mode 100755 index 00000000..31f122fb --- /dev/null +++ b/tools/centos-img-modify.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +############################################################################## +# Copyright (c) 2016 ZTE Coreporation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +set -x + +echo "r00tme" | passwd --stdin root + +sed -i 's/^SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config + +sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config +sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config + +if [ -z $ipaddr ] || [ -z $gwaddr ]; then + cat << EOF > /etc/sysconfig/network-scripts/ifcfg-ens3 +DEVICE="ens3" +BOOTPROTO="dhcp" +ONBOOT="yes" +TYPE="Ethernet" +USERCTL="yes" +PEERDNS="yes" +IPV6INIT="no" +PERSISTENT_DHCLIENT="1" +EOF + + cat << EOF > /etc/resolv.conf +nameserver 8.8.8.8 +EOF + +else + cat << EOF > /etc/sysconfig/network-scripts/ifcfg-ens3 +DEVICE="ens3" +NAME="ens3" +BOOTPROTO="static" +ONBOOT="yes" +TYPE="Ethernet" +IPADDR="${ipaddr}" +NETMASK="255.255.255.0" +GATEWAY="${gwaddr}" +IPV6INIT="no" +EOF + + cat << EOF > /etc/resolv.conf +nameserver ${gwaddr} +nameserver 8.8.8.8 +EOF + +fi + +host_name=daisy +echo ${host_name} > /etc/hostname +sed -i "/^127.0.0.1/s/ localhost / ${host_name} localhost /g" /etc/hosts + +# Allow console access via pwd +cat << EOF > /etc/cloud/cloud.cfg.d/default.cfg +disable_root: False +ssh_pwauth: True +EOF diff --git a/tools/daisy-img-modify.sh b/tools/daisy-img-modify.sh new file mode 100755 index 00000000..60b60d26 --- /dev/null +++ b/tools/daisy-img-modify.sh @@ -0,0 +1,242 @@ +#!/bin/bash + +############################################################################## +# Copyright (c) 2016 ZTE Coreporation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +# This script refers to the yardstick project. + +usage() +{ + cat << EOF +USAGE: `basename $0` [-c sub-Command] [-a IP Address] [-g Gateway IP address] [-s image Size in GB] + +OPTIONS: + -c sub-command to modify the content + -a IP address for the sub-command to set in the image + -g gateway IP address for the sub-command to set in the image + -s image size of gigabytes. If it is absent, the image size will not be changed. + +EXAMPLE: + sudo `basename $0` -c centos-img-modify.sh -a 10.20.11.2 -g 10.20.11.1 -s 100 +EOF +} + +while getopts "c:a:g:s:h" OPTION +do + case $OPTION in + c) + cmd=${OPTARG} + if [ ! -x $cmd ]; then + echo "The $cmd does not exist or is not executable." + usage + exit 1 + fi + ;; + a) + ipaddr=${OPTARG} + ;; + g) + gwaddr=${OPTARG} + ;; + s) + img_size=${OPTARG} + ;; + h) + usage + exit 0 + ;; + *) + echo "${OPTION} is not a valid argument" + usage + exit 0 + ;; + esac +done + + +set -e +set -x + +die() { + echo "error: $1" >&2 + exit 1 +} + +test $(id -u) -eq 0 || die "You need be root to run this script" + +cmd=${cmd:-$(cd "$(dirname "$0")"; pwd)/centos-img-modify.sh} +test -x $cmd + +if [[ -n $ipaddr ]]; then + export ipaddr=$ipaddr +fi + +if [[ -n $gwaddr ]]; then + export gwaddr=$gwaddr +fi + +mountdir="/mnt/daisy/centos" +workdir=${WORKDIR:-"/tmp/workdir/daisy"} +host=${HOST:-"cloud.centos.org"} +filename=CentOS-7-x86_64-GenericCloud-1608.qcow2 +image_xz_path="centos/7/images/${filename}.xz" +image_xz_url=${IMAGE_XZ_URL:-"http://${host}/${image_xz_path}"} +sha256sum_path="centos/7/images/sha256sum.txt" +sha256sum_url=${SHA256SUM_URL:-"http://${host}/${sha256sum_path}"} +imgfile="${workdir}/centos7.qcow2" +raw_imgfile="${workdir}/centos7.raw" + +# download and checksum base image, conditionally if local copy is outdated +download() { + test -d $workdir || mkdir -p $workdir + cd $workdir + rm -f sha256sum.txt + wget $sha256sum_url + + test -e $filename && grep $filename$ sha256sum.txt | sha256sum -c + if [ $? -ne 0 ]; then + rm -f $filename + rm -f ${filename}.xz + wget -nc --progress=dot:giga $image_xz_url + xz -d -k ${filename}.xz + grep $filename$ sha256sum.txt | sha256sum -c + fi + + for i in $(seq 0 9); do + [[ -e /dev/loop$i ]] || mknod -m 660 /dev/loop$i b 7 $i + done + + qemu-img convert $filename $raw_imgfile + cd - +} + +# install utils +install_utils() +{ + which kpartx || + if [ $? -ne 0 ]; then + yum install -y kpartx + fi + + which growpart || + if [ $? -ne 0 ]; then + yum install -y cloud-utils-growpart + fi + + which xfs_growfs || + if [ $? -ne 0 ]; then + yum install -y xfsprogs + fi +} + +# resize image +resize() { + install_utils + + # resize the image + qemu-img resize $raw_imgfile ${img_size}G + qemu-img info $raw_imgfile + loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ') + kpartx -av $raw_imgfile + sleep 2 + dmsetup ls + fdisk -l /dev/${loopdevice:0:5} || true + growpart /dev/${loopdevice:0:5} 1 + dmsetup clear $loopdevice + kpartx -dv $raw_imgfile +} + +# mount image +setup() { + mkdir -p $mountdir + + loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ') + + kpartx -av $raw_imgfile + sleep 2 + dmsetup ls + fdisk -l /dev/${loopdevice:0:5} || true + mount /dev/mapper/$loopdevice $mountdir + mount -t proc none ${mountdir}/proc + + if [[ -n $img_size ]]; then + xfs_growfs /dev/mapper/$loopdevice + fi + + chroot $mountdir df -h + cp $cmd $mountdir/$(basename $cmd) +} + +# modify image running a script using in a chrooted environment +modify() { + chroot $mountdir /$(basename $cmd) + rm -f $mountdir/$(basename $cmd) + + umount $mountdir/proc + umount $mountdir + + if dmsetup table | grep $loopdevice; then + dmsetup clear $loopdevice || true + fi + + qemu-img convert -c -o compat=0.10 -O qcow2 $raw_imgfile $imgfile +} + +# cleanup (umount) the image +cleanup() { + mount | grep $mountdir/proc && umount $mountdir/proc + mount | grep $mountdir && umount $mountdir + if [ -f $raw_imgfile ]; then + kpartx -dv $raw_imgfile || true + fi + rm -f $raw_imgfile + rm -rf $mountdir +} + +exitcode="" +error_trap() +{ + local rc=$? + + set +e + + if [ -z "$exitcode" ]; then + exitcode=$rc + fi + + dmesg -T | tail -50 + + cleanup + + echo "Image build failed with $exitcode" + + exit $exitcode +} + +main() { + cleanup + + trap "error_trap" EXIT SIGTERM + + download + if [[ -n $img_size ]]; then + echo img_size=$img_size + resize + fi + setup + modify + + trap - EXIT SIGTERM + cleanup + + echo "the modified image is found here: $imgfile" +} + +main + |