Better than BASH -
Scripting & Automation with Kotlin
October 7–10, 2019
Austin Convention Center
Ollie Hughes - Staff Software Engineer - Pivotal
@olliehughes82
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
What 3 things do you find in every software
project?
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
• At least one caffeine addict
• At least one Magic the Gathering player

• At least one Bash / Powershell / DOS or Perl script over 50
lines long
3
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
What kind of things do we still write scripts for?
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
• Continuous Integration & Deployment (CI /CD)
• Publishing artefacts
• Interacting with CLI tools

• AdHoc manual or repetitive tasks.
• Generating a report for the boss from issue tracker
• Interacting with APIs
5
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Choosing the right tool
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
• Creating cloud / IaaS resources ➡ Terraform / Cloud Formation / …
• Creating archives from build artefacts ➡ Maven / Gradle / …
• Re-inventing the wheel - not using pre-built tools such as Concourse
Resources or well supported Jenkins plugins
Scripting Anti-Patterns
7
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
We can do better!
8
while getopts ":a:" FOUND "${@}";
do
case ${FOUND} in
a) app_name="${OPTARG}"
;;
:) printf "argument missing from -%s optionn"
$OPTARG
usage
exit 2
;;
?) printf "unknown option: -%sn" $OPTARG
usage
exit 2
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
• Modern

• Good tooling

• Productive

• Great ecosystem
Why use Kotlin for scripting
9
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
• Standalone scripts for automation

• Supplementing CI pipelines & tools

• External configuration / YAML generation

• Embedded scripting within an application
• Dealing with API calls and serializing JSON
Good use cases for Kotlin Scripts
10
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Hello.kts
runner.sh
println(“Hello SpringOne”)
#!/bin/bash
kotlinc -script Hello.kts
A basic example
11
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
A basic example
12
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 13
⚠ ⚠ 🐞 Bug alert 🐞 ⚠ ⚠
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
https://github.com/holgerbrandl/kscript
Install with sdkman
Install with brew (Mac only)
curl -s "https://get.sdkman.io" | bash
source ~/.bash_profile
sdk install kotlin
sdk install kscript
brew install holgerbrandl/tap/kscript
Simplifying the script execution with kscript
14
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Simplifying the script execution with kscript
15
Hello.kts
#!/usr/bin/env kscript
println(“Hello SpringOne”)
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Importing 3rd party libraries
16
#!/usr/bin/env kscript
@file:MavenRepository("jitpack", “https://jitpack.io"
SNAPSHOT”)
@file:DependsOn(“com.github.kotlin.kotlinx~cli:kotlinx-cli-
jvm:-SNAPSHOT”)
import kotlinx.cli.*
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Parsing arguments
17
#!/usr/bin/env kscript
for (arg in args) {
println("arg: ${arg}")
}
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
• Packaging a script
• Inline / eval a script
• Load script into IntelliJ IDEA or REPL
Other kscript features
18
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Embedded scripting
19
Spring Cloud Contract Kotlin DSL uses a similar approach
KotlinScriptingHost().eval(File(“path/to/script.kts"))
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Testing Scripts
20
Then test framework has access to script like a normal import
@file:Import("path/to/externalScript.kts")
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Kscript vs Kotlin Scripting Support
21
• Kscript still the better solution for standalone scripts
• Kotlin Scripting Support useful for more advanced use cases
• See https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-
support.md
Unless otherwise indicated, these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Kotlin Scripting Limitations
22
• JVM startup penalty
• Distribution of JVM
• Kotlin/Native or GraalVM can solve these issues but dev cost is higher
Thanks
@ojhughes
<Related Session>
<Related Session>
#springone@s1p

Better Than BASH: Scripting Kotlin

  • 1.
    Better than BASH- Scripting & Automation with Kotlin October 7–10, 2019 Austin Convention Center Ollie Hughes - Staff Software Engineer - Pivotal @olliehughes82
  • 2.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What 3 things do you find in every software project?
  • 3.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ • At least one caffeine addict • At least one Magic the Gathering player
 • At least one Bash / Powershell / DOS or Perl script over 50 lines long 3
  • 4.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What kind of things do we still write scripts for?
  • 5.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ • Continuous Integration & Deployment (CI /CD) • Publishing artefacts • Interacting with CLI tools
 • AdHoc manual or repetitive tasks. • Generating a report for the boss from issue tracker • Interacting with APIs 5
  • 6.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Choosing the right tool
  • 7.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ • Creating cloud / IaaS resources ➡ Terraform / Cloud Formation / … • Creating archives from build artefacts ➡ Maven / Gradle / … • Re-inventing the wheel - not using pre-built tools such as Concourse Resources or well supported Jenkins plugins Scripting Anti-Patterns 7
  • 8.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ We can do better! 8 while getopts ":a:" FOUND "${@}"; do case ${FOUND} in a) app_name="${OPTARG}" ;; :) printf "argument missing from -%s optionn" $OPTARG usage exit 2 ;; ?) printf "unknown option: -%sn" $OPTARG usage exit 2
  • 9.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ • Modern
 • Good tooling
 • Productive
 • Great ecosystem Why use Kotlin for scripting 9
  • 10.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ • Standalone scripts for automation
 • Supplementing CI pipelines & tools
 • External configuration / YAML generation
 • Embedded scripting within an application • Dealing with API calls and serializing JSON Good use cases for Kotlin Scripts 10
  • 11.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Hello.kts runner.sh println(“Hello SpringOne”) #!/bin/bash kotlinc -script Hello.kts A basic example 11
  • 12.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ A basic example 12
  • 13.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 13 ⚠ ⚠ 🐞 Bug alert 🐞 ⚠ ⚠
  • 14.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ https://github.com/holgerbrandl/kscript Install with sdkman Install with brew (Mac only) curl -s "https://get.sdkman.io" | bash source ~/.bash_profile sdk install kotlin sdk install kscript brew install holgerbrandl/tap/kscript Simplifying the script execution with kscript 14
  • 15.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Simplifying the script execution with kscript 15 Hello.kts #!/usr/bin/env kscript println(“Hello SpringOne”)
  • 16.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Importing 3rd party libraries 16 #!/usr/bin/env kscript @file:MavenRepository("jitpack", “https://jitpack.io" SNAPSHOT”) @file:DependsOn(“com.github.kotlin.kotlinx~cli:kotlinx-cli- jvm:-SNAPSHOT”) import kotlinx.cli.*
  • 17.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Parsing arguments 17 #!/usr/bin/env kscript for (arg in args) { println("arg: ${arg}") }
  • 18.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ • Packaging a script • Inline / eval a script • Load script into IntelliJ IDEA or REPL Other kscript features 18
  • 19.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Embedded scripting 19 Spring Cloud Contract Kotlin DSL uses a similar approach KotlinScriptingHost().eval(File(“path/to/script.kts"))
  • 20.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Testing Scripts 20 Then test framework has access to script like a normal import @file:Import("path/to/externalScript.kts")
  • 21.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Kscript vs Kotlin Scripting Support 21 • Kscript still the better solution for standalone scripts • Kotlin Scripting Support useful for more advanced use cases • See https://github.com/Kotlin/KEEP/blob/master/proposals/scripting- support.md
  • 22.
    Unless otherwise indicated,these slides are © 2013-2019 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Kotlin Scripting Limitations 22 • JVM startup penalty • Distribution of JVM • Kotlin/Native or GraalVM can solve these issues but dev cost is higher
  • 23.