{
"$type": "site.standard.document",
"canonicalUrl": "https://unnecessary.tech/posts/openfaas-cobol",
"path": "/posts/openfaas-cobol",
"publishedAt": "2020-04-27T13:01:40.000Z",
"site": "at://did:plc:jx54v4rmscfwzit7fmgz24ba/site.standard.publication/3mnrsqmzz3w2e",
"tags": [
"programming",
"cloud",
"openfaas"
],
"textContent": "In early April a [couple][article1] [of][article2] [articles][article3] were\npublished stressing the need for COBOL programmers to once again come forward\nto use their skills to get us through a crisis of aging infrastructure. This\nwasn't the [first time][y2k] their expertise was required. Even back in 1998,\nCOBOL was described as\n\n> \"... a computer language that runs many of the United States' mainframe\n> computers but is so old that relatively few American programmers know it.\"\n>\n> — [Feder & Pollack, \"Computers and Year 2000: A Race for Security\n> (and Against Time)\", New York Times, 1998][y2k]\n\nSo why do these programs persist for decades? Why are new experts not hired to\nmaintain this vast amount of COBOL code? The answer is clearly because the\ncode works, and works well enough for modernization of the infrastructure to\nremain a low priority. Although the language gets a lot of blame, the truth is\nthat much like maintenance of roads and bridges, maintenance of computational\ninfrastructure has faltered, and nothing is better at showing the cracks in\nthat infrastructure than a crisis.\n\nModern COBOL\n\nIt would be generous to call me a novice COBOL programmer. My interest in\nlearning more about this \"ancient\" language was piqued by the recent articles.\nIs COBOL an unusable relic from the past? Can COBOL even be compiled on the\nmodern PC with a modern operating system? The answers are a qualified no, and\nan unqualified yes. COBOL, like other languages, reflects computing at the\ntime it was created. Languages do evolve along with the hardware they run on.\nGood examples are C and Fortran which have evolved with numerous revisions,\nbut the paradigms of languages rarely shift. COBOL retains the trappings of\nits origins. Its punch-card origins is revealed in its line formatting\n(columns 1-6 for line numbers, column 7 to indicate a comment or continuation,\n8-72 for statements, and columns 73-80 for marginalia, useful for card\nsequence numbering). The memory limits of the time are reflected in the\nflexibility of its variable definition blocks. \n\nCOBOL also reflects some interesting ideas. It is so verbose that it makes\nJava look rather terse, but this is done in the service of readability, which\nis also a goal of the Go language. Fundamentally, COBOL is an imperative\nlanguage and as such, its basic structures and statements can be fairly easily\ndecoded by most modern programmers. Although it may look somewhat archaic, and\nit may contain a lot of keywords, it is not outlandishly different from other\nmodern programming languages. The COBOL compiler which runs under\nLinux called [GnuCOBOL][compiler], and can be installed by the debian package\nmanager on any debian-based distribution. Armed with a tiny bit of knowledge, I\ndecided to merge the old with the new and write a serverless COBOL function.\n\nGoogle Cloud Run\n\nMy first step in putting a serverless COBOL function online was to use [Google\nCloud Run][cloudrun], a serverless offering from Google which will host any\ncontainers listening for web requests on an open port defined by the PORT\nenvironment variable (right now this is always port 8080). Cloud Run only\ncharges when there are active HTTP requests connected to the container, and\nallows multiple simultaneous connections to the container. There are a number\nof [examples and tutorials][awesomecloudrun] available, and there is even a\nsample of a [Fortran function][fortran] which is deployable to Cloud Run.\n\nI do not know how to write a webserver in COBOL, so [my COBOL\nfunction][cobolrun] includes an invocation server written in Go which in turn\nruns the COBOL program and returns its output. You can [try it out\nhere][runexample], or and put your name as the PATH component to get a\npersonalized hello message. After announcing what I had done on twitter, I got\nthe intriguing tweet below, which started a journey into [OpenFaas][openfaas].\n\n{{< x user=\"alexellisuk\" id=\"1247431951956480001\" >}}\n\nOpenFaaS\n\nI had heard of [OpenFaaS][openfaas] before, but at the time I felt I was too\nbusy to learn Kubernetes and OpenFaaS and abandoned it for later. When I got\nthe tweet, I figured now was the time, so I dove into spending weekends\nlearning Kubernetes and OpenFaaS. The fact that I had to stay home because of\nthe pandemic may have also played a role.\n\nThe [watchdog][watchdog] process itself doesn't require any specific\nknowledge of either Kubernetes or OpenFaaS in order to understand. It serves\nthe function of the invoker I previously wrote, but with more bells and whistles\nincluding health checks. Essentially the watchdog sends a request into\na process's standard input, and returns the standard output. It also provides\nseveral environment variables to describe the URL details and other HTTP\nheaders. This should be familiar to anyone who has used CGI before. \n\nI found that the Kubernetes landscape had changed and advanced\nsignificantly since my first attempts to use it. In particular Rancher's\n[k3s] project is much easier to run on low-end equipment. The companion [k3d]\nproject allows a developer to run k3s in a docker container, making setting up\na development Kubernetes environment extremely easy. To begin working with\nclusters, Alex Ellis's [k3sup] project can install k3s on any ssh accessible\nvm. I fired up k3d and worked through the [Kubernetes\nBasics][Kubernetesbasics] tutorial to become a little familiar with\nKubernetes.\n\nI then moved onto learning [OpenFaaS][openfaas] using the [OpenFaaS\nworkshop][workshop]. The workshop is an excellent introduction to OpenFaaS and\npresents an easy to follow set of labs to get a programmer quickly up to\nspeed. I was impressed with the simplicity of OpenFaaS and the ability to do\nadvanced things like asynchronous\nfunctions,\nauto-scaling, and\nsecrets\nmanagement. Plus\nit allows a user to avoid vendor lock-in. You can run your code in any cloud\nprovider or on premises (premises, by the way, is the singular form of that\nword, premise is a proposition from which one draws a conclusion or forms an\nargument). \n\nIn order to create a COBOL function, I created a [base COBOL container using\ndebian-slim][cobolcontainer], and I created an [OpenFaaS COBOL\ntemplate][cobolfaas] which contains the information on how to put a COBOL\nfunction into an OpenFaaS container, as well as a sample COBOL function. I\nalso submitted this template to the OpenFaaS store, so now a COBOL programmer\ncan easily begin writing an OpenFaaS COBOL function using the commands below.\n\nfollowed by\n\nThe standard practice for OpenFaaS is to send input data in the body of a POST\nrequest which is then passed to the function via standard input, so I adjusted\nthe sample COBOL program accordingly. You can invoke the COBOL function\nrunning on my Kubernetes cluster by issuing the following curl command:\n\nBut Why?\n\nEverything I have written so far explains what I did, but why did I do it?\nInitially I just wanted to learn a bit about COBOL and have some fun, after\nall serverless COBOL sounds a bit like an oxymoron. However, it became a tool\nfor learning about OpenFaaS, and in the end my COBOL template shows that\nOpenFaaS is extremely flexible, and also very easy to use. Creating functions\nusing your favorite language, or specialized environment is as easy as making\nan appropriate Docker container. Although it was not my initial goal, this\nproject has highlighted the usefulness, maturity, and flexibility of the\nOpenFaaS project. I imagine I will be using OpenFaaS a lot moving forward,\nand I encourage others to learn more about it. I would also encourage others\nto try any of the other resources I mention above. For anyone who wants to\nlearn more about COBOL, IBM has [published their COBOL course][ibmcourse] for\nfree on GitHub as part of the [Open Mainframe][openmainframe] project.\n\n[article1]: https://www.cnn.com/2020/04/08/business/coronavirus-cobol-programmers-new-jersey-trnd/index.html\n[article2]: https://www.npr.org/2020/04/22/841682627/cobol-cowboys-aim-to-rescue-sluggish-state-unemployment-systems\n[article3]: https://arstechnica.com/tech-policy/2020/04/ibm-scrambles-to-find-or-train-more-cobol-programmers-to-help-states/\n[y2k]: https://archive.nytimes.com/www.nytimes.com/library/tech/98/12/biztech/articles/27millennium.html?RefId=PjxYEutt2uFFwKZO\n[compiler]: https://sourceforge.net/projects/open-cobol/\n[cloudrun]: https://cloud.google.com/run/\n[awesomecloudrun]: https://github.com/steren/awesome-cloudrun\n[fortran]: https://github.com/zachmccormick/fortran-cloudrun\n[cobolrun]: https://github.com/devries/cloud-run-cobol\n[openfaas]: https://www.openfaas.com/\n[k3s]: https://k3s.io/\n[k3d]: https://github.com/rancher/k3d\n[k3sup]: https://github.com/alexellis/k3sup\n[watchdog]: https://docs.openfaas.com/architecture/watchdog/\n[cobolcontainer]: https://github.com/devries/docker-cobol\n[cobolfaas]: https://github.com/devries/openfaas-cobol-template\n[runexample]: https://cloud-run-cobol-j6z4gxi7tq-uc.a.run.app/CHRIS\n[workshop]: https://github.com/openfaas/workshop\n[kubernetesbasics]: https://kubernetes.io/docs/tutorials/kubernetes-basics/\n[ibmcourse]: https://github.com/openmainframeproject/cobol-programming-course\n[openmainframe]: https://www.openmainframeproject.org/",
"title": "Why I Created an OpenFaaS template for COBOL"
}