File: Windows.php

package info (click to toggle)
zendframework 1.10.6-1squeeze2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 29,480 kB
  • ctags: 46,247
  • sloc: xml: 233,973; php: 195,700; sql: 90; sh: 19; makefile: 10
file content (195 lines) | stat: -rw-r--r-- 8,521 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
124
125
126
127
128
129
130
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Service_Amazon
 * @subpackage Ec2
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Windows.php 20096 2010-01-06 02:05:09Z bkarwin $
 */

/**
 * @see Zend_Service_Amazon_Ec2_Abstract
 */
require_once 'Zend/Service/Amazon/Ec2/Abstract.php';

/**
 * @see Zend_Crypt_Hmac
 */
require_once 'Zend/Crypt/Hmac.php';

/**
 * @see Zend_Json
 */
require_once 'Zend/Json.php';

/**
 * An Amazon EC2 interface that allows yout to run, terminate, reboot and describe Amazon
 * Ec2 Instances.
 *
 * @category   Zend
 * @package    Zend_Service_Amazon
 * @subpackage Ec2
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Service_Amazon_Ec2_Instance_Windows extends Zend_Service_Amazon_Ec2_Abstract
{
    /**
     * Bundles an Amazon EC2 instance running Windows
     *
     * @param string $instanceId        The instance you want to bundle
     * @param string $s3Bucket          Where you want the ami to live on S3
     * @param string $s3Prefix          The prefix you want to assign to the AMI on S3
     * @param integer $uploadExpiration The expiration of the upload policy.  Amazon recommends 12 hours or longer.
     *                                  This is based in nubmer of minutes. Default is 1440 minutes (24 hours)
     * @return array                    containing the information on the new bundle operation
     */
    public function bundle($instanceId, $s3Bucket, $s3Prefix, $uploadExpiration = 1440)
    {
        $params = array();
        $params['Action'] = 'BundleInstance';
        $params['InstanceId'] = $instanceId;
        $params['Storage.S3.AWSAccessKeyId'] = $this->_getAccessKey();
        $params['Storage.S3.Bucket'] = $s3Bucket;
        $params['Storage.S3.Prefix'] = $s3Prefix;
        $uploadPolicy = $this->_getS3UploadPolicy($s3Bucket, $s3Prefix, $uploadExpiration);
        $params['Storage.S3.UploadPolicy'] = $uploadPolicy;
        $params['Storage.S3.UploadPolicySignature'] = $this->_signS3UploadPolicy($uploadPolicy);

        $response = $this->sendRequest($params);

        $xpath = $response->getXPath();

        $return = array();
        $return['instanceId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:instanceId/text())');
        $return['bundleId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:bundleId/text())');
        $return['state'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:state/text())');
        $return['startTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:startTime/text())');
        $return['updateTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:updateTime/text())');
        $return['progress'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:progress/text())');
        $return['storage']['s3']['bucket'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:bucket/text())');
        $return['storage']['s3']['prefix'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:prefix/text())');

        return $return;
    }

    /**
     * Cancels an Amazon EC2 bundling operation
     *
     * @param string $bundleId          The ID of the bundle task to cancel
     * @return array                    Information on the bundle task
     */
    public function cancelBundle($bundleId)
    {
        $params = array();
        $params['Action'] = 'CancelBundleTask';
        $params['BundleId'] = $bundleId;

        $response = $this->sendRequest($params);

        $xpath = $response->getXPath();

        $return = array();
        $return['instanceId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:instanceId/text())');
        $return['bundleId'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:bundleId/text())');
        $return['state'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:state/text())');
        $return['startTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:startTime/text())');
        $return['updateTime'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:updateTime/text())');
        $return['progress'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:progress/text())');
        $return['storage']['s3']['bucket'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:bucket/text())');
        $return['storage']['s3']['prefix'] = $xpath->evaluate('string(//ec2:bundleInstanceTask/ec2:storage/ec2:S3/ec2:prefix/text())');

        return $return;
    }

    /**
     * Describes current bundling tasks
     *
     * @param string|array $bundleId            A single or a list of bundle tasks that you want
     *                                          to find information for.
     * @return array                            Information for the task that you requested
     */
    public function describeBundle($bundleId = '')
    {
        $params = array();
        $params['Action'] = 'DescribeBundleTasks';

        if(is_array($bundleId) && !empty($bundleId)) {
            foreach($bundleId as $k=>$name) {
                $params['bundleId.' . ($k+1)] = $name;
            }
        } elseif(!empty($bundleId)) {
            $params['bundleId.1'] = $bundleId;
        }

        $response = $this->sendRequest($params);

        $xpath = $response->getXPath();

        $items = $xpath->evaluate('//ec2:bundleInstanceTasksSet/ec2:item');
        $return = array();

        foreach($items as $item) {
            $i = array();
            $i['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $item);
            $i['bundleId'] = $xpath->evaluate('string(ec2:bundleId/text())', $item);
            $i['state'] = $xpath->evaluate('string(ec2:state/text())', $item);
            $i['startTime'] = $xpath->evaluate('string(ec2:startTime/text())', $item);
            $i['updateTime'] = $xpath->evaluate('string(ec2:updateTime/text())', $item);
            $i['progress'] = $xpath->evaluate('string(ec2:progress/text())', $item);
            $i['storage']['s3']['bucket'] = $xpath->evaluate('string(ec2:storage/ec2:S3/ec2:bucket/text())', $item);
            $i['storage']['s3']['prefix'] = $xpath->evaluate('string(ec2:storage/ec2:S3/ec2:prefix/text())', $item);

            $return[] = $i;
            unset($i);
        }


        return $return;
    }

    /**
     * Generates the S3 Upload Policy Information
     *
     * @param string $bucketName        Which bucket you want the ami to live in on S3
     * @param string $prefix            The prefix you want to assign to the AMI on S3
     * @param integer $expireInMinutes  The expiration of the upload policy.  Amazon recommends 12 hours or longer.
     *                                  This is based in nubmer of minutes. Default is 1440 minutes (24 hours)
     * @return string                   Base64 encoded string that is the upload policy
     */
    protected function _getS3UploadPolicy($bucketName, $prefix, $expireInMinutes = 1440)
    {
        $arrParams = array();
        $arrParams['expiration'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", (time() + ($expireInMinutes * 60)));
        $arrParams['conditions'][] = array('bucket' => $bucketName);
        $arrParams['conditions'][] = array('acl' => 'ec2-bundle-read');
        $arrParams['conditions'][] = array('starts-with', '$key', $prefix);

        return base64_encode(Zend_Json::encode($arrParams));
    }

    /**
     * Signed S3 Upload Policy
     *
     * @param string $policy            Base64 Encoded string that is the upload policy
     * @return string                   SHA1 encoded S3 Upload Policy
     */
    protected function _signS3UploadPolicy($policy)
    {
        $hmac = Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'SHA1', $policy, Zend_Crypt_Hmac::BINARY);
        return $hmac;
    }
}