Class: Murakumo::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/util/murakumo_ec2_tags.rb,
lib/util/murakumo_ec2_client.rb,
lib/util/murakumo_ec2_instances.rb,
lib/util/murakumo_ec2_interfaces.rb,
lib/util/murakumo_self_ip_address.rb,
lib/util/murakumo_ec2_attach_interface.rb,
lib/util/murakumo_ec2_private_ip_addresses.rb

Defined Under Namespace

Classes: EC2Client

Constant Summary collapse

WAIT_LIMIT =
99
WAIT_INTERVAL =
0.3

Class Method Summary collapse

Class Method Details

.ec2_attach_interface(access_key, secret_key, if_id, dev_idx = 1, endpoint = nil, instance_id = nil) ⇒ Object



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
# File 'lib/util/murakumo_ec2_attach_interface.rb', line 13

def self.ec2_attach_interface(access_key, secret_key, if_id, dev_idx = 1, endpoint = nil, instance_id = nil)
  dev_idx = 1 unless dev_idx

  unless instance_id
    instance_id = Net::HTTP.get('169.254.169.254', '/latest/meta-data/instance-id')
  end

  check_own_attached(access_key, secret_key, endpoint, if_id, instance_id)

  ec2_detach_interface(access_key, secret_key, if_id, endpoint, :force) rescue nil

  wait_detach(access_key, secret_key, endpoint, if_id)

  ec2cli = Murakumo::Util::EC2Client.new(access_key, secret_key, endpoint)
  source = ec2cli.query('AttachNetworkInterface',
    'NetworkInterfaceId' => if_id, 'InstanceId' => instance_id, 'DeviceIndex' => dev_idx)

  errors = []

  REXML::Document.new(source).each_element('//Errors/Error') do |element|
    code = element.text('Code')
    message = element.text('Message')
    errors << "#{code}:#{message}"
  end

  raise errors.join(', ') unless errors.empty?
end

.ec2_detach_interface(access_key, secret_key, if_id, endpoint = nil, force = false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/util/murakumo_ec2_attach_interface.rb', line 41

def self.ec2_detach_interface(access_key, secret_key, if_id, endpoint = nil, force = false)
  interfaces = ec2_interfaces(access_key, secret_key, endpoint, if_id)

  if not interfaces or interfaces.empty?
    raise 'interface was not found'
  end

  interface = interfaces.first
  attachment_id = (interface['attachment'] || {})['attachmentId'] || ''

  if attachment_id.empty?
    raise 'attachmentId was not found'
  end

  ec2cli = Murakumo::Util::EC2Client.new(access_key, secret_key, endpoint)

  params = {'AttachmentId' => attachment_id}
  params['Force'] = true if force
  source = ec2cli.query('DetachNetworkInterface', params)

  errors = []

  REXML::Document.new(source).each_element('//Errors/Error') do |element|
    code = element.text('Code')
    message = element.text('Message')
    errors << "#{code}:#{message}"
  end

  raise errors.join(', ') unless errors.empty?
end

.ec2_instances(access_key, secret_key, endpoint = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/util/murakumo_ec2_instances.rb', line 10

def self.ec2_instances(access_key, secret_key, endpoint = nil)
  ec2cli = Murakumo::Util::EC2Client.new(access_key, secret_key, endpoint)
  source = ec2cli.query('DescribeInstances')
  instances = []

  items = REXML::Document.new(source).get_elements('//instancesSet/item')
  walk_item_list(items, instances)

  return instances
end

.ec2_interfaces(access_key, secret_key, endpoint = nil, if_id = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/util/murakumo_ec2_interfaces.rb', line 10

def self.ec2_interfaces(access_key, secret_key, endpoint = nil, if_id = nil)
  dev_idx = 1 unless dev_idx
  params = {}

  if if_id
    params.update('Filter.1.Name' => 'network-interface-id', 'Filter.1.Value' => if_id)
  end

  ec2cli = Murakumo::Util::EC2Client.new(access_key, secret_key, endpoint)

  source = ec2cli.query('DescribeNetworkInterfaces', params)
  interfaces = []

  items = REXML::Document.new(source).get_elements('//networkInterfaceSet/item')
  walk_item_list(items, interfaces)

  return interfaces
end

.ec2_private_ip_addresses(access_key, secret_key, endpoint = nil) ⇒ Object



10
11
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
# File 'lib/util/murakumo_ec2_private_ip_addresses.rb', line 10

def self.ec2_private_ip_addresses(access_key, secret_key, endpoint = nil)
  ec2cli = Murakumo::Util::EC2Client.new(access_key, secret_key, endpoint)
  source = ec2cli.query('DescribeInstances')

  parser = REXML::Parsers::PullParser.new(source)
  ip_addrs = []
  instance_id = nil
  status = nil

  while parser.has_next?
    event = parser.pull
    next if event.event_type != :start_element

    case event[0]
    when 'instanceId'
      instance_id = parser.pull[0]
    when 'instanceState'
      until event.event_type == :start_element and event[0] == 'name'
        event = parser.pull
      end

      status = parser.pull[0]
    when 'privateIpAddress'
      ip_addrs << [instance_id, parser.pull[0], status]
    end
  end

  return ip_addrs
end

.ec2_tags(access_key, secret_key, endpoint = nil, instance_id = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/util/murakumo_ec2_tags.rb', line 10

def self.ec2_tags(access_key, secret_key, endpoint = nil, instance_id = nil)
  unless instance_id
    instance_id = Net::HTTP.get('169.254.169.254', '/latest/meta-data/instance-id')
  end

  ec2cli = Murakumo::Util::EC2Client.new(access_key, secret_key, endpoint)
  source = ec2cli.query('DescribeTags', 'Filter.1.Name' => 'resource-id', 'Filter.1.Value' => instance_id)

  tags = {}

  REXML::Document.new(source).each_element('//tagSet/item') do |element|
    key = element.text('key')
    value = element.text('value')
    tags[key] = value
  end

  return tags
end

.self_ip_addressObject



5
6
7
# File 'lib/util/murakumo_self_ip_address.rb', line 5

def self.self_ip_address
  `/sbin/ifconfig eth0 | awk -F'[: ]+' '/^eth0/,/^$/{if(/inet addr/) print $4}'`.strip
end