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, #docker_images, #setup

Constructor Details

#initialize(environment, configs) ⇒ Postgres

Returns a new instance of Postgres.



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

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.



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

def configs
  @configs
end

#environmentObject (readonly)

Returns the value of attribute environment.



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

def environment
  @environment
end

Instance Method Details

#after_configurationObject



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

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

#base_nameObject



137
138
139
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 137

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

#database(&block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 97

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



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

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

#kubernetesObject



141
142
143
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 141

def kubernetes
  environment.kubernetes
end

#nameObject



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

def name
  :postgres
end

#password(password) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 59

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

#requires_credentials?Boolean

Returns:

  • (Boolean)


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

def requires_credentials?
  true
end

#resourcesObject



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

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

#rewritten_configsObject



44
45
46
47
48
49
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 44

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 81

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

#storage(amount) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 67

def storage(amount)
  database do
    spec do
      storage do
        resources do
          requests do
            set :storage, amount
          end
        end
      end
    end
  end
end

#user(user) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/kuby/plugins/rails_app/postgres.rb', line 51

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