Beispiel #1
0
class LiderPage(QWidget):
    def __init__(self, parent=None):
        super(LiderPage, self).__init__(parent)

        self.liderldap_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../../dist/lider_ldap.json')
        self.liderejabberd_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../../dist/lider_ejabberd.json')
        self.liderdb_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../../dist/liderdb.json')
        self.lider_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../../dist/lider.json')
        self.server_list_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../../dist/server_list.json')
        self.log_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../../dist/installer.log')
        if not os.path.exists(
                os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             '../../dist')):
            os.makedirs(
                os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             '../../dist'))

        self.ldap_layout = OpenLdapPage()
        self.ejabberd_layout = EjabberdPage()
        self.db_layout = DatabasePage()
        self.im = InstallManager()
        self.msg_box = MessageBox()
        self.status = StatusPage()
        self.lider_sunucu_pwd = None
        self.db_password = None
        self.util = Util()

        ## db parameters
        self.dbServerLabel = QLabel("Veritabanı Sunucu Adresi:")
        self.db_server_layout = QLineEdit()
        self.db_server_layout.setPlaceholderText("192.168.*.*")

        # OpenLDAP parameters
        self.ldapServerLabel = QLabel("LDAP Sunucu Adresi:")
        self.ldap_server = QLineEdit()
        self.ldap_server.setPlaceholderText("192.168.*.*")

        # Ejabberd parameters
        self.ejabberdServerLabel = QLabel("XMPP Sunucu Adresi:")
        self.ejabberd_server = QLineEdit()
        self.ejabberd_server.setPlaceholderText("192.168.*.*")

        self.fileServerLabel = QLabel("Dosya Sunucu Adresi:")
        self.file_server = QLineEdit()
        self.file_server.setPlaceholderText("192.168.*.*")
        # self.file_server.setDisabled(True)
        self.file_server.setVisible(False)
        self.fileServerLabel.setVisible(False)

        self.installButton = QPushButton("Kuruluma Başla")

        # if not os.path.exists(self.server_list_path):
        #     self.installButton.setDisabled(True)

        self.installButton.clicked.connect(self.lider_ahenk_install)

        self.liderLdapGroup = QGroupBox(
            "Lider Ahenk Sunucu Konfigürasyon Bilgileri")
        self.liderXmppGroup = QGroupBox("XMPP Konfigürasyon Bilgileri")
        self.liderDbGroup = QGroupBox("Veritabanı Konfigürasyon Bilgileri")

        # Install Status Layout
        statusGroup = QGroupBox()
        self.status.statusLabel.setText("Lider Kurulum Durumu:")
        statusGroup.setLayout(self.status.statusLayout)

        # add server ip to database layout
        self.liderDbGroup.setLayout(self.db_layout.dbLayout)

        # add server ip to ldap layout
        self.liderLdapGroup.setLayout(self.ldap_layout.ldapLayout)

        # add server ip to ejabberd layout
        self.ejabberd_layout.ejabberdLayout.removeWidget(
            self.ejabberd_layout.ldapServerLabel)
        self.ejabberd_layout.ejabberdLayout.removeWidget(
            self.ejabberd_layout.ldap_server)
        # self.ejabberd_layout.ejabberdLayout.addWidget(self.ejabberdServerLabel, 8, 0)
        # self.ejabberd_layout.ejabberdLayout.addWidget(self.ejabberd_server, 8, 1)
        self.liderXmppGroup.setLayout(self.ejabberd_layout.ejabberdLayout)

        self.liderGroup = QGroupBox()
        # liderLayout = QVBoxLayout()
        liderLayout = QGridLayout()
        # liderLayout.addSpacing(12)
        liderLayout.addWidget(self.installButton, 0, 1)
        # liderLayout.addStretch(1)
        self.liderGroup.setLayout(liderLayout)

        mainLayout = QVBoxLayout()
        # mainLayout.addWidget(self.liderDbGroup)
        mainLayout.addWidget(self.liderLdapGroup)
        # mainLayout.addWidget(self.liderXmppGroup)
        mainLayout.addWidget(self.liderGroup)
        mainLayout.addWidget(statusGroup)
        mainLayout.addSpacing(12)
        mainLayout.addStretch(1)

        self.setLayout(mainLayout)

        if self.util.is_exist(self.liderldap_path):
            with open(self.liderldap_path) as f:
                data = json.load(f)
            self.ldap_layout.ldap_base_dn.setText(data["l_base_dn"])
            self.ldap_layout.ldap_admin_pwd.setText(data["l_admin_pwd"])
            self.ldap_layout.ladmin_user.setText(data["ladmin_user"])
            self.ldap_layout.ladmin_pwd.setText(data["l_admin_pwd"])

    def check_control_button(self, idx):
        ## if select location is remote server
        if idx == 0:
            # self.checkControlButton.setEnabled(True)
            # self.file_server.setDisabled(True)
            self.file_server.setVisible(False)
            self.fileServerLabel.setVisible(False)

        else:
            # self.file_server.setDisabled(False)
            self.file_server.setVisible(True)
            self.fileServerLabel.setVisible(True)

    def lider_ahenk_install(self):

        if self.ldap_layout.ldap_base_dn.text(
        ) == "" or self.ldap_layout.ldap_admin_pwd.text(
        ) == "" or self.ldap_layout.ladmin_user.text(
        ) == "" or self.ldap_layout.ladmin_pwd.text() == "":
            self.msg_box.information("Lütfen LDAP ve XMPP bilgilerini giriniz")
        else:
            self.status.install_status.setText(
                "Lider Ahenk kurulumu devam ediyor")
            self.status.install_status.setStyleSheet("background-color: green")

            self.msg_box.information(
                "Lider Ahenk sunucu kurulumana başlanacak.")

            subprocess.Popen(["xterm", "-e", "tail", "-f", self.log_path])

            ## get connect and repo settings data
            with open(self.server_list_path) as f:
                server_data = json.load(f)

                self.database_install(server_data)
                time.sleep(5)
                self.ldap_install(server_data)
                time.sleep(5)
                self.ejabberd_install(server_data)
                time.sleep(5)
                self.lider_install(server_data)

    def database_install(self, server_data):

        if server_data["selection"] == "advanced":
            ip = server_data["Veritabanı"][0]["ip"]
            username = server_data["Veritabanı"][0]["username"]
            password = server_data["Veritabanı"][0]["password"]
            location = server_data["Veritabanı"][0]["location"]

        else:
            # selection is standart
            ip = server_data["ip"]
            username = server_data["username"]
            password = server_data["password"]
            location = server_data["location"]

        ## Random Password Generator for "databases user's password"
        chars = string.ascii_letters + string.digits
        rnd = random.SystemRandom()
        self.db_password = ''.join(rnd.choice(chars) for i in range(10))

        self.data = {
            'location': location,
            # Server Configuration
            'ip': ip,
            'username': username,
            'password': password,
            # Database Configuration
            'db_name': "liderdb",
            'db_username': "******",
            'db_password': self.db_password,
            # Repo Configuration
            'repo_addr': server_data["repo_addr"],
            'repo_key': server_data["repo_key"]
        }

        with open(self.liderdb_path, 'w') as f:
            json.dump(self.data, f, ensure_ascii=False)

        if self.data['location'] == 'remote':
            self.im.ssh_connect(self.data)
            self.im.install_mariadb(self.data)
            self.im.ssh_disconnect()
        else:
            self.im.install_mariadb(self.data)

    def ldap_install(self, server_data):

        if server_data["selection"] == "advanced":
            ip = server_data["OpenLDAP"][0]["ip"]
            username = server_data["OpenLDAP"][0]["username"]
            password = server_data["OpenLDAP"][0]["password"]
            location = server_data["OpenLDAP"][0]["location"]
            lider_server_addr = server_data["Lider"][0]["ip"]
        else:
            # selection is standart
            ip = server_data["ip"]
            username = server_data["username"]
            password = server_data["password"]
            location = server_data["location"]
            lider_server_addr = server_data["ip"]

        if self.ldap_layout.ldapStatusCombo.currentIndex() == 0:
            ldap_status = 'new'
        else:
            # if ldap_status is 'Güncelle'
            ldap_status = 'update'

        l_org_name = self.ldap_layout.ldap_base_dn.text().split('.')
        l_org_name = l_org_name[0]

        self.data = {
            'location': location,
            # Server Configuration
            'ip': ip,
            'username': username,
            'password': password,

            # OpenLDAP Configuration
            'l_base_dn': self.ldap_layout.ldap_base_dn.text(),
            'l_config_pwd': self.ldap_layout.ldap_admin_pwd.text(),
            'l_org_name': l_org_name,
            'l_config_admin_dn': "cn=admin,cn=config",
            'l_admin_cn': 'admin',
            'ladmin_user': self.ldap_layout.ladmin_user.text(),
            'l_admin_pwd': self.ldap_layout.ldap_admin_pwd.text(),
            'ladmin_pwd': self.ldap_layout.ldap_admin_pwd.text(),
            'ldap_status': ldap_status,
            'repo_addr': server_data["repo_addr"],
            'repo_key': server_data["repo_key"],
            'lider_server_addr': lider_server_addr,
            'simple_ldap_user': "******",
            'simple_ldap_user_pwd': "secret"

            # yeni ldap kur ya da varolan ldapı konfigüre et 'new' ya da 'update' parametreleri alıyor
        }

        with open(self.liderldap_path, 'w') as f:
            json.dump(self.data, f, ensure_ascii=False)

        if self.data['location'] == 'remote':
            self.im.ssh_connect(self.data)
            self.im.install_ldap(self.data)
            self.im.ssh_disconnect()
        else:
            self.im.install_ldap(self.data)

    def ejabberd_install(self, server_data):

        if server_data["selection"] == "advanced":
            ip = server_data["XMPP"][0]["ip"]
            username = server_data["XMPP"][0]["username"]
            password = server_data["XMPP"][0]["password"]
            location = server_data["XMPP"][0]["location"]
            self.ldap_server = server_data["OpenLDAP"][0]["ip"]
        else:
            # selection is standart
            ip = server_data["ip"]
            username = server_data["username"]
            password = server_data["password"]
            location = server_data["location"]
            self.ldap_server = server_data["ip"]

        ## Random Password Generator for "lider_sunucu"
        chars = string.ascii_letters + string.digits
        rnd = random.SystemRandom()
        self.lider_sunucu_pwd = ''.join(rnd.choice(chars) for i in range(10))

        self.data = {
            'location': location,
            # Server Configuration
            'ip': ip,
            'username': username,
            'password': password,
            # Ejabberd Configuration
            'e_service_name': "im.liderahenk.org",
            # 'e_service_name': self.e_service_name.text(),
            'e_username': '******',
            # 'e_user_pwd': self.ejabberd_layout.e_user_pwd.text(),
            'e_user_pwd': self.ldap_layout.ldap_admin_pwd.text(),
            'e_hosts': ip,
            'ldap_servers': self.ldap_server,
            'l_base_dn': self.ldap_layout.ldap_base_dn.text(),

            # Lider Configuration
            'lider_username': '******',
            'lider_user_pwd': self.lider_sunucu_pwd,
            'l_admin_pwd': self.ldap_layout.ldap_admin_pwd.text(),
            'repo_key': server_data["repo_key"],
            'repo_addr': server_data["repo_addr"]
        }

        with open(self.liderejabberd_path, 'w') as f:
            json.dump(self.data, f, ensure_ascii=False)

        if self.data['location'] == 'remote':
            self.im.ssh_connect(self.data)
            self.im.install_ejabberd(self.data)
            self.im.ssh_disconnect()
        else:
            self.im.install_ejabberd(self.data)

    def lider_install(self, server_data):

        if server_data["selection"] == "advanced":
            ip = server_data["Lider"][0]["ip"]
            username = server_data["Lider"][0]["username"]
            password = server_data["Lider"][0]["password"]
            location = server_data["Lider"][0]["location"]
            self.ldap_server = server_data["OpenLDAP"][0]["ip"]
            self.db_server = server_data["Veritabanı"][0]["ip"]
            self.ejabberd_server = server_data["XMPP"][0]["ip"]
            if server_data["Veritabanı"][0]["ip"] == ip:
                self.db_server = "127.0.0.1"
            else:
                self.db_server = server_data["Veritabanı"][0]["ip"]

        else:
            # selection is standart
            ip = server_data["ip"]
            username = server_data["username"]
            password = server_data["password"]
            location = server_data["location"]
            self.ldap_server = server_data["ip"]
            self.db_server = server_data["ip"]
            self.ejabberd_server = server_data["ip"]
            self.db_server = "127.0.0.1"

        self.data = {
            'location': location,

            # Server Configuration
            'ip': ip,
            'username': username,
            'password': password,
            # Database Configuration
            'db_server': self.db_server,
            'db_name': "liderdb",
            'db_username': "******",
            'db_password': self.db_password,

            # Ejabberd Configuration
            'e_service_name': "im.liderahenk.org",
            'e_hosts': self.ejabberd_server,
            'lider_username': '******',
            'lider_user_pwd': self.lider_sunucu_pwd,

            # OpenLDAP Configuration
            'l_base_dn': self.ldap_layout.ldap_base_dn.text(),
            'l_admin_cn': "admin",
            'l_admin_pwd': self.ldap_layout.ldap_admin_pwd.text(),
            'ldap_servers': self.ldap_server,

            # File Server Configuration
            'file_server': ip,
            'fs_username': username,
            'fs_username_pwd': password,
            'fs_plugin_path': '/home/{username}'.format(username=username),
            'fs_agreement_path': '/home/{username}'.format(username=username),
            'fs_agent_file_path': '/home/{username}'.format(username=username),

            # repository parameters
            'repo_key': server_data["repo_key"],
            'repo_addr': server_data["repo_addr"]
        }

        with open(self.lider_path, 'w') as f:
            json.dump(self.data, f, ensure_ascii=False)

        if self.data['location'] == 'remote':
            self.im.ssh_connect(self.data)
            self.im.install_lider(self.data)
            self.im.ssh_disconnect()
        else:
            self.im.install_lider(self.data)

        self.msg_box.information(
            "Lider Ahenk kurulumu tamamlandı.\n"
            "Kurulum loglarını Log sayfasından inceleyebilirsiniz")
        self.status.install_status.setText("Lider Ahenk kurulumu tamamlandı")
        self.status.install_status.setStyleSheet("background-color: cyan")
class LiderInstaller(object):
    def __init__(self, ssh_api, ssh_status):
        self.ssh_api = ssh_api
        self.ssh_status = ssh_status
        self.logger = Logger()
        self.util = Util()
        self.config_manager = ConfigManager()
        self.lider_conf_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../../conf/tr.org.liderahenk.cfg')
        self.db_conf_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../../conf/tr.org.liderahenk.datasource.cfg')
        self.lider_conf_out_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../../dist/tr.org.liderahenk.cfg')
        self.db_conf_out_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../../dist/tr.org.liderahenk.datasource.cfg')

    def install(self, data):

        repo_key = data["repo_key"]
        repo_key = repo_key.rsplit("/")[-1]

        if self.ssh_status == "Successfully Authenticated" or data[
                'location'] == 'local':
            cfg_data = self.config_manager.read()
            self.configure_lider_cfg(data)
            self.configure_db_cfg(data)
            result_code = self.ssh_api.run_command(
                cfg_data["cmd_soft_properties"])
            if result_code == 0:
                self.logger.info("software-properties-common paketi kuruldu")
            else:
                self.logger.error(
                    "software-properties-common paketi kurulamadı, result_code: "
                    + str(result_code))

            result_code = self.ssh_api.run_command(
                cfg_data["cmd_liderahenk_repo_key"].format(
                    data["repo_key"], repo_key))
            if result_code == 0:
                self.logger.info("Lider Ahenk repo key dosyası indirildi")
            else:
                self.logger.error(
                    "Lider Ahenk repo key dosyası indirilemedi, result_code: "
                    + str(result_code))

            result_code = self.ssh_api.run_command(
                cfg_data["cmd_liderahenk_repo_add"].format(data["repo_addr"]))
            if result_code == 0:
                self.logger.info("Lider Ahenk repo adresi eklendi")
            else:
                self.logger.error(
                    "Lider Ahenk repo adresi eklenemedi, result_code: " +
                    str(result_code))

            result_code = self.ssh_api.run_command(cfg_data["cmd_update"])
            if result_code == 0:
                self.logger.info("Paket listesi güncellendi(apt update)")
            else:
                self.logger.error(
                    "Paket listesi güncellenemdi, result_code: " +
                    str(result_code))

            result_code = self.ssh_api.run_command(
                cfg_data["cmd_lider_install"])
            if result_code == 0:
                self.logger.info("lider-server paketi kurulumu yapıldı")
            else:
                self.logger.error(
                    "lider-server paketi kurulamadı, result_code: " +
                    str(result_code))

            self.ssh_api.scp_file(self.lider_conf_out_path,
                                  cfg_data["lider_des_path"])
            self.ssh_api.scp_file(self.db_conf_out_path,
                                  cfg_data["lider_des_path"])
            self.ssh_api.run_command(cfg_data["cmd_cp_lider_cfg"])
            self.logger.info(
                "lider konfigürasyon dosyası LİDER sunucusuna kopyalandı")
            self.ssh_api.run_command(cfg_data["cmd_cp_db_cfg"])
            self.logger.info(
                "veritabanı konfigürasyon dosyası LİDER sunucusuna kopyalandı")
            result_code = self.ssh_api.run_command(
                cfg_data["cmd_lider_service"])
            if result_code == 0:
                self.logger.info("lider servisi başlatıldı")
            else:
                self.logger.error(
                    "lider servisi başlatılamadı, resuşt_code: " +
                    str(result_code))

            result_code = self.ssh_api.run_command(cfg_data["cmd_fs_dep"])
            if result_code == 0:
                self.logger.info(
                    "sshpass ve rsync paketleri başarıyla kuruldu")
            else:
                self.logger.error("sshpass ve rsync paketleri kurulamadı")

            agent_files_path = data['fs_agent_file_path'] + '/agent-files'
            if data['location'] == 'remote':
                self.ssh_api.run_command(
                    cfg_data["cmd_agents_files"].format(agent_files_path))
                self.logger.info("agent-files dizini oluşturuldu")
                self.ssh_api.run_command(
                    cfg_data["cmd_chown_agents_files"].format(
                        data["username"], agent_files_path))
                self.logger.info("agent-files dizini için owner değiştirildi")
            else:
                if not self.util.is_exist(agent_files_path):
                    self.util.create_directory_local(agent_files_path)
                    self.logger.info("agent-files dizini oluşturuldu")
                    self.util.change_owner(agent_files_path, data['username'],
                                           data['username'])
                    self.logger.info(
                        "agent-files dizini için owner değiştirildi")
                else:
                    self.logger.info(
                        "{0} dizini zaten var".format(agent_files_path))

            result_code = self.ssh_api.run_command(
                cfg_data["cmd_enable_lider_service"])
            self.logger.info("Lider servis olarak ayarlandı.")

        else:
            self.logger.error(
                "LİDER sunucusuna bağlantı sağlanamadığı için kurulum yapılamadı. Lütfen bağlantı ayarlarını kotrol ediniz!"
            )

    def configure_lider_cfg(self, data):
        l_base_dn = self.base_dn_parse(data)
        l_admin_dn = "cn=admin," + str(l_base_dn)

        lider_data = {
            "#LDAP_SERVER": data['ldap_servers'],
            "#LDAP_ADMIN_DN": l_admin_dn,
            "#LDAP_ADMIN_PWD": data['l_admin_pwd'],
            "#LDAP_ROOT_DN": l_base_dn,
            "#XMPP_SERVER": data['e_hosts'],
            "#LIDER_USERNAME": data['lider_username'],
            "#XMPP_USER_PWD": data['lider_user_pwd'],
            "#XMPP_SERVICE_NAME": data['e_service_name'],
            "#LDAP_BASE_DN": l_base_dn,
            "#FILE_SERVER": data['file_server'],
            "#FS_USERNAME": data['fs_username'],
            "#FS_PASSWORD": data['fs_username_pwd'],
            "#PLUGIN_PATH": data['fs_plugin_path'],
            "#AGREEMENT_PATH": data['fs_agreement_path'],
            "#AGENT_FILE_PATH": data['fs_agent_file_path']
        }

        self.f_lider = open(self.lider_conf_path, 'r+')
        lider_text = self.f_lider.read()

        txt = self.config_manager.replace_all(lider_text, lider_data)
        self.f_lider_out = open(self.lider_conf_out_path, 'w+')
        self.f_lider_out.write(str(txt))
        self.f_lider.close()
        self.f_lider_out.close()
        self.logger.info("tr.org.liderahenk.cfg dosyası oluşturuldu")

    def configure_db_cfg(self, data):
        db_data = {
            "#DBADDRESS": data['db_server'],
            "#DBDATABASE": data['db_name'],
            "#DBUSERNAME": data['db_username'],
            "#DBPASSWORD": data['db_password']
        }
        self.f_db = open(self.db_conf_path, 'r+')
        db_text = self.f_db.read()
        txt = self.config_manager.replace_all(db_text, db_data)
        self.f_db_out = open(self.db_conf_out_path, 'w+')
        self.f_db_out.write(txt)
        self.f_db.close()
        self.f_db_out.close()
        self.logger.info("tr.org.datasource.cfg dosyası oluşturuldu")

    def base_dn_parse(self, data):
        ### split for get data['base_dn']: liderahenk.org #BASECN and #BASEDN
        parse_dn = data["l_base_dn"].split('.')
        dn_list = []
        for dn in parse_dn:
            message = 'dc=' + str(dn) + ','
            dn_list.append(message)
        base_dn = ''.join(str(x) for x in dn_list)
        base_dn = base_dn.strip(',')
        return base_dn