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.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/herdst_worker/configuration/metadata.rb', line 16

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"]

    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.



12
13
14
# File 'lib/herdst_worker/configuration/metadata.rb', line 12

def aws_credentials
  @aws_credentials
end

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/herdst_worker/configuration/metadata.rb', line 12

def config
  @config
end

#config_suffixObject

Returns the value of attribute config_suffix.



13
14
15
# File 'lib/herdst_worker/configuration/metadata.rb', line 13

def config_suffix
  @config_suffix
end

#secret_json_keysObject

Returns the value of attribute secret_json_keys.



13
14
15
# File 'lib/herdst_worker/configuration/metadata.rb', line 13

def secret_json_keys
  @secret_json_keys
end

#secretsObject

Returns the value of attribute secrets.



12
13
14
# File 'lib/herdst_worker/configuration/metadata.rb', line 12

def secrets
  @secrets
end

#secrets_suffixObject

Returns the value of attribute secrets_suffix.



13
14
15
# File 'lib/herdst_worker/configuration/metadata.rb', line 13

def secrets_suffix
  @secrets_suffix
end

Instance Method Details

#[](key) ⇒ Object



51
52
53
# File 'lib/herdst_worker/configuration/metadata.rb', line 51

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

#[]=(key, value) ⇒ Object



56
57
58
# File 'lib/herdst_worker/configuration/metadata.rb', line 56

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

#get_aws_credentialsObject



61
62
63
# File 'lib/herdst_worker/configuration/metadata.rb', line 61

def get_aws_credentials
    self.get_aws_credentials! rescue nil
end

#get_aws_credentials!Object



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

def get_aws_credentials!
    credentials = self.is_prod? ? 
         make_request!(get_credentials_uri) :
         self.config.config_for(:aws)
    
    self.aws_credentials = Aws::Credentials.new(
        credentials["AccessKeyId"], 
        credentials["SecretAccessKey"], 
        credentials["Token"]
    )
    
    Aws.config.update(
        region: self.get_aws_region(credentials),
        credentials: self.aws_credentials
    )
    
    self.aws_credentials
end

#get_aws_region(credentials = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/herdst_worker/configuration/metadata.rb', line 122

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



46
47
48
# File 'lib/herdst_worker/configuration/metadata.rb', line 46

def get_secrets
    self.secrets
end

#get_services_infoObject



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

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/herdst_worker/configuration/metadata.rb', line 66

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



37
38
39
40
41
42
43
# File 'lib/herdst_worker/configuration/metadata.rb', line 37

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