Class: Capistrano::Configuration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/capistrano/configuration.rb,
lib/capistrano/configuration/filter.rb,
lib/capistrano/configuration/server.rb,
lib/capistrano/configuration/servers.rb,
lib/capistrano/configuration/question.rb,
lib/capistrano/configuration/variables.rb,
lib/capistrano/configuration/host_filter.rb,
lib/capistrano/configuration/null_filter.rb,
lib/capistrano/configuration/role_filter.rb,
lib/capistrano/configuration/empty_filter.rb,
lib/capistrano/configuration/scm_resolver.rb,
lib/capistrano/configuration/plugin_installer.rb,
lib/capistrano/configuration/validated_variables.rb

Defined Under Namespace

Classes: EmptyFilter, Filter, HostFilter, NullFilter, PluginInstaller, Question, RoleFilter, SCMResolver, Server, Servers, ValidatedVariables, Variables

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Configuration

Returns a new instance of Configuration



26
27
28
# File 'lib/capistrano/configuration.rb', line 26

def initialize(values={})
  @variables = ValidatedVariables.new(Variables.new(values))
end

Instance Attribute Details

#backendObject



85
86
87
# File 'lib/capistrano/configuration.rb', line 85

def backend
  @backend ||= SSHKit
end

#variablesObject (readonly)

Returns the value of attribute variables



22
23
24
# File 'lib/capistrano/configuration.rb', line 22

def variables
  @variables
end

Class Method Details

.envObject



13
14
15
# File 'lib/capistrano/configuration.rb', line 13

def self.env
  @env ||= new
end

.reset!Object



17
18
19
# File 'lib/capistrano/configuration.rb', line 17

def self.reset!
  @env = new
end

Instance Method Details

#add_cmdline_filter(type, values) ⇒ Object



141
142
143
# File 'lib/capistrano/configuration.rb', line 141

def add_cmdline_filter(type, values)
  cmdline_filters << Filter.new(type, values)
end

#add_filter(filter = nil, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/capistrano/configuration.rb', line 113

def add_filter(filter=nil, &block)
  if block
    raise ArgumentError, "Both a block and an object were given" if filter

    filter = Object.new
    def filter.filter(servers)
      block.call(servers)
    end
  elsif !filter.respond_to? :filter
    raise TypeError, "Provided custom filter <#{filter.inspect}> does " \
                     "not have a public 'filter' method"
  end
  @custom_filters ||= []
  @custom_filters << filter
end

#any?(key) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/capistrano/configuration.rb', line 47

def any?(key)
  value = fetch(key)
  if value && value.respond_to?(:any?)
    value.any?
  else
    !fetch(key).nil?
  end
end

#append(key, *values) ⇒ Object



39
40
41
# File 'lib/capistrano/configuration.rb', line 39

def append(key, *values)
  set(key, Array(fetch(key)).concat(values))
end

#ask(key, default = nil, options = {}) ⇒ Object



30
31
32
33
# File 'lib/capistrano/configuration.rb', line 30

def ask(key, default=nil, options={})
  question = Question.new(key, default, options)
  set(key, question)
end

#configure_backendObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/capistrano/configuration.rb', line 91

def configure_backend
  backend.configure do |sshkit|
    configure_sshkit_output(sshkit)
    sshkit.output_verbosity = fetch(:log_level)
    sshkit.default_env      = fetch(:default_env)
    sshkit.backend          = fetch(:sshkit_backend, SSHKit::Backend::Netssh)
    sshkit.backend.configure do |backend|
      backend.pty                = fetch(:pty)
      backend.connection_timeout = fetch(:connection_timeout)
      backend.ssh_options        = (backend.ssh_options || {}).merge(fetch(:ssh_options, {}))
    end
  end
end

#configure_scmObject



105
106
107
# File 'lib/capistrano/configuration.rb', line 105

def configure_scm
  Capistrano::Configuration::SCMResolver.new.resolve
end

#dry_run?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/capistrano/configuration.rb', line 150

def dry_run?
  fetch(:sshkit_backend) == SSHKit::Backend::Printer
end

#filter(list) ⇒ Object



145
146
147
148
# File 'lib/capistrano/configuration.rb', line 145

def filter(list)
  setup_filters if @filters.nil?
  @filters.reduce(list) { |l, f| f.filter l }
end

#install_plugin(plugin, load_hooks: true, load_immediately: false) ⇒ Object



154
155
156
157
158
# File 'lib/capistrano/configuration.rb', line 154

def install_plugin(plugin, load_hooks: true, load_immediately: false)
  installer.install(plugin,
                    load_hooks: load_hooks,
                    load_immediately: load_immediately)
end

#is_question?(key) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/capistrano/configuration.rb', line 56

def is_question?(key)
  value = fetch_for(key, nil)
  !value.nil? && value.is_a?(Question)
end

#primary(role) ⇒ Object



81
82
83
# File 'lib/capistrano/configuration.rb', line 81

def primary(role)
  servers.fetch_primary(role)
end

#remove(key, *values) ⇒ Object



43
44
45
# File 'lib/capistrano/configuration.rb', line 43

def remove(key, *values)
  set(key, Array(fetch(key)) - values)
end

#role(name, hosts, options = {}) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/capistrano/configuration.rb', line 61

def role(name, hosts, options={})
  if name == :all
    raise ArgumentError, "#{name} reserved name for role. Please choose another name"
  end

  servers.add_role(name, hosts, options)
end

#role_properties_for(names, &block) ⇒ Object



77
78
79
# File 'lib/capistrano/configuration.rb', line 77

def role_properties_for(names, &block)
  servers.role_properties_for(names, &block)
end

#roles_for(names) ⇒ Object



73
74
75
# File 'lib/capistrano/configuration.rb', line 73

def roles_for(names)
  servers.roles_for(names)
end

#scm_plugin_installed?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/capistrano/configuration.rb', line 160

def scm_plugin_installed?
  installer.scm_installed?
end

#server(name, properties = {}) ⇒ Object



69
70
71
# File 'lib/capistrano/configuration.rb', line 69

def server(name, properties={})
  servers.add_host(name, properties)
end

#serversObject



164
165
166
# File 'lib/capistrano/configuration.rb', line 164

def servers
  @servers ||= Servers.new
end

#set_if_empty(key, value = nil, &block) ⇒ Object



35
36
37
# File 'lib/capistrano/configuration.rb', line 35

def set_if_empty(key, value=nil, &block)
  set(key, value, &block) unless keys.include?(key)
end

#setup_filtersObject



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/capistrano/configuration.rb', line 129

def setup_filters
  @filters = cmdline_filters
  @filters += @custom_filters if @custom_filters
  @filters << Filter.new(:role, ENV["ROLES"]) if ENV["ROLES"]
  @filters << Filter.new(:host, ENV["HOSTS"]) if ENV["HOSTS"]
  fh = fetch_for(:filter, {}) || {}
  @filters << Filter.new(:host, fh[:hosts]) if fh[:hosts]
  @filters << Filter.new(:role, fh[:roles]) if fh[:roles]
  @filters << Filter.new(:host, fh[:host]) if fh[:host]
  @filters << Filter.new(:role, fh[:role]) if fh[:role]
end

#timestampObject



109
110
111
# File 'lib/capistrano/configuration.rb', line 109

def timestamp
  @timestamp ||= Time.now.utc
end