View Javadoc

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