Showing posts with label Systems Administrator. Show all posts
Showing posts with label Systems Administrator. Show all posts

Abusing Google App Services for Fun and Profit!

Getting back to your roots!

The company I have been working for, for the better part of a decade now was originally created as a Google Apps Service. The original income for the company would be built around helping companies adopt Google Apps and Office Suite, performing migrations out of OnPrem Data Centre mail services and into the Cloud.

This was a lot of fun, but as the Cloud has evolved from all those years ago and new services have come onboard, there has not been much time to play.

Although, despite the sentimental walk down memory lane, I have to say I do not miss the horrible GWMME (or GAMME as it was known back in the day!) with gapps to migrate data from clients on-prem up to their new Google Suite.

Dusting off the old code bases

Below is a small random value generator I made for fun, it uses backend data from the Google Sheet API to generate a small app which is embedded in to this web page.

What should I cook?


To change values click here then refresh for the update! 

You can find the code for this exercise here.


The science!

This is nothing complex, the values are stored in the same spreadsheet and we expose a single sheet to the world. By Abusing the Google app script magic functions, we can create a web app from a Google Office Element and have it display on another Free Service, drastically increasing our ability to make content! Even with no web experience!

What else is possible?
What else can we Abuse?

The Google API Service is vast and plugs in to numerous services, this blog itself can be posted to with the right credentials and data in the payload. A lot of the GSuite's power comes from App Script and can help make automated task an easier reality, adding additional function on top of the native office functionality (Function in Google Sheets for example).

As well as the above, App Script has some magic functions to enable a user to perform even more automation, it boasts an impressive set of built in triggers that can easily adopted.

To use a simple trigger, simply create a function that uses one of these reserved function names:

  • onOpen(e) runs when a user opens a spreadsheet, document, presentation, or form that the user has permission to edit.
  • onInstall(e) runs when a user installs an Editor add-on from within Google Docs, Sheets, Slides, or Forms.
  • onEdit(e) runs when a user changes a value in a spreadsheet.
  • onSelectionChange(e) runs when a user changes the selection in a spreadsheet.
  • doGet(e) runs when a user visits a web app or a program sends an HTTP GET request to a web app.
  • doPost(e) runs when a program sends an HTTP POST request to a web app.

Extending Permissions

As the Google App Script service runs on GCP, it is possible for a user to associate the runtime environment with their GCP account to give more fine gained permissions and to gel their creation back in to any Google Eco-System that has been built/provided on the Google Cloud Platform. This can be neatly tied in to your Google OAuth settings


And that's how it's done!

Have a play with the above and see what you should cook! Some of the Logic created here is just a fun PoC to be reused in other projects, in another post I will go into more detail on the codebase and setup so you, yourself can have your own random word generator and abuse the Google Apps Service!

Jenkins and PHPUnit


Starting from the Beginning

This blog post is another rehashing from the past, bringing some of the older knowledge forward, a lot of it is still relevant today to see behind the scene of the automation technology we are using and in that, gain understanding on how to improve it for the custom functionality we need in today society with modern enterprise. 

The following Blog was co-authored with my senior developer at the time (Zdenek M - Happy to link out, give me a shout, Buddy!) and has helped a number developers and sysadmins integrate Jenkins into their workflow with a variety of different plugins.

This isn't JenkinsX or Pipelines

No, that is correct! This is a very old school method of dealing with things. However, it does show off how the old Hudson (original fork of Jenkins and core code) can handle its plugin architecture.

The Article:

Continuous Integration is the process where code from a developer's repository is called by an automated system, built and tested against a set of standards that the developer has created in their tests. In this post we look at the steps to create a server that will automate your testing suite - and hopefully make you more productive... I have even tried to emulate the links going to website of that time via the Internet Archieve: Wayback Machine!

While working for a PHP Developer shop, we used to have a number of different tools for continuous integration:

Using all these different tools made things very confusing, and our sysadmin had to learn all these different technologies to try and keep a CI single system together. Some of the main issues with this configuration were:

  • Xinc – could be tricky to configure, it looks like a PHP application but doesn't behave like one, it has poor documentation, and to have Git support you have to migrate to the latest version
  • PhpUnderControl – did a really good job for us over the years, but the project looks half dead and project XML configuration can be quite tricky
  • Selenium Server – to be able to run tests in a web browser (Linux) we installed full X Server, which is not the best solution from resources point of view: it needs at least 2GB of RAM

To consolidate these systems and their configuration we opted for Jenkins CI which is a continuation of the old Hudson project, available under Creative Commons License, aimed at making CI as easy as possible to implement. The beauty of Jenkins (apart from its single tool structure that contains all CI elements) is that it is a single tool with a pretty, easy GUI that enables easy configuration of a project so that it can be tested.


Another massive benefit is that Jenkins is one of those brilliant tools (like Git) that is so customisable that you can bend it to work in whatever way you need. In this post we shall be looking at how Loft implemented Jenkins into our workflow practices.

So, let's get started...

While planning the server, we listed what services we would need from it:

  • Git and SVN abilities - for getting the projects out of our repositories
  • PHPUnit
  • Xdebug
  • Selenium Server
  • JUnit - for reports

(Not part of the plan but added for fun:)

  • Clover reports - code coverage

Once we had the plan the next step was the build...

Jenkins installation

First, we needed a server to set up as our Continuous Integration server (henceforth referred to as CI server). This was to be a Linux Box as it is far easier to work with than a Windows box, as well as  more secure and cheaper. A disadvantage is that you can't run tests in Internet Explorer on this box.

Luckily for us, Jenkins has it's own repo which can be installed on almost any distribution of Linux that has it's own package manager (Such as Yum or Apt). For RHEL and similar you can install the repository with:

sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum install jenkins

And for Debian/Ubuntu:

wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

This will do all the hard work of installing Jenkins to the server that we are working on, so all we have to do (as root) is start the service:

/etc/init.d/jenkins start

This will start Jenkins and the Jetty (Java-based webserver) that it runs on. Like us, you may decide that the Jetty is not fit for your purpose and might look for something like NginX or Apache to handle a proxy pass to the Jetty which will give you some extra security to hide your Jenkins Interface behind.

Most of Jenkins is very configurable from the management GUI. From this web page you can download and install new modules to the server which will allow you to use different tools to work out more information about your code and project. These plugins.... do just that: they plug into the project configuration area, which enables the user to add more build stages to the project and extra tests.

Selenium Server requirements

For functional testing we use Selenium Tests with the Selenium RC server to control requests to a web server which acts as a real user going through the site, clicking on links and filling in forms, all the while taking pictures of problems and assessing that all the correct information is displayed on the page. One problem with these tests is that they require a GUI or some form of desktop to use the web browser. In previous set ups of CI, Loft tended to use a Full Blown Desktop (Gnome or KDE) which made our sysadmin's life trickier.

In this case we decided to install a different type of GUI on to the server: one that could still be viewed over an encrypted VNC connection and give access to the web browser that Selenium is running, but would also be light enough that it would not impact on the CI potential of the server.

To remedy this we used a combination of:

  • xvfb - as the X server
  • x11vnc - acting as the vnc server with the -shared -forever -localhost options set
  • Firefox - the web browser

The installation of the above allowed us to configure a display on xvfb which acted as a blank x server. On top of this platform we could call our web browser to run the selenium tests.

Configuring the PHP project

To run tests, you need a few extra plugins, which are easy to install through Jenkins plugin manager:

  • Jenkins Mailer Plugin – to be able to send emails
  • Git Plugin – to update tests / code
  • SeleniumRC plugin – if you want to run Selenium tests
  • Clover PHP Plugin – to be able to publish code coverage
  • xUnit Plugin – for publishing test results

The project has a few sections which enable you to configure what to run, when to run it, and what to do with results.

1) Update code / Tests

Jenkins has excellent support for VCS – SVN, GIT – you can choose or even combine different repositories to update tested code or tests.



2) Build trigger

You can choose when the build is started. We use the option to Trigger builds remotely for builds triggered by post-commit (receive) hooks from Git.

Here is a sample post-receive hook used in a Git repo:

  #!/bin/sh 
PROJECT="/var/www/vhosts/project"
cd $PROJECT || exit
unset GIT_DIR
git pull
#start jenkins
wget http://jenkins:8080/job/project/build?token=sdsdjsdjksdjds787jk -O /dev/null

Or for functional tests we use the Build periodically option – for example to run tests every 4 hours.

3) Build

In our case, just a simple command to run all unit tests:

phpunit --log-junit results/phpunit/phpunit.xml -c tests/phpunit.xml  

4) Publishing results

Because we export test results to junit format, we can use JUnit (Java Unit test) Publish to publish results:



5) Notify developers

By publishing results, Jenkins knows if the build was successful or not. If not, you can notify developers - or even point a finger at the person who broke the build!

6) Selenium

If you need to start the Selenium server, you can start it for every build or check that server is running:

7) Code coverage

Code coverage is optional, but it can give you very interesting information:

Round Up!

So as you can see - it's an absolute breeze, far easier than in the past. I recommend you give it a try or expand to get in to the practice of CI/CD, it's not just for big businesses these days with most SCMs now offering a CI/CD testing facility dictated by a YAML file in your repository.

With the above you can even see how the beginnings of Jenkins Pipelines began to form with similar requests being handled with the above console, just now in a "Groovy" (Not so groovy!) Jenkinsfile!

Screen vs tmux

...Yet Another Comparison guide


Recently, at work this subject came up again... Whether or not I was fanning the flame war is another matter. Hilariously, much like an old article I had written before and again recently found (recorded below) "the war" had begun the same, an engineer had just discovered Screen...


I always love causing joining in a good flame war, regardless of the technology. I am a proud member of the church of Vi! For Example.. The reason being as it helps us to learn and challenge the way that we do things, I have learnt more about technology from heated discussions with passionate people that I have ever learned from a README.md file! In many ways I believe these discussions are necessary for a healthy development environment!


Opinions from the past

In terms of functionality not much has actually changed under the hood for both of these candidates, they both essentially do the same thing they have done for years. In many ways, they shouldn't change as testament to what was created.

Here is the post I wrote oh so many, many years ago when I was younger and more naive, or was I?

GNU Screen and tmux are terminal multiplexers designed for Unix-like platforms. They are basically window managers for text consoles instead of the X Window System. Screen is the more heard-of terminal multiplexer.

tmux-only features include:

  • Client/server system - a server instance is started automatically when a session runs as a client for that server, which leaves less of a footprint.
  • Synchronize-panes - duplicate input from any pane to all other panes in the same window. A bit like a clusterssh function to simultaneous input to all of the terms all at the same time.
    ctrl+b :set-window-option synchronize-panes [on|off]

Screen only features include:

  • Zmodem transfers - the abilty to transfer files when all you have is a serial connection available. Although this sysadmin does not need to use this feature.
  • Attaching to a serial tty - such as screen -r /dev/ttyS0 115200 in case you lose a session.
  • Naming of invidual panes - you can name each pane in case you loose track of things! TMUX wins for me

I first started using Screen, but I quickly found tmux to be the better option. With less of a footprint to deal with, I could have many hands on one server with out the need to keep spawning servers. I also found tmux had a better mastery of panes (as a terminator user very important!) and the conversion from Screen was simple after getting used to using ctrl+b instead of ctrl+a.


 

Back to the present war

This would continue as (regardless of the multiplexer they mentioned... I would have probably picked the opposite side) I would begin to argue a lot of the above points about TMUX over screen. There would be some back and forth banter about Key Bindings, much like the above article, finally a few days later the OP would return to the thread and agree...

In my humble opinion this just cements that TMUX is still the better option!!


Verdict!

It would appear that nearly a decade later, I am still correct in my promotion of the far superior TMUX. Out of the box it is noticeably more colourful, despite the key bindings being different to Screen that for some reason newbies find easier to pick up, most are willing to make the jump for the extra screen bling.


Of course if screen bling is you goal might I suggest:


$ fortune | cowsay | lolcat # add to a while loop for some pretty and informational graphics!

Buzzword Page

 A No Content Page used to power the About Me Section and highlight which topics need further coverage in the blog section as there are not many interesting blog posts that cam be written on some subjects...