Created
July 22, 2019 19:54
-
-
Save mbarkhau/60fc4bbe505914369ebd2fec1ab6d21b to your computer and use it in GitHub Desktop.
do_nothing_script.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Based on this https://news.ycombinator.com/item?id=20495739 | |
# I created this boilerplate which I think is a bit nicer to use. | |
import sys | |
class AttrDict(dict): | |
def __init__(self, *args, **kwargs): | |
super(AttrDict, self).__init__(*args, **kwargs) | |
self.__dict__ = self | |
_STEPS = [] | |
def step(func): | |
_STEPS.append(func) | |
return func | |
def wait_for_enter(): | |
print() | |
input(f"Press Enter to continue: ") | |
def main(args = sys.argv[1:]): | |
ctx = AttrDict() | |
try: | |
num_steps = len(_STEPS) | |
for step_num, step_func in enumerate(_STEPS): | |
step_name = step_func.__name__ | |
print(f"step {step_num + 1:>{len(str(num_steps))}}/{num_steps}: {step_name}") | |
if step_func.__doc__: | |
print(step_func.__doc__) | |
print() | |
step_func(ctx) | |
print() | |
print("Done.") | |
except KeyboardInterrupt: | |
print("Aborted.") | |
# Actual steps | |
@step | |
def enter_username(ctx): | |
out_username = None | |
while True: | |
in_username = input(f" Enter username (a-z): ") | |
if in_username.strip(): | |
out_username = in_username.lower().replace(" ", "_")[:10] | |
else: | |
in_username = out_username | |
if out_username == in_username: | |
break | |
else: | |
print(f" Invalid username, press enter to use '{out_username}' instead") | |
ctx.username = out_username | |
@step | |
def create_ssh_keypair(ctx): | |
print(f" Run:") | |
print(f" ssh-keygen -t rsa -f ~/{ctx.username}") | |
wait_for_enter() | |
@step | |
def git_commit(ctx): | |
print(f" Copy ~/new_key.pub into the `user_keys` Git repository, then run:") | |
print(f" git commit {ctx.username}") | |
print(f" git push") | |
wait_for_enter() | |
@step | |
def wait_for_build(ctx): | |
build_url = "http://example.com/builds/user_keys" | |
print(f" Wait for the build job at {build_url} to finish") | |
wait_for_enter() | |
@step | |
def retrieve_user_email(ctx): | |
dir_url = "http://example.com/directory" | |
print(f" Go to {dir_url}") | |
print(f" Find the email address for user `{ctx.username}`") | |
print() | |
ctx.email = input(f" Paste the email address and press enter: ") | |
@step | |
def send_private_key(ctx): | |
print(f" Go to 1Password") | |
print(f" Paste the contents of ~/new_key into a new document") | |
print(f" Share the document with {ctx.email}") | |
wait_for_enter() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment