Class: DLRacktables::System

Inherits:
Object
  • Object
show all
Defined in:
lib/dl_racktables/system.rb

Instance Method Summary collapse

Instance Method Details

#exist_iprange?(addr) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
# File 'lib/dl_racktables/system.rb', line 88

def exist_iprange?(addr)
  resp = HTTP.get("index.php?page=search&last_page=ipv4space&last_tab=default&q=#{addr}")

  doc = Nokogiri::HTML(resp.body)

  return doc.xpath("//div[@class='pagebar']//h2").to_s != "<h2>Nothing found for '#{addr}'</h2>"
end

#find_vmidObject



125
126
127
128
129
130
131
132
133
# File 'lib/dl_racktables/system.rb', line 125

def find_vmid
  resp = HTTP.get('index.php?andor=and&cfe=%7b$attr_10015_50413%7d+and+%7b$typeid_1504%7d&page=depot&tab=default')

  doc = Nokogiri::HTML(resp.body)

  if doc.xpath("//table[@class='cooltable']//tr").find {|x| x.text =~ /^vm(\d+)Unmounted$/}
    "vm#{$1}"
  end
end

#get_objid(vmid) ⇒ Object



28
29
30
31
32
33
# File 'lib/dl_racktables/system.rb', line 28

def get_objid(vmid)
  resp = HTTP.get("index.php?page=search&last_page=object&last_tab=edit&q=#{vmid}")
  doc = Nokogiri::HTML(resp.body)

  doc.xpath("//div[@class='pagebar']//input[@name='object_id']//@value").first.to_s
end

#register_vm(opts) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/dl_racktables/system.rb', line 150

def register_vm(opts)
  vmid = find_vmid
  is_success = false

  # update VM information on Racktables
  if validate_vmid(vmid)
    ret = update_vminfo(vmid, opts[:status], opts[:owner])
    if ret.is_a? Net::HTTPSuccess
      is_success = true
    end
  end

  if validate_ipaddr(opts[:ipaddr])
    update_ipinfo(vmid, opts[:ipaddr])
  end

  puts ({:is_success => is_success, :vmid => vmid}).to_json
end

#update_ipinfo(vmid, ipaddr) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/dl_racktables/system.rb', line 77

def update_ipinfo(vmid, ipaddr)
  HTTP.post('index.php?module=redirect&page=object&tab=ip&op=add', {
    'object_id' => get_objid(vmid).to_s,
    'submit.x' => '0',
    'submit.y' => '0',
    'bond_name' => '',
    'ip' => ipaddr,
    'bond_type' => 'regular',
  })
end

#update_vminfo(vmid, status = 0, owner = 0) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dl_racktables/system.rb', line 35

def update_vminfo(vmid, status = 0, owner = 0)
  HTTP.post('index.php?module=redirect&page=object&tab=edit&op=update', {
    'object_id' => get_objid(vmid),
    'object_type_id' => '1504',
    'object_name' => vmid,
    'object_label' => '',
    'object_asset_no' => '',
    '0_attr_id' => '10015',
    '0_value' => status.to_s,
    '1_attr_id' => '10020',
    '1_value' => '0',
    '2_attr_id' => '10012',
    '2_value' => '',
    '3_attr_id' => '10021',
    '3_value' => '0',
    '4_attr_id' => '4',
    '4_value' => '0',
    '5_attr_id' => '1',
    '5_value' => '',
    '6_attr_id' => '10013',
    '6_value' => '0',
    '7_attr_id' => '10023',
    '7_value' => '0',
    '8_attr_id' => '10024',
    '8_value' => '0',
    '9_attr_id' => '10009',
    '9_value' => '',
    '10_attr_id' => '10070',
    '10_value' => '',
    '11_attr_id' => '10078',
    '11_value' => owner.to_s,
    '12_attr_id' => '10080',
    '12_value' => '0',
    '13_attr_id' => '10077',
    '13_value' => '0',
    'num_attrs' => '14',
    'object_comment' => '',
    'submit.x' => '23',
    'submit.y' => '28',
  })
end

#validate_ipaddr(addr) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dl_racktables/system.rb', line 96

def validate_ipaddr(addr)
  if ! exist_iprange?(addr)
    return false
  end

  resp = HTTP.get("index.php?page=ipaddress&ip=#{addr}")

  doc = Nokogiri::HTML(resp.body)

  # is invalid input?
  if (doc.title =~ /#{addr}/) == nil
    return false
  end

  # chack target object is assigned for some machine
  unless doc.xpath("//div[@class='pagebar']//td[@class='pcright']//div").empty?
    return false
  end

  summary = doc.xpath("//div[@class='pagebar']//td[@class='pcleft']//table").text

  ret = false
  ret ||= summary.include? 'Name'
  ret ||= summary.include? 'Comment'
  ret ||= summary.include? 'Reserved:yes'

  ! ret
end

#validate_vmid(vmid) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/dl_racktables/system.rb', line 135

def validate_vmid(vmid)
  resp = HTTP.get("index.php?page=search&q=#{vmid}")

  doc = Nokogiri::HTML(resp.body)

  if doc.title =~ /search results for/
    # meaning invalid vmid is specified
    return false
  end

  if doc.xpath("//div[@class='portlet']//tr").find {|x| x.text =~ / Status:(.*)/}
    $1 == '在庫'
  end
end

#who_operate_vm_lastly(vmid) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dl_racktables/system.rb', line 7

def who_operate_vm_lastly(vmid)
  resp = HTTP.get("index.php?page=search&last_page=object&last_tab=edit&q=#{vmid}")

  doc = Nokogiri::HTML(resp.body)

  if doc.title =~ /search results for/
    # meaning invalid vmid is specified
    return false
  end

  # get responsible team if it's specified
  team = ''
  if responsible_team = doc.xpath("//div[@class='portlet']//tr").find {|x| x.text =~ /b-10/}
    team = responsible_team.xpath("td//select//option[@selected]").text
  end

  # get person who update this object lastly
  person = doc.xpath("//table[@class='cooltable']//tr").map {|x| x.children[1].text}.last

  {team: team, person: person}
end