Skip to content

Instantly share code, notes, and snippets.

View JayH5's full-sized avatar

Jamie Hewland JayH5

  • Yelp
  • London, UK
View GitHub Profile
@JayH5
JayH5 / vault-privkey.ctmpl
Created June 8, 2018 14:28
Consul Template template for Vault private key
{{ with secret "pki/vault/issue/server" "common_name=vault01" "alt_names=vault.service.internal,vault" "ip_sans=10.70.4.10,127.0.0.1" -}}
{{ .Data.private_key }}
{{- end }}
@JayH5
JayH5 / vault-fullchain.ctmpl
Created June 8, 2018 14:25
Consul Template template for Vault certificate
{{ with secret "pki/vault/issue/server" "common_name=vault01" "alt_names=vault.service.internal,vault" "ip_sans=10.70.4.10,127.0.0.1" -}}
{{ .Data.certificate }}
{{ range .Data.ca_chain -}}
{{ . }}
{{- end }}
{{- end }}
@JayH5
JayH5 / pipe-equivalent.ex
Created April 5, 2018 10:37
Elixir example: lookup key equivalents
word |> String.downcase() |> String.graphemes() |> Enum.sort() |> Enum.join()
Enum.join(Enum.sort(String.graphemes(String.downcase(word))))
@JayH5
JayH5 / dict-merge.py
Last active April 4, 2018 12:49
Python example: Dictionary merging
def merge_dicts(dict1, dict2):
merged_dict = dict1.copy()
merged_dict.update(dict2)
return merged_dict
@JayH5
JayH5 / anagram-counter.ex
Last active April 5, 2018 10:31
Elixir example: Anagram counter
defmodule Anagrams do
def anagram_counter(words, n) do
words
|> Enum.filter(fn word -> String.length(word) == n end)
|> Enum.reduce(%{}, fn word, lookup ->
Map.update(lookup, lookup_key(word), MapSet.new([word]), &MapSet.put(&1, word))
end)
|> Enum.count(fn {_key, anagram_words} -> MapSet.size(anagram_words) > 1 end)
end
@JayH5
JayH5 / anagram-counter.py
Last active April 5, 2018 10:30
Python example: Anagram counter
from collections import defaultdict
def anagram_counter(words, n):
lookup = defaultdict(set)
for word in words:
if len(word) == n:
lookup[lookup_key(word)].add(word)
num_anagrams = 0
for anagram_words in lookup.values():
@JayH5
JayH5 / networking-mode.ex
Last active April 24, 2018 08:15
Elixir example: Marathon networking mode
# Marathon 1.5+: there is a `networks` field
# Networking modes can't be mixed so using the first one is fine
def networking_mode(%{"networks" => [network | _]} = _app_json),
do: network |> Map.get("mode", "container") |> String.to_existing_atom()
# Pre-Marathon 1.5 Docker container networking mode
def networking_mode(%{"container" => %{"docker" => %{"network" => "USER"}}}),
do: :container
def networking_mode(%{"container" => %{"docker" => %{"network" => "BRIDGE"}}}),
@JayH5
JayH5 / networking-mode.py
Last active April 3, 2018 10:01
Python example: Marathon networking mode
def networking_mode(app_json):
# Marathon 1.5+: there is a `networks` field
networks = app_json.get('networks')
if networks:
# Modes cannot be mixed, so assigning the last mode is fine
return networks[-1].get('mode', 'container')
# Older Marathon: determine equivalent network mode
container = app_json.get('container')
if container is not None and 'docker' in container:
Envoy Marathon Marathon example Envoy example
Listener N/A N/A Listener.address = SocketAddress("0.0.0.0", 80)
Route marathon-lb app labels {"HAPROXY_0_VHOST": "za.goodinternet.org"} VirtualHost.domains = ["za.goodinternet.org"]
Cluster App ID + port index app ID = /cake-service, port index = 0 Cluster.name = "/cake-service_0"
Endpoint Task address task address = 10.1.1.21, port = 31539 Endpoint.address = SocketAddress("10.1.1.21", 31539)
Envoy Nginx HAProxy
Listener server listen frontend bind
Route server location frontend acl
Cluster upstream backend
Endpoint upstream server backend server