Class: Fog::AWS::Compute::Server

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/aws/models/compute/server.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#connection

Instance Method Summary collapse

Methods inherited from Model

#collection, #inspect, #reload, #to_json, #wait_for

Methods included from Fog::Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Attributes::InstanceMethods

#_dump, #attributes, #identity, #identity=, #merge_attributes, #new_record?, #requires

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



41
42
43
44
45
# File 'lib/fog/aws/models/compute/server.rb', line 41

def initialize(attributes={})
  @groups ||= ["default"] unless attributes[:subnet_id]
  @flavor_id ||= 'm1.small'
  super
end

Instance Attribute Details

#architectureObject

Returns the value of attribute architecture.



11
12
13
# File 'lib/fog/aws/models/compute/server.rb', line 11

def architecture
  @architecture
end

#passwordObject

Returns the value of attribute password.



38
39
40
# File 'lib/fog/aws/models/compute/server.rb', line 38

def password
  @password
end

#private_keyObject



110
111
112
# File 'lib/fog/aws/models/compute/server.rb', line 110

def private_key
  @private_key ||= File.read(private_key_path)
end

#private_key_pathObject



106
107
108
# File 'lib/fog/aws/models/compute/server.rb', line 106

def private_key_path
  File.expand_path(@private_key_path ||= Fog.credentials[:private_key_path])
end

#public_keyObject



118
119
120
# File 'lib/fog/aws/models/compute/server.rb', line 118

def public_key
  @public_key ||= File.read(public_key_path)
end

#public_key_pathObject



114
115
116
# File 'lib/fog/aws/models/compute/server.rb', line 114

def public_key_path
  File.expand_path(@public_key_path ||= Fog.credentials[:public_key_path])
end

Instance Method Details

#addressesObject



47
48
49
50
51
# File 'lib/fog/aws/models/compute/server.rb', line 47

def addresses
  requires :id

  connection.addresses(:server => self)
end

#console_outputObject



53
54
55
56
57
# File 'lib/fog/aws/models/compute/server.rb', line 53

def console_output
  requires :id

  connection.get_console_output(@id)
end

#destroyObject



59
60
61
62
63
64
# File 'lib/fog/aws/models/compute/server.rb', line 59

def destroy
  requires :id

  connection.terminate_instances(@id)
  true
end

#flavorObject



75
76
77
# File 'lib/fog/aws/models/compute/server.rb', line 75

def flavor
  @flavor ||= connection.flavors.all.detect {|flavor| flavor.id == @flavor_id}
end

#flavor=(new_flavor) ⇒ Object



71
72
73
# File 'lib/fog/aws/models/compute/server.rb', line 71

def flavor=(new_flavor)
  @flavor = new_flavor
end

#flavor_idObject



67
68
69
# File 'lib/fog/aws/models/compute/server.rb', line 67

def flavor_id
  @flavor && @flavor.id || @flavor_id
end

#key_pairObject



79
80
81
82
83
# File 'lib/fog/aws/models/compute/server.rb', line 79

def key_pair
  requires :key_name

  connection.keypairs.all(@key_name).first
end

#key_pair=(new_keypair) ⇒ Object



85
86
87
# File 'lib/fog/aws/models/compute/server.rb', line 85

def key_pair=(new_keypair)
  @key_name = new_keypair && new_keypair.name
end

#monitoring=(new_monitoring) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/fog/aws/models/compute/server.rb', line 90

def monitoring=(new_monitoring)
  if new_monitoring.is_a?(Hash)
    @monitoring = new_monitoring['state']
  else
    @monitoring = new_monitoring
  end
end

#placement=(new_placement) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/fog/aws/models/compute/server.rb', line 98

def placement=(new_placement)
  if new_placement.is_a?(Hash)
    @availability_zone = new_placement['availabilityZone']
  else
    @availability_zone = new_placement
  end
end

#ready?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/fog/aws/models/compute/server.rb', line 121

def ready?
  @state == 'running'
end

#rebootObject



125
126
127
128
129
# File 'lib/fog/aws/models/compute/server.rb', line 125

def reboot
  requires :id
  connection.reboot_instances(@id)
  true
end

#saveObject

Raises:



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
156
157
158
159
160
161
# File 'lib/fog/aws/models/compute/server.rb', line 131

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :image_id

  options = {
    'BlockDeviceMapping'          => block_device_mapping,
    'ClientToken'                 => client_token,
    'InstanceType'                => flavor_id,
    'KernelId'                    => kernel_id,
    'KeyName'                     => key_name,
    'Monitoring.Enabled'          => monitoring,
    'Placement.AvailabilityZone'  => availability_zone,
    'RamdiskId'                   => ramdisk_id,
    'SecurityGroup'               => groups,
    'SubnetId'                    => subnet_id,
    'UserData'                    => user_data
  }

  # If subnet is defined we are working on a virtual private cloud.
  # subnet & security group cannot co-exist. I wish VPC just ignored
  # the security group parameter instead, it would be much easier!
  if subnet_id
    options.delete('SecurityGroup')
  else
    options.delete('SubnetId')
  end

  data = connection.run_instances(image_id, 1, 1, options)
  merge_attributes(data.body['instancesSet'].first)
  true
end

#setup(credentials = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fog/aws/models/compute/server.rb', line 163

def setup(credentials = {})
  requires :identity, :ip_address, :public_key, :username
  sleep(10) # takes a bit before EC2 instances will play nice
  Fog::SSH.new(ip_address, username, credentials).run([
    %{mkdir .ssh},
    %{echo "#{public_key}" >> ~/.ssh/authorized_keys},
    %{passwd -l root},
    %{echo "#{attributes.to_json}" >> ~/attributes.json}
  ])
rescue Errno::ECONNREFUSED => e
  sleep(1)
  retry
end

#ssh(commands) ⇒ Object



177
178
179
180
181
# File 'lib/fog/aws/models/compute/server.rb', line 177

def ssh(commands)
  requires :identity, :ip_address, :private_key, :username
  @ssh ||= Fog::SSH.new(ip_address, username, :key_data => [private_key])
  @ssh.run(commands)
end

#startObject



183
184
185
186
187
# File 'lib/fog/aws/models/compute/server.rb', line 183

def start
  requires :id
  connection.start_instances(@id)
  true
end

#stopObject



189
190
191
192
193
# File 'lib/fog/aws/models/compute/server.rb', line 189

def stop
  requires :id
  connection.stop_instances(@id)
  true
end

#usernameObject



195
196
197
# File 'lib/fog/aws/models/compute/server.rb', line 195

def username
  @username ||= 'root'
end

#volumesObject



199
200
201
202
# File 'lib/fog/aws/models/compute/server.rb', line 199

def volumes
  requires :id
  connection.volumes(:server => self)
end