Skip to content

Commit a4c0b9b

Browse files
author
Henry Mollman
committed
Ready for PR
1 parent 22fa18a commit a4c0b9b

4 files changed

Lines changed: 0 additions & 278 deletions

File tree

init.sh

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,10 @@
22

33
# Entry-point script for dock initialization. Simply includes the `lib/dock.sh`
44
# library and calls the master initialization function.
5-
#
6-
# NOTE This script will automatically update the `lib/` directory before
7-
# the dock is initialized. This means that this script itself will not be
8-
# automatically updated. To do so a new AMI must be baked.
9-
#
105
# @author Ryan Sandor Richards
116

127
export DOCK_INIT_BASE=/opt/runnable/dock-init
138
export HOST_IP=$(hostname -i)
14-
export DONT_DELETE_KEYS=true
15-
export USE_EXIT=true
16-
export LOG_LEVEL=trace
17-
export FETCH_ORIGIN_ALL=true
189

1910
if [ -z "${CONSUL_PORT+x}" ]; then
2011
export CONSUL_PORT=8500

lib/dock.sh

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,11 @@
99
# @author Bryan Kendall
1010
# @module dock
1111

12-
source "${DOCK_INIT_BASE}/lib/aws.sh"
1312
source "${DOCK_INIT_BASE}/lib/cert.sh"
14-
source "${DOCK_INIT_BASE}/lib/consul.sh"
15-
source "${DOCK_INIT_BASE}/lib/container.sh"
16-
source "${DOCK_INIT_BASE}/lib/upstart.sh"
17-
1813
source "${DOCK_INIT_BASE}/lib/util/backoff.sh"
1914
source "${DOCK_INIT_BASE}/lib/util/log.sh"
2015
source "${DOCK_INIT_BASE}/lib/util/rollbar.sh"
2116

22-
# An "on exit" trap to clean up sensitive keys and files on the dock itself.
23-
# Note that this will have no effect if the `DONT_DELETE_KEYS` environment has
24-
# been set (useful for testing)
25-
dock::cleanup::exit_trap() {
26-
# Delete the keys unless the `DO_NOT_DELETE` flag is set
27-
if [[ "${DONT_DELETE_KEYS}" == "" ]]; then
28-
log::info '[CLEANUP TRAP] Removing Keys'
29-
rm -f "${CERT_PATH}"/ca-key.pem \
30-
"${CERT_PATH}"/pass \
31-
"${DOCK_INIT_BASE}"/consul-resources/template-config.hcl \
32-
"${DOCK_INIT_BASE}"/consul-resources/vault/**/auth-token \
33-
"${DOCK_INIT_BASE}"/consul-resources/vault/**/token-* \
34-
"${DOCK_INIT_BASE}"/key/rollbar.token
35-
fi
36-
}
37-
38-
# Sets the cleanup trap for the entire script
39-
dock::cleanup::set_exit_trap() {
40-
log::info "Setting key cleanup trap"
41-
trap 'dock::cleanup::exit_trap' EXIT
42-
}
43-
4417
# Sets the value of `$ORG_ID` as the org label in the docker configuration
4518
dock::set_config_org() {
4619
log::info "Setting organization id in docker configuration"
@@ -68,60 +41,3 @@ dock::generate_certs() {
6841
backoff dock::generate_certs_backoff
6942
}
7043

71-
# Generates the correct /etc/hosts file for the dock
72-
dock::generate_etc_hosts() {
73-
log::info "Generating /etc/hosts"
74-
75-
rollbar::fatal_trap \
76-
"Dock-Init: Failed to Add Host Registry Entry" \
77-
"Consul-Template was unable to realize the registry template."
78-
79-
local template=''
80-
template+="$DOCK_INIT_BASE/consul-resources/templates/hosts-registry.ctmpl"
81-
template+=":$DOCK_INIT_BASE/hosts-registry.txt"
82-
consul-template \
83-
-config="${DOCK_INIT_BASE}"/consul-resources/template-config.hcl \
84-
-once \
85-
-template="${template}"
86-
87-
rollbar::clear_trap
88-
}
89-
90-
# Sets the correct registry.runnable.com host
91-
dock::set_registry_host() {
92-
local registry_host=$(cat "$DOCK_INIT_BASE/hosts-registry.txt")
93-
log::info "Set registry host: $registry_host"
94-
echo "$registry_host" >> /etc/hosts
95-
}
96-
97-
# Remove docker key file so it generates a unique id
98-
dock::remove_docker_key_file() {
99-
log::info "Removing docker key.json"
100-
rm -f /etc/docker/key.json
101-
}
102-
103-
# Master function for performing all tasks and initializing the dock
104-
dock::init() {
105-
# Setup the exit trap and rollbar
106-
dock::cleanup::set_exit_trap
107-
rollbar::init
108-
109-
# Connect to and configure consul then collect various information we need
110-
consul::connect
111-
consul::get_environment
112-
consul::configure_consul_template
113-
aws::get_org_id
114-
115-
# Now that we have everything we need and consul is ready, initialize the dock
116-
dock::set_hostname
117-
dock::set_config_org
118-
dock::generate_certs
119-
dock::generate_etc_hosts
120-
dock::set_registry_host
121-
dock::remove_docker_key_file
122-
upstart::start
123-
container::start
124-
125-
# Give the all clear message!
126-
log::info "Init Done!"
127-
}

lib/upstart.sh

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,9 @@
55
# @author Ryan Sandor Richards
66
# @author Bryan Kendall
77

8-
source "${DOCK_INIT_BASE}/lib/consul.sh"
9-
source "${DOCK_INIT_BASE}/lib/util/backoff.sh"
108
source "${DOCK_INIT_BASE}/lib/util/log.sh"
119
source "${DOCK_INIT_BASE}/lib/util/rollbar.sh"
1210

13-
# Configures the template for a given service
14-
# @param $1 name Name of the service
15-
# @param $2 path Path to the servic
16-
upstart::configure_service() {
17-
local name="${1}"
18-
log::trace "Configuring $name"
19-
rollbar::fatal_trap \
20-
"Consul-Template: Failed to Render $name Config" \
21-
"Consule-Template was unable to realize the given template."
22-
23-
local template_path="$DOCK_INIT_BASE/consul-resources/templates/services"
24-
template_path+="/$name.conf.ctmpl"
25-
template_path+=":/etc/init/$name.conf"
26-
27-
consul-template \
28-
-config="$DOCK_INIT_BASE/consul-resources/template-config.hcl" \
29-
-once \
30-
-template="$template_path"
31-
echo "manual" > /etc/init/"$name".override
32-
33-
rollbar::clear_trap
34-
}
35-
36-
# Generates upstart scripts for thoses services that require environment info
37-
# from consul
38-
upstart::generate_scripts() {
39-
log::info "Configuring Upstart Scripts"
40-
upstart::configure_service "charon"
41-
log::trace "Done Generating Upstart Scripts"
42-
}
43-
44-
# Updates a service to the consul version, installs packages, then restarts it.
45-
# @param $1 Name of the service
46-
upstart::upstart_named_service() {
47-
local name="${1}"
48-
local attempt="${2}"
49-
local data='{"attempt":'"${attempt}"'}'
50-
local version="$(consul::get ${name}/version)"
51-
local key_path="$DOCK_INIT_BASE/key/id_rsa_runnabledock"
52-
53-
rollbar::warning_trap \
54-
"$name: Cannot Upstart Services" \
55-
"Attempting to upstart the services and failing." \
56-
"${data}"
57-
58-
log::info "Updating and restarting $name @ $version" &&
59-
cd "/opt/runnable/$name" &&
60-
ssh-agent bash -c "ssh-add $key_path; git fetch origin" &&
61-
git checkout "$version" &&
62-
ssh-agent bash -c "ssh-add $key_path; USERPROFILE=/home/ubuntu npm install --production" &&
63-
service "$name" restart
64-
65-
rollbar::clear_trap
66-
}
67-
68-
# Starts a service installed on the machine.
69-
# @param $1 Name of the service
70-
# @param $2 Attempt number
71-
upstart::upstart_service() {
72-
local name="${1}"
73-
local attempt="${2}"
74-
local data='{"attempt":'"${attempt}"'}'
75-
76-
rollbar::warning_trap \
77-
"$name: Cannot Upstart Service" \
78-
"Attempting to upstart the service and failing." \
79-
"${data}"
80-
81-
log::info "Starting $name"
82-
service "$name" restart
83-
84-
rollbar::clear_trap
85-
}
86-
8711
# Start dockers (due to manual override now set in /etc/init)
8812
upstart::start_docker() {
8913
log::info "Starting Docker"
@@ -107,49 +31,3 @@ upstart::start_docker() {
10731
attempt=$(( attempt + 1 ))
10832
done
10933
}
110-
111-
# Upstarts services that are supposed to be running on the dock.
112-
# @param $1 attempt Attempt number.
113-
upstart::upstart_services_with_backoff_params() {
114-
local attempt="${1}"
115-
upstart::upstart_named_service "krain" $attempt
116-
upstart::upstart_named_service "charon" $attempt
117-
upstart::upstart_service "datadog-agent" $attempt
118-
}
119-
120-
# Pulls the latest docker image for the runnable image builder
121-
# @param $1 attempt The current attempt for pulling image builder
122-
upstart::pull_image_builder() {
123-
local attempt="${1}"
124-
local name="image-builder"
125-
local version="$(consul::get $name/version)"
126-
127-
log::info "Pulling image-builder:$version (${attempt})"
128-
docker pull "registry.runnable.com/runnable/image-builder:$version"
129-
130-
if [[ "$?" -gt "0" ]]; then
131-
local data='{"attempt":'"${attempt}"'}'
132-
rollbar::report_warning \
133-
"Dock-Init: Cannot Upstart Services" \
134-
"Attempting to upstart the services and failing." \
135-
"${data}"
136-
return 1
137-
fi
138-
}
139-
140-
# Starts all services needed for the dock
141-
upstart::start() {
142-
log::info "Upstarting dock"
143-
upstart::generate_scripts
144-
upstart::start_docker
145-
backoff upstart::pull_image_builder
146-
backoff upstart::upstart_services_with_backoff_params
147-
}
148-
149-
# Stops all dock services
150-
upstart::stop() {
151-
log::info "Stopping all dock upstart services"
152-
service krain stop
153-
service charon stop
154-
service docker stop
155-
}

test/upstart.sh

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)