def print_help_external_ca(hi): """ help for external ca installation """ utils.print_H1('How to setup an external Certificate Authority') utils.print_normal( 'By default external CA will be automatically installed by `kubeadm-playground create` ', 'in case your cluster is configured with `certificateAuthority: external`' ) if hi.cluster.certificateAuthority != kubeadm_utils.CERTIFICATEAUTHORITY_TYPE_EXTERNAL: print utils.print_warning( 'The kubeadm playground currently does not require an external ca.', 'Please change your cluster api specification') utils.print_H2('Assisted mode') utils.print_normal( "* only if automatic installation was disabled during create") utils.print_code("kubeadm-playground exec external-ca") utils.print_H2('Manual mode') ssh_to(hi.bootstrapMaster.name) utils.print_normal('- Create an external CA by executing:') utils.print_code( "sudo %s alpha phase certs all --config /etc/kubernetes/kubeadm.conf" % (hi.kubeadm_binary), 'sudo rm /etcd/kubernetes/pki/ca.key', 'sudo rm /etcd/kubernetes/pki/front-proxy-ca.key') print
def print_help_external_vip(hi): """ help for external vip installation """ utils.print_H1 ('How to setup an external Vip/load balancer') if len(hi.etcds) == 0: print utils.print_warning ('The kubeadm playground currently does not require an external vip.', 'Please change your cluster api specification') utils.print_normal ('By default external etcd will be automatically installed by `kubeadm-playground create` ', "in case your cluster has more then one machines with role '%s'." % (cluster_api.ROLE_MASTER)) utils.print_H2 ('Assisted mode') utils.print_normal ("* only if automatic installation was disabled during create") utils.print_H2 ('Assisted mode') utils.print_normal ('By default external vip/load balancer will be automatically installed by `kubeadm-playground create` ', 'in case your cluster is configured with more than one machine with role `Master`', '' "The vip address will be %s (%s) and will balance following api server end points:" % (hi.kubernetes_vip_fqdn, hi.kubernetes_vip_ip)) for m in hi.masters: utils.print_normal ("- https://%s:6443" % (m.ip)) print utils.print_normal ('If automatic installation of external vip was disabled during create, it can be invoked afterwards with:') utils.print_code ("kubeadm-playground exec external-vip") utils.print_H2 ('Manual mode') utils.print_normal ('- Create an external VIP/load balancer similar to what described above.') utils.print_normal ("- Ensure that the VIP address is set in '/etc/kubernetes/kubeadm.conf' on %s." % (hi.bootstrapMaster.name)) print
def print_help_external_etcd(hi): """ help for external etcd installation """ utils.print_H1('How to install an external Etcd') utils.print_normal( 'By default external etcd will be automatically installed by `kubeadm-playground create` ', "in case your cluster has machines with role '%s'." % (cluster_api.ROLE_ETCD)) if len(hi.etcds) == 0: print utils.print_warning( 'The kubeadm playground currently does not require an external etcd.', 'Please change your cluster api specification') utils.print_H2('Assisted mode') utils.print_normal( "* only if automatic installation was disabled during create") utils.print_code("kubeadm-playground exec external-etcd") utils.print_H2('Manual mode') utils.print_normal('- Install etcd on following machines:') for m in hi.etcds: utils.print_normal(" - %s" % (m.name)) print utils.print_normal( "- Ensure that etcd endpoint and eventually etcd TLS certificates are set in '/etc/kubernetes/kubeadm.conf' on %s" % (hi.bootstrapMaster.name)) print
def print_help_kubeadm_init(hi): utils.print_H1 ('How to execute kubeadm init') utils.print_H2 ('Assisted mode') utils.print_code ("kubeadm-playground exec kubeadm-init") utils.print_H2 ('Manual mode') ssh_to (hi.bootstrapMaster.name) utils.print_normal ('- Initialize the kubernetes master node') utils.print_code ("sudo %s init --config /etc/kubernetes/kubeadm.conf" % (hi.kubeadm_binary))
def input_guess(screen, code, color_count, guess): global line selected = 0 utils.print_code(screen, utils.PREFIX, guess, selected) alpha = 0 key = screen.getkey() while key != "\n": # Confirm the guess when the user pressed the enter key if key == "KEY_UP": # Up arrow rolls back the selected color by 1 guess[selected] = (guess[selected] - 1) % color_count alpha = (alpha + 1) * int(alpha < 2) elif key == "KEY_DOWN" or key == "\t": # Down arrow/tab increments the selected color by 1 guess[selected] = (guess[selected] + 1) % color_count alpha = (alpha + 1) * int(alpha in [2, 3]) elif key == "KEY_LEFT": # Left arrow moves the selected color by 1 towards the left, and goes back all the way to the right when it reaches the side selected = (selected - 1) % len(code) alpha = (alpha + 1) * int(alpha in [4, 6]) elif key == "KEY_RIGHT": # Same for the right arrow but in the other direction selected = (selected + 1) % len(code) alpha = (alpha + 1) * int(alpha in [5, 7]) elif key == "a" or key == "A": alpha = (alpha + 1) * int(alpha == 9) elif key == "b" or key == "B": alpha = (alpha + 1) * int(alpha == 8) else: alpha = 0 if alpha == 10: for i in range(len(code)): guess[i] = code[i] utils.print_code(screen, utils.PREFIX, guess, i) screen.refresh() curses.napms(100) curses.napms(400) break utils.print_code( screen, utils.PREFIX, guess, selected) # Update the guess displayed after each key press key = screen.getkey() utils.print_code( screen, utils.PREFIX, guess, -1 ) # Print the final guess without the dot on the selected color because it's useless once the color is confirmed line += 1 screen.move( line, 0 ) # Move to the next time because the current guess has been confirmed return guess
def input_guess(screen, code_length, color_count, possibilities): global line screen.refresh() if not utils.DEBUG: curses.napms(250) # TODO: Animation? if len(possibilities) == color_count**code_length: guess = [0] * ((code_length + 1) // 2) + [1] * (code_length // 2) else: guess = random.choice(possibilities) possibilities.remove(guess) utils.print_code(screen, utils.PREFIX, guess, -1) line += 1 screen.move(line, 0) return guess
def input_guess(screen, code_length, color_count, guess): global line selected = 0 utils.print_code(screen, utils.PREFIX, guess, selected) key = screen.getkey() while key != "\n": # Confirm the guess when the user pressed the enter key if key == "KEY_UP": # Up arrow rolls back the selected color by 1 guess[selected] = (guess[selected] - 1) % color_count elif key == "KEY_DOWN" or key == "\t": # Down arrow/tab increments the selected color by 1 guess[selected] = (guess[selected] + 1) % color_count elif key == "KEY_LEFT": # Left arrow moves the selected color by 1 towards the left, and goes back all the way to the right when it reaches the side selected = (selected - 1) % code_length elif key == "KEY_RIGHT": # Same for the right arrow but in the other direction selected = (selected + 1) % code_length utils.print_code( screen, utils.PREFIX, guess, selected) # Update the guess displayed after each key press key = screen.getkey() utils.print_code( screen, utils.PREFIX, guess, -1 ) # Print the final guess without the dot on the selected color because it's useless once the color is confirmed line += 1 screen.move( line, 0 ) # Move to the next time because the current guess has been confirmed return guess
def print_help_kubeadm_join(hi): utils.print_H1("How to execute kubeadm join") if len(hi.nodes) == 0: print utils.print_warning( 'The kubeadm playground currently does not have worker nodes.', 'Please change your cluster api specification') utils.print_H2('Assisted mode') utils.print_code("kubeadm-playground exec kubeadm-join") utils.print_H2('Manual mode') utils.print_normal( "Repeat following steps for all the machines with role '%s'" % (cluster_api.ROLE_NODE)) if hi.kubernetes_cni_sysconf: utils.print_normal( "- Make required changes for %s CNI plugin to work:" % (hi.networkAddon)) utils.print_code('sudo sysctl net.bridge.bridge-nf-call-iptables=1') utils.print_normal('- Join the worker node') utils.print_code( "sudo %s join %s:6443 --token %s \\" % (hi.kubeadm_binary, hi.controlplaneEndpoint, hi.kubeadm_token), " --discovery-token-unsafe-skip-ca-verification")
def print_help_kubectl_apply_network(hi): utils.print_H1 ("How to install %s network addon" % (hi.networkAddon)) utils.print_H2 ('Assisted mode') utils.print_code ("kubeadm-playground exec kubectl-apply-network") utils.print_H2 ('Manual mode') ssh_to (hi.bootstrapMaster.name) if hi.kubernetes_cni_sysconf: utils.print_normal ("- Make required changes for %s CNI plugin to work:" % (hi.networkAddon)) utils.print_code ('sudo sysctl net.bridge.bridge-nf-call-iptables=1') utils.print_normal ("- Install %s CNI plugin:" % (hi.networkAddon)) utils.print_code ('kubectl apply \\', " -f %s" % (hi.kubernetes_cni_manifest_url))
def play_game(screen, color_count, max_attempts, code, attempts): global line, client_socket score = 0 if len(attempts) > max_attempts: attempts = attempts[:max_attempts] for i, attempt in enumerate(attempts): utils.print_code(screen, utils.PREFIX, attempt, -1) line += 1 screen.move(line, 0) answer_bytes = utils.receive_packet( client_socket ) # We read the server's answer for the current attempt and find the corresponding perfect and partial pin counts if len(answer_bytes) == 2 and int.from_bytes(answer_bytes, "big") == 418: answer_bytes = utils.receive_packet(client_socket) perfect = int.from_bytes(answer_bytes[:2], "big") partial = int.from_bytes(answer_bytes[2:4], "big") if perfect == len( code ): # If all the pins are perfect, that means the guessed code was the right one screen.move(line - 1, len(utils.PREFIX) + 1 + (len(code) + 1) * 2) screen.addstr("Correct!\n\r" ) # Print "correct" next to the last guessed line screen.move(line, 0) score = max_attempts - i # His score is the number of attempts left screen.addstr( "You cracked the code! Score: {}\n\r".format(score) ) # Print a nice message for the user to know he got it right and show his score line += 1 break else: # Show the perfect and partial pins next to the guess offset = len(utils.PREFIX) + 1 + (len(code) + 1) * 2 screen.addstr(line - 1, offset, "Result:") offset += len("Result:") + 1 for i in range(perfect + partial): attr = curses.color_pair(2 if i < perfect else 1) screen.addstr(line - 1, offset + i * 2, " ", attr) screen.move(line, 0) else: if attempts: guess = attempts[-1].copy( ) # We take the last attempt as starting point for the next guess if there is one else: guess = [0] * len( code ) # Otherwise, the user's default guess will be only red pins (it could be any color, but the 0-th color is easier and always exists) for attempt in range( len(attempts), max_attempts ): # Limits the number of attempts to the chosen amount guess = input_guess(screen, len(code), color_count, guess) attempts.append(guess.copy()) guess_bytes = bytes( ) # While in an Online Ranked Game, instead of comparing the codes locally, we send the guess to the server # If guess is not the same size as code, cut it off or append zeros if len(guess) > len(code): guess = guess[:len(code)] elif len(guess) < len(code): guess += [0] * (len(code) - len(guess)) for color in guess: # Encode guess' colors on 16 bits (max. 65535 colors) guess_bytes += (min(color, 0xFFFF) & 0xFFFF).to_bytes(2, "big") utils.send_packet(client_socket, guess_bytes) answer_bytes = utils.receive_packet( client_socket ) # We then read the server's answer and find the corresponding perfect and partial pin counts if len(answer_bytes) == 2 and int.from_bytes(answer_bytes, "big") == 418: answer_bytes = utils.receive_packet(client_socket) perfect = int.from_bytes(answer_bytes[:2], "big") partial = int.from_bytes(answer_bytes[2:4], "big") if perfect == len( code ): # If all the pins are perfect, that means the guessed code is the right one screen.move(line - 1, len(utils.PREFIX) + 1 + (len(code) + 1) * 2) screen.addstr( "Correct!\n\r" ) # Print "correct" next to the last guessed line screen.move(line, 0) score = max_attempts - attempt # His score is the number of attempts left screen.addstr( "You cracked the code! Score: {}\n\r".format(score) ) # Print a nice message for the user to know he got it right and show his score line += 1 break else: # Show the perfect and partial pins next to the guess offset = len(utils.PREFIX) + 1 + (len(code) + 1) * 2 screen.addstr(line - 1, offset, "Result:") offset += len("Result:") + 1 for i in range(perfect + partial): attr = curses.color_pair(2 if i < perfect else 1) screen.addstr(line - 1, offset + i * 2, " ", attr) screen.move(line, 0) else: # If we reached the maximum number of attempts without quitting the loop, it means the user failed to guess the code and he lost token = utils.receive_packet(client_socket).decode("utf8") _, _, _, _, _, code, _ = utils.decode_token( token ) # Retrieve the real code from the server once the player lost utils.print_code(screen, "You failed! The code was:", code, -1) line += 1 screen.move(line, 0) screen.refresh( ) # Update the screen before going to sleep, or it would "freeze" before updating, not showing the final text before resetting curses.napms(3000) return score
def play_game(screen, color_count, max_attempts, code, attempts): global line utils.print_code(screen, "The computer will try to guess the following code:", code, -1) line += 1 screen.move(line, 0) game_score = 0 if len(attempts) > max_attempts: attempts = attempts[:max_attempts] screen.refresh() possibilities = list( map(list, itertools.product(range(color_count), repeat=len(code)))) total_possibilities = len(possibilities) for i, attempt in enumerate(attempts): utils.print_code(screen, utils.PREFIX, attempt, -1) line += 1 screen.move(line, 0) perfect, partial = utils.compare_codes( attempt, code) # Compare the current attempt with the real code if perfect == len( code ): # If all the pins are perfect, that means the guessed code was the right one screen.move(line - 1, len(utils.PREFIX) + 1 + (len(code) + 1) * 2) screen.addstr("Correct!\n\r" ) # Print "correct" next to the last guessed line screen.move(line, 0) game_score = max_attempts - i # His score is the number of attempts left screen.addstr( "The computer cracked the code! Score: {}\n\r".format( game_score) ) # Print a nice message for the user to know he got it right and show his score line += 1 break else: # Show the perfect and partial pins next to the guess offset = len(utils.PREFIX) + 1 + (len(code) + 1) * 2 screen.addstr(line - 1, offset, "Result:") offset += len("Result:") + 1 for i in range(perfect + partial): attr = curses.color_pair(2 if i < perfect else 1) screen.addstr(line - 1, offset + i * 2, " ", attr) screen.move(line, 0) screen.refresh() # Remove all the remaining possibilities that don't match the new conditions possibilities = list( filter( lambda possibility: utils.compare_codes( attempt, possibility) == (perfect, partial), possibilities)) else: for attempt in range( len(attempts), max_attempts ): # Limits the number of attempts to the chosen amount guess = input_guess(screen, len(code), color_count, possibilities) attempts.append(guess.copy()) perfect, partial = utils.compare_codes( guess, code) # Compare the current attempt with the real code if perfect == len( code ): # If all the pins are perfect, that means the guessed code is the right one screen.move(line - 1, len(utils.PREFIX) + 1 + (len(code) + 1) * 2) screen.addstr( "Correct!\n\r" ) # Print "correct" next to the last guessed line screen.move(line, 0) game_score = max_attempts - attempt # His score is the number of attempts left screen.addstr( "The computer cracked the code! Score: {}\n\r".format( game_score) ) # Print a nice message for the user to know he got it right and show his score line += 1 break else: # Show the perfect and partial pins next to the guess offset = len(utils.PREFIX) + 1 + (len(code) + 1) * 2 screen.addstr(line - 1, offset, "Result:") offset += len("Result:") + 1 for i in range(perfect + partial): attr = curses.color_pair(2 if i < perfect else 1) screen.addstr(line - 1, offset + i * 2, " ", attr) screen.move(line, 0) screen.refresh() # Remove all the remaining possibilities that don't match the new conditions possibilities = list( filter( lambda possibility: utils.compare_codes( guess, possibility) == (perfect, partial), possibilities)) else: # If we reached the maximum number of attempts without quitting the loop, it means the user failed to guess the code and he lost screen.addstr("The computer failed! Progress: {}%".format( 100 - int(len(possibilities) / total_possibilities * 100))) line += 1 screen.move(line, 0) screen.refresh( ) # Update the screen before going to sleep, or it would "freeze" before updating, not showing the final text before resetting if not utils.DEBUG: curses.napms(3000) return game_score
def ssh_to(machine): utils.print_normal('From the guest machine:') utils.print_code("kubeadm-playground ssh %s" % (machine)) utils.print_normal('After connecting:') print
def play_game(screen, color_count, max_attempts, code, attempts): global line if utils.DEBUG: # If we're debugging and not in an Online Ranked Game, print the code at the beginning to be able to test without playing for real utils.print_code(screen, "Psst... The correct code is:", code, -1) line += 1 screen.move(line, 0) game_score = 0 if len(attempts) > max_attempts: attempts = attempts[:max_attempts] for i, attempt in enumerate(attempts): utils.print_code(screen, utils.PREFIX, attempt, -1) line += 1 screen.move(line, 0) perfect, partial = utils.compare_codes( attempt, code) # Compare the current attempt with the real code if perfect == len( code ): # If all the pins are perfect, that means the guessed code was the right one screen.move(line - 1, len(utils.PREFIX) + 1 + (len(code) + 1) * 2) screen.addstr("Correct!\n\r" ) # Print "correct" next to the last guessed line screen.move(line, 0) game_score = max_attempts - i # His score is the number of attempts left screen.addstr( "You cracked the code! Score: {}\n\r".format(game_score) ) # Print a nice message for the user to know he got it right and show his score line += 1 break else: # Show the perfect and partial pins next to the guess offset = len(utils.PREFIX) + 1 + (len(code) + 1) * 2 screen.addstr(line - 1, offset, "Result:") offset += len("Result:") + 1 for i in range(perfect + partial): attr = curses.color_pair(2 if i < perfect else 1) screen.addstr(line - 1, offset + i * 2, " ", attr) screen.move(line, 0) else: if attempts: guess = attempts[-1].copy( ) # We take the last attempt as starting point for the next guess if there is one else: guess = [0] * len( code ) # Otherwise, the user's default guess will be only red pins (it could be any color, but the 0-th color is easier and always exists) for attempt in range( len(attempts), max_attempts ): # Limits the number of attempts to the chosen amount guess = input_guess(screen, code, color_count, guess) attempts.append(guess.copy()) perfect, partial = utils.compare_codes( guess, code) # Compare the guessed code with the real code if perfect == len( code ): # If all the pins are perfect, that means the guessed code is the right one screen.move(line - 1, len(utils.PREFIX) + 1 + (len(code) + 1) * 2) screen.addstr( "Correct!\n\r" ) # Print "correct" next to the last guessed line screen.move(line, 0) game_score = max_attempts - attempt # His score is the number of attempts left screen.addstr( "You cracked the code! Score: {}\n\r".format(game_score) ) # Print a nice message for the user to know he got it right and show his score line += 1 break else: # Show the perfect and partial pins next to the guess offset = len(utils.PREFIX) + 1 + (len(code) + 1) * 2 screen.addstr(line - 1, offset, "Result:") offset += len("Result:") + 1 for i in range(perfect + partial): attr = curses.color_pair(2 if i < perfect else 1) screen.addstr(line - 1, offset + i * 2, " ", attr) screen.move(line, 0) else: # If we reached the maximum number of attempts without quitting the loop, it means the user failed to guess the code and he lost utils.print_code(screen, "You failed! The code was:", code, -1) line += 1 screen.move(line, 0) screen.refresh( ) # Update the screen before going to sleep, or it would "freeze" before updating, not showing the final text before resetting curses.napms(3000) return game_score
def main(): """Driver code for Abstract syntax tree Generation""" #read source code provided by user arg_parser = argparse.ArgumentParser(description="C compiler for x86_64") arg_parser.add_argument('source_code', help="location of source code file") arg_parser.add_argument('-o', help="output file name, \{default a.out\}", default="a.out") arg_parser.add_argument( '-f', help="name of file for additional files, \{default a\}", default="a") arg_parser.add_argument('-c', action='store_true', help="output object file") arg_parser.add_argument('-d', action='store_true', help="output assembly") arg_parser.add_argument('-a', action='store_true', help="output ast") arg_parser.add_argument('-s', action='store_true', help="output symbol table") arg_parser.add_argument('-t', action='store_true', help="output 3 address code") arg_parser.add_argument('-l', action='store_true', help="output lexeme table") arg_parser.add_argument( '-stdc', action='store_true', help= "linker method, if specified it'll uses custom elf entry else from standard X86-64-linux.so" ) arg_parser.add_argument( '-n', action="store_false", help="only create till asm, , do not create executable") args = arg_parser.parse_args() try: source_code = open(args.source_code, "r").read() except FileNotFoundError: print( "source file cannot be open/read.\nCheck the file name or numbers of arguments!!" ) sys.exit(-1) source_file = args.source_code file_name = args.f grammar = get_grammar(source_file, source_code, debug=1) if len(Errors.get_all_error()): for error in Errors.get_all_error(): print(error) return tac_code = grammar.code tac_code = remove_none(tac_code) #to remove redundant labels tac_code = remove_label(tac_code) print_asm(tac_code, stdc=args.stdc) #assembly if args.d: os.system("cp temp.asm {}.asm".format(file_name)) if args.n: asm_file = "temp.asm" os.system('yasm -g dwarf2 -f elf64 temp.asm 2> temp') os.system('touch temp') if args.c: os.system("cp temp.o " + file_name + ".o") if args.stdc: os.system( "ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o {} temp.o -lc -lm 2> temp" .format(args.o)) else: os.system( "ld -o {} -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o -lc temp.o /usr/lib/x86_64-linux-gnu/crtn.o -lm 2> temp" .format(args.o)) os.system("rm -rf temp.asm temp.o temp") else: os.system("rm -rf temp.asm") # ast if args.a: Graph = draw_ast(grammar) Graph.draw(file_name + '.png', format='png') # symbol table if args.s: print_csv(sym_table=sym_table, filename=file_name + ".csv") # 3AC if args.t: print_code(tac_code, filename=file_name + ".3ac") # lexeme if args.l: print_lexeme(source_code, file_name + ".lex")