Class: Seira::Db::Create

Inherits:
Object
  • Object
show all
Includes:
Commands
Defined in:
lib/seira/db/create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#gcloud, gcloud, kubectl, #kubectl, #tsh, tsh

Constructor Details

#initialize(app:, action:, args:, context:) ⇒ Create

Returns a new instance of Create.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/seira/db/create.rb', line 12

def initialize(app:, action:, args:, context:)
  @app = app
  @action = action
  @args = args
  @context = context

  # We allow overriding the version, so you could specify a mysql version but much of the
  # below assumes postgres for now
  @version = 'POSTGRES_9_6'
  @cpu = 1 # Number of CPUs
  @memory = 4 # GB
  @storage = 10 # GB
  @replica_for = nil
  @make_highly_available = false

  @root_password = nil
  @proxyuser_password = nil
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



6
7
8
# File 'lib/seira/db/create.rb', line 6

def action
  @action
end

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/seira/db/create.rb', line 6

def app
  @app
end

#argsObject (readonly)

Returns the value of attribute args.



6
7
8
# File 'lib/seira/db/create.rb', line 6

def args
  @args
end

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/seira/db/create.rb', line 6

def context
  @context
end

#cpuObject (readonly)

Returns the value of attribute cpu.



8
9
10
# File 'lib/seira/db/create.rb', line 8

def cpu
  @cpu
end

#make_highly_availableObject (readonly)

Returns the value of attribute make_highly_available.



8
9
10
# File 'lib/seira/db/create.rb', line 8

def make_highly_available
  @make_highly_available
end

#memoryObject (readonly)

Returns the value of attribute memory.



8
9
10
# File 'lib/seira/db/create.rb', line 8

def memory
  @memory
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/seira/db/create.rb', line 8

def name
  @name
end

#prefixObject (readonly)

Returns the value of attribute prefix.



10
11
12
# File 'lib/seira/db/create.rb', line 10

def prefix
  @prefix
end

#proxyuser_passwordObject (readonly)

Returns the value of attribute proxyuser_password.



9
10
11
# File 'lib/seira/db/create.rb', line 9

def proxyuser_password
  @proxyuser_password
end

#replica_forObject (readonly)

Returns the value of attribute replica_for.



8
9
10
# File 'lib/seira/db/create.rb', line 8

def replica_for
  @replica_for
end

#root_passwordObject (readonly)

Returns the value of attribute root_password.



9
10
11
# File 'lib/seira/db/create.rb', line 9

def root_password
  @root_password
end

#storageObject (readonly)

Returns the value of attribute storage.



8
9
10
# File 'lib/seira/db/create.rb', line 8

def storage
  @storage
end

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/seira/db/create.rb', line 8

def version
  @version
end

Instance Method Details

#add(existing_instances) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/seira/db/create.rb', line 39

def add(existing_instances)
  if !args.empty? && (args[0].start_with? '--prefix=')
    @prefix = args[0].split('=')[1]
  end

  if prefix.nil?
    puts "missing --prefix= for add command. Must be the first argument."
    exit(1)
  end

  # remove prefix from the head of the list since we don't want to pass it to gcloud
  args.pop

  @name = "#{app}-#{prefix}-#{Seira::Random.unique_name(existing_instances)}"
  puts "Attempting to create #{name}"
  run_create_command

  update_root_password
  create_proxy_user

  secrets_name = "#{name}-credentials"
  kubectl("create secret generic  #{secrets_name} --from-literal=ROOT_PASSWORD=#{root_password} --from-literal=PROXYUSER_PASSWORD=#{proxyuser_password}", context: context)
  puts "Credentials were saved in #{secrets_name}"
end

#configure(instance_name, master_name) ⇒ Object



64
65
66
67
68
69
# File 'lib/seira/db/create.rb', line 64

def configure(instance_name, master_name)
  @name = instance_name
  @replica_for = master_name

  configure_created_db
end

#run(existing_instances) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/seira/db/create.rb', line 31

def run(existing_instances)
  @name = "#{app}-#{Seira::Random.unique_name(existing_instances)}"

  run_create_command

  configure_created_db
end