Class: Idlc::Deploy::Config

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/idlc-sdk-deploy/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region) ⇒ Config

Returns a new instance of Config.



147
148
149
150
151
152
153
154
# File 'lib/idlc-sdk-deploy/config.rb', line 147

def initialize(region)
  @region = region

  Idlc::Utility.check_for_creds

rescue Idlc::Utility::MissingCredentials => e
  msg("WARN: #{e.message}\nFalling back to implicit authentication.")
end

Class Method Details

.add_deployment_var(key, value) ⇒ Object



13
14
15
# File 'lib/idlc-sdk-deploy/config.rb', line 13

def add_deployment_var(key, value)
  ENV["TF_VAR_#{key}"] = value
end

.get_deployment_output(key) ⇒ Object



21
22
23
# File 'lib/idlc-sdk-deploy/config.rb', line 21

def get_deployment_output(key)
  `#{Terraform::Binary::Command.binary} output #{key}`.strip!
end

.get_deployment_var(key) ⇒ Object



17
18
19
# File 'lib/idlc-sdk-deploy/config.rb', line 17

def get_deployment_var(key)
  ENV["TF_VAR_#{key}"]
end

.get_env_metadata(env_key) ⇒ Object



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
# File 'lib/idlc-sdk-deploy/config.rb', line 41

def (env_key)
  client = Idlc::AWSLambdaProxy.new()

  request = {
    service: 'deploy',
    method: 'GET',
    lambda: 'metadata',
    pathParameters: {
      jobName: env_key
    }
  }
   = client.fetch(request)['deployments'].first

  request = {
    service: 'config',
    method: 'GET',
    lambda: "accounts",
    pathParameters: {
      accountName: ['environment']['account_alias']
    }
  }
   = client.fetch(request)

  ['account'] = ['accounts'].first

  request = {
    service: 'config',
    method: 'GET',
    lambda: "applications",
    pathParameters: {
      appName: ['environment']['application_name'].downcase
    }
  }
  application = client.fetch(request)

  ['application'] = application['applications'].first

  # find db and fs instance
  ['instances'].each do |instance|
    if (instance['hostname'].start_with?('db') || instance['hostname'].start_with?('rds'))
      ['db_instance'] = instance
    end

    if (instance['hostname'].start_with?('fs'))
      ['fs_instance'] = instance
    end
  end

  
end

.get_instanceObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/idlc-sdk-deploy/config.rb', line 101

def get_instance
  # Get the current instance id from the instance metadata.
   = 'http://169.254.169.254/latest/meta-data/'
  instance_id = Net::HTTP.get( URI.parse(  + 'instance-id' ) )

  # Create instance object with instance id.
  instance = Aws::EC2::Instance.new( id: instance_id, region: ENV['AWS_REGION'] )

  # save some instance info
  i = {}
  i['instance_id'] = instance_id

  # save tags
  i['tags'] = {}
  instance.tags.each do |tag|
    # Grab all of the tags as node attributes
    i['tags'][tag.key] = tag.value
  end

  i
end

.get_regionObject



92
93
94
95
96
97
98
99
# File 'lib/idlc-sdk-deploy/config.rb', line 92

def get_region
  # Get the current az from the instance metadata.
   = 'http://169.254.169.254/latest/meta-data/'
  az = Net::HTTP.get( URI.parse(  + 'placement/availability-zone' ) )

  # return
  az[0..-2]
end

.load_tasksObject



7
8
9
10
11
# File 'lib/idlc-sdk-deploy/config.rb', line 7

def load_tasks
  Dir.glob("#{__dir__}/tasks/*.rake").each do |task_file|
    load task_file
  end
end

.set_hostname(instance) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/idlc-sdk-deploy/config.rb', line 123

def set_hostname (instance)
  hostname = instance['tags']['Name']

  unless (instance['tags']['Name'].start_with?('db') || instance['tags']['Name'].start_with?('fs'))
    # Use instance id for unique hostname
    hostname = instance['tags']['Name'][0..4] + '-' + instance['instance_id'][2..10]
  end

  ec2_instance = Aws::EC2::Instance.new( id: instance['instance_id'], region: ENV['AWS_REGION'] )
  ec2_instance.create_tags(
    dry_run: false,
    tags: [ # required
      {
        key: 'hostname',
        value: hostname
      }
    ]
  )

  #return
  hostname
end

.whoamiObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/idlc-sdk-deploy/config.rb', line 25

def whoami
  # This method is meant to be run on an instance inside of a chef run to
  # provision instance and environment metadata.

  ENV['AWS_REGION'] = get_region

  # Get the current instance id from the instance metadata.
  instance = get_instance

  # return environment metadata
   = (instance['tags']['environment_key'])
  ['hostname'] = set_hostname(instance)

  
end

Instance Method Details

#configure_state(bucket, sub_bucket, working_directory) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/idlc-sdk-deploy/config.rb', line 156

def configure_state(bucket, sub_bucket, working_directory)
  validate_environment

  tf_version = Terraform::Binary::config.version.split('.')

  configure_tfstatev8(bucket, sub_bucket, working_directory) if tf_version[0].to_i == 0 && tf_version[1].to_i <= 8
  configure_tfstatev9(bucket, sub_bucket, working_directory) if tf_version[0].to_i >= 0 && tf_version[1].to_i > 8
end

#parse(config_file) ⇒ Object

Raises:

  • (ArgumentError)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/idlc-sdk-deploy/config.rb', line 165

def parse(config_file)
  raise ArgumentError, "#{config_file} does not exist" unless File.exist? config_file
  Config.add_deployment_var('inf_config_file', config_file)

  # Parse the config file
  YAML.load_file(config_file)['configuration'].each do |section, body|
    next if section == 'dynamics' # skip the dynamics sections
    next unless (section =~ /overrides/).nil? # skip the app overrides sections
    next if body.nil?
    body.each do |key, value|
      debug("#{section}: #{key} = #{value}")
      Config.add_deployment_var(key, value)
    end
  end
end