Class: InfraCommand

Inherits:
PangeaCommand show all
Includes:
Constants
Defined in:
lib/pangea/cli/subcommands/infra.rb

Constant Summary collapse

NAME =
:infra

Constants included from Constants

Constants::ARTIFACT_FILE, Constants::CACHE_DIR, Constants::EXTENSIONS, Constants::PROJECT_SRC_DIRS, Constants::PROJECT_VERSION

Instance Method Summary collapse

Instance Method Details

#cfg_synthObject



60
61
62
# File 'lib/pangea/cli/subcommands/infra.rb', line 60

def cfg_synth
  @cfg_synth ||= Config.resolve_configurations
end

#common_run_checksObject



90
91
92
# File 'lib/pangea/cli/subcommands/infra.rb', line 90

def common_run_checks
  reject_empty_configurations
end

#configObject



86
87
88
# File 'lib/pangea/cli/subcommands/infra.rb', line 86

def config
  Say.terminal JSON.pretty_generate(cfg_synth)
end

#helpObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pangea/cli/subcommands/infra.rb', line 38

def help
  <<~HELP
    Usage: pangea infra command [OPTIONS] SUBCOMMAND

    Arguments:
      SUBCOMMAND  subcommand for pangea infra

    Subcommands:
      plan    plan infrastructure
      apply   apply infrastructure
      config  show run configuration
  HELP
end

#pangea_homeObject



52
53
54
# File 'lib/pangea/cli/subcommands/infra.rb', line 52

def pangea_home
  File.join(Dir.home, %(.pangea))
end

#plan(argv) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pangea/cli/subcommands/infra.rb', line 64

def plan(argv)
  Say.terminal %(planning!)
  parse(argv)
  namespaces = cfg_synth[:namespace].keys.map(&:to_sym)
  namespaces.each do |namespace|
    system(%(mkdir -p #{File.join(terraform_files_home.to_s, namespace.to_s)})) unless Dir.exist?(
      File.join(
        terraform_files_home.to_s, namespace.to_s
      )
    )
    processed = process_modules(ns)
    template = processed[:template]
    ns_home = File.join(terraform_files_home, ns.to_s)
    File.write(
      File.join(ns_home, %(template.tf.json)),
      JSON[template]
    )
    system(%(cd #{ns_home} && terraform init))
    system(%(cd #{ns_home} && terraform plan))
  end
end

#process_modules(namespace) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pangea/cli/subcommands/infra.rb', line 94

def process_modules(namespace)
  template = {}
  namespaces = cfg_synth[:namespace].keys.map(&:to_sym)
  namespaces.each do |namespace_name|
    ns = cfg_synth[:namespace][namespace_name]
    context_names = ns.keys.map(&:to_sym)

    context_names.each do |cn|
      context = ns[cn]
      modules = context[:modules]

      next unless namespace_name.to_s.eql?(namespace.to_s)

      modules.each_key do |mod_key|
        mod = modules[mod_key]
        template.merge!(PangeaModule.process(mod))
      end
    end
  end
  { template: template, namespaces: cfg_synth[:namespace].keys.map(&:to_sym) }
end

#reject_empty_configurationsObject



31
32
33
34
35
36
# File 'lib/pangea/cli/subcommands/infra.rb', line 31

def reject_empty_configurations
  if cfg_synth.empty?
    Say.terminal %(configuration empty, exiting...)
    exit
  end
end

#run(argv) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/pangea/cli/subcommands/infra.rb', line 116

def run(argv)
  common_run_checks

  if argv.length < 2
    puts help
    exit
  end

  config if argv[1].to_s.eql?(%(config))
  plan(argv) if argv[1].to_s.eql?(%(plan))
  apply(argv) if argv[1].to_s.eql?(%(apply))

  # preflight checks for the command execution
  # check_run
  # check_target(params[:target], cfg_synth)

  # targets = params[:target].split('.').map(&:to_sym)
  # process_target(targets, cfg_synth)

  ###########################################################################
  # modules
  # modules can be fetched from git or local path
  # modules can have a virtual environment to execute in
  ###########################################################################

  # namespaces = cfg_synth[:namespace].keys.map(&:to_sym)

  # namespaces.each do |namespace_name|
  #   namespace     = cfg_synth[:namespace][namespace_name]
  #   context_names = namespace.keys.map(&:to_sym)
  #
  #   context_names.each do |cn|
  #     context = namespace[cn]
  #     modules = context[:modules]
  #
  #     modules.each do |mod|
  #       PangeaModule.process(mod)
  #     end
  #   end
  # end

  ###########################################################################

  # provide some kind of default exit of the command execution
  # exit
end

#terraform_files_homeObject



56
57
58
# File 'lib/pangea/cli/subcommands/infra.rb', line 56

def terraform_files_home
  File.join(pangea_home, %(terraform))
end