Thursday, November 17, 2016

Delete Git Branch Local and Remote

Executive Summary

$ git branch -d 
$ git push origin --delete 

Delete Local Branch

To delete the local branch use:
$ git branch -d branch_name
Note: The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch]

Delete Remote Branch [Updated on 1-Feb-2012]

As of Git v1.7.0, you can delete a remote branch using
$ git push origin --delete 
which might be easier to remember than
$ git push origin :

Friday, September 02, 2016

Gradle Dependencies

https://docs.gradle.org/current/userguide/tutorial_gradle_command_line.html
gradle -q api:dependencies --configuration testCompile

Wednesday, August 17, 2016

Externalize Common Gradle Functions


Content of helpers/common-methods.gradle:

// Define methods as usual
def commonMethod1(param){
    return true
}
def commonMethod2(param){
    return true
}

// Export methods by turning them into closures
ext{
    commonMethod1 = this.&commonMethod1
    otherNameForMethod2 = this.&commonMethod2
}
And this is how I use those methods in another script:

// Use double-quotes, otherwise $ won't work
apply from: "http://myhelpers.com/helpers/common-methods.gradle"

// You can also use URLs
//apply from: "https://bitbucket.org/mb/build_scripts/raw/master/common-methods.gradle"

task myBuildTask{    
    def myVar = commonMethod1("parameter1")
    otherNameForMethod2(myVar)    
}

Thursday, August 04, 2016

Trust Relationship and Policies for AWS API Gateway and Lambdas

Your Policy for the lambda should set up everything your lambda is allowed to do. This includes passing a role.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1470153553000",
            "Effect": "Allow",
            "Action": [
                "dynamodb:*"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-west-2:439753510372:table/YoYoDyne_Products"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "lambda:InvokeFunction"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:*:*:*"
            ]
        },
        {
            "Sid": "Stmt1449789105000",
            "Effect": "Allow",
            "Action": [
                "iam:PassRole"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
Your policy also needs to have a trust relationship.
{
   "Version": "2012-10-17",
   "Statement": [
       {
           "Sid": "",
           "Effect": "Allow",
           "Principal": {
               "Service": ["lambda.amazonaws.com", "apigateway.amazonaws.com"]
           },
           "Action": "sts:AssumeRole"
       }
   ]
}

Tuesday, August 02, 2016

Java HTTPS Over VPN: Unrecognized SSL message, plaintext connection?

Many times a VPN will screw around with the IPV4 settings for secure connections.
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
Add these VM options to avoid problems:
-Djsse.enableSNIExtension=false
-Djava.net.preferIPv4Stack=true

Wednesday, June 15, 2016

Find What Process is Running on a Port OSX

17:12:52 ~/data/github/dronze {feature/#58} $ lsof -i :8000
COMMAND   PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    29680 claytongraham  143u  IPv6 0xb9a0c6ac4b96e77f      0t0  TCP *:irdmi (LISTEN)

Monday, June 13, 2016

Minimum Mongo Restore Permissions

So this would work to restore a single database: Backup
sudo mongodump -h localhost -d stuffdb --username restoreSingleDB --password Moon1234 \
  -o stuffdb.20160724.dump
Restore
db.addUser( {
    user: "restoreSingleDB",
    pwd: "Moon1234",
    roles: [ "readWrite", "userAdmin" ]
} )

mongorestore --db  --username restoreSingleDB --password Moon1234 /
So this would work to restore all databases including user data:
db.addUser( {
    user: "restoreAll",
    pwd: "Moon1234",
    roles: [ "readWriteAnyDatabase", "userAdminAnyDatabase" ]
} )

mongorestore --username restoreAll --password Moon1234 /

Sunday, June 05, 2016

Documenting a REST API

I have found that MkDocs is pretty good. I can integrate it into a CICD build with little effort. What was not obvious was best practices for writing API docs. So I create a template! Check it out:

Subdomain CNAME with Cloudfront

Automation is good. It means less mistakes and simple cookbooks for runbooks and deployments. Here is a simple recipe for deploying a subdomain CNAME with cloudfront.

Saturday, May 21, 2016

Upload HTTPS Cert to Cloudfront

23:28:23 ~/data/dronze/keys $ aws iam upload-server-certificate \
   --server-certificate-name dronze.com --certificate-body file://dronze_com_crt.pem \
   --private-key file://dronze_com.key --certificate-chain file://dronze_com.ca-bundle \
   --path /cloudfront/production/
{
    "ServerCertificateMetadata": {
        "ServerCertificateId": "ASCAIDAGVOZBPZ6VJTK7A",
        "ServerCertificateName": "dronze.com",
        "Expiration": "2017-05-22T23:59:59Z",
        "Path": "/cloudfront/production/",
        "Arn": "arn:aws:iam::705212546939:server-certificate/cloudfront/production/dronze.com",
        "UploadDate": "2016-05-22T06:30:39.224Z"
    }
}

Tuesday, May 03, 2016

How to set your log4j2 file to support the lambda appender.

Add to your gradle config:

compile 'com.amazonaws:aws-lambda-java-log4j:1.0.0'
and How to set your log4j2 file to support the lambda appender.



Monday, May 02, 2016

Allowing profiles to be passed to bootRun

Allowing profiles to be passed to bootRun


$ ./gradlew bootRun -PjvmArgs="-Dspring.profiles.active=docker"

Spring Profiles

Spring Profiles

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Any@Component or @Configuration can be marked with @Profile to limit when it is loaded:
@Configuration
@Profile("production")
public class ProductionConfiguration {

    // ...

}
In the normal Spring way, you can use a spring.profiles.active Environment property to specify which profiles are active. You can specify the property in any of the usual ways, for example you could include it in your application.properties:
spring.profiles.active=dev,hsqldb
or specify on the command line using the switch --spring.profiles.active=dev,hsqldb.

Thursday, April 21, 2016

Installing Amazon Web Service Plugin for ElasticSearch

https://www.elastic.co/guide/en/elasticsearch/plugins/current/cloud-aws.html

The Amazon Web Service (AWS) Cloud plugin uses the AWS API for unicast discovery, and adds support for using S3 as a repository for Snapshot/Restore.

Installationedit

This plugin can be installed using the plugin manager:
sudo bin/plugin install cloud-aws
-> Installing repository-s3...
Trying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/repository-s3/2.3.1/repository-s3-2.3.1.zip ...
ERROR: failed to download out of all possible locations..., use --verbose to get detailed information
# bin/plugin install cloud-aws
-> Installing cloud-aws...
Trying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/cloud-aws/2.3.1/cloud-aws-2.3.1.zip ...
Downloading .............................................................................................................................................................................................................................................................................................................................................DONE
Verifying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/cloud-aws/2.3.1/cloud-aws-2.3.1.zip checksums if available ...
Downloading .DONE
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.lang.RuntimePermission getClassLoader
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
Installed cloud-aws into /usr/share/elasticsearch/plugins/cloud-aws
# service elasticsearch restart
[....] Stopping Elasticsearch Server:

Tuesday, April 19, 2016

Spring Boot CORS Not Working?

For the life of me I could not get the CORS Configuration for Spring Boot to write the needed headers to the response. I wrote my own filter that could read the CORS Config and used it in WebMVC Configuration to solve it.


Monday, April 11, 2016

Rsync Without Git

Just add an explicit exclude for .git:
rsync -a --exclude='.git/' --include='*.c' --include='*.sh' --include='*/' --exclude='*' ~/c/ ~/Dropbox/Public/c
Another option is to create ~/.cvsignore containing the following line along with any other directories you'd like to exclude:
.git/

Tuesday, February 23, 2016

Thursday, February 18, 2016

Find In Files Recursively

Its helpful to be able to search for a string recursively in a directory. Here is the basic way:

grep -rnw '/path/to/somewhere/' -e "pattern"
-r or -R is recursive, -n is line number and -w stands match the whole word. -l (lower-case L) can be added to just give the file name of matching files.
Along with these, --exclude or --include parameter could be used for efficient searching. Something like below:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
This will only search through the files which have .c or .h extensions. Similarly a sample use of --exclude:
grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
Above will exclude searching all the files ending with .o extension. Just like exclude file it's possible to exclude/include directories through --exclude-dir and --include-dir parameter; for example, the following shows how to integrate --exclude-dir:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"


Replace Recursively


find /home/www -type f -print0 | xargs -0 sed -i 's/subdomainA.example.com/subdomainB.example.com/g'

Wednesday, February 17, 2016

Truncating Git History

Look, sometimes you want to be a revisionist, and the only way to get rid of a pesky diff that is taking a lot of commit time is to truncate the commit log. So in the rare occasion you need to do it here is how.

Create a Truncate Script


Use the Commit Sha to Truncate To

source ~/data/bin/git-truncate a71f4386f9d5e3b707067e66fd5f76a1cc06c11c

Force the Push to Origin

git push origin master --force

Thursday, January 21, 2016

Basic Screen Commands

Screen is an extremely useful tool. When you dont want to make a service and just need something running in the background. Here are the basic screen commands.