r81200 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81199‎ | r81200 | r81201 >
Date:23:53, 29 January 2011
Author:laner
Status:deferred (Comments)
Tags:
Comment:
* Added floating ip output to instance list
* Removed cast calls from code pulling info from classes, and added them to the output of the classes
* Added support for cloud-init via $wgOpenStackManagerInstanceUserData
** Can currently add cloudconfig, scripts, and upstarts
** cloudconfig is currently an array that is converted to YAML, whereas scripts and upstarts load from given files
* Upping version to 1.1
Modified paths:
  • /trunk/extensions/OpenStackManager/OpenStackManager.i18n.php (modified) (history)
  • /trunk/extensions/OpenStackManager/OpenStackManager.php (modified) (history)
  • /trunk/extensions/OpenStackManager/OpenStackNovaAddress.php (modified) (history)
  • /trunk/extensions/OpenStackManager/OpenStackNovaController.php (modified) (history)
  • /trunk/extensions/OpenStackManager/OpenStackNovaInstance.php (modified) (history)
  • /trunk/extensions/OpenStackManager/OpenStackNovaKeypair.php (modified) (history)
  • /trunk/extensions/OpenStackManager/OpenStackNovaSecurityGroup.php (modified) (history)
  • /trunk/extensions/OpenStackManager/OpenStackNovaSecurityGroupRule.php (modified) (history)
  • /trunk/extensions/OpenStackManager/special/SpecialNovaInstance.php (modified) (history)

Diff [purge]

Index: trunk/extensions/OpenStackManager/special/SpecialNovaInstance.php
@@ -350,6 +350,7 @@
351351 $header .= Html::element( 'th', array(), wfMsg( 'openstackmanager-instancestate' ) );
352352 $header .= Html::element( 'th', array(), wfMsg( 'openstackmanager-instancetype' ) );
353353 $header .= Html::element( 'th', array(), wfMsg( 'openstackmanager-instanceip' ) );
 354+ $header .= Html::element( 'th', array(), wfMsg( 'openstackmanager-instancepublicip' ) );
354355 $header .= Html::element( 'th', array(), wfMsg( 'openstackmanager-securitygroups' ) );
355356 $header .= Html::element( 'th', array(), wfMsg( 'openstackmanager-availabilityzone' ) );
356357 $header .= Html::element( 'th', array(), wfMsg( 'openstackmanager-imageid' ) );
@@ -361,7 +362,7 @@
362363 if ( ! in_array( $project, $userProjects ) ) {
363364 continue;
364365 }
365 - $instanceName = (string)$instance->getInstanceName();
 366+ $instanceName = $instance->getInstanceName();
366367 $instanceName = htmlentities( $instanceName );
367368 $title = Title::newFromText( $instanceName, NS_VM );
368369 $instanceNameLink = $sk->link( $title, $instanceName, array(), array(), array() );
@@ -369,7 +370,14 @@
370371 $instanceOut .= Html::element( 'td', array(), $instance->getInstanceId() );
371372 $instanceOut .= Html::element( 'td', array(), $instance->getInstanceState() );
372373 $instanceOut .= Html::element( 'td', array(), $instance->getInstanceType() );
373 - $instanceOut .= Html::element( 'td', array(), $instance->getInstancePrivateIP() );
 374+ $privateip = $instance->getInstancePrivateIP();
 375+ $publicip = $instance->getInstancePublicIP();
 376+ $instanceOut .= Html::element( 'td', array(), $privateip );
 377+ if ( $privateip != $publicip ) {
 378+ $instanceOut .= Html::element( 'td', array(), $publicip );
 379+ } else {
 380+ $instanceOut .= Html::element( 'td', array(), '' );
 381+ }
374382 $groupsOut = '';
375383 foreach ( $instance->getSecurityGroups() as $group ) {
376384 $groupsOut .= Html::element( 'li', array(), $group );
@@ -452,7 +460,7 @@
453461
454462 if ( $host ) {
455463 $title = Title::newFromText( $wgOut->getPageTitle() );
456 - $job = new OpenStackNovaHostJob( $title, array( 'instanceid' => (string)$instance->getInstanceId() ) );
 464+ $job = new OpenStackNovaHostJob( $title, array( 'instanceid' => $instance->getInstanceId() ) );
457465 $job->insert();
458466 $out = Html::element( 'p', array(), wfMsgExt( 'openstackmanager-createdinstance', array(),
459467 $instance->getInstanceID(), $instance->getImageId(),
Index: trunk/extensions/OpenStackManager/OpenStackManager.i18n.php
@@ -79,6 +79,7 @@
8080 'openstackmanager-instancestate' => 'Instance State',
8181 'openstackmanager-instancetype' => 'Instance Type',
8282 'openstackmanager-instanceip' => 'Instance IP',
 83+ 'openstackmanager-instancepublicip' => 'Instance Floating IP',
8384 'openstackmanager-securitygroups' => 'Security Groups',
8485 'openstackmanager-availabilityzone' => 'Availability Zone',
8586 'openstackmanager-imageid' => 'Image ID',
Index: trunk/extensions/OpenStackManager/OpenStackNovaInstance.php
@@ -42,14 +42,14 @@
4343 * @return
4444 */
4545 function getReservationId() {
46 - return $this->instance->reservationId;
 46+ return (string)$this->instance->reservationId;
4747 }
4848
4949 /**
5050 * @return
5151 */
5252 function getInstanceId() {
53 - return $this->instance->instancesSet->item->instanceId;
 53+ return (string)$this->instance->instancesSet->item->instanceId;
5454 }
5555
5656 /**
@@ -57,7 +57,7 @@
5858 */
5959 function getInstancePrivateIP() {
6060 # Though this is unintuitive, privateDnsName is the private IP
61 - return $this->instance->instancesSet->item->privateDnsName;
 61+ return (string)$this->instance->instancesSet->item->privateDnsName;
6262 }
6363
6464 /**
@@ -65,56 +65,56 @@
6666 */
6767 function getInstancePublicIP() {
6868 # Though this is unintuitive, privateDnsName is the private IP
69 - return $this->instance->instancesSet->item->dnsName;
 69+ return (string)$this->instance->instancesSet->item->dnsName;
7070 }
7171
7272 /**
7373 * @return
7474 */
7575 function getInstanceName() {
76 - return $this->instance->instancesSet->item->displayName;
 76+ return (string)$this->instance->instancesSet->item->displayName;
7777 }
7878
7979 /**
8080 * @return
8181 */
8282 function getInstanceState() {
83 - return $this->instance->instancesSet->item->instanceState->name;
 83+ return (string)$this->instance->instancesSet->item->instanceState->name;
8484 }
8585
8686 /**
8787 * @return
8888 */
8989 function getInstanceType() {
90 - return $this->instance->instancesSet->item->instanceType;
 90+ return (string)$this->instance->instancesSet->item->instanceType;
9191 }
9292
9393 /**
9494 * @return
9595 */
9696 function getImageId() {
97 - return $this->instance->instancesSet->item->imageId;
 97+ return (string)$this->instance->instancesSet->item->imageId;
9898 }
9999
100100 /**
101101 * @return
102102 */
103103 function getKeyName() {
104 - return $this->instance->instancesSet->item->keyName;
 104+ return (string)$this->instance->instancesSet->item->keyName;
105105 }
106106
107107 /**
108108 * @return
109109 */
110110 function getOwner() {
111 - return $this->instance->ownerId;
 111+ return (string)$this->instance->ownerId;
112112 }
113113
114114 /**
115115 * @return
116116 */
117117 function getAvailabilityZone() {
118 - return $this->instance->instancesSet->item->placement->availabilityZone;
 118+ return (string)$this->instance->instancesSet->item->placement->availabilityZone;
119119 }
120120
121121 /**
@@ -122,7 +122,7 @@
123123 */
124124 function getRegion() {
125125 # NOTE: This is non-existant in openstack for now
126 - return $this->instance->instancesSet->item->region;
 126+ return (string)$this->instance->instancesSet->item->region;
127127 }
128128
129129 /**
@@ -131,7 +131,7 @@
132132 function getSecurityGroups() {
133133 $groups = array();
134134 foreach ( $this->instance->groupSet->item as $group ) {
135 - $groups[] = $group->groupId;
 135+ $groups[] = (string)$group->groupId;
136136 }
137137 return $groups;
138138 }
@@ -140,7 +140,7 @@
141141 * @return
142142 */
143143 function getLaunchTime() {
144 - return $this->instance->instancesSet->item->launchTime;
 144+ return (string)$this->instance->instancesSet->item->launchTime;
145145 }
146146
147147 }
Index: trunk/extensions/OpenStackManager/OpenStackNovaKeypair.php
@@ -16,14 +16,14 @@
1717 * @return
1818 */
1919 function getKeyName() {
20 - return $this->keypair->keyName;
 20+ return (string)$this->keypair->keyName;
2121 }
2222
2323 /**
2424 * @return
2525 */
2626 function getKeyFingerprint() {
27 - return $this->keypair->keyFingerprint;
 27+ return (string)$this->keypair->keyFingerprint;
2828 }
2929
3030 }
Index: trunk/extensions/OpenStackManager/OpenStackNovaSecurityGroupRule.php
@@ -17,21 +17,21 @@
1818 *
1919 */
2020 function getToPort() {
21 - return $this->rule->toPort;
 21+ return (string)$this->rule->toPort;
2222 }
2323
2424 /**
2525 * @return
2626 */
2727 function getFromPort() {
28 - return $this->rule->fromPort;
 28+ return (string)$this->rule->fromPort;
2929 }
3030
3131 /**
3232 * @return
3333 */
3434 function getIPProtocol() {
35 - return $this->rule->ipProtocol;
 35+ return (string)$this->rule->ipProtocol;
3636 }
3737
3838 /**
@@ -40,7 +40,7 @@
4141 function getIPRanges() {
4242 $ranges = array();
4343 foreach ( $this->rule->ipRanges->item as $iprange ) {
44 - $ranges[] = $iprange->cidrIp;
 44+ $ranges[] = (string)$iprange->cidrIp;
4545 }
4646 return $ranges;
4747 }
@@ -52,8 +52,8 @@
5353 $groups = array();
5454 foreach ( $this->rule->groups->item as $group ) {
5555 $properties = array();
56 - $properties['groupname'] = $group->groupName;
57 - $properties['project'] = $group->userId;
 56+ $properties['groupname'] = (string)$group->groupName;
 57+ $properties['project'] = (string)$group->userId;
5858 $groups[] = $properties;
5959 }
6060 return $groups;
Index: trunk/extensions/OpenStackManager/OpenStackNovaController.php
@@ -173,6 +173,8 @@
174174 * @return null|OpenStackNovaInstance
175175 */
176176 function createInstance( $instanceName, $image, $key, $instanceType, $availabilityZone, $groups ) {
 177+ global $wgOpenStackManagerInstanceUserData;
 178+
177179 # 1, 1 is min and max number of instances to create.
178180 # We never want to make more than one at a time.
179181 $options = array();
@@ -182,6 +184,48 @@
183185 $options['InstanceType'] = $instanceType;
184186 $options['Placement.AvailabilityZone'] = $availabilityZone;
185187 $options['DisplayName'] = $instanceName;
 188+ if ( $wgOpenStackManagerInstanceUserData ) {
 189+ $random_hash = md5(date('r', time()));
 190+ $endl = $this->getLineEnding();
 191+ $boundary = '===============' . $random_hash .'==';
 192+ $userdata = 'Content-Type: multipart/mixed; boundary="' . $boundary .'"' . $endl;
 193+ $userdata .= 'MIME-Version: 1.0' . $endl;
 194+ $boundary = '--' . $boundary;
 195+ $userdata .= $endl;
 196+ $userdata .= $boundary;
 197+ if ( $wgOpenStackManagerInstanceUserData['cloud-config'] ) {
 198+ $userdata .= $endl . $this->getAttachmentMime( Spyc::YAMLDump( $wgOpenStackManagerInstanceUserData['cloud-config'] ), 'text/cloud-config', 'cloud-config.txt' );
 199+ $userdata .= $endl . $boundary;
 200+ }
 201+ if ( $wgOpenStackManagerInstanceUserData['scripts'] ) {
 202+ $i = 0;
 203+ foreach ( $wgOpenStackManagerInstanceUserData['scripts'] as $script ) {
 204+ $stat = @stat( $script );
 205+ if ( ! $stat ) {
 206+ continue;
 207+ }
 208+ $scripttext = file_get_contents( $script );
 209+ $userdata .= $endl . $this->getAttachmentMime( $scripttext, 'text/x-shellscript', 'wiki-script-' . $i . '.sh' );
 210+ $userdata .= $endl . $boundary;
 211+ $i = $i + 1;
 212+ }
 213+ }
 214+ if ( $wgOpenStackManagerInstanceUserData['upstarts'] ) {
 215+ $i = 0;
 216+ foreach ( $wgOpenStackManagerInstanceUserData['upstarts'] as $upstart ) {
 217+ $stat = @stat( $upstart );
 218+ if ( ! $stat ) {
 219+ continue;
 220+ }
 221+ $upstarttext = file_get_contents( $upstart );
 222+ $userdata .= $endl . $this->getAttachmentMime( $upstarttext, 'text/upstart-job', 'wiki-upstart-config-' . $i . '.conf' );
 223+ $userdata .= $endl . $boundary;
 224+ $i = $i + 1;
 225+ }
 226+ }
 227+ $userdata .= '--';
 228+ $options['UserData'] = base64_encode( $userdata );
 229+ }
186230 if ( count( $groups ) > 1 ) {
187231 $options['SecurityGroup'] = $groups;
188232 } else if ( count( $groups ) == 1 ) {
@@ -198,6 +242,25 @@
199243 return $instance;
200244 }
201245
 246+ function getAttachmentMime( $attachmenttext, $mimetype, $filename ) {
 247+ $endl = $this->getLineEnding();
 248+ $attachment = 'Content-Type: ' . $mimetype . '; charset="us-ascii"'. $endl;
 249+ $attachment .= 'MIME-Version: 1.0' . $endl;
 250+ $attachment .= 'Content-Transfer-Encoding: 7bit' . $endl;
 251+ $attachment .= 'Content-Disposition: attachment; filename="' . $filename . '"' . $endl;
 252+ $attachment .= $endl;
 253+ $attachment .= $attachmenttext;
 254+ return $attachment;
 255+ }
 256+
 257+ function getLineEnding() {
 258+ if ( wfIsWindows() ) {
 259+ return "\r\n";
 260+ } else {
 261+ return "\n";
 262+ }
 263+ }
 264+
202265 /**
203266 * @param $instanceId
204267 * @return
Index: trunk/extensions/OpenStackManager/OpenStackNovaSecurityGroup.php
@@ -21,21 +21,21 @@
2222 * @return
2323 */
2424 function getGroupName() {
25 - return $this->group->groupName;
 25+ return (string)$this->group->groupName;
2626 }
2727
2828 /**
2929 * @return
3030 */
3131 function getGroupDescription() {
32 - return $this->group->groupDescription;
 32+ return (string)$this->group->groupDescription;
3333 }
3434
3535 /**
3636 * @return
3737 */
3838 function getOwner() {
39 - return $this->group->ownerId;
 39+ return (string)$this->group->ownerId;
4040 }
4141
4242 /**
Index: trunk/extensions/OpenStackManager/OpenStackNovaAddress.php
@@ -29,7 +29,7 @@
3030 * @return
3131 */
3232 function getPublicIP() {
33 - return $this->address->publicIp;
 33+ return (string)$this->address->publicIp;
3434 }
3535
3636 /**
Index: trunk/extensions/OpenStackManager/OpenStackManager.php
@@ -21,7 +21,7 @@
2222 'path' => __FILE__,
2323 'name' => 'OpenStackManager',
2424 'author' => 'Ryan Lane',
25 - 'version' => '1.0',
 25+ 'version' => '1.1',
2626 'url' => 'http://mediawiki.org/wiki/Extension:OpenStackManager',
2727 'descriptionmsg' => 'openstackmanager-desc',
2828 );
@@ -61,6 +61,11 @@
6262 'availableclasses' => array(),
6363 'availablevariables' => array(),
6464 );
 65+$wgOpenStackManagerInstanceUserData = array(
 66+ 'cloud-config' => array(),
 67+ 'scripts' => array(),
 68+ 'upstarts' => array(),
 69+ );
6570
6671 $dir = dirname( __FILE__ ) . '/';
6772

Comments

#Comment by Reedy (talk | contribs)   07:23, 30 January 2011

As a thought, I bet your message keys, and descriptions are now "somewhat mature", might be worth asking for the TW guys to give it another review and push out to TW? :)

#Comment by Reedy (talk | contribs)   07:48, 30 January 2011

Also, I've left a couple of TODO's in your code :)

#Comment by Ryan lane (talk | contribs)   17:37, 30 January 2011

Yeah, I think the messages are stable enough now to let TW at them. I'll likely tweak them some in the future, but nothing major :). I'll take a look at the TODOs.

Status & tagging log