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.



24
25
26
27
# File 'lib/mrsk/configuration.rb', line 24

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

Class Method Details

.argumentize(argument, attributes) ⇒ Object



19
20
21
# File 'lib/mrsk/configuration.rb', line 19

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

.load_file(file) ⇒ Object



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

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



70
71
72
# File 'lib/mrsk/configuration.rb', line 70

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

#env_argsObject



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

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

#hostsObject



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

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



95
96
97
# File 'lib/mrsk/configuration.rb', line 95

def master_key
  ENV["RAILS_MASTER_KEY"] || File.read(Pathname.new(File.expand_path("config/master.key")))
end

#primary_hostObject



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

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

#repositoryObject



66
67
68
# File 'lib/mrsk/configuration.rb', line 66

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

#role(name) ⇒ Object



34
35
36
# File 'lib/mrsk/configuration.rb', line 34

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

#rolesObject



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

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

#service_with_versionObject



74
75
76
# File 'lib/mrsk/configuration.rb', line 74

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

#ssh_optionsObject



91
92
93
# File 'lib/mrsk/configuration.rb', line 91

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

#ssh_userObject



87
88
89
# File 'lib/mrsk/configuration.rb', line 87

def ssh_user
  config.ssh_user || "root"
end

#versionObject



62
63
64
# File 'lib/mrsk/configuration.rb', line 62

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