~lutostag/ubuntu/utopic/maas/1.5.2

« back to all changes in this revision

Viewing changes to contrib/preseeds_v2/enlist_userdata

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2013-06-03 17:59:31 UTC
  • mto: This revision was merged to the branch mainline in revision 31.
  • Revision ID: package-import@ubuntu.com-20130603175931-1ifwga0vj04p8vu6
Tags: upstream-1.4+bzr1527+dfsg
ImportĀ upstreamĀ versionĀ 1.4+bzr1527+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
   END_IPMI_CONFIG
56
56
 
57
57
   add_bin "maas-ipmi-autodetect" <<"END_MAAS_IPMI_AUTODETECT"
58
 
   #!/usr/bin/python
59
 
   import os
60
 
   import commands
61
 
   import glob
62
 
   import re
63
 
   import string
64
 
   import random
65
 
   import time
66
 
   import json
67
 
 
68
 
   def detect_ipmi():
69
 
       # XXX: andreserl 2013-04-09 bug=1064527: Try to detect if node
70
 
       # is a Virtual Machine. If it is, do not try to detect IPMI.
71
 
       with open('/proc/cpuinfo', 'r') as cpuinfo:
72
 
           for line in cpuinfo:
73
 
               if line.startswith('model name') and 'QEMU' in line:
74
 
                   return (False, None)
75
 
 
76
 
       (status, output) = commands.getstatusoutput('ipmi-locate')
77
 
       show_re = re.compile('(IPMI\ Version:) (\d\.\d)')
78
 
       res = show_re.search(output)
79
 
       if res == None:
80
 
           found = glob.glob("/dev/ipmi[0-9]")
81
 
           if len(found):
82
 
               return (True, "UNKNOWN: %s" % " ".join(found))
83
 
           return (False, "")
84
 
       return (True, res.group(2))
85
 
 
86
 
   def is_ipmi_dhcp():
87
 
       (status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="Lan_Conf:IP_Address_Source"')
88
 
       show_re = re.compile('IP_Address_Source\s+Use_DHCP')
89
 
       res = show_re.search(output)
90
 
       if res == None:
91
 
           return False
92
 
       return True
93
 
 
94
 
   def set_ipmi_network_source(source):
95
 
       (status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="Lan_Conf:IP_Address_Source=%s"' % source)
96
 
 
97
 
   def get_ipmi_ip_address():
98
 
       (status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="Lan_Conf:IP_Address"')
99
 
       show_re = re.compile('([0-9]{1,3}[.]?){4}')
100
 
       res = show_re.search(output)
101
 
       return res.group()
102
 
 
103
 
   def get_ipmi_user_number(user):
104
 
       for i in range(1, 17):
105
 
           ipmi_user_number = "User%s" % i
106
 
           (status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="%s:Username"' % ipmi_user_number)
107
 
           if user in output:
108
 
               return ipmi_user_number
109
 
       return None
110
 
 
111
 
   def commit_ipmi_user_settings(user, password):
112
 
       ipmi_user_number = get_ipmi_user_number(user)
113
 
       if ipmi_user_number is None:
114
 
           (status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="User10:Username=%s"' % user)
115
 
           ipmi_user_number = get_ipmi_user_number(user)
116
 
       (status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Username=%s"' % (ipmi_user_number, user))
117
 
       (status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Password=%s"' % (ipmi_user_number, password))
118
 
       (status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Enable_User=Yes"' % ipmi_user_number)
119
 
       (status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Lan_Enable_IPMI_Msgs=Yes"' % ipmi_user_number)
120
 
       (status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Lan_Privilege_Limit=Administrator"' % ipmi_user_number)
121
 
 
122
 
   def commit_ipmi_settings(config):
123
 
       (status, output) = commands.getstatusoutput('bmc-config --commit --filename %s' % config)
124
 
 
125
 
   def get_maas_power_settings(user, password, ipaddress):
126
 
       return "%s,%s,%s" % (user, password, ipaddress)
127
 
 
128
 
   def get_maas_power_settings_json(user, password, ipaddress):
129
 
       power_params = {"power_address": ipaddress, "power_pass": password, "power_user": user}
130
 
       return json.dumps(power_params) 
131
 
 
132
 
   def generate_random_password(min=8,max=15):
133
 
       length=random.randint(min,max)
134
 
       letters=string.ascii_letters+string.digits
135
 
       return ''.join([random.choice(letters) for _ in range(length)])
136
 
 
137
 
   def main():
138
 
 
139
 
       import argparse
140
 
 
141
 
       parser = argparse.ArgumentParser(
142
 
           description='send config file to modify IPMI settings with')
143
 
       parser.add_argument("--configdir", metavar="folder",
144
 
           help="specify config file directory", default=None)
145
 
       parser.add_argument("--dhcp-if-static", action="store_true",
146
 
           dest="dhcp", help="set network source to DHCP if Static", default=False)
147
 
       parser.add_argument("--commission-creds", action="store_true",
148
 
           dest="commission_creds", help="Create IPMI temporary credentials", default=False)
149
 
 
150
 
       args = parser.parse_args()
151
 
 
152
 
       # Check whether IPMI exists or not.
153
 
       (status, ipmi_version) = detect_ipmi()
154
 
       if status != True:
155
 
           # if False, then failed to detect ipmi
156
 
           exit(1)
157
 
 
158
 
       # Check whether IPMI is being set to DHCP. If it is not, and
159
 
       # '--dhcp-if-static' has been passed,  Set it to IPMI to DHCP.
160
 
       if not is_ipmi_dhcp() and args.dhcp:
161
 
           set_ipmi_network_source("Use_DHCP")
162
 
           # allow IPMI 120 seconds to obtain an IP address
163
 
           time.sleep(120)
164
 
 
165
 
       # create user/pass
166
 
       IPMI_MAAS_USER="maas"
167
 
       IPMI_MAAS_PASSWORD=generate_random_password()
168
 
 
169
 
       # Configure IPMI user/password
170
 
       commit_ipmi_user_settings(IPMI_MAAS_USER, IPMI_MAAS_PASSWORD)
171
 
 
172
 
       # Commit other IPMI settings
173
 
       if args.configdir:
174
 
           for file in os.listdir(args.configdir):
175
 
               commit_ipmi_settings(os.path.join(args.configdir, file))
176
 
 
177
 
       # get the IP address
178
 
       IPMI_IP_ADDRESS = get_ipmi_ip_address()
179
 
       if IPMI_IP_ADDRESS == "0.0.0.0":
180
 
           # if IPMI_IP_ADDRESS is 0.0.0.0, wait 60 seconds and retry.
181
 
           set_ipmi_network_source("Static")
182
 
           time.sleep(2)
183
 
           set_ipmi_network_source("Use_DHCP")
184
 
           time.sleep(60)
185
 
           IPMI_IP_ADDRESS = get_ipmi_ip_address()
186
 
 
187
 
       if IPMI_IP_ADDRESS is None or IPMI_IP_ADDRESS == "0.0.0.0":
188
 
           # Exit (to not set power params in MAAS) if no IPMI_IP_ADDRESS
189
 
           # has been detected
190
 
           exit(1)
191
 
 
192
 
       if args.commission_creds:
193
 
           print get_maas_power_settings_json(IPMI_MAAS_USER, IPMI_MAAS_PASSWORD, IPMI_IP_ADDRESS)
194
 
       else:
195
 
           print get_maas_power_settings(IPMI_MAAS_USER, IPMI_MAAS_PASSWORD, IPMI_IP_ADDRESS)
196
 
 
197
 
   if __name__ == '__main__':
198
 
       main()
 
58
   {{maas_ipmi_autodetect_py}}
199
59
   END_MAAS_IPMI_AUTODETECT
200
60
 
201
61
   # we could obtain the interface that booted from the kernel cmdline