img(coreos): Initial commit of CoreOS image build

- Mostly imported from github.com/racker/teeth-agent-image
This commit is contained in:
Jay Faulkner 2014-03-04 14:47:48 -08:00
commit c5d2b5282f
8 changed files with 379 additions and 0 deletions

4
README.md Normal file
View File

@ -0,0 +1,4 @@
teeth-agent images
==================
coreos - Builds a CoreOS Ramdisk and Kernel suitable for running teeth-agent

17
coreos/Makefile Normal file
View File

@ -0,0 +1,17 @@
default: docker coreos
docker:
./docker_build.bash
coreos:
mkdir UPLOAD
./coreos-oem-inject.py oem UPLOAD
clean:
rm -rf teeth-agent
rm -f oem/container.tar.gz
rm -f UPLOAD/coreos_production_pxe_image-oem.cpio.gz
rm -f UPLOAD/coreos_production_pxe.vmlinuz
docker_clean:
./docker_clean.bash

33
coreos/README.md Normal file
View File

@ -0,0 +1,33 @@
# teeth-agent CoreOS Image builder.
Builds a CoreOS image suitable for running the teeth-agent on a server.
# Requirements
Must be run from a linux machine with a working docker installation and python-pip
Run the following locally or from a virtualenv to install the python requirements
```
pip install -r requirements.txt
```
# Instructions
To create a docker repository and embed it into a CoreOS pxe image:
```
make
```
To just create the docker repository in oem/container.tar.gz:
```
make docker
```
To embed the oem/ directory into a CoreOS pxe image:
Note: In order to have the ability to ssh into the created image, you need to
pass ssh keys in via the kernel command line for CoreOS, or create
oem/authorized_keys with the keys you need added before building the image.
```
make coreos
```

246
coreos/coreos-oem-inject.py Executable file
View File

@ -0,0 +1,246 @@
#!/usr/bin/env python
import os
import sys
import time
import requests
import tempfile
import shutil
from plumbum import local, cmd
COREOS_VERSION="197.0.0"
COREOS_ARCH="amd64-generic"
COREOS_BASE_URL="http://storage.core-os.net/coreos/{}/{}".format(COREOS_ARCH, COREOS_VERSION)
COREOS_PXE_DIGESTS="coreos_production_pxe_image.cpio.gz.DIGESTS.asc"
COREOS_PXE_KERNEL="coreos_production_pxe.vmlinuz"
COREOS_PXE_IMAGE="coreos_production_pxe_image.cpio.gz"
COREOS_PXE_IMAGE_URL = "{}/{}".format(COREOS_BASE_URL, COREOS_PXE_IMAGE)
COREOS_PXE_KERNEL_URL = "{}/{}".format(COREOS_BASE_URL, COREOS_PXE_KERNEL)
COREOS_PXE_DIGESTS_URL = "{}/{}".format(COREOS_BASE_URL, COREOS_PXE_DIGESTS)
def get_etag(cache_name):
etag_file = "{}.etag".format(cache_name)
if not os.path.exists(etag_file):
return None
with open(etag_file, 'rb') as fp:
etag = fp.read()
etag.strip()
return etag
def save_etag(cache_name, etag):
etag_file = "{}.etag".format(cache_name)
with open(etag_file, 'w+b') as fp:
fp.write(etag)
def cache_file(cache_name, remote_url):
print("{} <- {}".format(cache_name, remote_url))
etag = get_etag(cache_name)
headers = {}
if etag:
headers['If-None-Match'] = etag
start = time.time()
r = requests.get(remote_url, headers=headers)
if r.status_code == 304:
print("[etag-match]")
return
if r.status_code != 200:
raise RuntimeError('Failed to download {}, got HTTP {} Status Code.'.format(remote_url, r.status_code))
with open(cache_name, 'w+b') as fp:
fp.write(r.content)
print("{} bytes in {} seconds".format(len(r.content), time.time() - start))
save_etag(cache_name, r.headers['etag'])
def inject_oem(archive, oem_dir, output_file):
d = tempfile.mkdtemp(prefix="oem-inject")
try:
with local.cwd(d):
dest_oem_dir = os.path.join(d, 'usr', 'share', 'oem')
uz = cmd.gunzip["-c", archive]
extract = cmd.cpio["-iv"]
chain = uz | extract
print chain
chain()
shutil.copytree(oem_dir, dest_oem_dir)
find = cmd.find['.', '-depth', '-print']
cpio = cmd.cpio['-o', '-H', 'newc']
gz = cmd.gzip
chain = find | cmd.sort | cpio | gz > output_file
print chain
chain()
finally:
shutil.rmtree(d)
return output_file
def validate_digests(digests, target, hash_type='sha1'):
with local.cwd(os.path.dirname(digests)):
gethashes = cmd.grep['-i', '-A1', '^# {} HASH$'.format(hash_type), digests]
forthis = cmd.grep[os.path.basename(target)]
viasum = local[hash_type + "sum"]['-c', '/dev/stdin']
chain = gethashes | forthis | viasum
print chain
chain()
def main():
if len(sys.argv) != 3:
print("usage: {} [oem-directory-to-inject] [output-directory]".format(os.path.basename(__file__)))
return
oem_dir = os.path.abspath(os.path.expanduser(sys.argv[1]))
output_dir = os.path.abspath(os.path.expanduser(sys.argv[2]))
if not os.path.exists(oem_dir):
print("Error: {} doesn't exist.".format(oem_dir))
return
if not os.path.exists(os.path.join(oem_dir, 'run.sh')):
print("Error: {} is missing oem.sh".format(oem_dir))
return
here = os.path.abspath(os.path.dirname(__file__))
top_cache_dir = os.path.join(os.path.dirname(here), ".image_cache")
cache_dir = os.path.join(top_cache_dir, COREOS_ARCH, COREOS_VERSION)
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
orig_cpio = os.path.join(cache_dir, COREOS_PXE_IMAGE)
digests = os.path.join(cache_dir, COREOS_PXE_DIGESTS)
kernel = os.path.join(cache_dir, COREOS_PXE_KERNEL)
cache_file(digests, COREOS_PXE_DIGESTS_URL)
gpg_verify_file(digests)
cache_file(kernel, COREOS_PXE_KERNEL_URL)
validate_digests(digests, kernel)
cache_file(orig_cpio, COREOS_PXE_IMAGE_URL)
validate_digests(digests, orig_cpio)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_kernel = os.path.join(output_dir, os.path.basename(kernel))
output_cpio = os.path.join(output_dir, os.path.basename(orig_cpio).replace('.cpio.gz', '-oem.cpio.gz'))
inject_oem(orig_cpio, oem_dir, output_cpio)
shutil.copy(kernel, output_kernel)
def gpg_verify_file(ascfile):
d = tempfile.mkdtemp(prefix="oem-gpg-validate")
try:
tmpring = os.path.join(d, 'tmp.gpg')
key = os.path.join(d, 'coreos.key')
with open(key, 'w+b') as fp:
fp.write(gpg_key())
i = cmd.gpg['--batch',
'--no-default-keyring',
'--keyring',
tmpring,
'--import',
key]
print(i)
i()
r = cmd.gpg['--batch',
'--no-default-keyring',
'--keyring',
tmpring,
'--verify',
ascfile]
print(r)
r()
finally:
shutil.rmtree(d)
def gpg_key():
GPG_LONG_ID="50E0885593D2DCB4"
GPG_KEY="""-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v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5nMVbn3FxIemTYEbUFUbqa0eB3GRFoDz4iBGR4NqwIboP317S27NLDY
J8L6KmXTyNh8/Cm2l7wKlkwi3ItBGoAT+j3cOG988+3slgM9vXMaQRRQv9O1aTs1
ZAai+Jq7AGjGh4ZkuG0cDZ2DuBy22XsUNboxQeHbQTsAPzQfvi+fQByUi6TzxiW0
BeiJ6tEeDHDzdA==
=4Qn0
-----END PGP PUBLIC KEY BLOCK-----
"""
return GPG_KEY
if __name__ == "__main__":
main()

26
coreos/docker_build.bash Executable file
View File

@ -0,0 +1,26 @@
#!/bin/bash
#
# docker_build.bash - Prepares and outputs a tarball'd docker repository
# suitable for injection into a coreos pxe image
#
set -e
OUTPUT_FILE="oem/container.tar.gz"
# If there's already a container.tar.gz, don't overwrite it -- instead, bail
if [[ -e "${OUTPUT_FILE}" ]]; then
echo "${OUTPUT_FILE} already exists. Will not overwrite. Exiting."
exit 1
fi
# Build the docker image
cd ../../teeth-agent
docker build -t oemdocker .
cd -
# Export the oemdocker repository to a tarball so it can be embedded in CoreOS
# TODO: Investigate running a container and using "export" to flatten the
# image to shrink the CoreOS fs size. This will also require run.sh to
# use docker import instead of docker load as well.
docker save oemdocker | gzip > ${OUTPUT_FILE}

16
coreos/docker_clean.bash Executable file
View File

@ -0,0 +1,16 @@
#!/bin/bash
#
# Cleans up docker images and containers
containers=$(docker ps -a -q)
images=$(docker images -q)
# All the docker commands followed by || true because occassionally docker
# will fail to remove an image or container, & I want make to keep going anyway
if [[ ! -z "$containers" ]]; then
docker rm $containers || true
fi
if [[ ! -z "$images" ]]; then
docker rmi $images || true
fi

31
coreos/oem/run.sh Executable file
View File

@ -0,0 +1,31 @@
#!/bin/bash
set -e
# CoreOS by default only has an OEM partition of 2GB. This isn't large enough
# for some images. Remount it with a larger size. Note: When CoreOS changes to
# r/w /, instead of remounting here, we'll use rootflags= to set the size.
mount -o remount,size=20G /media/state
cd /usr/share/oem/
mkdir -pm 0700 /home/core/.ssh
# TODO: Use proper https://github.com/coreos/init/blob/master/bin/update-ssh-keys script
if [[ -e authorized_keys ]]; then
cat authorized_keys >> /home/core/.ssh/authorized_keys
fi
chown -R core:core /home/core/.ssh/
# We have to wait until docker is started to proceed
# In a perfect world I'd use inotifywait, but that doesn't exist on coreos
while [ ! -e /var/run/docker.sock ]; do
sleep 1;
done
# TODO: Use docker import (and export the image) to shrink image size
docker load < container.tar.gz
systemctl enable --runtime /usr/share/oem/system/*
systemctl start teeth-agent.service

View File

@ -0,0 +1,6 @@
[Service]
ExecStart=/usr/bin/docker run -p 9999:9999 -privileged=true -v=/sys:/mnt/sys oemdocker /usr/local/bin/teeth-agent --api-url http://10.127.75.252:8081
Restart=always
[Install]
WantedBy=oem.target