From 76b5673b1c9ec576a7415945430b6748b65cac35 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Mon, 8 Oct 2018 11:24:40 +0100
Subject: [PATCH] Setup and remove ci builds

---
 src/freedombone-ci | 191 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 191 insertions(+)
 create mode 100755 src/freedombone-ci

diff --git a/src/freedombone-ci b/src/freedombone-ci
new file mode 100755
index 000000000..9061d3869
--- /dev/null
+++ b/src/freedombone-ci
@@ -0,0 +1,191 @@
+#!/bin/bash
+#  _____               _           _
+# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
+# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
+# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
+#
+#                              Freedom in the Cloud
+#
+# Script used to run builds for continuous integration
+#
+# License
+# =======
+#
+# Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+PROJECT_NAME='freedombone'
+INSTALL_DIR=/root/build
+
+target="$1"
+web_dir=/var/www/html
+build_info=
+if [ "$2" ]; then
+    web_dir="$2"
+    build_info="$3"
+fi
+
+function remove_ci_daemon {
+    ci_target="$1"
+    ci_build_info="$2"
+
+    ci_extra=
+    if [ "$ci_build_info" ]; then
+        ci_extra="-${ci_build_info}"
+    fi
+    birbci --remove "${PROJECT_NAME}-${ci_target}${ci_extra}"
+}
+
+function create_ci_daemon {
+    ci_target="$1"
+    ci_build_info="$2"
+
+    ci_extra=
+    if [ "$ci_build_info" ]; then
+        ci_extra="-${ci_build_info}"
+    fi
+    script_filename="/etc/birbci/${PROJECT_NAME}-${ci_target}${ci_extra}.sh"
+    install_name="${PROJECT_NAME}-${ci_target}${ci_extra}"
+
+    remove_ci_daemon "${ci_target}" "${ci_build_info}"
+
+    { echo '#!/bin/bash';
+      echo "/usr/local/bin/${PROJECT_NAME}-ci ${ci_target} ${web_dir} ${ci_build_info}"; } > "/etc/birbci/${PROJECT_NAME}-${ci_target}.sh"
+    birbci --repo https://code.freedombone.net/bashrc/freedombone --branch stretch --script "${script_filename}" -w "${web_dir}/index.html" --root yes --synchronous yes --install "${install_name}"
+}
+
+if [ ! "$1" ]; then
+    exit 1
+fi
+
+if [[ "$1" == 'remove'* ]]; then
+    arch=$(uname -a)
+    if [[ "$arch" == *'x86'* || "$arch" == *'amd64'* ]]; then
+        remove_ci_daemon amd64
+        remove_ci_daemon amd64 onion
+        remove_ci_daemon i386 meshclient
+    fi
+
+    remove_ci_daemon cubieboard2
+    remove_ci_daemon cubieboard2 onion
+    remove_ci_daemon a20-olinuxino-lime2
+    remove_ci_daemon a20-olinuxino-lime2 onion
+    remove_ci_daemon banana-pro
+    remove_ci_daemon banana-pro onion
+    remove_ci_daemon beagleboneblack
+    remove_ci_daemon beagleboneblack onion
+    remove_ci_daemon beagleboneblack mesh
+    remove_ci_daemon cubietruck
+    remove_ci_daemon cubietruck onion
+fi
+
+if [[ "$1" == 'setup'* ]]; then
+    # install birbci
+    if [ -d /etc/birbci ]; then
+        rm -rf /etc/birbci
+    fi
+    if [ -d "$INSTALL_DIR" ]; then
+        rm -rf "$INSTALL_DIR"
+    fi
+    mkdir /etc/birbci
+    mkdir -p "$INSTALL_DIR"
+    git clone https://code.freedombone.net/bashrc/birbci "$INSTALL_DIR/birbci"
+    cd "$INSTALL_DIR/birbci" || exit 2
+    make install
+
+    # setup the web server
+    { echo '    server {';
+      echo '  listen 80 default_server;';
+      echo '  listen [::]:80 default_server;';
+      echo '';
+      echo '  root /var/www/html;';
+      echo '  server_name _;';
+      echo '';
+      echo '  index index.html;';
+      echo '';
+      echo '  location / {';
+      echo "    try_files \$uri \$uri/ =404;";
+      echo '  }';
+      echo '';
+      echo '  location /downloads {';
+      echo '      autoindex on;';
+      echo '  }';
+      echo '';
+      echo '  location ^~ /.well-known/ {';
+      echo '      allow all;';
+      echo '  }';
+      echo '}'; } > /etc/nginx/sites-available/default
+    exit 0
+fi
+
+# Now add some builds
+if [[ "$1" == 'setuptest' ]]; then
+    create_ci_daemon a20-olinuxino-lime2
+fi
+
+if [[ "$1" == 'setup' ]]; then
+    arch=$(uname -a)
+    if [[ "$arch" == *'x86'* || "$arch" == *'amd64'* ]]; then
+        create_ci_daemon amd64
+        create_ci_daemon amd64 onion
+        create_ci_daemon i386 meshclient
+    fi
+
+    create_ci_daemon cubieboard2
+    create_ci_daemon cubieboard2 onion
+    create_ci_daemon a20-olinuxino-lime2
+    create_ci_daemon a20-olinuxino-lime2 onion
+    create_ci_daemon banana-pro
+    create_ci_daemon banana-pro onion
+    create_ci_daemon beagleboneblack
+    create_ci_daemon beagleboneblack onion
+    create_ci_daemon beagleboneblack mesh
+    create_ci_daemon cubietruck
+    create_ci_daemon cubietruck onion
+fi
+
+extra='--sata sda2'
+if [[ "$target" == 'beaglebone'* || "$target" == 'amd'* || "$target" == 'i386'* || "$target" == 'x86'* ]]; then
+    extra=''
+fi
+targetstr="${target}"
+if [ "$build_info" ]; then
+    targetstr="${target}-${build_info}"
+fi
+downloads_directory=${web_dir}/downloads/${targetstr}
+build_directory=/etc/birbci/${PROJECT_NAME}-${targetstr}-build
+if [ -d "${build_directory}" ]; then
+    rm -rf "${build_directory}"
+fi
+mkdir -p "${build_directory}"
+if [ ! -d "${downloads_directory}" ]; then
+    mkdir -p "${downloads_directory}"
+fi
+cd "${build_directory}" || exit 1
+if [ ! "$build_info" ]; then
+    /usr/local/bin/${PROJECT_NAME}-image -t "${target}" ${extra} --ci yes
+else
+    if [[ "$build_info" == 'onion' ]]; then
+        /usr/local/bin/${PROJECT_NAME}-image -t "${target}" ${extra} --onion yes --ci yes
+    fi
+    if [[ "$build_info" == 'mesh'* ]]; then
+        /usr/local/bin/${PROJECT_NAME}-image -t "${target}" -v "${build_info}" ${extra} --ci yes
+    fi
+fi
+xz --no-warn --verbose --keep --threads=3 -3 "${build_directory}"/*.img
+mv "${build_directory}"/*.xz "${downloads_directory}"
+rm "${build_directory}"/*.img
+chown -R www-data:www-data "${downloads_directory}"
+exit 0
-- 
GitLab