Class: Kuby::Plugins::RailsApp::Postgres

Inherits:
Kuby::Plugin show all
Defined in:
lib/kuby/plugins/rails_app/postgres.rb

Constant Summary collapse

ROLE =
'web'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Kuby::Plugin

#after_deploy, #after_setup, #before_deploy, #before_setup, #configure, #dockerfiles, #setup

Constructor Details

#initialize(environment, configs) ⇒ Postgres

Returns a new instance of Postgres.



13
14
15
16
17
18
19
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 13

def initialize(environment, configs)
  @environment = environment
  @configs = configs

  user(config['username'])
  password(config['password'])
end

Instance Attribute Details

#configsObject (readonly)

Returns the value of attribute configs.



11
12
13
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 11

def configs
  @configs
end

#environmentObject (readonly)

Returns the value of attribute environment.



11
12
13
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 11

def environment
  @environment
end

Instance Method Details

#after_configurationObject



29
30
31
32
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 29

def after_configuration
  environment.docker.package_phase.add(:postgres_dev)
  environment.docker.package_phase.add(:postgres_client)
end

#base_nameObject



118
119
120
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 118

def base_name
  @base_name ||= "#{kubernetes.selector_app}-#{ROLE}"
end

#database(&block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 78

def database(&block)
  context = self

  @database ||= Kuby::KubeDB.postgres do
    api_version 'kubedb.com/v1alpha1'

     do
      name "#{context.base_name}-postgres"
      namespace context.kubernetes.namespace..name
    end

    spec do
      database_secret do
        secret_name context.secret..name
      end

      version '11.2'
      standby_mode 'Hot'
      streaming_mode 'asynchronous'
      storage_type 'Durable'

      storage do
        storage_class_name context.kubernetes.provider.storage_class_name
        access_modes ['ReadWriteOnce']

        resources do
          requests do
            add :storage, '10Gi'
          end
        end
      end

      termination_policy 'DoNotTerminate'
    end
  end

  @database.instance_eval(&block) if block
  @database
end

#hostObject



34
35
36
37
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 34

def host
  # host is the same as the name thanks to k8s DNS
  @host ||= database..name
end

#kubernetesObject



122
123
124
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 122

def kubernetes
  environment.kubernetes
end

#nameObject



21
22
23
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 21

def name
  :postgres
end

#password(password) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 54

def password(password)
  secret do
    data do
      set :POSTGRES_PASSWORD, password
    end
  end
end

#resourcesObject



25
26
27
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 25

def resources
  @resources ||= [secret, database]
end

#rewritten_configsObject



39
40
41
42
43
44
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 39

def rewritten_configs
  # deep dup
  @rewritten_configs ||= Marshal.load(Marshal.dump(configs)).tap do |new_configs|
    new_configs[environment.name]['host'] = host
  end
end

#secret(&block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 62

def secret(&block)
  context = self

  @secret ||= KubeDSL.secret do
     do
      name "#{context.base_name}-postgres-secret"
      namespace context.kubernetes.namespace..name
    end

    type 'Opaque'
  end

  @secret.instance_eval(&block) if block
  @secret
end

#user(user) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 46

def user(user)
  secret do
    data do
      set :POSTGRES_USER, user
    end
  end
end