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



89
90
91
# File 'lib/capistrano/configuration.rb', line 89

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



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

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

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/capistrano/configuration.rb', line 117

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
55
56
57
58
# File 'lib/capistrano/configuration.rb', line 47

def any?(key)
  value = fetch(key)
  if value && value.respond_to?(:any?)
    begin
      return value.any?
    rescue ArgumentError # rubocop:disable Lint/HandleExceptions
      # Gracefully ignore values whose `any?` method doesn't accept 0 args
    end
  end

  !value.nil?
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



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/capistrano/configuration.rb', line 95

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



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

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

#dry_run?Boolean

Returns:

  • (Boolean)


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

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

#filter(list) ⇒ Object



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

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



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

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)


60
61
62
63
# File 'lib/capistrano/configuration.rb', line 60

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

#primary(role) ⇒ Object



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

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



65
66
67
68
69
70
71
# File 'lib/capistrano/configuration.rb', line 65

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



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

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

#roles_for(names) ⇒ Object



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

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

#scm_plugin_installed?Boolean

Returns:

  • (Boolean)


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

def scm_plugin_installed?
  installer.scm_installed?
end

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



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

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

#serversObject



168
169
170
# File 'lib/capistrano/configuration.rb', line 168

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



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/capistrano/configuration.rb', line 133

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



113
114
115
# File 'lib/capistrano/configuration.rb', line 113

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