def init(self, name, password, dataDir, port, host=None): """ Method to initialize a server component. :param name: Name of the component to be initialized. """ pgcCmd = PGC_HOME + os.sep + "pgc --json init " + name + " --datadir " + dataDir + " --port " + port if port == '': pgcCmd = pgcCmd.split(' --port')[0] if dataDir == '': pgcCmd = pgcCmd.split(' --datadir')[0] if host: pgc_host_info = util.get_pgc_host(host) cred_info = util.get_credentials_by_uuid( pgc_host_info.get('ssh_cred_id')) enc_secret = util.get_value("GLOBAL", "SECRET", "") enc_key = "{0}{1}".format(enc_secret, cred_info.get("cred_uuid")) ssh_username = cred_info.get("ssh_user") password = "" if cred_info.get("ssh_passwd"): password = util.decrypt(cred_info.get("ssh_passwd"), enc_key) ssh_key = "" if cred_info.get("ssh_key"): ssh_key = util.decrypt(cred_info.get("ssh_key"), enc_key) sudo_pwd = "" if cred_info.get("ssh_sudo_pwd"): sudo_pwd = util.decrypt(cred_info.get("ssh_sudo_pwd"), enc_key) ssh_host = pgc_host_info.get('host') from PgcRemote import PgcRemote remote = PgcRemote(ssh_host, ssh_username, password=password, ssh_key=ssh_key) remote.connect() is_file_added = remote.add_file('/tmp/.pgpass', password) remote.disconnect() pgcCmd = pgcCmd + " --pwfile /tmp/.pgpass --host \"" + host + "\"" if util.is_postgres(name) and not host: pgpass_file = PGC_HOME + os.sep + name + os.sep + ".pgpass" if not os.path.isfile(pgpass_file): password_file = open(pgpass_file, 'w') password_file.write(password + '\n') password_file.close() os.chmod(pgpass_file, 0600) pgcProcess = subprocess.Popen(pgcCmd, stdout=subprocess.PIPE, shell=True) for line in iter(pgcProcess.stdout.readline, ''): try: ln = (line).rstrip('\n') if type(eval(ln)) is list: yield self.session.publish('com.bigsql.onInit', ln) except: pass
def get_list(p_isOLD, p_isExtensions, p_isJSON, p_isTEST, p_showLATEST, p_comp=None, p_relnotes=None, p_return=False): # r_sup_plat = util.like_pf("r.sup_plat") r_sup_plat = "1 = 1" if p_isOLD: exclude_comp = "" else: exclude_comp = " AND v.component NOT IN (SELECT component FROM components)" parent_comp_condition = "" installed_category_conditions = " AND p.category > 0 " available_category_conditions = " AND p.is_extension = 0" ext_component = "" if p_isExtensions: installed_category_conditions = " AND p.is_extension = 1" available_category_conditions = " AND p.is_extension = 1" if p_comp != "all": ext_component = " AND parent = '" + p_comp + "' " installed = \ "SELECT p.category, g.description as category_desc, g.short_desc as short_cat_desc, \n" + \ " c.component, c.version, c.port, c.status, r.stage, \n" + \ " coalesce((select is_current from versions where c.component = component AND c.version = version),0), \n" + \ " c.datadir, p.is_extension, \n" + \ " coalesce((select parent from versions where c.component = component and c.version = version),'') as parent, \n" + \ " coalesce((select release_date from versions where c.component = component and c.version = version),'20200101'), \n" + \ " c.install_dt, r.disp_name, \n" + \ " coalesce((select release_date from versions where c.component = component and is_current = 1),'20200101') \n" + \ " FROM components c, releases r, projects p, categories g \n" + \ " WHERE c.component = r.component AND r.project = p.project \n" + \ " AND p.category = g.category \n" + \ " AND " + r_sup_plat + installed_category_conditions + ext_component available = \ "SELECT c.category, c.description, c.short_desc as short_cat_desc, v.component, v.version, 0, 'NotInstalled', \n" + \ " r.stage, v.is_current, '', p.is_extension, v.parent as parent, v.release_date, '', \n" + \ " r.disp_name, \n" + \ " coalesce((select release_date from versions where v.component = component and is_current = 1),'20200101') \n" + \ " FROM versions v, releases r, projects p, categories c \n" + \ " WHERE v.component = r.component AND r.project = p.project \n" + \ " AND p.category = c.category \n" + \ " AND " + util.like_pf("v.platform") + " \n" + \ " AND " + r_sup_plat + exclude_comp + available_category_conditions + ext_component extensions = \ "SELECT c.category, c.description, c.short_desc as short_cat_desc, v.component, v.version, 0, 'NotInstalled', \n" + \ " r.stage, v.is_current, '', p.is_extension, v.parent as parent, v.release_date, '', \n" + \ " r.disp_name, \n" + \ " coalesce((select release_date from versions where v.component = component and is_current = 1),'20200101') \n" + \ " FROM versions v, releases r, projects p, categories c \n" + \ " WHERE v.component = r.component AND r.project = p.project \n" + \ " AND p.is_extension = 1 AND p.category = c.category \n" + \ " AND " + util.like_pf("v.platform") + " \n" + \ " AND v.parent in (select component from components) AND " + r_sup_plat + exclude_comp if p_isExtensions: sql = installed + "\n UNION \n" + available + "\n ORDER BY 1, 3, 4, 6" else: sql = installed + "\n UNION \n" + available + "\n UNION \n" + extensions + "\n ORDER BY 1, 3, 4, 6" try: c = con.cursor() c.execute(sql) data = c.fetchall() headers = [ 'Category', 'Component', 'Version', 'ReleaseDt', 'Stage', 'Status', 'Updates' ] keys = [ 'short_cat_desc', 'component', 'version', 'release_date', 'stage', 'status', 'current_version' ] jsonList = [] kount = 0 previous_version = None previous_comp = None for row in data: compDict = {} kount = kount + 1 category = str(row[0]) category_desc = str(row[1]) short_cat_desc = str(row[2]) comp = str(row[3]) version = str(row[4]) port = str(row[5]) if previous_comp and previous_version: if previous_comp == comp and previous_version == version: continue previous_version = version previous_comp = comp if str(row[6]) == "Enabled": status = "Installed" else: status = str(row[6]) if status == "NotInstalled" and p_isJSON == False: status = "" stage = str(row[7]) if stage in ("soon", "bring-own", "included"): continue is_current = str(row[8]) if is_current == "0" and status in ("", "NotInstalled"): if not p_isOLD: continue current_version = get_current_version(comp) is_update_available = 0 cv = Version.coerce(current_version) iv = Version.coerce(version) if cv > iv: is_update_available = 1 if is_update_available == 0: updates = 0 current_version = "" else: updates = 1 if (port == "0") or (port == "1"): port = "" datadir = row[9] if row[9] is None: datadir = "" else: datadir = str(row[9]).strip() is_extension = row[10] parent = row[11] disp_name = row[14] release_desc = '' release_date = '1970-01-01' curr_rel_date = '1970-01-01' curr_rel_dt = str(row[15]) rel_dt = str(row[12]) if len(rel_dt) == 8: release_date = rel_dt[0:4] + "-" + rel_dt[4:6] + "-" + rel_dt[ 6:8] curr_rel_date = curr_rel_dt[0:4] + "-" + curr_rel_dt[ 4:6] + "-" + curr_rel_dt[6:8] compDict['is_new'] = 0 try: rd = datetime.datetime.strptime(release_date, '%Y-%m-%d') today_date = datetime.datetime.today() date_diff = (today_date - rd).days if date_diff <= 30: compDict['is_new'] = 1 if p_showLATEST and date_diff > 30: continue except Exception as e: pass if util.is_postgres(comp): if port > "" and status == "Installed" and datadir == "": status = "NotInitialized" port = "" ins_date = str(row[13]) install_date = "" compDict['is_updated'] = 0 if ins_date: install_date = ins_date[0:4] + "-" + ins_date[ 5:7] + "-" + ins_date[8:10] try: insDate = datetime.datetime.strptime( install_date, '%Y-%m-%d') today_date = datetime.datetime.today() date_diff = (today_date - insDate).days if date_diff <= 30: compDict['is_updated'] = 1 except Exception as e: pass if p_relnotes and p_isJSON: rel_version = version if current_version != "": rel_version = current_version rel_notes = str(util.get_relnotes(comp, rel_version)) markdown_text = unicode(rel_notes, sys.getdefaultencoding(), errors='ignore').strip() html_text = mistune.markdown(markdown_text) compDict['rel_notes'] = html_text compDict['category'] = category compDict['category_desc'] = category_desc compDict['short_cat_desc'] = short_cat_desc compDict['component'] = comp compDict['version'] = version compDict['is_extension'] = is_extension compDict['disp_name'] = disp_name compDict['release_desc'] = release_desc compDict['port'] = port compDict['release_date'] = release_date compDict['install_date'] = install_date compDict['curr_release_date'] = curr_rel_date compDict['status'] = status compDict['stage'] = stage compDict['updates'] = updates compDict['is_current'] = is_current compDict['current_version'] = current_version compDict['parent'] = parent jsonList.append(compDict) if p_return: return jsonList if p_isJSON: print(json.dumps(jsonList, sort_keys=True, indent=2)) else: if len(jsonList) >= 1: if p_showLATEST: print("New components released in the last 30 days.") print(api.format_data_to_table(jsonList, keys, headers)) except Exception as e: fatal_error(e, sql, "meta.get_list()") sys.exit(0)