Gradle Update Project Version number through gradle task

Posted on

Create a gradle.properties file and add a version property

version=1.0.0-SNAPSHOT

Then in build.gradle file add

task patchVersion {
    doLast{
        Properties props = new Properties()
        File propsFile = new File('gradle.properties')
        props.load(propsFile.newDataInputStream())
        def stable_project_version = props.getProperty('version') as String
        if (stable_project_version.indexOf('-SNAPSHOT') > 0){
            throw new GradleException('Cannot patch a non SNAPSHOT version')
        } else {
            def (major, minor, patch) = stable_project_version.tokenize('.')
            patch = String.valueOf(patch.toInteger() + 1)
            String snapshot_project_version = major + '.' + minor + '.' + patch + '-SNAPSHOT'
            props.setProperty('version', snapshot_project_version)
            props.store(propsFile.newWriter(), null)
            println(snapshot_project_version)
        }
    }
}

task releaseVersion {
    doLast{
        Properties props = new Properties()
        File propsFile = new File('gradle.properties')
        props.load(propsFile.newDataInputStream())
        def snapshot_project_version = props.getProperty('version') as String
        if (snapshot_project_version.indexOf('-SNAPSHOT') > 0){
            def stable_project_version = snapshot_project_version - '-SNAPSHOT'
            props.setProperty('version', stable_project_version)
            props.store(propsFile.newWriter(), null)
            println(stable_project_version)
        } else {
            throw new GradleException('Cannot release a non SNAPSHOT version')
        }
    }
}

Run releaseVersion to automatically update SNAPSHOT version to stable version.
Eg. if your project version is 1.0.0-SNAPSHOT

gradle releaseVersion

will update the version in gradle.properties to 1.0.0

Run patchVersion to automatically update Stable version to the next patch version.
Eg. if your version is 1.0.0

gradle patchVersion

will update the version in gradle.properties to 1.0.1-SNAPSHOT

Advertisements

JSON to csv string in python

Posted on Updated on

from cStringIO import StringIO
import csv


def json_to_csv(data):
    output = StringIO()
    writer = csv.DictWriter(output, data[0].keys())
    writer.writeheader()
    for row in data:
        writer.writerow(row)
    return output


print (write_csv([{"name": "Dolor", "age": "23"}, {"name": "John Doe", "age": "32"}, {"age": "42", "name": "Lorem Ipsum"}], 'output.csv').getvalue())

# output
# age,name
# 23,Abiral
# 32,Lorem
# 42,asdfasd

AWS VPC and VPC Peering

Posted on Updated on

 

Creating a VPC

  1. Create a new VPC with CIDR block
    Eg .
    10.0.0.0/16
    Number of ips = 2 ^ (32-16)
    Ips range in the VPC (1.0.0.0 – 10.0.255.255)
  2. Create Public and private Subnets inside the VPC
    Eg. CIDR block (10.0.0.0/24) – 256 ips in a subnet
    Public subnet will have Internet gateway attached to their route table while private ones won’t
  3. Create an Internet Gateway and attach it to the VPC, Instance in the VPC will be able to connect to the internet though the Internet gateway
  4. Create a NAT Gateway inside a public subnet, NAT gateway allows one way traffic from Private Subnet to the internet
  5. Create 2 Route tables one for public subnet and one for private subnet
    Assign subnet to corresponding route table through subnet Association Tab

    1. For Private Subnet, add these routes
      1. For outgoing connection-
        Destination: 0.0.0.0/0 —- Target: NAT-gateway(eg. nat-foobar)
        Now instance in private subnet can access the outside world through NAT but outside world can’t connect to the instances
    2. For Public Sunet, add these routes
      1. For in and out connection through internet
        Destination: 0.0.0.0/0 —- Target: internet-gateway(eg. igw-loremipsum)
        Now instances in public subnet can access and be accessed from the internet through the Internet Gateway

VPC Peering

  1. Make sure the ip CIDR block of the 2 VPCS do not collide. If the existing VPC has CIDR block 10.0.0.0/16, then the new VPC should use CIDR block like 10.1.0.0/16 so the ips in the two VPCs don’t collide
  2. Creating Peering Connection from Peering Connections Page
    1. Give a proper Peering connection name tag
    2. Select new VPC as the Requester VPC
    3. Select old VPC as the another VPC and submit
    4. Click on the Newly Created Peering Connection and press Actions and press Accept Request to accept the peering.
  3. Now the route tables need to be updated to add routes to each others ips
    1. Edit the private and public Route table of both VPC and add an entry on each
      Destination = CIDR block of the other VPC
      Target = Peering connection. Eg pcx-foobar
    2. Edit DNS Settings:
      Check both checkboxes
      Allow DNS resolution from peer VPC (vpc-dc700db8) to private IP
      Allow DNS resolution from local VPC (vpc-6bcf1b0f) to private IP
  4. For accessing rds from other VPC, add the CIDR block of the current VPC in the security group of rds instance.

Find max and min number from a number having the same number of digits as that number

Posted on Updated on

foo_number = 12345
min = pow(10, len(str(foo_number)) - 1) # 10000
max = pow(10, len(str(foo_number))) - 1 # 99999

Tregex Patterns

Posted on Updated on

Symbol Meaning
A << B A dominates B
A >> B A is dominated by B
A < B A immediately dominates B
A > B A is immediately dominated by B
A $ B A is a sister of B (and not equal to B)
A .. B A precedes B
A . B A immediately precedes B
A ,, B A follows B
A , B A immediately follows B
A <<, B B is a leftmost descendant of A
A <<- B B is a rightmost descendant of A
A >>, B A is a leftmost descendant of B
A >>- B A is a rightmost descendant of B
A <, B B is the first child of A
A >, B A is the first child of B
A <- B B is the last child of A
A >- B A is the last child of B
A <` B B is the last child of A
A >` B A is the last child of B
A <i B B is the ith child of A (i > 0)
A >i B A is the ith child of B (i > 0)
A <-i B B is the ith-to-last child of A (i > 0)
A >-i B A is the ith-to-last child of B (i > 0)
A <: B B is the only child of A
A >: B A is the only child of B
A <<: B A dominates B via an unbroken chain (length > 0) of unary local trees.
A >>: B A is dominated by B via an unbroken chain (length > 0) of unary local trees.
A $++ B A is a left sister of B (same as $.. for context-free trees)
A $– B A is a right sister of B (same as $,, for context-free trees)
A $+ B A is the immediate left sister of B (same as $. for context-free trees)
A $- B A is the immediate right sister of B (same as $, for context-free trees)
A $.. B A is a sister of B and precedes B
A $,, B A is a sister of B and follows B
A $. B A is a sister of B and immediately precedes B
A $, B A is a sister of B and immediately follows B
A <+(C) B A dominates B via an unbroken chain of (zero or more) nodes matching description C
A >+(C) B A is dominated by B via an unbroken chain of (zero or more) nodes matching description C
A .+(C) B A precedes B via an unbroken chain of (zero or more) nodes matching description C
A ,+(C) B A follows B via an unbroken chain of (zero or more) nodes matching description C
A <<# B B is a head of phrase A
A >># B A is a head of phrase B
A <# B B is the immediate head of phrase A
A ># B A is the immediate head of phrase B
A == B A and B are the same node
A <= B A and B are the same node or A is the parent of B
A : B [this is a pattern-segmenting operator that places no constraints on the relationship between A and B]
A <… { B ; C ; … } A has exactly B, C, etc as its subtree, with no other children.

Source: https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/tregex/TregexPattern.html

Git shell command checkout out branch if exists else create one

Posted on Updated on

git rev-parse --quiet --verify <branch-name> && git checkout <branch-name> || git checkout -b <branch-name>

Amazon AWS Codedeploy and Jenkins

Posted on Updated on

Things to Note:

Assign new instance to an IAM Role required for codedeploy to work on that instance while creating the instance, you can’t assign an instance to a new role once it has been initiated

While using Jenkins Aws Codedeploy plugin for Jenkins, the symbolic links are not preserved when the files in the workspace directory are zipped so there may be some error.

To preserver symlinks what I did was

tar the required files and then allow Jenkins aws codedeploy to zip that tar file and deploy it on AWS, then on appspec.yaml file  I ran a post install script to untar the tar archive.