Class: Awsam::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/awsam/account.rb

Constant Summary collapse

DEFAULT_REGION =
"us-east-1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, params) ⇒ Account

Returns a new instance of Account.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/awsam/account.rb', line 13

def initialize(name, params)
  if name == Awsam::DEFAULT_LINK_NAME
    # We require this for our default account symlink
    raise "Can not name an account: #{Awsam::DEFAULT_LINK_NAME}"
  end
  @name = name
  @params = params

  @params[:aws_region] ||= DEFAULT_REGION

  load_keys
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



11
12
13
# File 'lib/awsam/account.rb', line 11

def keys
  @keys
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/awsam/account.rb', line 11

def name
  @name
end

Class Method Details

.load_from_disk(dir) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/awsam/account.rb', line 26

def self.load_from_disk(dir)
  name = File.basename(dir)
  conffile = File.join(dir, 'conf.yml')

  return nil unless File.exist?(conffile)

  File.open(conffile) do |yf|
    @conf = YAML::load(yf)
  end

  Account.new(name, @conf)
end

Instance Method Details

#access_keyObject



131
132
133
# File 'lib/awsam/account.rb', line 131

def access_key
  @params[:access_key]
end

#aws_regionObject



139
140
141
# File 'lib/awsam/account.rb', line 139

def aws_region
  @params[:aws_region]
end

#descObject

Export params…need better way to do this



127
128
129
# File 'lib/awsam/account.rb', line 127

def desc
  @params[:description]
end

#ec2_urlObject



143
144
145
# File 'lib/awsam/account.rb', line 143

def ec2_url
  "https://ec2.#{aws_region}.amazonaws.com"
end

#find_key(name) ⇒ Object



73
74
75
# File 'lib/awsam/account.rb', line 73

def find_key(name)
  @keys[name]
end

#get_default_keyObject



101
102
103
104
# File 'lib/awsam/account.rb', line 101

def get_default_key
  dflt = Utils::get_default(conf_file('keys'))
  @keys[dflt]
end

#get_environObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/awsam/account.rb', line 56

def get_environ
  envs = {
    "AMAZON_ACCESS_KEY_ID"     => @params[:access_key],
    "AWS_ACCESS_KEY_ID"        => @params[:access_key],
    "AWS_ACCESS_KEY"           => @params[:access_key],

    "AMAZON_SECRET_ACCESS_KEY" => @params[:secret_key],
    "AWS_SECRET_ACCESS_KEY"    => @params[:secret_key],
    "AWS_SECRET_KEY"           => @params[:secret_key],

    "AMAZON_AWS_ID"            => @params[:aws_id],
    "AWS_DEFAULT_REGION"       => @params[:aws_region],

    "EC2_URL"                  => ec2_url
  }
end

#import_key(name, path) ⇒ Object



77
78
79
# File 'lib/awsam/account.rb', line 77

def import_key(name, path)
  @keys[name] = Key.import(conf_file, name, path)
end

#load_keysObject



39
40
41
42
43
44
45
46
# File 'lib/awsam/account.rb', line 39

def load_keys
  @keys = Hash.new
  base = conf_file('keys')
  return unless File.exist?(base)
  Utils::confdir_scan(base) do |name|
    @keys[name] = Key.new(File.join(base, name))
  end
end


52
53
54
# File 'lib/awsam/account.rb', line 52

def print_environ(set_export)
  Utils::bash_environ(get_environ, set_export)
end


48
49
50
# File 'lib/awsam/account.rb', line 48

def print_unset_environ
  Utils::bash_unset_environ(get_environ)
end

#removeObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/awsam/account.rb', line 106

def remove
  dir = conf_file
  acct = Awsam::Accounts::get_default
  if acct && acct.name == @name
    # Need to remove default link if we're the default account
    Awsam::Accounts::remove_default
  end

  FileUtils.rm_rf(dir)
end

#remove_key(name) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/awsam/account.rb', line 81

def remove_key(name)
  return false unless @keys.has_key?(name)

  dflt = get_default_key
  Utils::remove_default(conf_file('keys')) if dflt && dflt.name == name
  @keys[name].remove
  @keys.delete(name)
  true
end

#saveObject



117
118
119
120
121
122
123
124
# File 'lib/awsam/account.rb', line 117

def save
  dir = File.join(Awsam::get_accts_dir, @name)
  FileUtils.mkdir(dir) unless File.exist?(dir)

  File.open(File.join(dir, 'conf.yml'), "w", 0600) do |out|
    YAML.dump(@params, out )
  end
end

#secret_keyObject



135
136
137
# File 'lib/awsam/account.rb', line 135

def secret_key
  @params[:secret_key]
end

#set_default_key(keyname) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/awsam/account.rb', line 91

def set_default_key(keyname)
  key = @keys[keyname]
  unless key
    $stderr.puts "No key named #{keyname}"
    return false
  end

  Utils::set_default(conf_file('keys'), keyname)
end