diff options
| author | Carlos Konstanski <carlos.konstanski@verizonwireless.com> | 2019-10-30 14:33:05 -0600 |
|---|---|---|
| committer | Carlos Konstanski <carlos.konstanski@verizonwireless.com> | 2019-10-30 14:33:05 -0600 |
| commit | 7cab8996742b8128bed197f9457afd2af160cf5e (patch) | |
| tree | dfd5b40b99e0935ba65b34d973f6e79a6bb39b24 | |
initial commit
22 files changed, 527 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb09eb8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +*swp +README.html diff --git a/bootstrap/.gitignore b/bootstrap/.gitignore new file mode 100644 index 0000000..ecbfae1 --- /dev/null +++ b/bootstrap/.gitignore @@ -0,0 +1,2 @@ +config.yaml +config.yaml.gentoo-basimage diff --git a/bootstrap/config.yaml.example b/bootstrap/config.yaml.example new file mode 100644 index 0000000..e7d8996 --- /dev/null +++ b/bootstrap/config.yaml.example @@ -0,0 +1,4 @@ +registry: + url: ~ + username: ~ + password: ~ diff --git a/bootstrap/pillar_config.rb b/bootstrap/pillar_config.rb new file mode 100755 index 0000000..44fa02c --- /dev/null +++ b/bootstrap/pillar_config.rb @@ -0,0 +1,60 @@ +#!/usr/bin/ruby + +require "digest/sha1" +require "digest" +require "getoptlong" +require "json" +require "socket" +require "yaml" + +def sha() + seed = "" + File.open("/dev/urandom", "r") do |f| + seed = "#{f.sysread(24)}" + end + Digest::SHA1.hexdigest(seed) +end + +def process_yaml(build_basedir, local_build) + env_yaml_path = "#{File.dirname($0)}/config.yaml" + if ! File.file?(env_yaml_path) + STDERR.puts "Unable to find config.yaml. Exiting." + exit(1) + end + tag = sha()[0..23] + yml = YAML.load_file(env_yaml_path) + yml["gentoo_baseimage"] = {} + yml["gentoo_baseimage"]["image_name"] = "gentoo_baseimage" + yml["gentoo_baseimage"]["container_name"] = "gentoo_baseimage" + yml["gentoo_baseimage"]["build_dir"] = "#{build_basedir}/gentoo_baseimage" + yml["ansible"] = {} + yml["ansible"]["build_dir"] = "#{build_basedir}/ansible" + yml["ci"] = {} + yml["ci"]["build_dir"] = "#{build_basedir}/ci" + yml["ci"]["tag"] = tag + yml["ci"]["local_build"] = YAML.load(local_build) + yml +end + +def main() + build_basedir = nil + local_build = "false" + opts = GetoptLong.new(["--build-basedir", "-d", GetoptLong::REQUIRED_ARGUMENT], + ["--local-build", "-l", GetoptLong::REQUIRED_ARGUMENT]) + opts.each do |opt, arg| + case opt + when "--build-basedir" + build_basedir = arg + when "--local-build" + local_build = arg + end + end + if ! build_basedir + raise "--build-basedir must be supplied." + end + yml = process_yaml(build_basedir, local_build) + puts yml.to_json +end + +main() +exit(0) diff --git a/bootstrap/run_salt.sh b/bootstrap/run_salt.sh new file mode 100755 index 0000000..47a9a4f --- /dev/null +++ b/bootstrap/run_salt.sh @@ -0,0 +1,24 @@ +#!/bin/bash -e + +working_dir="${1}" +build_basedir="${2}" +local_build="${3}" +python=$(which python2 || false) + +salt_running_p() { + ps -ef | grep -F "salt-call" | grep -v "grep" + return ${?} +} + +while salt_running_p; do + sleep 5 +done + +${python} $(which salt-call) \ + --local \ + --pillar-root=${working_dir}/pillar \ + --file-root=${working_dir}/salt \ + state.highstate \ + pillar="$($(dirname ${0})/pillar_config.rb --build-basedir=${build_basedir} --local-build=${local_build})" + +exit 0 diff --git a/build-docker-containers.sh b/build-docker-containers.sh new file mode 100755 index 0000000..5c5038f --- /dev/null +++ b/build-docker-containers.sh @@ -0,0 +1,19 @@ +#!/bin/bash -e + +local_build="${1}" +working_dir="$(pwd)" +builddir="/tmp/gentoo_baseimage_build" + +# clean the build files +sudo rm -rf ${builddir} + +# configure salt +cp ${HOME}/config.yaml.gentoo-baseimage ${working_dir}/bootstrap/config.yaml + +# run salt to create build scripts +sudo ${working_dir}/bootstrap/run_salt.sh ${working_dir} ${builddir} ${local_build} + +# build new docker container +sudo ${builddir}/ci/build.sh + +exit 0 diff --git a/pillar/gentoo_baseimage/ci.sls b/pillar/gentoo_baseimage/ci.sls new file mode 100644 index 0000000..2a01969 --- /dev/null +++ b/pillar/gentoo_baseimage/ci.sls @@ -0,0 +1,21 @@ +ci: + dirs: + data: + name: data + mode: "0755" + relative_to_buildroot: True + files: + build_sh: + name: build.sh + path: . + source: build.sh + mode: "0755" + is_template: True + relative_to_buildroot: True + cloudcfg_sh: + name: cloudcfg.sh + path: . + source: cloudcfg.sh + mode: "0755" + is_template: False + relative_to_buildroot: True diff --git a/pillar/gentoo_baseimage/gentoo_baseimage.sls b/pillar/gentoo_baseimage/gentoo_baseimage.sls new file mode 100644 index 0000000..f50b4d1 --- /dev/null +++ b/pillar/gentoo_baseimage/gentoo_baseimage.sls @@ -0,0 +1,61 @@ +gentoo_baseimage: + dirs: + data: + name: data + mode: "0755" + relative_to_buildroot: True + data_root: + name: data/root + mode: "0755" + relative_to_buildroot: True + data_etc_portage_package_use: + name: data/etc/portage/package.use + mode: "0755" + relative_to_buildroot: True + data_etc_portage_repos_conf: + name: data/etc/portage/repos.conf + mode: "0755" + relative_to_buildroot: True + files: + build_sh: + name: build.sh + path: . + source: build.sh + mode: "0755" + is_template: True + relative_to_buildroot: True + dockerfile: + name: Dockerfile + path: . + source: Dockerfile + mode: "0644" + is_template: True + relative_to_buildroot: True + installdeps: + name: installdeps.sh + path: data + source: installdeps.sh + mode: "0755" + is_template: True + relative_to_buildroot: True + make_conf: + name: make.conf + path: data/etc/portage + source: make.conf + mode: "0644" + is_template: True + relative_to_buildroot: True + package_use: + name: x86_64-pc-linux-gnu + path: data/etc/portage/package.use + source: package.use + mode: "0644" + is_template: True + relative_to_buildroot: True + gentoo_conf: + name: gentoo.conf + path: data/etc/portage/repos.conf + source: gentoo.conf + mode: "0644" + is_template: True + relative_to_buildroot: True diff --git a/pillar/top.sls b/pillar/top.sls new file mode 100644 index 0000000..27db632 --- /dev/null +++ b/pillar/top.sls @@ -0,0 +1,4 @@ +base: + "*": + - gentoo_baseimage.ci + - gentoo_baseimage.gentoo_baseimage diff --git a/salt/_modules/logger_mod.py b/salt/_modules/logger_mod.py new file mode 100644 index 0000000..708483c --- /dev/null +++ b/salt/_modules/logger_mod.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +''' +Execution module to provide logging +========================== +:maintainer: Joe Julian <me@joejulian.name> +:maturity: new +:platform: Linux +.. versionadded:: 2014.7.0 +''' + +__virtualname__ = 'logger' + +import logging as log +from pprint import pformat +import json +import yaml + +def pretty(var): + return pformat(yaml.load(json.dumps(var))) + +def __virtual__(): + return __virtualname__ + +def debug(var, string = ""): + ''' + Print a var to a debug output. + ''' + ret = string + pretty(var) + log.debug(ret) + return ret + +def info(var, string = ""): + ''' + Print a var to a info output. + ''' + ret = string + pretty(var) + log.info(ret) + return ret + +def warning(var, string = ""): + ''' + Print a var to a warning output. + ''' + ret = string + pretty(var) + log.warning(ret) + return ret + +def error(var, string = ""): + ''' + Print a var to a error output. + ''' + ret = string + pretty(var) + log.error(ret) + return ret + +def critical(var, string = ""): + ''' + Print a var to a critical output. + ''' + ret = string + pretty(var) + log.critical(ret) + return ret diff --git a/salt/_states/logging.py b/salt/_states/logging.py new file mode 100644 index 0000000..35856cc --- /dev/null +++ b/salt/_states/logging.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +''' +Logging output +========================== +:maintainer: Joe Julian <me@joejulian.name> +:maturity: new +:platform: Linux +.. versionadded:: 2014.7.0 +:configuration: See :py:mod:`salt.modules.logging` for setup instructions. +.. code-block:: yaml + debugmyvar: + logger.debug: + - name: myvar + - string: "myvar is :" +''' + +__virtual_name__ = 'logging' + +def __virtual__(): + return __virtual_name__ + +def debug(name, obj, string = ''): + ret = {'name': name, + 'changes': [], + 'result': True, + 'comment': __salt__['logger.debug'](obj, string)} + return ret + +def info(name, obj, string = ''): + ret = {'name': name, + 'changes': [], + 'result': True, + 'comment': __salt__['logger.info'](obj, string)} + return ret + +def warning(name, obj, string = ''): + ret = {'name': name, + 'changes': [], + 'result': True, + 'comment': __salt__['logger.warning'](obj, string)} + return ret + +def error(name, obj, string = ''): + ret = {'name': name, + 'changes': [], + 'result': True, + 'comment': __salt__['logger.error'](obj, string)} + return ret + +def critical(name, obj, string = ''): + ret = {'name': name, + 'changes': [], + 'result': True, + 'comment': __salt__['logger.critical'](obj, string)} + return ret diff --git a/salt/gentoo_baseimage/ci/files/build.sh b/salt/gentoo_baseimage/ci/files/build.sh new file mode 100644 index 0000000..b42f01b --- /dev/null +++ b/salt/gentoo_baseimage/ci/files/build.sh @@ -0,0 +1,23 @@ +#!/bin/bash -e + +mark_for_gc() { + image_name="${1}" + for tag in $(curl -u '{{ pillar["registry"]["username"] }}:{{ pillar["registry"]["password"] }}' -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' https://{{ pillar["registry"]["url"] }}/v2/${image_name}/tags/list | jq '.tags' | grep -o -P '[a-fA-F0-9]+'); do + digest=$(curl -u '{{ pillar["registry"]["username"] }}:{{ pillar["registry"]["password"] }}' -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' https://{{ pillar["registry"]["url"] }}/v2/${image_name}/manifests/${tag} | jq '.config.digest' | awk -F\" '{print $2}') + if [ "${digest}" != "" ]; then + curl -u '{{ pillar["registry"]["username"] }}:{{ pillar["registry"]["password"] }}' -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -X DELETE https://{{ pillar["registry"]["url"] }}/v2/${image_name}/manifests/${digest} + fi + done +} + +{% if pillar["ci"]["local_build"] != True %} +echo "Login to docker registry" +echo '{{ pillar["registry"]["password"] }}' | docker login --username {{ pillar["registry"]["username"] }} --password-stdin {{ pillar["registry"]["url"] }} +{% endif %} + +{% if pillar["ci"]["local_build"] != True %} +mark_for_gc "{{ pillar["gentoo_baseimage"]["image_name"] }}" +{% endif %} +{{ pillar["gentoo_baseimage"]["build_dir"] }}/build.sh + +exit 0 diff --git a/salt/gentoo_baseimage/ci/files/cloudcfg.sh b/salt/gentoo_baseimage/ci/files/cloudcfg.sh new file mode 100644 index 0000000..400f7cf --- /dev/null +++ b/salt/gentoo_baseimage/ci/files/cloudcfg.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Prepare the instance for ansible. This amounts to installing python2. + +python=$(which python || which python3 || which python2 || false) + +case "$(${python} -m platform)" in + *Ubuntu*) + export DEBIAN_FRONTEND=noninteractive + apt-get update + apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade + apt-get -y install aptitude python python2.7 python3-netaddr ntp ntpdate language-pack-de + update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 + update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1 + update-alternatives --set python /usr/bin/python3.6 + ;; + *centos*|*redhat*) + # RHEL systems still use python2 by default; nothing to do + ;; + *gentoo*) + if [ ! -d /usr/portage/sys-devel/gcc ]; then + emerge-webrsync + fi + emerge --oneshot --update dev-lang/python:2.7 app-portage/gentoolkit net-misc/chrony + rc-update add chrony default + ;; +esac + +exit 0 diff --git a/salt/gentoo_baseimage/ci/init.sls b/salt/gentoo_baseimage/ci/init.sls new file mode 100644 index 0000000..510af92 --- /dev/null +++ b/salt/gentoo_baseimage/ci/init.sls @@ -0,0 +1,30 @@ +{% for dir in pillar["ci"]["dirs"].items() %} +database_dirs_{{ dir[0] }}: + file.directory: +{% if dir[1]["relative_to_buildroot"] %} + - name: {{ pillar["ci"]["build_dir"] }}/{{ dir[1]["name"] }} +{% else %} + - name: {{ dir[1]["name"] }} +{% endif %} + - user: 0 + - group: 0 + - mode: {{ dir[1]["mode"] }} + - makedirs: True +{% endfor %} + +{% for file in pillar["ci"]["files"].items() %} +ci_files_{{ file[0] }}: + file.managed: +{% if file[1]["relative_to_buildroot"] %} + - name: {{ pillar["ci"]["build_dir"] }}/{{ file[1]["path"] }}/{{ file[1]["name"] }} +{% else %} + - name: {{ file[1]["path"] }}/{{ file[1]["name"] }} +{% endif %} + - source: salt://gentoo_baseimage/ci/files/{{ file[1]["source"] }} +{% if file[1]["is_template"] %} + - template: jinja +{% endif %} + - user: 0 + - group: 0 + - mode: {{ file[1]["mode"] }} +{% endfor %} diff --git a/salt/gentoo_baseimage/gentoo_baseimage/files/Dockerfile b/salt/gentoo_baseimage/gentoo_baseimage/files/Dockerfile new file mode 100644 index 0000000..67c9f23 --- /dev/null +++ b/salt/gentoo_baseimage/gentoo_baseimage/files/Dockerfile @@ -0,0 +1,6 @@ +FROM gentoo/stage3-amd64 +MAINTAINER Carlos Konstanski <carlos.konstanski@verizonwireless.com> +ADD docker-start.tar.xz / +RUN /installdeps.sh +ENTRYPOINT [ "/bin/bash" ] +CMD [] diff --git a/salt/gentoo_baseimage/gentoo_baseimage/files/build.sh b/salt/gentoo_baseimage/gentoo_baseimage/files/build.sh new file mode 100644 index 0000000..beffb0c --- /dev/null +++ b/salt/gentoo_baseimage/gentoo_baseimage/files/build.sh @@ -0,0 +1,46 @@ +#!/bin/bash -e + +# Do not call directly. Meant to be called from CI build script. + +pushd {{ pillar["gentoo_baseimage"]["build_dir"] }} 2>/dev/null + +echo "Creating tmp directory" +rm -rf tmp +mkdir tmp +rm -f docker-start.tar.xz + +echo "Populating with custom files" +rsync -aq data/ tmp/ + +echo "Repackaging into .xz file" +tar Jcvf docker-start.tar.xz -C tmp/ . + +echo "Stopping existing docker containers" +for cont in $(docker ps | grep -F '{{ pillar["gentoo_baseimage"]["container_name"] }}' | awk '{print $1}'); do + docker stop ${cont} +done +docker container prune -f + +echo "Deleting existing docker images" +for image in $(docker images | grep -F '{{ pillar["gentoo_baseimage"]["image_name"] }}' | awk '{print $3}'); do + docker rmi --force ${image} +done +for image in $(docker images | grep -F 'gentoo' | awk '{print $3}'); do + docker rmi --force ${image} +done + +echo "Building docker image" +docker build -t {{ pillar["gentoo_baseimage"]["image_name"] }}:{{ pillar["ci"]["tag"] }} . + +{% if pillar["ci"]["local_build"] != True %} +echo "Push docker image to the registry" +docker tag {{ pillar["gentoo_baseimage"]["image_name"] }}:{{ pillar["ci"]["tag"] }} {{ pillar["registry"]["url"] }}/{{ pillar["gentoo_baseimage"]["image_name"] }}:{{ pillar["ci"]["tag"] }} +docker push {{ pillar["registry"]["url"] }}/{{ pillar["gentoo_baseimage"]["image_name"] }}:{{ pillar["ci"]["tag"] }} +{% endif %} + +echo "Cleaning up" +rm -rf tmp +rm -f docker-start.tar.xz + +popd 2>/dev/null +exit 0 diff --git a/salt/gentoo_baseimage/gentoo_baseimage/files/gentoo.conf b/salt/gentoo_baseimage/gentoo_baseimage/files/gentoo.conf new file mode 100644 index 0000000..7e42017 --- /dev/null +++ b/salt/gentoo_baseimage/gentoo_baseimage/files/gentoo.conf @@ -0,0 +1,8 @@ +[DEFAULT] +main-repo = gentoo + +[gentoo] +location = /usr/portage +sync-type = webrsync +auto-sync = yes +sync-rsync-verify-metamanifest = no diff --git a/salt/gentoo_baseimage/gentoo_baseimage/files/installdeps.sh b/salt/gentoo_baseimage/gentoo_baseimage/files/installdeps.sh new file mode 100644 index 0000000..e154656 --- /dev/null +++ b/salt/gentoo_baseimage/gentoo_baseimage/files/installdeps.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +emaint sync -a +eselect profile set default/linux/amd64/17.1 +emerge -1 portage ; emerge --metadata +USE="-filecaps" emerge --deep --newuse --update @world +emerge --deep --newuse --update @world +emerge gentoolkit +revdep-rebuild +perl-cleaner --all +env-update +source /etc/profile +eselect news read all +eselect news purge + +exit 0 diff --git a/salt/gentoo_baseimage/gentoo_baseimage/files/make.conf b/salt/gentoo_baseimage/gentoo_baseimage/files/make.conf new file mode 100644 index 0000000..de50ae9 --- /dev/null +++ b/salt/gentoo_baseimage/gentoo_baseimage/files/make.conf @@ -0,0 +1,18 @@ +CHOST="x86_64-pc-linux-gnu" +CFLAGS="-O2 -pipe" +CXXFLAGS="${CFLAGS}" +FCFLAGS="${CFLAGS}" +FFLAGS="${CFLAGS}" +USE="-X -webstart -alsa -cups -gtk -vala -introspection" +ABI_X86="64 32" +EMERGE_DEFAULT_OPTS="--quiet-build=y --with-bdeps=y --nospinner --verbose-conflicts --changed-deps-report=n --keep-going=y" +FEATURES="-preserve-libs" +ACCEPT_LICENSE="* -@EULA" +MAKEOPTS="-j{{ grains["num_cpus"] }}" +PORTAGE_NICENESS="19" +FEATURES="-preserve-libs metadata-transfer -sandbox -network-sandbox -usersandbox" +PORTDIR="/usr/portage" +DISTDIR="/usr/portage/distfiles" +PKGDIR="/usr/portage/packages" +PORTAGE_ELOG_SYSTEM="save" +PORTAGE_GPG_DIR="/root/.gnupg" diff --git a/salt/gentoo_baseimage/gentoo_baseimage/files/package.use b/salt/gentoo_baseimage/gentoo_baseimage/files/package.use new file mode 100644 index 0000000..fc40a05 --- /dev/null +++ b/salt/gentoo_baseimage/gentoo_baseimage/files/package.use @@ -0,0 +1,2 @@ +x11-libs/cairo X +app-text/ghostscript-gpl cups diff --git a/salt/gentoo_baseimage/gentoo_baseimage/init.sls b/salt/gentoo_baseimage/gentoo_baseimage/init.sls new file mode 100644 index 0000000..b312018 --- /dev/null +++ b/salt/gentoo_baseimage/gentoo_baseimage/init.sls @@ -0,0 +1,30 @@ +{% for dir in pillar["gentoo_baseimage"]["dirs"].items() %} +gentoo_baseimage_dirs_{{ dir[0] }}: + file.directory: +{% if dir[1]["relative_to_buildroot"] %} + - name: {{ pillar["gentoo_baseimage"]["build_dir"] }}/{{ dir[1]["name"] }} +{% else %} + - name: {{ dir[1]["name"] }} +{% endif %} + - user: 0 + - group: 0 + - mode: {{ dir[1]["mode"] }} + - makedirs: True +{% endfor %} + +{% for file in pillar["gentoo_baseimage"]["files"].items() %} +gentoo_baseimage_files_{{ file[0] }}: + file.managed: +{% if file[1]["relative_to_buildroot"] %} + - name: {{ pillar["gentoo_baseimage"]["build_dir"] }}/{{ file[1]["path"] }}/{{ file[1]["name"] }} +{% else %} + - name: {{ file[1]["path"] }}/{{ file[1]["name"] }} +{% endif %} + - source: salt://gentoo_baseimage/gentoo_baseimage/files/{{ file[1]["source"] }} +{% if file[1]["is_template"] %} + - template: jinja +{% endif %} + - user: 0 + - group: 0 + - mode: {{ file[1]["mode"] }} +{% endfor %} diff --git a/salt/top.sls b/salt/top.sls new file mode 100644 index 0000000..27db632 --- /dev/null +++ b/salt/top.sls @@ -0,0 +1,4 @@ +base: + "*": + - gentoo_baseimage.ci + - gentoo_baseimage.gentoo_baseimage |
