Class: CloudenvHQ

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudenv-hq.rb

Constant Summary collapse

VERSION =
'0.2.6'.freeze
API_HOST =
'https://app.cloudenv.com'.freeze
READ_PATH =
'/api/v1/envs'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CloudenvHQ

Returns a new instance of CloudenvHQ.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
# File 'lib/cloudenv-hq.rb', line 12

def initialize(options = {})
  @environment = ENV['RAILS_ENV'] || ENV['RACK_ENV']

  if ENV['CLOUDENV_BEARER_TOKEN'] || options[:bearer]
    @bearer = ENV['CLOUDENV_BEARER_TOKEN'] || options[:bearer]
  else
    @bearer_filename = File.expand_path(ENV['CLOUDENV_BEARER_FILE'] || '~/.cloudenvrc')
    @bearer = IO.read(@secret_key).strip if File.exist?(@bearer_filename)
  end

  if ENV['CLOUDENV_APP_SLUG'] && ENV['CLOUDENV_APP_SECRET_KEY']
    @app = ENV['CLOUDENV_APP_SLUG']
    @secret_key = ENV['CLOUDENV_APP_SECRET_KEY']
  else
    @secret_key_filename = File.expand_path(ENV['CLOUDENV_APP_SECRET_KEY_FILE'] || '.cloudenv-secret-key')
    @secret_key = Pathname.new(@secret_key_filename)

    until File.exist?(@secret_key) || @secret_key.expand_path == Pathname.new('/.cloudenv-secret-key')
      @secret_key = @secret_key.parent.parent + @secret_key_filename
    end

    if File.exist?(@secret_key)
      data = YAML.safe_load(IO.read(@secret_key))

      @app = data['slug']
      @secret_key = data['secret-key']
    end
  end

  if @bearer && @app && @secret_key
    if @environment
      data = `curl -s -H "Authorization: Bearer #{@bearer}" "https://app.cloudenv.com/api/v1/envs?name=#{@app}&environment=#{@environment}&version=#{VERSION}&lang=ruby" | openssl enc -a -aes-256-cbc -md sha512 -d -pass pass:"#{@secret_key}" 2> /dev/null`
      file = Tempfile.new('cloudenv')
      file.write(data)
      file.close
      Dotenv.load(file.path)
      file.unlink
    end

    data = `curl -s -H "Authorization: Bearer #{@bearer}" "https://app.cloudenv.com/api/v1/envs?name=#{@app}&environment=default&version=#{VERSION}&lang=ruby" | openssl enc -a -aes-256-cbc -md sha512 -d -pass pass:"#{@secret_key}" 2> /dev/null`
    file = Tempfile.new('cloudenv')
    file.write(data)
    file.close
    Dotenv.load(file.path)
    file.unlink
  else
    warn 'WARNING: cloudenv could not find a .cloudenv-secret-key in the directory path or values for both ENV["CLOUDENV_APP_SLUG"] and ENV["CLOUDENV_APP_SECRET_KEY"]'
  end
end