Class: Mrsk::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/mrsk/configuration.rb

Defined Under Namespace

Classes: Role

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, validate: true) ⇒ Configuration

Returns a new instance of Configuration.



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

def initialize(config, validate: true)
  @config = ActiveSupport::InheritableOptions.new(config)
  ensure_required_keys_present if validate
end

Class Method Details

.argumentize(argument, attributes) ⇒ Object



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

def argumentize(argument, attributes)
  attributes.flat_map { |k, v| [ argument, "#{k}=#{v}" ] }
end

.load_file(file) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/mrsk/configuration.rb', line 9

def load_file(file)
  if file.exist?
    new YAML.load(ERB.new(IO.read(file)).result).symbolize_keys
  else
    raise "Configuration file not found in #{file}"
  end
end

Instance Method Details

#absolute_imageObject



68
69
70
# File 'lib/mrsk/configuration.rb', line 68

def absolute_image
  "#{repository}:#{version}"
end

#env_argsObject



77
78
79
80
81
82
83
# File 'lib/mrsk/configuration.rb', line 77

def env_args
  if config.env.present?
    self.class.argumentize "-e", config.env
  else
    []
  end
end

#hostsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mrsk/configuration.rb', line 36

def hosts
  hosts =
    case
    when ENV["HOSTS"]
      ENV["HOSTS"].split(",")
    when ENV["ROLES"]
      role_names = ENV["ROLES"].split(",")
      roles.select { |r| role_names.include?(r.name) }.flat_map(&:hosts)
    else
      roles.flat_map(&:hosts)
    end

    if hosts.any?
      hosts
    else
      raise ArgumentError, "No hosts found"
    end
end

#master_keyObject



93
94
95
# File 'lib/mrsk/configuration.rb', line 93

def master_key
  ENV["RAILS_MASTER_KEY"] || File.read(Rails.root.join("config/master.key"))
end

#primary_hostObject



55
56
57
# File 'lib/mrsk/configuration.rb', line 55

def primary_host
  role(:web).hosts.first
end

#repositoryObject



64
65
66
# File 'lib/mrsk/configuration.rb', line 64

def repository
  [ config.registry["server"], image ].compact.join("/")
end

#role(name) ⇒ Object



32
33
34
# File 'lib/mrsk/configuration.rb', line 32

def role(name)
  roles.detect { |r| r.name == name.to_s }
end

#rolesObject



28
29
30
# File 'lib/mrsk/configuration.rb', line 28

def roles
  @roles ||= role_names.collect { |role_name| Role.new(role_name, config: self) }
end

#service_with_versionObject



72
73
74
# File 'lib/mrsk/configuration.rb', line 72

def service_with_version
  "#{service}-#{version}"
end

#ssh_optionsObject



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

def ssh_options
  { user: ssh_user, auth_methods: [ "publickey" ] }
end

#ssh_userObject



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

def ssh_user
  config.ssh_user || "root"
end

#versionObject



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

def version
  @version ||= ENV["VERSION"] || `git rev-parse HEAD`.strip
end