def __init__(self): self.pem_dir = os.environ['PEM_DIR'] Logger.__init__(self) self.spreadsheet = GoogleSpreadsheet() self.logger.info('Downloading spreadsheets') self.users = self.spreadsheet.get_users() self.instances = self.spreadsheet.get_instances() self.permissions = self.spreadsheet.get_permissions() self.logger.info('Spreadsheets are downloaded')
class E2C2(Logger): CREATE_USER_ON_INSTANCE = "ssh -i {PEM_PATH} {INSTANCE} 'sudo useradd {USER}; sudo passwd -d {USER}; sudo mkdir /home/{USER};sudo chown {USER}:{USER} /home/{USER}'" ADD_USER_TO_SUDOERS_GROUP = "ssh -i {PEM_PATH} {INSTANCE} 'sudo usermod -a -G sudo {USER}'" HOME_DIR = '/home/{USER}/' SSH_DIR = '/home/{USER}/.ssh' CREATE_DIR = "ssh -i {PEM_PATH} {INSTANCE} \ 'sudo -u {USER} mkdir -p {SSH_DIR}'" ADD_USER_KEY_TO_AUTHORIZED_KEYS = "ssh -i {PEM_PATH} {INSTANCE} \"sudo -u {USER} sh -c \'echo \"{USER_KEY}\" >> {SSH_DIR}/authorized_keys\'\"" CHECK_USER_EXISTS = "ssh -i {PEM_PATH} {INSTANCE} 'cut -d: -f1 /etc/passwd | grep {USER}'" DELETE_USER_ON_INSTANCE = "ssh -i {PEM_PATH} {INSTANCE} 'sudo userdel -r {USER}'" def __init__(self): self.pem_dir = os.environ['PEM_DIR'] Logger.__init__(self) self.spreadsheet = GoogleSpreadsheet() self.logger.info('Downloading spreadsheets') self.users = self.spreadsheet.get_users() self.instances = self.spreadsheet.get_instances() self.permissions = self.spreadsheet.get_permissions() self.logger.info('Spreadsheets are downloaded') def formatted_json(self, json_): return json.dumps(json_, sort_keys=True, indent=4, separators=(',', ': ')) def get_public_key(self, user): self.logger.info("Get public key by %s" % user) return self.users[user].replace("\n", "").strip() def get_host(self, instance): self.logger.info("Get host by %s" % instance) return self.instances[instance]['host'] def get_pem_file(self, instance): return self.pem_dir + self.instances[instance]['key'] def user_exists(self, user, instance): command = self.CHECK_USER_EXISTS.format( PEM_PATH=self.get_pem_file(instance), USER=user, INSTANCE=self.get_host(instance) ) self.logger.debug("CHECK_USER_EXISTS:\n%s" % command) result = self.execute_shell_command(command) if result != "": return True else: return False def delete_user(self, user, instance): if self.user_exists(user, instance): self.logger.debug("DELETE_USER_ON_INSTANCE:\n user found") command = self.DELETE_USER_ON_INSTANCE.format( PEM_PATH=self.get_pem_file(instance), USER=user, INSTANCE=self.get_host(instance) ) self.logger.debug("DELETE_USER_ON_INSTANCE:\n%s" % command) self.execute_shell_command(command) else: self.logger.debug("DELETE_USER_ON_INSTANCE:\n user not found") def create_user_on_instance(self, user, instance): if self.user_exists(user, instance): self.logger.debug("CREATE_USER_ON_INSTANCE:\n user found") else: command = self.CREATE_USER_ON_INSTANCE.format( PEM_PATH=self.get_pem_file(instance), USER=user, INSTANCE=self.get_host(instance) ) self.logger.debug("CREATE_USER_ON_INSTANCE:\n%s" % command) self.execute_shell_command(command) command = self.CREATE_DIR.format( USER=user, SSH_DIR=self.SSH_DIR.format(USER=user), PEM_PATH=self.get_pem_file(instance), INSTANCE=self.get_host(instance) ) self.logger.debug("CREATE_DIR:\n%s" % command) self.execute_shell_command(command) def add_user_key_to_instance(self, user, instance): if self.user_exists(user, instance): self.logger.debug("ADD_USER_KEY_TO_INSTANCE:\n user found") command = self.ADD_USER_KEY_TO_AUTHORIZED_KEYS.format( USER=user, USER_KEY=self.get_public_key(user), SSH_DIR=self.SSH_DIR.format(USER=user), PEM_PATH=self.get_pem_file(instance), INSTANCE=self.get_host(instance) ) self.logger.debug("ADD_USER_KEY_TO_AUTHORIZED_KEYS:\n%s" % command) self.execute_shell_command(command) else: self.logger.debug("ADD_USER_KEY_TO_INSTANCE:\n user not found") def add_user_to_sudoers_group(self, user, instance): if self.user_exists(user, instance): self.logger.debug("ADD_USER_TO_SUDOERS_GROUP:\n user found") command = self.ADD_USER_TO_SUDOERS_GROUP.format( USER=user, SSH_DIR=self.SSH_DIR.format(USER=user), PEM_PATH=self.get_pem_file(instance), INSTANCE=self.get_host(instance) ) self.logger.debug("ADD_USER_TO_SUDOERS_GROUP:\n%s" % command) self.execute_shell_command(command) else: self.logger.debug("ADD_USER_TO_SUDOERS_GROUP:\n user not found") def execute_shell_command(self, command): cmd = Popen(command, shell=True, stdout=PIPE, stderr=STDOUT) result = str(cmd.communicate()[0]) self.logger.debug("SHELL_COMMAND_RESULT:\n%s" % result) return result
class E2C2(Logger): CREATE_USER_ON_INSTANCE = "ssh -i {PEM_PATH} {INSTANCE} 'sudo useradd {USER}; sudo passwd -d {USER}; sudo mkdir /home/{USER};sudo chown {USER}:{USER} /home/{USER}'" ADD_USER_TO_SUDOERS_GROUP = "ssh -i {PEM_PATH} {INSTANCE} 'sudo usermod -a -G sudo {USER}'" HOME_DIR = '/home/{USER}/' SSH_DIR = '/home/{USER}/.ssh' CREATE_DIR = "ssh -i {PEM_PATH} {INSTANCE} \ 'sudo -u {USER} mkdir -p {SSH_DIR}'" ADD_USER_KEY_TO_AUTHORIZED_KEYS = "ssh -i {PEM_PATH} {INSTANCE} \"sudo -u {USER} sh -c \'echo \"{USER_KEY}\" >> {SSH_DIR}/authorized_keys\'\"" CHECK_USER_EXISTS = "ssh -i {PEM_PATH} {INSTANCE} 'cut -d: -f1 /etc/passwd | grep {USER}'" DELETE_USER_ON_INSTANCE = "ssh -i {PEM_PATH} {INSTANCE} 'sudo userdel -r {USER}'" def __init__(self): self.pem_dir = os.environ['PEM_DIR'] Logger.__init__(self) self.spreadsheet = GoogleSpreadsheet() self.logger.info('Downloading spreadsheets') self.users = self.spreadsheet.get_users() self.instances = self.spreadsheet.get_instances() self.permissions = self.spreadsheet.get_permissions() self.logger.info('Spreadsheets are downloaded') def formatted_json(self, json_): return json.dumps(json_, sort_keys=True, indent=4, separators=(',', ': ')) def get_public_key(self, user): self.logger.info("Get public key by %s" % user) return self.users[user].replace("\n", "").strip() def get_host(self, instance): self.logger.info("Get host by %s" % instance) return self.instances[instance]['host'] def get_pem_file(self, instance): return self.pem_dir + self.instances[instance]['key'] def user_exists(self, user, instance): command = self.CHECK_USER_EXISTS.format( PEM_PATH=self.get_pem_file(instance), USER=user, INSTANCE=self.get_host(instance)) self.logger.debug("CHECK_USER_EXISTS:\n%s" % command) result = self.execute_shell_command(command) if result != "": return True else: return False def delete_user(self, user, instance): if self.user_exists(user, instance): self.logger.debug("DELETE_USER_ON_INSTANCE:\n user found") command = self.DELETE_USER_ON_INSTANCE.format( PEM_PATH=self.get_pem_file(instance), USER=user, INSTANCE=self.get_host(instance)) self.logger.debug("DELETE_USER_ON_INSTANCE:\n%s" % command) self.execute_shell_command(command) else: self.logger.debug("DELETE_USER_ON_INSTANCE:\n user not found") def create_user_on_instance(self, user, instance): if self.user_exists(user, instance): self.logger.debug("CREATE_USER_ON_INSTANCE:\n user found") else: command = self.CREATE_USER_ON_INSTANCE.format( PEM_PATH=self.get_pem_file(instance), USER=user, INSTANCE=self.get_host(instance)) self.logger.debug("CREATE_USER_ON_INSTANCE:\n%s" % command) self.execute_shell_command(command) command = self.CREATE_DIR.format( USER=user, SSH_DIR=self.SSH_DIR.format(USER=user), PEM_PATH=self.get_pem_file(instance), INSTANCE=self.get_host(instance)) self.logger.debug("CREATE_DIR:\n%s" % command) self.execute_shell_command(command) def add_user_key_to_instance(self, user, instance): if self.user_exists(user, instance): self.logger.debug("ADD_USER_KEY_TO_INSTANCE:\n user found") command = self.ADD_USER_KEY_TO_AUTHORIZED_KEYS.format( USER=user, USER_KEY=self.get_public_key(user), SSH_DIR=self.SSH_DIR.format(USER=user), PEM_PATH=self.get_pem_file(instance), INSTANCE=self.get_host(instance)) self.logger.debug("ADD_USER_KEY_TO_AUTHORIZED_KEYS:\n%s" % command) self.execute_shell_command(command) else: self.logger.debug("ADD_USER_KEY_TO_INSTANCE:\n user not found") def add_user_to_sudoers_group(self, user, instance): if self.user_exists(user, instance): self.logger.debug("ADD_USER_TO_SUDOERS_GROUP:\n user found") command = self.ADD_USER_TO_SUDOERS_GROUP.format( USER=user, SSH_DIR=self.SSH_DIR.format(USER=user), PEM_PATH=self.get_pem_file(instance), INSTANCE=self.get_host(instance)) self.logger.debug("ADD_USER_TO_SUDOERS_GROUP:\n%s" % command) self.execute_shell_command(command) else: self.logger.debug("ADD_USER_TO_SUDOERS_GROUP:\n user not found") def execute_shell_command(self, command): cmd = Popen(command, shell=True, stdout=PIPE, stderr=STDOUT) result = str(cmd.communicate()[0]) self.logger.debug("SHELL_COMMAND_RESULT:\n%s" % result) return result
#!/usr/bin/python #import cgi #import cgitb; cgitb.enable() import os, sys import string import re from GoogleSpreadsheet import GoogleSpreadsheet spreadsheet_id = "REPLACE_THIS_WITH_GOOGLE_SPREADHSHEET_ID" worksheet_id = "od6" spreadsheet = GoogleSpreadsheet(spreadsheet_id, worksheet_id) nav_spreadsheet = GoogleSpreadsheet(spreadsheet_id, worksheet_id) def gatherNavData(spreadsheet): navigation = [] for row in spreadsheet: pageid = row['pageid'] if pageid: navigation.append(row) return (navigation) def printHeader(): print '''<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <link rel="stylesheet" type="text/css" href="/styles.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() {
#!/usr/bin/python from GoogleSpreadsheet import GoogleSpreadsheet#, SpreadsheetRow # import time import csv # spreadsheet_id = '0AiiHlTECOi8edHFxQWRwN0kxeTQ3ZzdCOXhQX2Z1ZGc' spreadsheet_id = '11-0Vzp45Ery-VP9K9Qk3FXjZPWHeOIGuN3MxY1_8MRs' worksheet_id = 'od6' gs = GoogleSpreadsheet(spreadsheet_id, worksheet_id) ### read input file & build dictionary file_in = open('/var/www/html/papers/update/export_out.tab', 'r') headers = [str(x).upper() for x in file_in.readline().rstrip('\n').split('\t')] #total_lines = 0 total_dict = {} order = [] # order of PMID for line_num, line in enumerate(file_in): fields = line.rstrip('\n').split('\t') row_dict = {} for i, val in enumerate(fields): row_dict[headers[i]] = val # if row_dict["pmid"] == "": order.append(row_dict["PMID"]) total_dict[row_dict["PMID"]] = row_dict # else: # order.append(row_dict["labid"]) # total_dict[row_dict["labid"]] = row_dict file_in.close()