|
1 |
| -configuration.rb |
| 1 | +require_relative 'configuration/filter' |
| 2 | +require_relative 'configuration/question' |
| 3 | +require_relative 'configuration/server' |
| 4 | +require_relative 'configuration/servers' |
| 5 | + |
| 6 | +module Ferry |
| 7 | + class Configuration |
| 8 | + |
| 9 | + def initialize(config = nil) |
| 10 | + @config ||= config |
| 11 | + end |
| 12 | + |
| 13 | + def self.env |
| 14 | + @env ||= new |
| 15 | + end |
| 16 | + |
| 17 | + def self.reset! |
| 18 | + @env = new |
| 19 | + end |
| 20 | + |
| 21 | + def ask(key, default=nil, options={}) |
| 22 | + question = Question.new(key, default, options) |
| 23 | + set(key, question) |
| 24 | + end |
| 25 | + |
| 26 | + def set(key, value) |
| 27 | + config[key] = value |
| 28 | + end |
| 29 | + |
| 30 | + def set_if_empty(key, value) |
| 31 | + config[key] = value unless config.has_key? key |
| 32 | + end |
| 33 | + |
| 34 | + def delete(key) |
| 35 | + config.delete(key) |
| 36 | + end |
| 37 | + |
| 38 | + def fetch(key, default=nil, &block) |
| 39 | + value = fetch_for(key, default, &block) |
| 40 | + while callable_without_parameters?(value) |
| 41 | + value = set(key, value.call) |
| 42 | + end |
| 43 | + return value |
| 44 | + end |
| 45 | + |
| 46 | + def keys |
| 47 | + config.keys |
| 48 | + end |
| 49 | + |
| 50 | + def role(name, hosts, options={}) |
| 51 | + if name == :all |
| 52 | + raise ArgumentError.new("#{name} reserved name for role. Please choose another name") |
| 53 | + end |
| 54 | + |
| 55 | + servers.add_role(name, hosts, options) |
| 56 | + end |
| 57 | + |
| 58 | + def server(name, properties={}) |
| 59 | + servers.add_host(name, properties) |
| 60 | + end |
| 61 | + |
| 62 | + def roles_for(names) |
| 63 | + servers.roles_for(names) |
| 64 | + end |
| 65 | + |
| 66 | + def role_properties_for(names, &block) |
| 67 | + servers.role_properties_for(names, &block) |
| 68 | + end |
| 69 | + |
| 70 | + def primary(role) |
| 71 | + servers.fetch_primary(role) |
| 72 | + end |
| 73 | + |
| 74 | + def backend |
| 75 | + @backend ||= SSHKit |
| 76 | + end |
| 77 | + |
| 78 | + attr_writer :backend |
| 79 | + |
| 80 | + def configure_backend |
| 81 | + backend.configure do |sshkit| |
| 82 | + sshkit.format = fetch(:format) |
| 83 | + sshkit.output_verbosity = fetch(:log_level) |
| 84 | + sshkit.default_env = fetch(:default_env) |
| 85 | + sshkit.backend = fetch(:sshkit_backend, SSHKit::Backend::Netssh) |
| 86 | + sshkit.backend.configure do |backend| |
| 87 | + backend.pty = fetch(:pty) |
| 88 | + backend.connection_timeout = fetch(:connection_timeout) |
| 89 | + backend.ssh_options = (backend.ssh_options || {}).merge(fetch(:ssh_options,{})) |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def timestamp |
| 95 | + @timestamp ||= Time.now.utc |
| 96 | + end |
| 97 | + |
| 98 | + def setup_filters |
| 99 | + @filters = cmdline_filters.clone |
| 100 | + @filters << Filter.new(:role, ENV['ROLES']) if ENV['ROLES'] |
| 101 | + @filters << Filter.new(:host, ENV['HOSTS']) if ENV['HOSTS'] |
| 102 | + fh = fetch_for(:filter,{}) || {} |
| 103 | + @filters << Filter.new(:host, fh[:host]) if fh[:host] |
| 104 | + @filters << Filter.new(:role, fh[:role]) if fh[:role] |
| 105 | + end |
| 106 | + |
| 107 | + def add_cmdline_filter(type, values) |
| 108 | + cmdline_filters << Filter.new(type, values) |
| 109 | + end |
| 110 | + |
| 111 | + def filter list |
| 112 | + setup_filters if @filters.nil? |
| 113 | + @filters.reduce(list) { |l,f| f.filter l } |
| 114 | + end |
| 115 | + |
| 116 | + private |
| 117 | + |
| 118 | + def cmdline_filters |
| 119 | + @cmdline_filters ||= [] |
| 120 | + end |
| 121 | + |
| 122 | + def servers |
| 123 | + @servers ||= Servers.new |
| 124 | + end |
| 125 | + |
| 126 | + def config |
| 127 | + @config ||= Hash.new |
| 128 | + end |
| 129 | + |
| 130 | + def fetch_for(key, default, &block) |
| 131 | + if block_given? |
| 132 | + config.fetch(key, &block) |
| 133 | + else |
| 134 | + config.fetch(key, default) |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + def callable_without_parameters?(x) |
| 139 | + x.respond_to?(:call) && ( !x.respond_to?(:arity) || x.arity == 0) |
| 140 | + end |
| 141 | + end |
| 142 | +end |
0 commit comments