4.05.2024

Linux systemd services

https://www.tecmint.com/list-all-running-services-under-systemd-in-linux/

1. List all services
# systemctl --type=service

2. Get service status
# systemctl status sentinel_26379.service

3. Restart service
# systemctl restart sentinel_26379.service

8.10.2020

iterm2 Clear all Sessions on Current Tab

On mac, save this as /Users/[username]/Library/Application Support/iTerm2/Scripts/AutoLaunch/clear_current_tab.py
#!/usr/bin/env python3

import asyncio
import iterm2
import time

async def main(connection):
    app = await iterm2.async_get_app(connection)
    @iterm2.RPC
    async def clear_current_tab():
        code = b'\x1b' + b']1337;ClearScrollback' + b'\x07'
        window = app.current_terminal_window
        tab = window.current_tab
        for session in tab.sessions:
            await session.async_inject(code)
    await clear_current_tab.async_register(connection)

iterm2.run_forever(main)
Now bind it to a keystroke in Prefs > Keys by selecting the action Invoke Script Function and giving it the invocation clear_current_tab().

7.14.2020

Use nvm For Managing Dev Environments For Any Version of Angular

Install nodejs
1. Install xcode
$ xcode-select --install
2. Install nvm
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
3. Open another bash session to get settings from nvm install
4. List installed node versions
$ nvm list
5. Or list from the cloud (last 9 versions)
$ nvm ls-remote | tail -n9
6. Install v14.5.0
$ nvm install v14.5.0
7. Setup this version as default
$ nvm use v14.5.0
$ nvm alias default v14.5.0
8. Check node version
$ node -v
v14.5.0
9. Update npm
$ npm install -g npm
10. Check npm version
$ npm -v
6.14.6

Install yarn package manager
1. $ npm install -g yarn

Install angular cli on local project
1. Go to project base directory
$ cd ./project/ui
2. Add angular cli (with versioning if needed)
$ yarn add @angular/cli@9.1.12
3. Check version
$ ng --version
Angular CLI: 9.1.12
Node: 14.5.0
OS: darwin x64
Angular: 9.1.12

3.20.2020

Revert back to previous Django Migration

This procedure is only for the development environment while actively working on changes. Say you have several migrations already created but since after making changes realized that some of them are unnecessary and you want to come up with just one migration before checking in code for production deploy. This procedure will revert back migrations to previous level and then will give opportunity to create just one migration file that can then be deployed to production. This can potentially damage databases if not applied properly so use your discernment.
1. Show migrations for an app
$ python3 manage.py showmigrations orchestrator
...
 [X] 0051_auto_20191217_2328
 [X] 0052_auto_20200212_2250
 [X] 0053_job_miscellaneous

2. If you want to revert back the last 2 migrations
$ python3 manage.py migrate orchestrator 0051_auto_20191217_2328
Operations to perform:
  Target specific migration: 0051_auto_20191217_2328, from orchestrator
Running migrations:
  Rendering model states... DONE
  Unapplying orchestrator.0053_job_miscellaneous... OK
  Unapplying orchestrator.0052_auto_20200212_2250... OK

3. Show migrations again
$ python3 manage.py showmigrations orchestrator
...
 [X] 0051_auto_20191217_2328
 [ ] 0052_auto_20200212_2250
 [ ] 0053_job_miscellaneous

4. Delete the undesired migration files
$ rm 0052_auto_20200212_2250.py 0053_job_miscellaneous.py

5. Show migrations again
$ python3 manage.py showmigrations orchestrator
...
 [X] 0051_auto_20191217_2328

6. Now make a new migration. This will have all latest changes in model
$ python3 manage.py makemigrations
Migrations for 'orchestrator':
  orchestrator/migrations/0052_job_miscellaneous.py
    - Add field miscellaneous to job

7. Now apply new migration
$ python3 manage.py migrate
Running migrations:
  Applying orchestrator.0052_job_miscellaneous... OK

3.08.2019

Use CSS flexbox for elements to wrap automatically while centered

I learned from this post:
.flex-container-center {
  padding: 0 20px 0 20px;
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
}

12.13.2018

Getting Started with Angular NgRx

1. Install ngrx
$ yarn add @ngrx/store

2.  Install Redux Store dev tools
Install chrome browser Redux DevTools extension
$ yarn add @ngrx/store-devtools

3. Install NgRx Effects
$ yarn add @ngrx/effects

12.12.2018

Typescript (Javascript) Cheat Sheet

1. Falsy values
false — boolean false
0 — number zero
“” — empty string
null
undefined
NaN — Not A Number

2. Immutability conversion
mutable:
this.currentUser.classes.push(classId);

immutable:
this.currentUser = Object.assign({}, this.currentUser,
     {classes: this.currentUser.classes.concat([classId])});

mutable:
user.classes = user.classes || [];
this.currentUser = user;

immutable:
this.currentUser = Object.assign({}, user,
     {classes: user.classes || []});

10.01.2018

Bash script for switching branches in Git

This script follows my workflow of switching between different branches in my dev environment:
1. Stash changes in my current branch.
2. Checkout the other branch.
3. Apply previously saved stash on the switched branch.
#!/bin/bash

echo "Switching to $1 branch..."
BRANCH=$(git branch | grep \* | cut -d ' ' -f2)
STASHNAME="$BRANCH-changes"
git stash save "$STASHNAME"
git checkout $1
LATEST=$(git stash list | grep $1 | head -n 1)
echo "Latest stash: $LATEST"
echo "Do you want to apply this stash? [y/n]: "
read apply_stash
if [ "$apply_stash" == "y" ]; then
    STASHED=$(git stash list | grep $1 | head -n 1 | cut -d ":" -f1)
    git stash apply "$STASHED"
fi

9.30.2018

Getting Started with NativeScript

Setup development environment using this post.
1. Install node.
2. Install NativeScript CLI
$ npm install -g nativescript
3. Test install
$ tns
4. Install iOS and Android requirements
$ ruby -e "$(curl -fsSL https://www.nativescript.org/setup/mac)"
5. Source bash profile
$ source ~/.bash_profile
6. Verify
$ tns doctor
No issues were detected.

Create first app
1. Create Android Virtual Device(AVD)
$ sdkmanager "system-images;android-25;google_apis;x86"
$ avdmanager create avd -n test -k "system-images;android-25;google_apis;x86"
2. Create app
$ tns create HelloWorld --template tns-template-blank-ng
3. Run the app
$ cd HelloWorld
$ tns run android

8.31.2018

Python Packaging

Following this quickstart, this and this.

1. Pick a name and make sure it is unique after checking on pypi, you might want to make this public later on.

2. Create the scaffolding.
maestroclient
├── LICENSE.txt
├── README.txt
├── maestroclient
│   └── __init__.py
└── setup.py

3. Edit setup.py to contain...
from setuptools import setup

setup(name='maestroclient',
      version='0.1',
      description='Client for accessing Maestro API',
      author='Sam Dela Cruz',
      author_email='s@gmail.com',
      license='MIT',
      packages=['maestroclient'],
      install_requires=[
          'requests',
          'urllib3',
      ],
      zip_safe=False)

4. Create a distribution.
$ python3 setup.py sdist

5. Publish on pypi, just so the project name can be reserved to pypi.
$ twine upload dist/*

6. After building the package, if publishing to pypi is not desired, the package can also be hosted anywhere and can be installed using pip as follows...
$ pip3 install http://whatever.web/maestroclient-0.1.tar.gz

3.28.2018

Docker Cheat Sheet

List docker images
$ docker images

Run an image to make a container (terminal interactive - bash shell)
$ docker run -ti ubuntu:latest bash

List containers
$ docker ps

Stop a container
$ docker stop musing_easley

List stopped containers
$ docker ps --filter "status=exited"

List all containers
$ docker ps -a

Create a new image and tag
$ docker commit [container_id]
$ docker tag [image_id] [image_name]
OR do in one shot:
$ docker commit [container_id] [image_name]

=== Running Things in Docker ===

Delete container when exits
$ docker run --rm -ti ubuntu sleep 5
$ docker run -ti ubuntu bash -c "sleep 3; echo all done"

Start a detached container - leave containers running
$ docker run -d -ti ubuntu bash

Attach to running container
$ docker attach [container_name]

Detach from a container - leave container running
Ctrl+p, Ctrl+q

=== Run more things in a Container ===

Start a shell on a running container (Great for debugging and DB admin)
$ docker exec -ti simpleapp_redis_1 sh

=== Looking at Container Output ===

Look at output
$ docker logs [container_name]

=== Stopping and Removing Containers ===

Kill a container
$ docker kill [container_name]

Remove container
$ docker rm musing_easley

Remove all stopped containers
$ docker rm $(docker ps -a -q)

=== Resource Constraints ===

Memory limits
$ docker run --memory maximum-allowed-memory image-name command

CPU Limits
$ docker run --cpu-shares (relative to other containers)
$ docker run --cpu-quota (to limit it in general)

=== Lessons from the Field ===
1.  Dont let your containers fetch dependencies when they start.
2.  Dont leave important things in unnamed stopped containers.

=== Exposing Ports ===

Expose ports like "-p [outside:inside]"
$ docker run --rm -ti -p 45678:45678 -p 45679:45679 --name echo-server ubuntu:20.04 bash

host.docker.internal - special DNS name which resolves to the internal IP address used by the host, cause localhost wont work.

Exposing ports dynamically (dont specify outside port)
$ docker run --rm -ti -p 45678 -p 45679 --name echo-server ubuntu:20.04 bash

Look which outside port it assigned
$ docker port echo-server

Exposing UDP ports
$ docker run -p outside:inside/protocol (tcp/udp)
$ docker run -p 1234:1234/udp

=== Container Networking ===

Look at existing networks
$ docker network ls

Create a new network
$ docker network create [network_name]
$ docker network create learning

Put machines in the network
$ docker run --rm -ti --net learning --name catserver ubuntu:20.04 bash

Connect networks together
$ docker network connect [network1] [network2]

=== Images ===

List images
$ docker images

Create image
$ docker commit [container_id] my-image-name

Tag image
$ docker commit [container_id] my-image-name:version-tag
$ docker commit 1d622ef86b13 my-new-image:v2.1

Example of name structure:
registry.example.com:port/organization/image-name:version-tag

Cleaning up - delete images
$ docker rmi image-name:tag
$ docker rmi image-id

=== Volumes - Sharing data between containers and the host ===

Persistent - data is permanent
Ephemeral - not permanent

Volumes are not part of images

Make a local host folder that is shared with a container
$ mkdir /Users/sdelacruz/example
$ docker run -ti -v /Users/sdelacruz/example:/shared-folder ubuntu:20.04 bash
root@d492826b8e97:/# touch /shared-folder/sample
$ ls example/
sample

Sharing Data between Containers

volumes-from
Shared "discs" that exist only as long as they are being used

Container 1:
$ docker run -ti -v /shared-data ubuntu:20.04 bash
root@90a67521af1c:/# echo hello > /shared-data/data-file

List running container
$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
90a67521af1c        ubuntu:20.04        "bash"              47 seconds ago      Up 46 seconds                           inspiring_benz

Container 2:
$ docker run -ti --volumes-from inspiring_benz ubuntu:20.04 bash
root@c880cb8099aa:/# ls /shared-data/
data-file

When exiting both containers, the shared volumes will be GONE (Ephemeral)

=== Docker Registries ===

Registries manage and distribute images

Finding images
$ docker search ubuntu

Using Docker Hub
$ docker login

Pull and Push
$ docker pull debian:sid
$ docker tag debian:sid [docker-io-login]/[image-name]:[tag-name]
e.g., $ docker tag debian:sid thearthur/test-image-name:v99.9
$ docker push [docker-io-login]/[image-name]:[tag-name]
e.g., $ docker push thearthur/test-image-name:v99.9

REMEMBER:
1. Dont push images with sensitive info like passwords!  Clean up images.
2. Be aware of what containers being fetched, make sure coming from trusted sources.

=== Docker Files ===

Small program to create an image
Dockerfiles are not shell scripts
Processes you start on one line will not be running on the next line

Run docker file to create image
$ docker build -t name-of-resulting-image .

=== The most basic Dockerfile ===
FROM busybox
RUN echo "building simple docker image."
CMD echo "hello container"

=== Another example ===
FROM debian:sid
RUN apt-get -y update
RUN apt-get install nano
CMD ["bin/nano", "/tmp/notes"]

$ docker build -t example/nanoer .
$ docker run --rm -ti example/nanoer

=== Adding a File through Docker Build ===
(continuing from previous step)
FROM example/nanoer
ADD notes.txt /notes.text
CMD ["/bin/nano", "/notes.txt"]

$ docker build -t example/notes .

=== Dockerfile Syntax ===
FROM - Which image to download and start from, must be first command in Dockerfile
e.g., FROM java:8

MAINTAINER - defines author of this Dockerfile
e.g., MAINTAINER Firstname Lastname <email@example.com>

RUN - runs the command line, waits for it to finish, and saves the result
e.g., RUN unzip install.zip /opt/install/
e.g., RUN echo hello docker

ADD
- Add local files
e.g., ADD run.sh /run.sh

- Adds the contents of tar archives
e.g., ADD project.tar.gz /install/

- Works with URLs
e.g, ADD https://project.example.com/download/1.0/project.rpm /project/

END - sets environment variables, both during the build and when running the result
e.g., ENV DB_HOST=db.production.example.com

ENTRYPOINT - specifies the start of the command to run

CMD - specifies the whole command to run

Shell Form
e.g., nano notes.txt

Exec Form
e.g., ["/bin/nano", "notes.txt"]

EXPOSE - maps a port into the container
e.g., EXPOSE 8080

VOLUME - defines shared or ephemeral volumes
e.g., VOLUME ["/host/path" "/container/path/"]
e.g., VOLUME ["/shared-data"]

Avoid defining shared folders in Dockerfiles

WORKDIR - sets the directory the container starts in
e.g., WORKDIR /install/

USER - sets which user the container will run as
e.g., USER arthur
e.g., USER 1000

=== Multi-Project (Multi-stage??) Dockerfiles ===
FROM ubuntu:20.04 as builder
RUN apt-get update
RUN apt-get -y install curl
RUN curl https://google.com | wc -c > google-size

FROM alpine
COPY --from=builder /google-size /google-size
ENTRYPOINT echo google is this big; cat google-size

=== Preventing Golden Image Problem ===
Include installers in your project
Have a canonical build that builds everything completely from scratch
Tag your builds with the git hash of the code that built it
Use small base images, such as Alpine
Build images you share publicly from Dockerfiles, always
Dont ever leave passwords in layers; delete files in the same step

=== Registry ===
Docker Registry
Nexus

Run your own registry
$ docker run -d -p 5000:5000 --restart=always --name registry registry:2
$ docker tag ubuntu:20.04 localhost:5000/mycompany/my-ubuntu:99
$ docker push localhost:5000/mycompany/my-ubuntu:99

Storage Options
-local storage
-Amazon/Google/Microsoft

Save images to local
$ docker save -o my-images.tar.gz debian:sid busybox ubuntu:20.04

Load images from local
$ docker load -i my-images.tar.gz

=== Orchestration ===
Start containers - and restart them if they fail
Service discovery - allow them to find each other
Resource allocation - match containers to computers

=== Docker Compose ===
Single machine coordination
Designed for testing and development, but not for production
Brings up all containers, volumes, networks, etc. with one command

=== Kubernetes ===
For large deployments
Containers run programs
Pods group containers together
Services make pods available to others
Labels are used for very advanced service discovery
Advantages of Kubernetes
	- Makes scripting large operations possible with the kubectl command
    - Very flexible overlay networking
    - Runs equally well on your hardware or a cloud provider
    - Built-in service discovery
    
=== EC2 Container Service (ECS) ===
Task definitions - define a set of containers that always run together
Tasks - actually makes a container run right now
Services and exposes it to the Net
Advantages of ECS
	- Connects load balancers (ELBs) to services
    - Can create your own host instances in AWS
    - Make your instances start the agent and join the cluster
    - Pass the docker control socket into the agent
    - Provides docker repos - and its easy to run your own repo
    - Containers (tasks) can be part of CloudFormation stacks
    
Others:
Docker Swarm
Google Kubernetes Engine
Azure Kubernetes Service

12.27.2017

Celery Commands Etc

This summary is not available. Please click here to view the post.

12.14.2017

Getting Started with Kubernetes

1. Install Minikube
$ brew update
$ brew install kubectl
$ brew cask install minikube

2. Install xhyve
$ brew install docker-machine-driver-xhyve

3. Set superuser privileges for xhyve (this appears during installation)
$ sudo chown root:wheel /usr/local/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve
$ sudo chmod u+s /usr/local/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve

4. Start minikube
$ minikube start --vm-driver=xhyve

5. Test
$ kubectl config current-context
minikube

6. Get cluster nodes info
$ kubectl get nodes
NAME       STATUS    ROLES     AGE       VERSION
minikube   Ready         22m       v1.8.0

7. Stop cluster
$ minikube stop
Stopping local Kubernetes cluster...
Machine stopped.

8. Delete minikube
$ minikube delete
Deleting local Kubernetes cluster...
Machine deleted.

9. Show available kubernetes versions
$ minikube get-k8s-versions
The following Kubernetes versions are available when using the localkube bootstrapper:
 - v1.8.0
 - v1.7.5
 - v1.7.4
...

10. Get v1.8.0
$ minikube start --vm-driver=xhyve --kubernetes-version="v1.8.0"

11. Start dashboard
$ minikube dashboard


10.18.2017

Get Custom Settings in Django

1. Insert additional settings this way...

In settings.py, or settings/development.py, can insert whatever data structure...

RESOURCE_CREDENTIALS = {
    'client': {
        'USERNAME': 'root',
        'PASSWORD': 'abc123',
    },
    'server': {
        'USERNAME': 'root',
        'PASSWORD': 'ABC123!',
    }
}


2. Then from the app access it this way...

e.g., in views.py...

from django.conf import settings

RESOURCE_CREDENTIALS = getattr(settings, 'RESOURCE_CREDENTIALS')
USERNAME = RESOURCE_CREDENTIALS["client"]["USERNAME"]
PASSWORD = RESOURCE_CREDENTIALS["client"]["PASSWORD"]


10.03.2017

How to Integrate Clarity UI on your Angular App

I heart Clarity UI.

Install

$ yarn add clarity-icons $ yarn add @webcomponents/custom-elements@1.0.0 $ yarn add clarity-ui $ yarn add clarity-angular

Use clarity on Angular

$ vi .angular-cli.json "styles": [ ... "../node_modules/clarity-icons/clarity-icons.min.css", "../node_modules/clarity-ui/clarity-ui.min.css" ], "scripts": [ ... "../node_modules/@webcomponents/custom-elements/custom-elements.min.js", "../node_modules/clarity-icons/clarity-icons.min.js" ], $ vi app.module.ts import { ClarityModule } from "clarity-angular"; imports: [ ... ClarityModule.forRoot(), ... ],

9.21.2017

Angular Development Production Settings

This came from this post

Angular apps created using angular-cli will have src/environments directory.

1. Add another file in there called environment.staging.ts with the following entry...
export const environment = {
  production: false,
  envName: 'staging'
};

2. Modify .angular.cli.json
"environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts",
        "staging": "environments/environment.staging.ts"
      }

3. Now when creating a build or serving the app..
$ ng build -prod
$ ng build -staging
$ ng serve -prod
$ ng serve -staging

4. To use...
import { environment } from './environment';
export class MyappAppComponent {  
  title = 'myapp works!';
  environmentName = environment.envName;
}

9.13.2017

VS Code Cheat Sheet

Full list of shortcuts
1. Detach file to another window
⌘K, O

2. Format file
⇧ + Opt + F

3. Preview side-by-side
⌘K, V 

4. Screen shot
⇧⌘3 - whole screen
⇧⌘4 - selection

9.08.2017

Celery Tasks Parallel and Chained Execution Workflow

In celery, it is very easy to chain and parallelize execution of tasks, e.g. to satisfy this example workflow...
      _____
     |task1|      1. exec task1
    /      \
 __/__    __\__
|task2|  |task3|  2. parallel task1 & task2
   \        /
    \ _____/
     |task4|      3. exec task4 when both task1 & task2 are done

from celery import chain, group, chord
from orchestrator.tasks.host_tasks import acquire_hosts, prepare_hosts, return_hosts
from orchestrator.tasks.job_tasks import start_job, stop_job
from orchestrator.tasks.vcenter_tasks import acquire_vcenter, return_vcenter


def start_performance_run_workflow(job_id):
    print('Starting performance run workflows for job %s' % job_id)
    # The effect of this chain, is that only the first one is routed
    # to the "regular.priority" queue
    # subsequent tasks are routed to "celery" default queue
    workflow = chain(
        acquire_hosts.si(job_id),
        chord(
            (prepare_hosts.si(job_id), acquire_vcenter.si(job_id)),
            start_job.si(job_id)
        )
    ).apply_async(queue="regular.priority")

Angular Material Table Dynamic Height

For md-table, the height can be dynamically adjusted this way...
1. sample-table.component.html
<div class="example-container mat-elevation-z8" [style.max-height.px]="tableHeight">
  ...
  <md-table #table [dataSource]="dataSource" mdSort>

2. sample-table.component.ts
import { HostListener } from '@angular/core';

export class SampleTableComponent {
  tableHeight: number;

  ngOnInit() {
    this.tableHeight = window.innerHeight - 90;
  }

  @HostListener('window:resize', ['$event'])
  onWindowResize(event){
    this.tableHeight = event.target.innerHeight - 90;
    console.log("height: " + this.tableHeight );
  }

8.23.2017

Use httpie to play with your APIs

Got tired of using curl. I switched to httpie.

1. Install
$ pip3 install httpie

2. I use jwt
$ pip3 install -U httpie-jwt-auth

3. First get token
$ http POST :8001/api-token-auth/ 'username=admin' 'password=abc123'

4. Now set env variables
$ export JWT_AUTH_TOKEN=your_token
$ export JWT_AUTH_PREFIX=JWT

5. Now do whatever
$ http --auth-type=jwt :8001/users/

6. If this error is encountered...
SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

Use --verify=no as in...

$ http --verify=no POST ...