Skip to content

Commit 868748d

Browse files
committed
Merge branch 'feature/support_rds' into develop
2 parents 8e40a9c + 7dbbb6e commit 868748d

11 files changed

+923
-16
lines changed

samples/07-rds/build.gradle

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// -*- coding: utf-8; mode: groovy -*-
2+
3+
import jp.classmethod.aws.gradle.rds.AmazonRDSDeleteDBInstanceTask;
4+
import jp.classmethod.aws.gradle.rds.AmazonRDSMigrateDBInstanceTask;
5+
import jp.classmethod.aws.gradle.rds.AmazonRDSRebootDBInstanceTask;
6+
import jp.classmethod.aws.gradle.rds.AmazonRDSWaitInstanceStatusTask;
7+
8+
buildscript {
9+
repositories {
10+
mavenCentral()
11+
maven { url "http://public-maven.classmethod.info/release" }
12+
maven { url "http://public-maven.classmethod.info/snapshot" }
13+
}
14+
dependencies {
15+
classpath "jp.classmethod.aws:gradle-aws-plugin:0.+"
16+
}
17+
}
18+
19+
apply plugin: "aws-rds"
20+
aws {
21+
profileName "default"
22+
region "ap-northeast-1"
23+
}
24+
25+
if (hasProperty('instanceId') == false) { ext.instanceId = 'i-12345678' }
26+
27+
task migrateDBInstance(type: AmazonRDSMigrateDBInstanceTask) {
28+
dbInstanceIdentifier = "hogex"
29+
allocatedStorage = 5
30+
dbInstanceClass = "db.t2.micro"
31+
engine = "MySQL"
32+
masterUsername = "root"
33+
masterUserPassword = "passW0rd"
34+
vpcSecurityGroupIds = [ "sg-d3958fbf" ]
35+
dbSubnetGroupName = "default"
36+
multiAZ = false
37+
publiclyAccessible = true
38+
}
39+
40+
task rebootDBInstance(type: AmazonRDSRebootDBInstanceTask) {
41+
dbInstanceIdentifier = "hogex"
42+
}
43+
44+
task deleteDBInstance(type: AmazonRDSDeleteDBInstanceTask) {
45+
dbInstanceIdentifier = "hogex"
46+
skipFinalSnapshot = true
47+
}
48+
49+
task waitDBInstanceStatus(type: AmazonRDSWaitInstanceStatusTask) {
50+
dbInstanceIdentifier = "hogex"
51+
successStatuses = [ "available", "terminated" ]
52+
waitStatuses = [
53+
"backing-up",
54+
"creating",
55+
"deleting",
56+
"modifying",
57+
"rebooting",
58+
"renaming",
59+
"resetting-master-credentials"
60+
]
61+
mustRunAfter migrateDBInstance, rebootDBInstance, deleteDBInstance
62+
}

src/main/java/jp/classmethod/aws/gradle/cloudformation/AmazonCloudFormationMigrateStackTask.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class AmazonCloudFormationMigrateStackTask extends ConventionTask {
4545
);
4646

4747
public AmazonCloudFormationMigrateStackTask() {
48-
setDescription("Create/Migrate cfn stack.");
48+
setDescription("Create / Migrate cfn stack.");
4949
setGroup("AWS");
5050
}
5151

@@ -65,21 +65,18 @@ public void createOrUpdateStack() throws InterruptedException {
6565
try {
6666
DescribeStacksResult describeStackResult = cfn.describeStacks(new DescribeStacksRequest().withStackName(stackName));
6767
Stack stack = describeStackResult.getStacks().get(0);
68-
if (stack == null) {
69-
getLogger().info("stack "+stackName+" not found");
70-
createStack(cfn);
71-
} else if (stack.getStackStatus().equals("DELETE_COMPLETE")) {
72-
getLogger().warn("deleted stack "+stackName+" already exists");
68+
if (stack.getStackStatus().equals("DELETE_COMPLETE")) {
69+
getLogger().warn("deleted stack {} already exists", stackName);
7370
deleteStack(cfn);
7471
createStack(cfn);
75-
} else if (stableStatuses.contains(stack.getStackStatus()) == false) {
76-
throw new GradleException("invalid status for update: " + stack.getStackStatus());
77-
} else {
72+
} else if (stableStatuses.contains(stack.getStackStatus())) {
7873
updateStack(cfn);
74+
} else {
75+
throw new GradleException("invalid status for update: " + stack.getStackStatus());
7976
}
8077
} catch (AmazonServiceException e) {
8178
if (e.getMessage().contains("does not exist")) {
82-
getLogger().warn("stack "+stackName+" not found");
79+
getLogger().warn("stack {} not found", stackName);
8380
createStack(cfn);
8481
} else if (e.getMessage().contains("No updates are to be performed.")) {
8582
// ignore
@@ -95,7 +92,7 @@ private void updateStack(AmazonCloudFormation cfn) {
9592
String cfnTemplateUrl = getCfnTemplateUrl();
9693
List<Parameter> cfnStackParams = getCfnStackParams();
9794

98-
getLogger().info("update stack: " + stackName);
95+
getLogger().info("update stack: {}", stackName);
9996
UpdateStackRequest req = new UpdateStackRequest()
10097
.withStackName(stackName)
10198
.withTemplateURL(cfnTemplateUrl)
@@ -104,16 +101,16 @@ private void updateStack(AmazonCloudFormation cfn) {
104101
req.setCapabilities(Arrays.asList(Capability.CAPABILITY_IAM.toString()));
105102
}
106103
UpdateStackResult updateStackResult = cfn.updateStack(req);
107-
getLogger().info("update requested: " + updateStackResult.getStackId());
104+
getLogger().info("update requested: {}", updateStackResult.getStackId());
108105
}
109106

110107
private void deleteStack(AmazonCloudFormation cfn) throws InterruptedException {
111108
// to enable conventionMappings feature
112109
String stackName = getStackName();
113110

114-
getLogger().info("delete stack: " + stackName);
111+
getLogger().info("delete stack: {}", stackName);
115112
cfn.deleteStack(new DeleteStackRequest().withStackName(stackName));
116-
getLogger().info("delete requested: " + stackName);
113+
getLogger().info("delete requested: {}", stackName);
117114
Thread.sleep(3000);
118115
}
119116

@@ -123,7 +120,7 @@ private void createStack(AmazonCloudFormation cfn) {
123120
String cfnTemplateUrl = getCfnTemplateUrl();
124121
List<Parameter> cfnStackParams = getCfnStackParams();
125122

126-
getLogger().info("create stack: " + stackName);
123+
getLogger().info("create stack: {}", stackName);
127124

128125
CreateStackRequest req = new CreateStackRequest()
129126
.withStackName(stackName)
@@ -133,6 +130,6 @@ private void createStack(AmazonCloudFormation cfn) {
133130
req.setCapabilities(Arrays.asList(Capability.CAPABILITY_IAM.toString()));
134131
}
135132
CreateStackResult createStackResult = cfn.createStack(req);
136-
getLogger().info("create requested: " + createStackResult.getStackId());
133+
getLogger().info("create requested: {}", createStackResult.getStackId());
137134
}
138135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright 2013-2014 Classmethod, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package jp.classmethod.aws.gradle.rds;
17+
18+
import java.util.List;
19+
20+
import lombok.Getter;
21+
import lombok.Setter;
22+
23+
import org.gradle.api.GradleException;
24+
import org.gradle.api.internal.ConventionTask;
25+
import org.gradle.api.tasks.TaskAction;
26+
27+
import com.amazonaws.services.rds.AmazonRDS;
28+
import com.amazonaws.services.rds.model.CreateDBInstanceRequest;
29+
import com.amazonaws.services.rds.model.DBInstance;
30+
31+
32+
public class AmazonRDSCreateDBInstanceTask extends ConventionTask {
33+
34+
@Getter @Setter
35+
private String dbName;
36+
37+
@Getter @Setter
38+
private String dbInstanceIdentifier;
39+
40+
@Getter @Setter
41+
private Integer allocatedStorage;
42+
43+
@Getter @Setter
44+
private String dbInstanceClass;
45+
46+
@Getter @Setter
47+
private String engine;
48+
49+
@Getter @Setter
50+
private String masterUsername;
51+
52+
@Getter @Setter
53+
private String masterUserPassword;
54+
55+
@Getter @Setter
56+
private List<String> vpcSecurityGroupIds;
57+
58+
@Getter @Setter
59+
private String dbSubnetGroupName;
60+
61+
@Getter @Setter
62+
private String preferredMaintenanceWindow;
63+
64+
@Getter @Setter
65+
private String dbParameterGroupName;
66+
67+
@Getter @Setter
68+
private Integer backupRetentionPeriod;
69+
70+
@Getter @Setter
71+
private String preferredBackupWindow;
72+
73+
@Getter @Setter
74+
private Integer port;
75+
76+
@Getter @Setter
77+
private Boolean multiAZ;
78+
79+
@Getter @Setter
80+
private String engineVersion;
81+
82+
@Getter @Setter
83+
private Boolean autoMinorVersionUpgrade;
84+
85+
@Getter @Setter
86+
private String licenseModel;
87+
88+
@Getter @Setter
89+
private Integer iops;
90+
91+
@Getter @Setter
92+
private String optionGroupName;
93+
94+
@Getter @Setter
95+
private Boolean publiclyAccessible;
96+
97+
@Getter @Setter
98+
private String characterSetName;
99+
100+
@Getter @Setter
101+
private String storageType;
102+
103+
@Getter @Setter
104+
private String tdeCredentialArn;
105+
106+
@Getter @Setter
107+
private String tdeCredentialPassword;
108+
109+
@Getter @Setter
110+
private Boolean storageEncrypted;
111+
112+
@Getter @Setter
113+
private String kmsKeyId;
114+
115+
@Getter
116+
private DBInstance dbInstance;
117+
118+
119+
public AmazonRDSCreateDBInstanceTask() {
120+
setDescription("Create RDS instance.");
121+
setGroup("AWS");
122+
}
123+
124+
@TaskAction
125+
public void createDBInstance() {
126+
// to enable conventionMappings feature
127+
String dbInstanceIdentifier = getDbInstanceIdentifier();
128+
String dbInstanceClass = getDbInstanceClass();
129+
String engine = getEngine();
130+
131+
if (dbInstanceClass == null) throw new GradleException("dbInstanceClass is required");
132+
if (dbInstanceIdentifier == null) throw new GradleException("dbInstanceIdentifier is required");
133+
if (engine == null) throw new GradleException("engine is required");
134+
135+
AmazonRDSPluginExtension ext = getProject().getExtensions().getByType(AmazonRDSPluginExtension.class);
136+
AmazonRDS rds = ext.getClient();
137+
138+
CreateDBInstanceRequest request = new CreateDBInstanceRequest()
139+
.withDBName(getDbName())
140+
.withDBInstanceIdentifier(dbInstanceIdentifier)
141+
.withAllocatedStorage(getAllocatedStorage())
142+
.withDBInstanceClass(dbInstanceClass)
143+
.withEngine(engine)
144+
.withMasterUsername(getMasterUsername())
145+
.withMasterUserPassword(getMasterUserPassword())
146+
.withVpcSecurityGroupIds(getVpcSecurityGroupIds())
147+
.withDBSubnetGroupName(getDbSubnetGroupName())
148+
.withPreferredMaintenanceWindow(getPreferredMaintenanceWindow())
149+
.withDBParameterGroupName(getDbParameterGroupName())
150+
.withBackupRetentionPeriod(getBackupRetentionPeriod())
151+
.withPreferredBackupWindow(getPreferredBackupWindow())
152+
.withPort(getPort())
153+
.withMultiAZ(getMultiAZ())
154+
.withEngineVersion(getEngineVersion())
155+
.withAutoMinorVersionUpgrade(getAutoMinorVersionUpgrade())
156+
.withLicenseModel(getLicenseModel())
157+
.withIops(getIops())
158+
.withOptionGroupName(getOptionGroupName())
159+
.withPubliclyAccessible(getPubliclyAccessible())
160+
.withCharacterSetName(getCharacterSetName())
161+
.withStorageType(getStorageType())
162+
.withTdeCredentialArn(getTdeCredentialArn())
163+
.withTdeCredentialPassword(getTdeCredentialPassword())
164+
.withStorageEncrypted(getStorageEncrypted())
165+
.withKmsKeyId(getKmsKeyId());
166+
dbInstance = rds.createDBInstance(request);
167+
getLogger().info("Create RDS instance requested: {}", dbInstance.getDBInstanceIdentifier());
168+
}
169+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2013-2014 Classmethod, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package jp.classmethod.aws.gradle.rds;
17+
18+
import lombok.Getter;
19+
import lombok.Setter;
20+
21+
import org.gradle.api.GradleException;
22+
import org.gradle.api.internal.ConventionTask;
23+
import org.gradle.api.tasks.TaskAction;
24+
25+
import com.amazonaws.services.rds.AmazonRDS;
26+
import com.amazonaws.services.rds.model.DBInstance;
27+
import com.amazonaws.services.rds.model.DBInstanceNotFoundException;
28+
import com.amazonaws.services.rds.model.DeleteDBInstanceRequest;
29+
30+
31+
public class AmazonRDSDeleteDBInstanceTask extends ConventionTask {
32+
33+
@Getter @Setter
34+
private String dbInstanceIdentifier;
35+
36+
@Getter @Setter
37+
private boolean skipFinalSnapshot;
38+
39+
@Getter @Setter
40+
private String finalDBSnapshotIdentifier;
41+
42+
@Getter
43+
private DBInstance dbInstance;
44+
45+
46+
public AmazonRDSDeleteDBInstanceTask() {
47+
setDescription("Delete RDS instance.");
48+
setGroup("AWS");
49+
}
50+
51+
@TaskAction
52+
public void deleteDBInstance() {
53+
// to enable conventionMappings feature
54+
String dbInstanceIdentifier = getDbInstanceIdentifier();
55+
56+
if (dbInstanceIdentifier == null) throw new GradleException("dbInstanceIdentifier is required");
57+
58+
AmazonRDSPluginExtension ext = getProject().getExtensions().getByType(AmazonRDSPluginExtension.class);
59+
AmazonRDS rds = ext.getClient();
60+
61+
try {
62+
DeleteDBInstanceRequest request = new DeleteDBInstanceRequest()
63+
.withDBInstanceIdentifier(dbInstanceIdentifier)
64+
.withSkipFinalSnapshot(isSkipFinalSnapshot())
65+
.withFinalDBSnapshotIdentifier(getFinalDBSnapshotIdentifier());
66+
dbInstance = rds.deleteDBInstance(request);
67+
getLogger().info("Delete RDS instance requested: {}", dbInstance.getDBInstanceIdentifier());
68+
} catch (DBInstanceNotFoundException e) {
69+
getLogger().warn(e.getMessage());
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)