forked from sormy/gentoo-ami-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgentoo-ami-builder.sh
executable file
·221 lines (172 loc) · 6.74 KB
/
gentoo-ami-builder.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash
SCRIPT_DIR="$( cd "$(dirname "$0")" ; pwd -P )"
# shellcheck source=lib/app-lib.sh
source "$SCRIPT_DIR/lib/app-lib.sh"
# shellcheck source=lib/opt.sh
source "$SCRIPT_DIR/lib/opt.sh"
# shellcheck source=lib/app-phases.sh
source "$SCRIPT_DIR/lib/app-phases.sh"
# shellcheck source=lib/app-trap.sh
source "$SCRIPT_DIR/lib/app-trap.sh"
# shellcheck source=lib/bundle.sh
source "$SCRIPT_DIR/lib/bundle.sh"
# shellcheck source=lib/elib.sh
source "$SCRIPT_DIR/lib/elib.sh"
# shellcheck source=lib/disk.sh
source "$SCRIPT_DIR/lib/disk.sh"
# shellcheck source=lib/ena.sh
source "$SCRIPT_DIR/lib/ena.sh"
APP_NAME="gentoo-ami-builder"
APP_DESCRIPTION="Gentoo AMI Builder"
APP_VERSION="1.0.1"
# Security group with incoming connection available on SSH port (22).
EC2_SECURITY_GROUP="default"
# SSH key pair that will be used to connect to build instance.
# The private key should be available locally to log into host.
EC2_KEY_PAIR=""
# Instance type that will be used as compile host.
# Recommended is compute-optimized instance type.
EC2_INSTANCE_TYPE="c5.2xlarge"
# Default volume size in GB.
EC2_VOLUME_SIZE="20"
# Default volume type.
EC2_VOLUME_TYPE="gp2"
# Set to the latest Amazon Linux AMI. You could find it in AWS console.
EC2_AMAZON_IMAGE_ID="ami-b70554c8" # Amazon Linux 2 AMI as of 2018-08-12
# Default user name to log into Amazon Linux.
AMAZON_USER="ec2-user"
# Default user name to log into Gentoo Linux.
GENTOO_USER="root"
# Highly recommended options for SSH during bootstrap.
SSH_OPTS="-o ConnectTimeout=5
-o KbdInteractiveAuthentication=no
-o ChallengeResponseAuthentication=no
-o UserKnownHostsFile=/dev/null
-o StrictHostKeyChecking=no
-o LogLevel=error"
# Recommended default options for wget.
WGET_OPTS="--quiet"
# Recommended default options for emerge.
EMERGE_OPTS="--quiet"
# Recommended default options for genkernel.
GENKERNEL_OPTS="--no-color"
# By default it also includes EFI, but it is useless for AWS instances.
GRUB_PLATFORMS="pc"
# Gentoo profile, see README for more details.
GENTOO_PROFILE="amd64"
# Available Gentoo architectures in EC2 are amd64 and x86.
GENTOO_ARCH="amd64"
# Primary Gentoo mirror to look for Gentoo stage tarballs and portage snapshots.
GENTOO_MIRROR="http://distfiles.gentoo.org"
# Target AMI image prefix.
GENTOO_IMAGE_NAME_PREFIX="Gentoo Linux"
# Enable/disable colors in output.
COLOR="yes"
# Required if you are would like to use already running instance.
EC2_INSTANCE_ID=""
# Used for debugging purposes if something goes wrong.
SKIP_PHASES=""
# Wait for keypress each time before rebooting the instance (could be useful for debug purposes).
PAUSE_BEFORE_REBOOT="no"
# Terminate instance on failure (keeping it up could be useful for debug purposes).
TERMINATE_ON_FAILURE="yes"
# Application phase script filenames.
APP_PHASE2_SCRIPT=""
APP_PHASE3_SCRIPT=""
APP_PHASE4_SCRIPT=""
APP_PHASE5_SCRIPT=""
# Store instance public ip to be able to log into over SSH.
EC2_PUBLIC_IP=""
# Stop on any error required to properly handle errors.
set -e
opt_config "
--instance-type \
--amazon-image-id \
--security-group \
--key-pair \
--gentoo-profile \
--gentoo-mirror \
--gentoo-image-name \
--resume-instance-id \
--skip-phases \
--pause-before-reboot \
--terminate-on-failure \
--color \
"
# Parse arguments and make them available for opt_get() function.
opt_parse "$@"
# Show help screen immeditely if selected.
if [ "$(opt_cmd)" = "help" ]; then
show_help
exit
fi
# Show version screen immeditely if selected.
if [ "$(opt_cmd)" = "version" ]; then
show_version
exit
fi
# Override default values if they are passed from command line.
OPT="$(opt_get --instance-type)"; [ -z "$OPT" ] || EC2_INSTANCE_TYPE="$OPT"
OPT="$(opt_get --amazon-image-id)"; [ -z "$OPT" ] || EC2_AMAZON_IMAGE_ID="$OPT"
OPT="$(opt_get --security-group)"; [ -z "$OPT" ] || EC2_SECURITY_GROUP="$OPT"
OPT="$(opt_get --key-pair)"; [ -z "$OPT" ] || EC2_KEY_PAIR="$OPT"
OPT="$(opt_get --gentoo-profile)"; [ -z "$OPT" ] || GENTOO_PROFILE="$OPT"
OPT="$(opt_get --gentoo-mirror)"; [ -z "$OPT" ] || GENTOO_MIRROR="$OPT"
OPT="$(opt_get --gentoo-image-name)"; [ -z "$OPT" ] || GENTOO_IMAGE_NAME_PREFIX="$OPT"
OPT="$(opt_get --resume-instance-id)"; [ -z "$OPT" ] || EC2_INSTANCE_ID="$OPT"
OPT="$(opt_get --skip-phases)"; [ -z "$OPT" ] || SKIP_PHASES="$OPT"
OPT="$(opt_get --pause-before-reboot)"; [ -z "$OPT" ] || PAUSE_BEFORE_REBOOT="$OPT"
OPT="$(opt_get --terminate-on-failure)";[ -z "$OPT" ] || TERMINATE_ON_FAILURE="$OPT"
OPT="$(opt_get --color)"; [ -z "$OPT" ] || COLOR="$OPT"
# If resume is enabled then we should skip first phase.
if [ -n "$EC2_INSTANCE_ID" ]; then
SKIP_PHASES="1${SKIP_PHASES}"
fi
# Auto detect gentooo architecture based on provided gentoo profile.
GENTOO_ARCH=$(echo "$GENTOO_PROFILE" | grep -q '^\(amd64\|x32\)' && echo "amd64" || echo "x86")
# Add profile name into default image name prefix.
GENTOO_IMAGE_NAME_PREFIX="$GENTOO_IMAGE_NAME_PREFIX ($GENTOO_PROFILE)"
# Die on configuration that are well know to not work with current version of the script.
[ "$GENTOO_ARCH" = "x86" ] && edie "Gentoo x86 Architecture is not supported yet."
# Install error handler that will terminate instance and cleanup temporary files.
trap app_exit_trap EXIT
# Set colors enabled/disabled based on configuration.
elog_set_colors "$COLOR"
# Create temporary files that will be used for bootstrapping.
bundle_phase_files
# Show smalli intro screen with basic information about selected parameters.
show_intro
# Show header with timestamp.
show_header
# Run phase 1 if enabled: Prepare Instance
if ! is_phase_skipped 1; then
# sets EC2_INSTANCE_ID and EC2_PUBLIC_IP
show_phase1_prepare_instance "$EC2_AMAZON_IMAGE_ID" "$AMAZON_USER"
else
# try to use existing instance, should be passed from command line
show_phase1_use_instance
fi
# Run phase 2 if enabled: Prepare Root
if ! is_phase_skipped 2; then
show_phase2_prepare_root "$AMAZON_USER" "$EC2_PUBLIC_IP" "$APP_PHASE2_SCRIPT"
fi
# Run phase 3 if enabled: Build Root
if ! is_phase_skipped 3; then
show_phase3_build_root "$AMAZON_USER" "$EC2_PUBLIC_IP" "$APP_PHASE3_SCRIPT"
fi
# Run phase 4 if enabled: Switch Root
if ! is_phase_skipped 4; then
show_phase4_switch_root "$EC2_INSTANCE_ID" "$AMAZON_USER" "$GENTOO_USER" \
"$EC2_PUBLIC_IP" "$APP_PHASE4_SCRIPT"
fi
# Run phase 5 if enabled: Migrate Root
if ! is_phase_skipped 5; then
show_phase5_migrate_boot "$EC2_INSTANCE_ID" "$GENTOO_USER" "$EC2_PUBLIC_IP" \
"$APP_PHASE5_SCRIPT"
fi
# Run phase 6 if enabled: Build AMI
if ! is_phase_skipped 6; then
show_phase6_build_ami "$EC2_INSTANCE_ID" "$GENTOO_IMAGE_NAME_PREFIX"
fi
# Show footer with timestamp and duration of the process.
show_footer