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



125
126
127
# File 'lib/awsam/account.rb', line 125

def access_key
  @params[:access_key]
end

#aws_regionObject



133
134
135
# File 'lib/awsam/account.rb', line 133

def aws_region
  @params[:aws_region]
end

#descObject

Export params…need better way to do this



121
122
123
# File 'lib/awsam/account.rb', line 121

def desc
  @params[:description]
end

#ec2_urlObject



137
138
139
# File 'lib/awsam/account.rb', line 137

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

#find_key(name) ⇒ Object



67
68
69
# File 'lib/awsam/account.rb', line 67

def find_key(name)
  @keys[name]
end

#get_default_keyObject



95
96
97
98
# File 'lib/awsam/account.rb', line 95

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

#import_key(name, path) ⇒ Object



71
72
73
# File 'lib/awsam/account.rb', line 71

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


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/awsam/account.rb', line 48

def print_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
  }

  Utils::bash_environ(envs)
end

#removeObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/awsam/account.rb', line 100

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



75
76
77
78
79
80
81
82
83
# File 'lib/awsam/account.rb', line 75

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



111
112
113
114
115
116
117
118
# File 'lib/awsam/account.rb', line 111

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



129
130
131
# File 'lib/awsam/account.rb', line 129

def secret_key
  @params[:secret_key]
end

#set_default_key(keyname) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/awsam/account.rb', line 85

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