Class: Formatron::CloudFormation::Template::VPC::Subnet::Instance::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/formatron/cloud_formation/template/vpc/subnet/instance/setup.rb

Overview

Adds setup scripts to an instance rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(setup:, sub_domain:, hosted_zone_name:, os:, wait_condition_handle:) ⇒ Setup

rubocop:disable Metrics/MethodLength



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/formatron/cloud_formation/template/vpc/subnet/instance/setup.rb', line 13

def initialize(
  setup:,
  sub_domain:,
  hosted_zone_name:,
  os:,
  wait_condition_handle:
)
  @setup = setup
  @wait_condition_handle = wait_condition_handle
  @sub_domain = sub_domain
  @hosted_zone_name = hosted_zone_name
  @scripts = @setup.script unless @setup.nil?
  @variables = @setup.variable unless @setup.nil?
  @os = os
end

Instance Method Details

#merge(instance:) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



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
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
# File 'lib/formatron/cloud_formation/template/vpc/subnet/instance/setup.rb', line 32

def merge(instance:)
  env = {}
  @variables.each do |key, value|
    env[key] = value.value
  end unless @variables.nil?
  if @os.eql? 'windows'
    script_key = 'script-0'
    script = "C:\\formatron\\#{script_key}.bat"
    files = {}
    commands = {}
    @scripts.each_index do |index|
      script_key = "script-#{index}"
      script = "C:\\formatron\\#{script_key}.bat"
      files[script] = {
        content: @scripts[index]
      }
      commands[script_key] = {
        command: script,
        env: env
      }
    end unless @scripts.nil?
    setup_script_index = @scripts.nil? ? 0 : @scripts.length
    signal_script_index = setup_script_index + 1
    script_key = "script-#{setup_script_index}"
    script = "C:\\formatron\\#{script_key}.bat"
    files[script] = {
      content: Scripts.windows_common(
        sub_domain: @sub_domain,
        hosted_zone_name: @hosted_zone_name
      )
    }
    commands[script_key] = {
      command: script,
      env: env,
      waitAfterCompletion: 'forever'
    }
    script_key = "script-#{signal_script_index}"
    script = "C:\\formatron\\#{script_key}.bat"
    files[script] = {
      content: Scripts.windows_signal(
        wait_condition_handle: @wait_condition_handle
      )
    }
    commands[script_key] = {
      command: script,
      env: env
    }
  else
    script_key = 'script-0'
    script = "/tmp/formatron/#{script_key}.sh"
    files = {
      "#{script}" => {
        content: Scripts.linux_common(
          sub_domain: @sub_domain,
          hosted_zone_name: @hosted_zone_name
        ),
        mode: '000755',
        owner: 'root',
        group: 'root'
      }
    }
    commands = {
      "#{script_key}" => {
        command: script,
        env: env
      }
    }
    @scripts.each_index do |index|
      script_key = "script-#{index + 1}"
      script = "/tmp/formatron/#{script_key}.sh"
      files[script] = {
        content: @scripts[index],
        mode: '000755',
        owner: 'root',
        group: 'root'
      }
      commands[script_key] = {
        command: script,
        env: env
      }
    end unless @scripts.nil?
  end
  instance[:Metadata] = {
    Comment1: 'Create setup scripts',
    'AWS::CloudFormation::Init' => {
      config: {
        files: files,
        commands: commands
      }
    }
  }
end