Monday, October 12, 2020

Exploring ELK (Elastic) Stack for hack-a-thon

At my current gig, our group finally got to do hack-a-thon week and I joined a team project that tied together a few of the technologies I've been testing and sporadically using the last five to ten years of my career in automation and quality assurance testing.

Ultimately, the purpose of this hack-a-thon project I joined was to explore the collection, pushing, indexing, and analysis/charting from logs that we collect from our own Docker swarm containers and storage nodes.

It's funny that I haven't used a stack like this until now - having heard about it plenty in the past - but sometimes things come together nicely where all the technologies do mesh well; especially within the orchestration framework of Kubernetes and containerization support of Docker.

Like any stack exploration, there were some parts of ELK I hadn't really used before: Logstash and Kibana. But to have experience in other parts of the stack made the process go a little easier to get a bit more done for week of work with a POC.

 Technologies used:

  1. Elasticsearch (v. 7.9.2)
  2. Logstash (v. 7.9.2)
  3. Kibana (v. 7.9.2)
  4. Java (openjdk version "1.8.0_265")
  5. Nginx (nginx version: nginx/1.14.0 (Ubuntu) )
  6. Docker (Docker version 19.03.12, build 48a66213fe)
  7. Ubuntu 18.04 (gotten from docker hub)
  8. Kubernetes (10.4.1)
  9. Artifactory (v 6.8.7)

I had the thought before starting with ELK that Grafana with something like Prometheus would also serve this purpose well, but ultimately my mind was changed and I was really psyched to have revisited and tied all these ideas together for something that could be matured for better log analysis and alerting.

The most rewarding parts of this project was seeing how well stuff could work in one container (not having to assign one process to an individual container - for the time being)  and letting the deployed container run in a Kubernetes namespace with plenty of RAM and volume space.

The biggest gotchas were with Kubernetes and making sure that I had the proper kubeconfig file, namespace created and a proper YAML file for creating a service and deployment to a node within my namespace.

 


Wednesday, June 10, 2020

Using Ansible with vCenter and SSH calls

Can't believe it took me so long to explore Ansible for configuration management and automation.

I heard about it back in 2014 and tested against it since we used it to install our micro services framework. 

I used a good intro video tutorial to try making a vCenter connection and listing hosts.

Problems encountered were the YAML syntax, using local versus remote connections, SSH with sudo, small syntax errors and using the ansible.cfg and hosts file under /etc/ansible, which I created manually

Initial vCenter Tests:

Here's some initial code for connecting to vCenter:

cat auth_vcenter.yml
---
  - hosts: localhost
    vars:
      user_readme: 'Welcome to vCenter tests'
    tasks:
    - debug:
        msg: "Starting test aginst vcenter"
    - name: Including Secret Environment Items
      include_vars:
        file: secret67.yml
        name: secret
    - name: vcenter login
      uri:
        url: "https://{{secret.vcenter}}/rest/com/vmware/cis/session"
        force_basic_auth: yes
        method: POST
        user: "{{secret.username}}"
        password: "{{secret.password}}"
        status_code: 200
        validate_certs: no
      register: login
    - name: Get hosts from vCenter
      uri:
        url: "https://{{secret.vcenter}}/rest/vcenter/host"
        force_basic_auth: yes
        validate_certs: no
        headers:
          Cookie: "{{login.set_cookie}}"
      register: vchosts
    - debug: var=login
    - debug: var=vchosts

This works with a YAML secrets file which for initial testing is fine but passwords should be encrypted so it's not a longterm solution:

cat secret67.yml
---
username: administrator@vsphere.local
password:
vcenter: 10.117.180.10

Initial SSH Tests:

Here's some other code for trying out an automated SSH connection with some initial commands

One that that got me was authentication errors so to make the login process work I had to run:

ssh-copy-id -i ~/.ssh/id_rsa.pub username@10.117.155.123

cat testone.yml
---
  - hosts: 10.117.155.123
    vars:
      user_readme: 'Welcome to this machine!!!! Hey good!'
    tasks:
    - debug:
        msg: "Starting test against server"
    - name: List contents
      command: ls -lta
      register: out1
    - name: Touch file
      file:
        path: $HOME/test_server.txt
        state: touch
      register: out2
    - name: Create target directory
      file: path=~/testit state=directory mode=0755
      register: out3
    - name: simple file try
      copy:
        dest: ~/testit/README.txt
        content:  " {{ user_readme }} "
      register: out4
    - debug: var=out1
    - debug: var=out2
    - debug: var=out3
    - debug: var=out4

Hosts and config Files:

For the SSH testing these initial files seems to work for me. Very brief and to the point.

$ cat ansible.cfg
[defaults]
remote_user=

$ cat hosts
all:
  vars:
    ansible_ssh_user=
    ansible_ssh_pass=
  hosts:
    10.117.180.10


Summary:

To watch the video, setup Ansible on Mac, and get these tests working should take a day or two, max, in my opinion.

Next steps are to see about using Ansible for further automation testing.

Tuesday, December 03, 2019

Pycharm IDE. How to fix missing source directories in Project view. Doesn't show file or folder structure.

Possibly due to a git merge between two of my computers on the same GIT project I lost the project structure. <
I was able to research this and re-add the content root under Preferences > Project > Project Structure. I wasn't as intuitive as I initially thought but here is the dialogue where I add it.
All fixed!

Tuesday, June 18, 2019

Being more pythonic with the shortcut conditional : A if C else B

I had these two blocks of "if else" code that I found too crude.

test_case_data = None
test_case_data2 = None

if test_already_exists(tests_object_list, test1_name) is not None:
  print(">>>> Test 1 already exists. skipping the add")
else:
  print(">>>> Test 1 doesn't exist. adding")
  test_case_data = add_test_case(api_conn, int(project_id), test1_name)

if test_already_exists(tests_object_list, test2_name) is not None:
  print(">>>> Test 2 already exists. skipping the add")
else:
  print(">>>> Test 2 doesn't exist. adding")
  test_case_data2 = add_test_case(api_conn, int(project_id), test2_name) 
 

I took the code and applied Guido's "ternary solution," Link here.

 
All of the above code became three-four lines of code and I was able to apply a one-line Pythonic truthy and falsy evaluation on the return object. Very lambda-ish. 


[Correction: What I had earlier with data = func_call if True else other_func_call, was incorrect. Corrected below. So it's really A if A (when A is not None) else B (which in this case creates a new test case object.]

for current_test in tests_lists:
  test_exists_data = test_already_exists() # Note returns None or or existing test data
  test_case_data = test_exists_data if test_exists_data else add_test_case()


  ...further code that doesn't contribute to A if C else B 
 
As a side note: This same elegance in code I also found with the use of the "with," statement using the Context Manager. But for this, a lot more compact, readable and Pythonic.

Tuesday, May 21, 2019

When the default pip install won't work. Using pip directly with a github repo

I work at a company where we cannot publish some (or all) Python modules to pyPI.

95% of the time before last week I would use:

pip/pip3 install [module name]

to install modules that I needed.

If that doesn't work and you have access to the git repo, another command that might be successful is installing directly from the repo. Here's an example:

pip3 install git+https://github.com/[username]/repo

And if you've downloaded the repo using git clone and there's a setup.py, which should be standard for a properly maintained module - you can run:

pip3 install -e .

Remember, sometimes you need to work around less-standard, less user-friendly modules so thankfully there are other pip options that can hopefully serve your needs.

Wednesday, May 09, 2018

Trying out my Python 3 skills on HackerRank

I've been using HackerRank to get my Python coding chops up a little better then where they've been before.

Tried a medium challenge on HackerRank called Piling Up

My solution isn't as elegant as others but it works and constantly reduces the size of the cube size list that need to be rearranged from horizontal to vertical where the block size needs to be equal or smaller than the block below it.


for tests in range(int(input())):
    cube_length = int(input())
    list_of_cubes = list(map(int, input().split()))

    last_value_placed_on_stack = 0
    all_items_on_placed_on_stack = False
    cannot_stack_anymore = False

    a = 0
    z = int(len(list_of_cubes)) - 1

    while a < int(len(list_of_cubes)):
        if cannot_stack_anymore:
            break
        if int(len(list_of_cubes)) == 1:
            if int(list_of_cubes[a]) <= last_value_placed_on_stack:
                all_items_on_placed_on_stack = True
                break
            else:
                break
        while z > 0:
            if list_of_cubes[a] >= int(list_of_cubes[z]):
                if list_of_cubes[a] <= last_value_placed_on_stack or last_value_placed_on_stack == 0:
                    last_value_placed_on_stack = list_of_cubes[a]
                    list_of_cubes.pop(a)
                    z = z - 1
                    break
                else:
                    cannot_stack_anymore = True
                    break
            if list_of_cubes[z] >= int(list_of_cubes[a]):
                if list_of_cubes[z] <= last_value_placed_on_stack or last_value_placed_on_stack == 0:
                    last_value_placed_on_stack = list_of_cubes[z]
                    list_of_cubes.pop(z)
                    z = z - 1   
                    break
                else:
                    cannot_stack_anymore = True
                    break

    if all_items_on_placed_on_stack:
        print("Yes")
    else:
        print("No")

Friday, April 27, 2018

Computing Fibonacci in Go

Had a coding whiteboard one of my first interviews back in early April.

It's nice to use Go Playground to prove it out and validate it after you get home.

For the whiteboard I had to do it iteratively. I added the recursive func after the fact

Here's the Go Playground link: https://play.golang.org/p/KDQaQlq8ONl

package main

import (
"fmt"
)

func main() {
fmt.Println("Hello, playground. Test for computing Fibonacci numbers")

computeFibonacci(10)
computeFibonacciRecursively(10, 0, 1)
}

func computeFibonacci(numberOfNumbers int) {

penultimate := 0
ultimate := 1

fmt.Printf("\nNext number (iterative): %d", penultimate)
fmt.Printf("\nNext number (iterative): %d", ultimate)

for i := 2; i < numberOfNumbers; i++ {

currentSum := penultimate + ultimate
fmt.Printf("\nNext number (iterative): %d", currentSum)
penultimate = ultimate
ultimate = currentSum

}

}

func computeFibonacciRecursively(numberOfNumbers int, penultimate int, ultimate int) {

if numberOfNumbers == 0 {
return
} else {
fmt.Printf("\nNext number (recurse method): %d", penultimate)
numberOfNumbers--
computeFibonacciRecursively(numberOfNumbers, ultimate, penultimate+ultimate)

}

}

Exploring ELK (Elastic) Stack for hack-a-thon

At my current gig, our group finally got to do hack-a-thon week and I joined a team project that tied together a few of the technologies I&#...