Module: Capnotify

Defined in:
lib/capnotify/plugin/message.rb,
lib/capnotify.rb,
lib/capnotify/plugin.rb,
lib/capnotify/version.rb,
lib/capnotify/component.rb,
lib/capnotify/plugin/details.rb,
lib/capnotify/plugin/overview.rb

Overview

Capnotify built-in plugin for Custom messages in email This adds a “message” section that will include ‘notification_msg’ if it’s set For example:

cap deploy -s notification_msg="Just getting a hotfix deployed"

Defined Under Namespace

Modules: Plugin Classes: Component

Constant Summary collapse

VERSION =
"0.2.1"

Class Method Summary collapse

Class Method Details

.load_into(config) ⇒ Object



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
# File 'lib/capnotify.rb', line 10

def self.load_into(config)
  config.load do
    Capistrano.plugin :capnotify, ::Capnotify::Plugin

    # conditionally set a capistrano var if it hasn't been set, yet.
    # this functin was ganked from the built-in capistrano deploy recipe
    # since we can't count on this function being defined, we redefine here.
    def _cset(name, *args, &block)
      unless exists?(name)
        set(name, *args, &block)
      end
    end

    # some configuration
    # The paths to the built-in templates
    # Set these in your deployment recipes if you want a custom template
    # These paths are used when building the deployment notification emails
    _cset :capnotify_deployment_notification_html_template_path, capnotify.built_in_template_for('default_notification.html.erb')
    _cset :capnotify_deployment_notification_text_template_path, capnotify.built_in_template_for('default_notification.txt.erb')

    # get the name of the user deploying
    # if using git, this will read that from your git config
    # otherwise will use the currently logged-in user's name
    # TODO: Support SCM other than git.
    # TODO: Support a method other than `whoami` for getting the user's name
    _cset(:deployer_username) do
      if exists?(:scm) && fetch(:scm).to_sym == :git
        `git config user.name`.chomp
      else
        `whoami`.chomp
      end
    end

    # built-in values:

    # This is the list of components to use for the notification
    set :capnotify_component_list, []

    # The name of the application. Used in pretty much every built-in message
    # by default, the output should be: "STAGE APPNAME @ BRANCH"
    # override this to change the default behavior for capnotify.appname
    _cset(:capnotify_appname) do
      name = [ fetch(:stage, nil), fetch(:application, nil) ].compact.join(" ")
      if fetch(:branch, nil)
        name = "#{ name } @ #{ branch }"
      end
      name
    end

    # default messages:
    # (these can be overridden)

    # short message for the start of running migrations
    _cset(:capnotify_migrate_start_msg) do
      "#{ capnotify.appname } migration starting."
    end

    # short message for the completion of running migrations
    _cset(:capnotify_migrate_complete_msg) do
      "#{ capnotify.appname } migration completed."
    end

    # short message for the start of a deployment
    _cset(:capnotify_deploy_start_msg) do
      "#{ capnotify.appname } deployment starting.\nRef: #{ fetch(:real_revision) }"
    end

    # short message for the completion of a deployment
    _cset(:capnotify_deploy_complete_msg) do
      "#{ capnotify.appname } deployment completed.\nRef: #{ fetch(:real_revision) }"
    end

    # short message for putting up a maintenance page
    _cset(:capnotify_maintenance_up_msg) do
      "#{ capnotify.appname } maintenance page is now up."
    end

    # short message for taking down a maintenance page
    _cset(:capnotify_maintenance_down_msg) do
      "#{ capnotify.appname } maintenance page has been taken down."
    end

    # full email message to notify of deployment (html)
    # when called, will compile the template and return the complete data as a string
    _cset(:capnotify_deployment_notification_html) do
      capnotify.build_template( fetch(:capnotify_deployment_notification_html_template_path) )
    end

    # full email message to notify of deployment (plain text)
    # when called, will compile the template and return the complete data as a string
    _cset(:capnotify_deployment_notification_text) do
      data = capnotify.build_template( fetch(:capnotify_deployment_notification_text_template_path) )

      # clean up the text output (remove leading spaces and more than 2 newlines in a row
      data.gsub(/^ +/, '').gsub(/\n{3,}/, "\n\n")
    end

    # before update_code, fetch the current revision
    # this is needed to ensure that no matter when capnotify is run, it will have the correct previous (currently deployed) revision
    # it will have the correct starting point.
    before 'deploy:update_code' do
      set :capnotify_previous_revision, fetch(:current_revision, nil) # the revision that's currently deployed at this moment
    end


    on(:load) do
      unless fetch(:capnotify_off, nil)
        # register the callbacks
        # These callbacks can be disabled by setting the following variables to a truthy value:
        #  * capnotify_disable_deploy_hooks
        #  * capnotify_disable_migrate_hooks
        #  * capnotify_disable_maintenance_hooks

        # deploy start/complete
        unless fetch(:capnotify_disable_deploy_hooks, false)
          before('deploy') { trigger :deploy_start }
          after('deploy')  { trigger :deploy_complete }
        end

        # migration start/complete
        unless fetch(:capnotify_disable_migrate_hooks, false)
          before('deploy:migrate') { trigger :migrate_start }
          after('deploy:migrate')  { trigger :migrate_complete }
        end

        # maintenance start/complete
        unless fetch(:capnotify_disable_maintenance_hooks, false)
          after('deploy:web:disable') { trigger :maintenance_page_up }
          after('deploy:web:enable')  { trigger :maintenance_page_down }
        end

        # load the default plugins
        # disable loading them by setting capnotify_disable_default_components to a truthy value
        unless fetch(:capnotify_disable_default_components, false)
          capnotify.load_default_plugins
        end

        # prints out a splash screen if capnotify_show_splash is set to true
        # defaults to being silent.
        capnotify.print_splash if fetch(:capnotify_show_splash, false)
      end
    end

  end
end