Merge lp:~schkovich/charms/trusty/nginx/trunk into lp:~hp-discover/charms/trusty/nginx/trunk

Proposed by Goran Miskovic
Status: Needs review
Proposed branch: lp:~schkovich/charms/trusty/nginx/trunk
Merge into: lp:~hp-discover/charms/trusty/nginx/trunk
Diff against target: 185 lines (+136/-4)
6 files modified
config.yaml (+9/-1)
hooks/config-changed (+8/-0)
hooks/install (+11/-3)
hooks/start (+2/-0)
hooks/stop (+2/-0)
lib/bash/helpers.sh (+104/-0)
To merge this branch: bzr merge lp:~schkovich/charms/trusty/nginx/trunk
Reviewer Review Type Date Requested Status
Marco Ceppi Pending
Review via email: mp+253673@code.launchpad.net

Description of the change

Add options to select of of two Nginx PPA: stable or mainline.

To post a comment you must log in.

Unmerged revisions

11. By Goran Miskovic

Using local instead of remote unit name in a log message

10. By Goran Miskovic <email address hidden>

Added config-changed hook, created helpers lib and move common logic to it

9. By Goran Miskovic <email address hidden>

Added optional stable or mainline PPA

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'config.yaml'
2--- config.yaml 2014-06-05 22:10:48 +0000
3+++ config.yaml 2015-03-20 13:33:58 +0000
4@@ -2,4 +2,12 @@
5 something:
6 type: int
7 default: 9001
8- description: "A short description of the configuration option"
9+ description: "PHP-FPM pool port."
10+ stable:
11+ type: boolean
12+ default: false
13+ description: "Use stable PPA. https://launchpad.net/~nginx/+archive/ubuntu/stable"
14+ mainline:
15+ type: boolean
16+ default: false
17+ description: "Use mainline PPA. See https://launchpad.net/~nginx/+archive/ubuntu/development"
18\ No newline at end of file
19
20=== added file 'hooks/config-changed'
21--- hooks/config-changed 1970-01-01 00:00:00 +0000
22+++ hooks/config-changed 2015-03-20 13:33:58 +0000
23@@ -0,0 +1,8 @@
24+#!/bin/bash
25+
26+set -eux
27+
28+DIR="${BASH_SOURCE%/*}"
29+if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
30+
31+source "${DIR}/install"
32
33=== modified file 'hooks/install'
34--- hooks/install 2014-06-08 02:20:29 +0000
35+++ hooks/install 2015-03-20 13:33:58 +0000
36@@ -2,7 +2,15 @@
37
38 set -eux
39
40-apt-get install -y nginx-full python-cheetah
41-
42-mkdir -p /srv
43+DIR="${BASH_SOURCE%/*}"
44+if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
45+
46+source "${DIR}/../lib/bash/helpers.sh"
47+
48+configureNginxPpa
49+apt-get update --quiet
50+installPackage 'nginx-full'
51+idempotentInstall 'python-cheetah'
52+
53+mkdir -p /srv || true
54 rm -f /etc/nginx/sites-enabled/*default* || true
55
56=== modified file 'hooks/start'
57--- hooks/start 2014-06-05 22:10:48 +0000
58+++ hooks/start 2015-03-20 13:33:58 +0000
59@@ -1,3 +1,5 @@
60 #!/bin/bash
61
62+set -eux
63+
64 service nginx restart || service nginx start
65
66=== modified file 'hooks/stop'
67--- hooks/stop 2014-06-05 22:10:48 +0000
68+++ hooks/stop 2015-03-20 13:33:58 +0000
69@@ -1,3 +1,5 @@
70 #!/bin/bash
71
72+set -eux
73+
74 service nginx stop
75
76=== added directory 'lib'
77=== added directory 'lib/bash'
78=== added file 'lib/bash/helpers.sh'
79--- lib/bash/helpers.sh 1970-01-01 00:00:00 +0000
80+++ lib/bash/helpers.sh 2015-03-20 13:33:58 +0000
81@@ -0,0 +1,104 @@
82+#!/usr/bin/env bash
83+
84+set -eux
85+
86+# Installs given package
87+# Expects one argument: package-name
88+# installPackage "puppet" will install Puppet
89+# Overwrites any existing configuration files(s)
90+# see: https://bugs.launchpad.net/ubuntu/+source/dpkg/+bug/92265/comments/4
91+function installPackage {
92+ local package="${1}"
93+ local exitcode=0
94+ DEBIAN_FRONTEND=noninteractive bash -c "apt-get -qq -o Dpkg::Options::='--force-confnew' -y install ${package}" >/dev/null || exitcode=$?
95+ if [ "0" -eq "${exitcode}" ]; then
96+ juju-log "Successfully installed package ${package}";
97+ else
98+ # a chance to log the error
99+ juju-log "Error ${exitcode} installing package ${package}";
100+ # exit with the same code
101+ exit ${exitcode}
102+ fi
103+}
104+
105+# Checks if given package is installed
106+# Expects one argument: package-name
107+# isPackageInstalled "puppet" will check if Puppet is installed
108+# returns 0 case when package is installed otherwise returns 33
109+# When running with e flag set returning any other code than 0
110+# will stop execution.
111+function isPackageInstalled {
112+ # http://superuser.com/questions/43342/how-can-i-display-the-list-of-all-packages-installed-on-my-debian-system
113+ if dpkg --get-selections | grep -q "^${1}[[:space:]]*install$" >/dev/null; then
114+ juju-log "Package ${1} is already installed.";
115+ return 0
116+ else
117+ juju-log "Package ${1} is not installed.";
118+ return 33
119+ fi
120+}
121+
122+# Purges given package
123+# Expects one argument: package-name
124+# purgePackage "puppet" will completely remove Puppet installation
125+function purgePackage {
126+ local package="${1}"
127+ local exitcode=0
128+ isPackageInstalled ${package} || exitcode=$?
129+ # if package is installed and there was no error
130+ if [ "0" -eq "${exitcode}" ]; then
131+ juju-log "Purging old ${package} installation at ${JUJU_UNIT_NAME:-unset}"
132+ apt-get purge "${package}" --yes
133+ apt-get autoremove --yes
134+ # if return code is not 33 exit with that code
135+ elif [ "33" -ne "${exitcode}" ]; then
136+ exit ${exitcode}
137+ fi
138+}
139+
140+# Installs package if it is not already installed
141+# Expects one argument: package-name
142+# idempotentInstall "puppet" will check if package "puppet" is installed and case it is not it will install it
143+function idempotentInstall {
144+ local package="${1}"
145+ local exitcode=0
146+ if [[ -z "${package// }" ]];
147+ then
148+ juju-log "Could not install. No package name given";
149+ exit 1;
150+ fi
151+ isPackageInstalled ${package} || exitcode=$?
152+ if [ "0" -ne "${exitcode}" ]; then
153+ juju-log "Installing ${package}"
154+ installPackage ${package}
155+ fi
156+}
157+
158+# Based on configuration will add or remove stable or mainline Nginx PPA
159+# @see config.yaml
160+# If both packages are configured to be added error will be triggered
161+function configureNginxPpa {
162+
163+ # The first letter will be capital when set from GUI causing error: helpers.sh: line 85: True: command not found
164+ local stable=$(config-get "stable" | tr '[:upper:]' '[:lower:]')
165+ local mainline=$(config-get "mainline" | tr '[:upper:]' '[:lower:]')
166+
167+ if ${stable} && ${mainline}; then
168+ juju-log "Either stable or mainline PPA could be added."
169+ exit 1
170+ fi
171+
172+ if ${stable}; then
173+ apt-add-repository --yes ppa:nginx/stable
174+ else
175+ apt-add-repository --remove --yes ppa:nginx/stable
176+ fi
177+
178+ if ${mainline};
179+ then
180+ apt-add-repository --yes ppa:nginx/development
181+ else
182+ apt-add-repository --remove --yes ppa:nginx/development
183+ fi
184+
185+}

Subscribers

People subscribed via source and target branches