Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XAWS-4: Added CDK code for XWiki demo installation with unit tests. #6

Merged
merged 10 commits into from
Jun 28, 2021
Merged
2 changes: 2 additions & 0 deletions xwiki-demo-cdk/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*.d.ts
node_modules


# CDK asset staging directory
.cdk.staging
cdk.out
.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -17,12 +18,15 @@
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
import * as cdk from '@aws-cdk/core';
import 'source-map-support/register'
import { App } from '@aws-cdk/core'
import { EC2XwikiDemo } from '../lib/stacks/ec2-xwiki-demo'
import { xwikidownload } from '../lib/stacks/config'

export class XwikiDemoCdkStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

// The code that defines your stack goes here
const app = new App()
new EC2XwikiDemo(app, 'ec2XwikiDemo', {
xwiki: xwikidownload,
env: {
region: 'eu-central-1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont hard code the region. Get it from some configuration file as discussed

}
}
})
95 changes: 95 additions & 0 deletions xwiki-demo-cdk/cdk.context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"vpc-provider:account=656019072197:filter.isDefault=true:region=us-east-1:returnAsymmetricSubnets=true": {
"vpcId": "vpc-740af709",
"vpcCidrBlock": "172.31.0.0/16",
"availabilityZones": [],
"subnetGroups": [
{
"name": "Public",
"type": "Public",
"subnets": [
{
"subnetId": "subnet-257c0c68",
"cidr": "172.31.16.0/20",
"availabilityZone": "us-east-1a",
"routeTableId": "rtb-8f8ff7f1"
},
{
"subnetId": "subnet-20d8757f",
"cidr": "172.31.32.0/20",
"availabilityZone": "us-east-1b",
"routeTableId": "rtb-8f8ff7f1"
},
{
"subnetId": "subnet-f1319097",
"cidr": "172.31.0.0/20",
"availabilityZone": "us-east-1c",
"routeTableId": "rtb-8f8ff7f1"
},
{
"subnetId": "subnet-0e8b242f",
"cidr": "172.31.80.0/20",
"availabilityZone": "us-east-1d",
"routeTableId": "rtb-8f8ff7f1"
},
{
"subnetId": "subnet-7a3dc94b",
"cidr": "172.31.48.0/20",
"availabilityZone": "us-east-1e",
"routeTableId": "rtb-8f8ff7f1"
},
{
"subnetId": "subnet-47f76d49",
"cidr": "172.31.64.0/20",
"availabilityZone": "us-east-1f",
"routeTableId": "rtb-8f8ff7f1"
}
]
}
]
},
"vpc-provider:account=656019072197:filter.isDefault=true:region=eu-central-1:returnAsymmetricSubnets=true": {
"vpcId": "vpc-16268b7c",
"vpcCidrBlock": "172.31.0.0/16",
"availabilityZones": [],
"subnetGroups": [
{
"name": "Public",
"type": "Public",
"subnets": [
{
"subnetId": "subnet-f691059c",
"cidr": "172.31.16.0/20",
"availabilityZone": "eu-central-1a",
"routeTableId": "rtb-708ea91a"
},
{
"subnetId": "subnet-afe724d3",
"cidr": "172.31.32.0/20",
"availabilityZone": "eu-central-1b",
"routeTableId": "rtb-708ea91a"
},
{
"subnetId": "subnet-1711c75b",
"cidr": "172.31.0.0/20",
"availabilityZone": "eu-central-1c",
"routeTableId": "rtb-708ea91a"
}
]
}
]
},
"availability-zones:account=656019072197:region=us-east-1": [
"us-east-1a",
"us-east-1b",
"us-east-1c",
"us-east-1d",
"us-east-1e",
"us-east-1f"
],
"availability-zones:account=656019072197:region=eu-central-1": [
"eu-central-1a",
"eu-central-1b",
"eu-central-1c"
]
}
2 changes: 1 addition & 1 deletion xwiki-demo-cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"app": "npx ts-node --prefer-ts-exts bin/xwiki-demo-cdk.ts",
"app": "npx ts-node --prefer-ts-exts Bin/xwiki-demo-cdk.ts",
"context": {
"@aws-cdk/core:enableStackNameDuplicates": "true",
"aws-cdk:enableDiffNoFail": "true",
Expand Down
7 changes: 7 additions & 0 deletions xwiki-demo-cdk/lib/stacks/Ec2-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { StackProps } from '@aws-cdk/core'

export interface ec2props extends StackProps{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add JavaDoc for all classes. Explain parameters in comments. What's the use of each.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Please suggest the changes if required


xwiki: string

}
1 change: 1 addition & 0 deletions xwiki-demo-cdk/lib/stacks/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const xwikidownload = 'https://nexus.xwiki.org/nexus/content/groups/public/org/xwiki/platform/xwiki-platform-distribution-flavor-jetty-hsqldb/13.1/xwiki-platform-distribution-flavor-jetty-hsqldb-13.1.zip'
124 changes: 124 additions & 0 deletions xwiki-demo-cdk/lib/stacks/ec2-xwiki-demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/env node
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
import { Stack, App, CfnOutput } from '@aws-cdk/core'
import { Vpc, SecurityGroup, Peer, Port, Instance, InstanceType, InstanceClass, InstanceSize, MachineImage, AmazonLinuxGeneration, SubnetType, UserData } from '@aws-cdk/aws-ec2'
import { Role, ServicePrincipal } from '@aws-cdk/aws-iam'
import { ec2props } from '../stacks/Ec2-model'

export class EC2XwikiDemo extends Stack {
constructor (scope: App, id: string, props: ec2props) {
super(scope, id, props)

const defaultVpc = new Vpc(this, 'VPC', {
cidr: '10.0.0.0/16',
maxAzs: 2,
subnetConfiguration: [
{
name: 'public',
subnetType: SubnetType.PUBLIC,
cidrMask: 27
}
]
})

const role = new Role(
this,
'ec2-xwiki-demo-instance-role',
{ assumedBy: new ServicePrincipal('ec2.amazonaws.com') }
)

const securityGroup = new SecurityGroup(
this,
'ec2-xwiki-demo-sg',
{
vpc: defaultVpc,
allowAllOutbound: true, // will let xwiki instance send outbound traffic
securityGroupName: 'ec2-xwiki-demo-sg'
}
)

// configuring the security group to allow inbound traffic at specific ports
securityGroup.addIngressRule(
Peer.anyIpv4(),
Port.tcp(22),
'Allows SSH access from Internet'
)

securityGroup.addIngressRule(
Peer.anyIpv4(),
Port.tcp(80),
'Allows HTTP access from Internet'
)

securityGroup.addIngressRule(
Peer.anyIpv4(),
Port.tcp(443),
'Allows HTTPS access from Internet'
)

securityGroup.addIngressRule(
Peer.anyIpv6(),
Port.tcp(8080),
'Allows port 8080 on Ipv6'
)

securityGroup.addIngressRule(
Peer.anyIpv4(),
Port.tcp(8080),
'Allows port 8080 on Ipv4'

)

// provesioning the EC2 instance inside the VPC containg xwiki
const instance = new Instance(this, 'ec2-xwiki-demo', {
vpc: defaultVpc,
role: role,
securityGroup: securityGroup,
instanceName: 'ec2-xwiki-demo',
instanceType: InstanceType.of( // t2.micro has free tier usage in aws
InstanceClass.T2,
InstanceSize.MEDIUM
),
machineImage: MachineImage.latestAmazonLinux({
generation: AmazonLinuxGeneration.AMAZON_LINUX_2
}),

keyName: 'test', // user will first create this key in console

userData: UserData.forLinux({
shebang: `#! /bin/bash \n sudo yum -q -y install java-1.8.0-openjdk \n sudo yum -q -y install unzip \n mkdir xwikihome \n wget -q -O xwiki_packer.zip ${props.xwiki} \n unzip -q xwiki_packer.zip -d /home/ec2-user/xwikihome`
})

})

// const commands = ['sudo yum -q -y install java-1.8.0-openjdk && sudo yum -q -y install unzip && mkdir xwikihome && wget -q -O xwiki_packer.zip https://nexus.xwiki.org/nexus/content/groups/public/org/xwiki/platform/xwiki-platform-distribution-flavor-jetty-hsqldb/13.1/xwiki-platform-distribution-flavor-jetty-hsqldb-13.1.zip && unzip -q xwiki_packer.zip -d /home/ec2-user/xwikihome']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these lines if they are not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

// // adding the commands that will install xwiki in the instance
// instance.addUserData(
// fs.readFileSync('lib/stacks/user_script.sh', 'utf8')
// )

// cdk lets us output prperties of the resources we create after they are created
// we want the ip address of this new instance so we can ssh into it later
new CfnOutput(this, 'xwiki-demo-instance-output', {
value: instance.instancePublicIp
})
}
}
11 changes: 11 additions & 0 deletions xwiki-demo-cdk/lib/stacks/user_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! /bin/bash
# become root user

sudo yum -q -y install java-1.8.0-openjdk
sudo yum -q -y install unzip
mkdir xwikihome
wget -q -O xwiki_packer.zip https://nexus.xwiki.org/nexus/content/groups/public/org/xwiki/platform/xwiki-platform-distribution-flavor-jetty-hsqldb/13.1/xwiki-platform-distribution-flavor-jetty-hsqldb-13.1.zip
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file.

unzip -q xwiki_packer.zip -d /home/ec2-user/xwikihome