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"
108source " ${DOCK_INIT_BASE} /lib/util/log.sh"
119source " ${DOCK_INIT_BASE} /lib/util/rollbar.sh"
1210
13- # Generates upstart scripts for the dock
14- upstart::generate_scripts () {
15- log::info " Generating Upstart Scripts"
16- rollbar::fatal_trap \
17- " Dock-Init: Failed to Generate Upstart Script" \
18- " Failed to generate the upstart scripts."
19- upstart::generate_scripts
20- rollbar::clear_trap
21- }
22-
23- # Configures the template for a given service
24- # @param $1 name Name of the service
25- # @param $2 path Path to the servic
26- upstart::configure_service () {
27- local name=" ${1} "
28- log::trace " Configuring $name "
29- rollbar::fatal_trap \
30- " Consul-Template: Failed to Render $name Config" \
31- " Consule-Template was unable to realize the given template."
32-
33- local template_path=" $DOCK_INIT_BASE /consul-resources/templates/services"
34- template_path+=" /$name .conf.ctmpl"
35- template_path+=" :/etc/init/$name .conf"
36-
37- consul-template \
38- -config=" $DOCK_INIT_BASE /consul-resources/template-config.hcl" \
39- -once \
40- -template=" $template_path "
41- echo " manual" > /etc/init/" $name " .override
42-
43- rollbar::clear_trap
44- }
45-
46- # Generates upstart scripts for thoses services that require environment info
47- # from consul
48- upstart::generate_scripts () {
49- log::info " Configuring Upstart Scripts"
50- upstart::configure_service " charon"
51- log::trace " Done Generating Upstart Scripts"
52- }
53-
54- # Updates a service to the consul version, installs packages, then restarts it.
55- # @param $1 Name of the service
56- upstart::upstart_named_service () {
57- local name=" ${1} "
58- local attempt=" ${2} "
59- local data=' {"attempt":' " ${attempt} " ' }'
60- local version=" $( consul::get ${name} /version) "
61- local key_path=" $DOCK_INIT_BASE /key/id_rsa_runnabledock"
62-
63- rollbar::warning_trap \
64- " $name : Cannot Upstart Services" \
65- " Attempting to upstart the services and failing." \
66- " ${data} "
67-
68- log::info " Updating and restarting $name @ $version " &&
69- cd " /opt/runnable/$name " &&
70- ssh-agent bash -c " ssh-add $key_path ; git fetch origin" &&
71- git checkout " $version " &&
72- ssh-agent bash -c " ssh-add $key_path ; USERPROFILE=/home/ubuntu npm install --production" &&
73- service " $name " restart
74-
75- rollbar::clear_trap
76- }
77-
78- # Starts a service installed on the machine.
79- # @param $1 Name of the service
80- # @param $2 Attempt number
81- upstart::upstart_service () {
82- local name=" ${1} "
83- local attempt=" ${2} "
84- local data=' {"attempt":' " ${attempt} " ' }'
85-
86- rollbar::warning_trap \
87- " $name : Cannot Upstart Service" \
88- " Attempting to upstart the service and failing." \
89- " ${data} "
90-
91- log::info " Starting $name "
92- service " $name " restart
93-
94- rollbar::clear_trap
95- }
96-
9711# Start dockers (due to manual override now set in /etc/init)
9812upstart::start_docker () {
9913 log::info " Starting Docker"
@@ -105,7 +19,7 @@ upstart::start_docker() {
10519
10620 log::info " Waiting for Docker"
10721 local attempt=1
108- local timeout=1
22+ local timeout=.5
10923 while [ ! -e /var/run/docker.sock ]
11024 do
11125 log::info " Docker Sock N/A ($attempt )"
@@ -115,52 +29,5 @@ upstart::start_docker() {
11529 rollbar::report_warning " ${title} " " ${message} " " $data "
11630 sleep $timeout
11731 attempt=$(( attempt + 1 ))
118- timeout=$(( timeout * 2 ))
11932 done
12033}
121-
122- # Upstarts services that are supposed to be running on the dock.
123- # @param $1 attempt Attempt number.
124- upstart::upstart_services_with_backoff_params () {
125- local attempt=" ${1} "
126- upstart::upstart_named_service " krain" $attempt
127- upstart::upstart_named_service " charon" $attempt
128- upstart::upstart_service " datadog-agent" $attempt
129- }
130-
131- # Pulls the latest docker image for the runnable image builder
132- # @param $1 attempt The current attempt for pulling image builder
133- upstart::pull_image_builder () {
134- local attempt=" ${1} "
135- local name=" image-builder"
136- local version=" $( consul::get $name /version) "
137-
138- log::info " Pulling image-builder:$version (${attempt} )"
139- docker pull " registry.runnable.com/runnable/image-builder:$version "
140-
141- if [[ " $? " -gt " 0" ]]; then
142- local data=' {"attempt":' " ${attempt} " ' }'
143- rollbar::report_warning \
144- " Dock-Init: Cannot Upstart Services" \
145- " Attempting to upstart the services and failing." \
146- " ${data} "
147- return 1
148- fi
149- }
150-
151- # Starts all services needed for the dock
152- upstart::start () {
153- log::info " Upstarting dock"
154- upstart::generate_scripts
155- upstart::start_docker
156- backoff upstart::pull_image_builder
157- backoff upstart::upstart_services_with_backoff_params
158- }
159-
160- # Stops all dock services
161- upstart::stop () {
162- log::info " Stopping all dock upstart services"
163- service krain stop
164- service charon stop
165- service docker stop
166- }
0 commit comments