View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
18   */
19  package org.jclouds.aws.ec2;
20  
21  import static org.jclouds.Constants.PROPERTY_ENDPOINT;
22  import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_AMI_QUERY;
23  import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_AMI_QUERY;
24  import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_AMIs;
25  import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_REGIONS;
26  import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIMEOUT_NODE_SUSPENDED;
27  import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_AMI_OWNERS;
28  
29  import java.util.Properties;
30  import java.util.logging.Logger;
31  
32  import org.jclouds.aws.domain.Region;
33  
34  /**
35   * Builds properties used in EC2 Clients
36   * 
37   * @author Adrian Cole
38   */
39  public class AWSEC2PropertiesBuilder extends org.jclouds.ec2.EC2PropertiesBuilder {
40     @Override
41     protected Properties defaultProperties() {
42        Properties properties = super.defaultProperties();
43        // sometimes, like in ec2, stop takes a very long time, perhaps
44        // due to volume management. one example spent 2 minutes moving
45        // from stopping->stopped state on an ec2 micro
46        properties.setProperty(PROPERTY_TIMEOUT_NODE_SUSPENDED, 120 * 1000 + "");
47        // auth fail sometimes happens in EC2, as the rc.local script that injects the
48        // authorized key executes after ssh has started
49        properties.setProperty("jclouds.ssh.max-retries", "7");
50        properties.setProperty("jclouds.ssh.retry-auth", "true");
51        properties.setProperty(PROPERTY_ENDPOINT, "https://ec2.us-east-1.amazonaws.com");
52        properties.putAll(Region.regionProperties());
53        properties.remove(PROPERTY_EC2_AMI_OWNERS);
54        // amazon, alestic, canonical, and rightscale
55        properties.setProperty(PROPERTY_EC2_AMI_QUERY,
56                 "owner-id=137112412989,063491364108,099720109477,411009282317;state=available;image-type=machine");
57        // amis that work with the cluster instances
58        properties.setProperty(PROPERTY_EC2_CC_REGIONS, Region.US_EAST_1);
59        properties
60                 .setProperty(
61                          PROPERTY_EC2_CC_AMI_QUERY,
62                          "virtualization-type=hvm;architecture=x86_64;owner-id=137112412989,099720109477;hypervisor=xen;state=available;image-type=machine;root-device-type=ebs");
63        return properties;
64     }
65  
66     public AWSEC2PropertiesBuilder() {
67        super();
68     }
69  
70     public AWSEC2PropertiesBuilder(Properties properties) {
71        super(properties);
72     }
73  
74     @Override
75     public Properties build() {
76        Properties props = super.build();
77        warnAndReplaceIfUsingOldImageKey(props);
78        warnAndReplaceIfUsingOldCCImageKey(props);
79        return props;
80     }
81  
82     protected void warnAndReplaceIfUsingOldImageKey(Properties props) {
83        if (props.containsKey(PROPERTY_EC2_AMI_OWNERS)) {
84           StringBuilder query = new StringBuilder();
85           String owners = properties.remove(PROPERTY_EC2_AMI_OWNERS).toString();
86           if ("*".equals(owners))
87              query.append("state=available;image-type=machine");
88           else if (!"".equals(owners))
89              query.append("owner-id=").append(owners).append(";state=available;image-type=machine");
90           else if ("".equals(owners))
91              query = new StringBuilder();
92           props.setProperty(PROPERTY_EC2_AMI_QUERY, query.toString());
93           Logger.getAnonymousLogger().warning(
94                    String.format("Property %s is deprecated, please use new syntax: %s=%s", PROPERTY_EC2_AMI_OWNERS,
95                             PROPERTY_EC2_AMI_QUERY, query.toString()));
96        }
97     }
98  
99     protected void warnAndReplaceIfUsingOldCCImageKey(Properties props) {
100       if (props.containsKey(PROPERTY_EC2_CC_AMIs)) {
101          String amis = properties.remove(PROPERTY_EC2_CC_AMIs).toString();
102          String value = ("".equals(amis)) ? "" : "image-id=" + amis.replace("us-east-1/", "");
103          props.setProperty(PROPERTY_EC2_CC_AMI_QUERY, value);
104          Logger.getAnonymousLogger().warning(
105                   String.format("Property %s is deprecated, please use new syntax: %s=%s", PROPERTY_EC2_CC_AMIs,
106                            PROPERTY_EC2_CC_AMI_QUERY, value));
107       }
108    }
109 
110 }