Class: HerdstWorker::Configuration::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/herdst_worker/configuration/metadata.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, name, config) ⇒ Metadata

Returns a new instance of Metadata.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/herdst_worker/configuration/metadata.rb', line 35

def initialize(env, name, config)
    self.config = config
    self.config_suffix = "-#{name}-service-config"
    self.secrets_suffix = "-#{name}-service-secrets"
    
    self.secrets = {}.with_indifferent_access
    self.secrets["ENV"] = env
    self.secret_json_keys = ["STRIPE_PLANS"]

    Aws::Credentials.include HerdstWorker::ExpiringCredentials

    begin
        self.reload!
    rescue Exception => ex
        if self.is_prod?
            puts "Failed to load metadata: #{ex.message}"
        else
            raise ex
        end
    end
end

Instance Attribute Details

#aws_credentialsObject

Returns the value of attribute aws_credentials.



31
32
33
# File 'lib/herdst_worker/configuration/metadata.rb', line 31

def aws_credentials
  @aws_credentials
end

#configObject

Returns the value of attribute config.



31
32
33
# File 'lib/herdst_worker/configuration/metadata.rb', line 31

def config
  @config
end

#config_suffixObject

Returns the value of attribute config_suffix.



32
33
34
# File 'lib/herdst_worker/configuration/metadata.rb', line 32

def config_suffix
  @config_suffix
end

#secret_json_keysObject

Returns the value of attribute secret_json_keys.



32
33
34
# File 'lib/herdst_worker/configuration/metadata.rb', line 32

def secret_json_keys
  @secret_json_keys
end

#secretsObject

Returns the value of attribute secrets.



31
32
33
# File 'lib/herdst_worker/configuration/metadata.rb', line 31

def secrets
  @secrets
end

#secrets_suffixObject

Returns the value of attribute secrets_suffix.



32
33
34
# File 'lib/herdst_worker/configuration/metadata.rb', line 32

def secrets_suffix
  @secrets_suffix
end

Instance Method Details

#[](key) ⇒ Object



72
73
74
# File 'lib/herdst_worker/configuration/metadata.rb', line 72

def [](key)
    self.secrets[key.to_s.upcase]
end

#[]=(key, value) ⇒ Object



77
78
79
# File 'lib/herdst_worker/configuration/metadata.rb', line 77

def []=(key, value)
    self.secrets[key.to_s.upcase] = value
end

#get_aws_credentialsObject



82
83
84
# File 'lib/herdst_worker/configuration/metadata.rb', line 82

def get_aws_credentials
    self.get_aws_credentials! rescue nil
end

#get_aws_credentials!Object



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
# File 'lib/herdst_worker/configuration/metadata.rb', line 123

def get_aws_credentials!
    credentials = self.is_prod? ? 
         make_request!(get_credentials_uri) :
         self.config.config_for(:aws)
    
    expiry_time = (Time.parse(credentials["Expiration"]).utc - 5.minutes) rescue (Time.now.utc + 1.hour)

    # Set credential info
    self.aws_credentials = Aws::Credentials.new(
        credentials["AccessKeyId"], 
        credentials["SecretAccessKey"], 
        credentials["Token"]
    )
    self.aws_credentials.set_expiry(expiry_time.to_i)

    # Set global config
    Aws.config.update(
        region: self.get_aws_region(credentials),
        credentials: self.aws_credentials
    )

    if self.is_prod?
        # Refresh the credentials just before the expiry time
        expiry_in_seconds = (expiry_time - Time.now.utc).to_i
        expiry_in_seconds = 60 if expiry_in_seconds < 60

        Concurrent::ScheduledTask.execute(expiry_in_seconds) do
            self.get_aws_credentials
        end
    end

    self.aws_credentials
end

#get_aws_region(credentials = nil) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/herdst_worker/configuration/metadata.rb', line 158

def get_aws_region(credentials = nil)
    default_region = "ap-southeast-2"
    
    if self.is_prod?            
        self["AWS_REGION"] = (ENV["AWS_REGION"] || default_region)
    else
        self["AWS_REGION"] = credentials && credentials["Region"] || default_region
    end
end

#get_secretsObject



67
68
69
# File 'lib/herdst_worker/configuration/metadata.rb', line 67

def get_secrets
    self.secrets
end

#get_services_infoObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/herdst_worker/configuration/metadata.rb', line 105

def get_services_info
    info = self.get_tasks_info

    ecs_client = Aws::ECS::Client.new(
        :region => self.secrets["AWS_REGION"],
        :credentials => self.aws_credentials
    )

    ecs_client.describe_services({
        :cluster => info["tasks"][0]["cluster_arn"],
        :services => [
            info["tasks"][0]["group"].to_s.sub("service:", "")
        ],
        :include => ["TAGS"]
    })
end

#get_tasks_infoObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/herdst_worker/configuration/metadata.rb', line 87

def get_tasks_info
     = make_request!()

    ecs_client = Aws::ECS::Client.new(
        :region => self.secrets["AWS_REGION"],
        :credentials => self.aws_credentials
    )
    
    ecs_client.describe_tasks(
        :cluster => ["Cluster"],
        :tasks => [
            ["TaskARN"]
        ],
        :include => ["TAGS"]
    )
end

#reload!Object



58
59
60
61
62
63
64
# File 'lib/herdst_worker/configuration/metadata.rb', line 58

def reload!
    self.get_aws_credentials!
    self.set_task_info!
    self.get_config_and_secrets!
    
    self
end