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- # 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)
8812upstart::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- }
0 commit comments