Skip to content

Commit

Permalink
Merge pull request #46 from IBM-Cloud/syyang/ISSUE-34
Browse files Browse the repository at this point in the history
ISSUE-34 refactor build process
  • Loading branch information
syyang-in-cloud authored Aug 5, 2020
2 parents fbbb0a1 + fd38f58 commit abe2e98
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 300 deletions.
32 changes: 16 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ ubuntu-build:
docker build . -f Dockerfile.ubuntu -t $(IMAGE_NAME):$(IMAGE_VERSION_LATEST)

build-images:
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/ubuntu/bionic/base ; ./packer-build.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/ubuntu/bionic/docker; ./packer-build.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/centos/7/base ; ./packer-build.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/centos/7/docker ; ./packer-build.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-build.sh packer/ubuntu/bionic/base "
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-build.sh packer/ubuntu/bionic/docker "
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-build.sh packer/centos/7/base "
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-build.sh packer/centos/7/docker "

cleanup:
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/ubuntu/bionic/base ; ./packer-delete.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/ubuntu/bionic/docker; ./packer-delete.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/centos/7/base ; ./packer-delete.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/centos/7/docker ; ./packer-delete.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-delete.sh packer/ubuntu/bionic/base "
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-delete.sh packer/ubuntu/bionic/docker"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-delete.sh packer/centos/7/base "
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-delete.sh packer/centos/7/docker "

ubuntu-bionic-base:
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/ubuntu/bionic/base ; ./packer-build.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/ubuntu/bionic/base ; ./packer-delete.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-build.sh packer/ubuntu/bionic/base "
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-delete.sh packer/ubuntu/bionic/base "

ubuntu-bionic-docker:
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/ubuntu/bionic/docker; ./packer-build.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/ubuntu/bionic/docker; ./packer-delete.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-build.sh packer/ubuntu/bionic/docker "
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-delete.sh packer/ubuntu/bionic/docker"

centos-7-base:
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/centos/7/base ; ./packer-build.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/centos/7/base ; ./packer-delete.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-build.sh packer/centos/7/base "
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-delete.sh packer/centos/7/base "

centos-7-docker:
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/centos/7/docker ; ./packer-build.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "cd packer/centos/7/docker ; ./packer-delete.sh"
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-build.sh packer/centos/7/docker "
docker run --privileged -v `pwd`:/ibmcloud-image-builder ${IMAGE_NAME}:${IMAGE_VERSION_LATEST} /bin/bash -c "./packer-delete.sh packer/centos/7/docker "

.PHONY: all
74 changes: 45 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,41 @@

https://hub.docker.com/r/syibm/ibmcloud-image-builder

# v0.1.0 Release Readiness: Planned to release on 07/06/2020

The first official release: v0.1.0
- [x] templates for base and docker per each OS : Ubuntu 18.04, CentOS 7
- [x] CI build
- [x] github releases is ready
- [x] github packages is ready
- [x] DockerHub image repository: https://hub.docker.com/r/syibm/ibmcloud-image-builder
# Try it out

This project is set up to run on Mac or Linux.

## Dependencies

Before you can build a custom image you must install the following dependencies:

* [Docker](https://www.docker.com/products/docker-desktop)
* make

### Installing make on macOS

To install the `make` command on macOS, you need to install the Xcode command line tools by executing the following command in your terminal:

```
xcode-select --install
```

When the dialog pops up click on the *Install* button.

## Building your first custom image

Start by pulling an already built image from the Docker registry. This will download, encrypt, and package the image for deployment.

```
git clone git@github.com:IBM-Cloud/ibmcloud-image-builder.git
cd ibmcloud-image-builder
docker pull syibm/ibmcloud-image-builder
docker tag syibm/ibmcloud-image-builder ibmcloud-image-builder
make build-images
```



# Motivation

Expand Down Expand Up @@ -102,7 +129,7 @@ $ docker tag syibm/ibmcloud-image-builder ibmcloud-image-builder
$ make build-images
```

With building the dev docker:
Or we can build the docker image(`make build`) locally as below:
```
$ git clone git@github.com:IBM-Cloud/ibmcloud-image-builder.git
$ cd ibmcloud-image-builder
Expand All @@ -115,16 +142,13 @@ The extra `docker` templates in addtion to `base` templates are for the informat

1. copy the existing folder and rename the directory
2. change either shell/user-data.sh or ansible/playbook.yml
3. change the image name in packer-builder.sh ... hmm, this needs to be refactored later.



# How to build an encrypted image with your DEK (Data Encryption Key)

By default all the images will be encrypted with a given `encryption key`: `JustMySimpleSecret`. If the image required to be encrypted with a different encryption key, then attach to the build docker, and
```
cd "proper directory"
./packer-build.sh "Your DEK here"
./packer-build.sh packer/os/platform "Your DEK here"
```

```
Expand All @@ -135,19 +159,15 @@ $ tree -L 5
│ ├── base
│ │ ├── ansible
│ │ │ └── playbook.yml
│ │ ├── centos.json
│ │ ├── http
│ │ ├── packer-build.sh
│ │ ├── packer-delete.sh
│ │ ├── packer.json
│ │ └── shell
│ │ └── user-data.sh
│ └── docker
│ ├── ansible
│ │ └── playbook.yml
│ ├── centos.json
│ ├── http
│ ├── packer-build.sh
│ ├── packer-delete.sh
│ ├── packer.json
│ └── shell
│ └── user-data.sh
└── ubuntu
Expand All @@ -156,24 +176,20 @@ $ tree -L 5
│ │ ├── ansible
│ │ │ └── playbook.yml
│ │ ├── http
│ │ ├── packer-build.sh
│ │ ├── packer-delete.sh
│ │ ├── shell
│ │ │ └── user-data.sh
│ │ └── ubuntu.json
│ │ ├── packer.json
│ │ └── shell
│ │ └── user-data.sh
│ └── docker
│ ├── ansible
│ │ └── playbook.yml
│ ├── http
│ ├── packer-build.sh
│ ├── packer-delete.sh
│ ├── shell
│ │ └── user-data.sh
│ └── ubuntu.json
│ ├── packer.json
│ └── shell
│ └── user-data.sh
├── focal
└── xenial
22 directories, 20 files
22 directories, 12 files
```

# Example Virtual Server Instance created with the custom image generated with one of the templates
Expand Down
33 changes: 19 additions & 14 deletions packer/centos/7/base/packer-build.sh → packer-build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#!/bin/bash
set -ex

# change directory
cd $1

# prepare to build
DIR_TO_FILENAME=$(echo "$1" | sed 's#packer/##g' | tr '/' '-')
NEW_IMAGE="output-qemu/ibmcloud-$DIR_TO_FILENAME-amd64-100G.qcow2"
ENCRYPTED_IMAGE="output-qemu/ibmcloud-encrypted-$DIR_TO_FILENAME-amd64-100G.qcow2"

if [ -n "$2" ]; then
SECRET="$2"
else
SECRET="JustMySimpleSecret"
fi

# prepare ssh keys
if [[ -f "~/.ssh/id_rsa.pub" ]]; then
export PACKER_PUBLIC_KEY=~/.ssh/id_rsa.pub
export PACKER_PRIVATE_KEY=~/.ssh/id_rsa
Expand Down Expand Up @@ -29,25 +43,16 @@ cloud-localds disk-ssh-pub.img user-data
ansible-galaxy install geerlingguy.docker

# build the images
# base

PACKER_LOG=0 packer build centos.json
PACKER_LOG=0 packer build packer.json

# Time to make the below as a function at the next PR
NEW_IMAGE="output-qemu/ibmcloud-centos-7-cloudimg-amd64-100G.qcow2"
ENCRYPTED_IMAGE="output-qemu/ibmcloud-centos-7-cloudimg-amd64-100G-encrypted.qcow2"

qemu-img resize output-qemu/centos-7.qcow2 100G
qemu-img convert -f qcow2 -O qcow2 output-qemu/centos-7.qcow2 ${NEW_IMAGE}
qemu-img resize output-qemu/packer.qcow2 100G
qemu-img convert -f qcow2 -O qcow2 output-qemu/packer.qcow2 ${NEW_IMAGE}
qemu-img info ${NEW_IMAGE}
rm output-qemu/centos-7.qcow2
rm output-qemu/packer.qcow2

# create an example encrypted image
if [ -n "$1" ]; then
SECRET="$1"
else
SECRET="JustMySimpleSecret"
fi

BASE64_ENCODED_SECRET=$(echo -n $SECRET | base64)

qemu-img convert -O qcow2 \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
set -ex

cd $1
# delete the images and cleanup
rm -rf output-qemu
rm -rf packer_cache
Expand Down
9 changes: 0 additions & 9 deletions packer/centos/7/base/packer-delete.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"variables": {
"image_url": "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2003.qcow2",
"image_checksum_url": "https://cloud.centos.org/centos/7/images/sha256sum.txt",
"image_name": "centos-7",
"image_name": "packer",
"ssh_private_key_file": "{{env `PACKER_PRIVATE_KEY`}}"
},
"builders": [
Expand Down
70 changes: 0 additions & 70 deletions packer/centos/7/docker/packer-build.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"variables": {
"image_url": "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2003.qcow2",
"image_checksum_url": "https://cloud.centos.org/centos/7/images/sha256sum.txt",
"image_name": "centos-7",
"image_name": "packer",
"ssh_private_key_file": "{{env `PACKER_PRIVATE_KEY`}}"
},
"builders": [
Expand Down
Loading

0 comments on commit abe2e98

Please sign in to comment.