Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create-demo-cluster « terraform « _support - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e8a57e3c4349c1e720e7d1c85af8d3cd982f8d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'erb'
require 'etc'
require 'io/console'
require 'json'

require_relative 'helper.rb'

TFVARS = 'terraform.tfvars'
HOSTS = 'hosts.ini'

def main
  unless gcloud_appliction_default_logged_in?
    run!(%w[gcloud auth application-default login])
  end

  run!(%w[terraform get])

  unless terraform_initialized?
    run!(%w[terraform init])
  end

  unless File.exist?(TFVARS)
    render!(TFVARS, 'terraform.tfvars.erb')
  end

  run!(%w[terraform apply])

  @tfstate = JSON.parse(File.read("terraform.tfstate"))

  unless File.exist?(HOSTS)
    render!(HOSTS, 'hosts.ini.erb')
  end
end

def praefect_demo_cluster_name
  default_name = "#{username}-#{Time.now.utc.strftime('%Y%m%d')}"
  get_input('Enter a name for your demo cluster', default_name)
end

def praefect_sql_password
  @praefect_sql_password ||= get_input(
    'Enter a password for the praefect PostgreSQL user',
    'PRAEFECT_SQL_PASSWORD',
    echo: false
  )
end

def gitlab_root_password
  get_input(
    'Enter a password for the root GitLab user',
    'GITLAB_ROOT_PASSWORD',
    echo: false
  )
end

def username
  Etc.getlogin
end

def ssh_pubkey
  default_path = File.join(Etc.getpwnam(username).dir, '.ssh/id_rsa.pub')
  pubkey_path = get_input('Enter the path to your SSH public key', default_path)
  pubkey = File.read(pubkey_path).chomp

  unless pubkey.start_with?('ssh-')
    # Protect against accidentally using the private key
    abort "contents of #{path} do not look like an SSH pubkey"
  end

  pubkey
end

def get_input(prompt, default, echo: true)
  puts "#{prompt} (default: #{default})."
  print "> "

  input = echo ? gets.chomp : STDIN.noecho(&:gets).chomp

  input.empty? ? default : input
end

def render!(file, template_path)
  IO.write(file, ERB.new(File.read(template_path)).result(binding))
end

def gcloud_appliction_default_logged_in?
  system(
    *%w[gcloud auth application-default print-access-token],
    out: '/dev/null',
    err: '/dev/null'
  )
end

main